Ejemplo n.º 1
0
            def _execute_search():
                # Open loading screen.
                # self.loading_screen = LoadingModalView()
                # self.loading_screen.loading_text.text = 'Retrieving rate structures...'
                # self.loading_screen.open()

                if self.utility_ref_table.empty:
                    try:
                        self._download_utility_ref_table()
                    except requests.ConnectionError:
                        popup = ConnectionErrorPopup()
                        popup.popup_text.text = 'There was an issue connecting to and downloading the lists of utilities. Check your connection settings and try again.'
                        popup.open()
                        return
                    finally:
                        self.search_button.disabled = False

                # Filter DataFrame by search type/query and drop duplicate entries.
                if search_type == 'state':
                    utility_data_filtered = self.utility_ref_table.loc[
                        self.utility_ref_table['state'].str.lower(
                        ).str.contains(search_query)
                        | self.utility_ref_table['state name'].str.lower(
                        ).str.contains(search_query)]
                elif search_type == 'zip':
                    utility_data_filtered = self.utility_ref_table.loc[
                        self.utility_ref_table[search_type] == search_query]
                else:
                    utility_data_filtered = self.utility_ref_table.loc[
                        self.utility_ref_table[search_type].str.lower(
                        ).str.contains(search_query)]

                utility_data_filtered = utility_data_filtered[[
                    'eiaid', 'utility_name', 'state', 'ownership'
                ]]
                utility_data_filtered.drop_duplicates(inplace=True)

                logging.info(
                    'RateStructureDM: Utility table filter completed.')
                self.search_button.disabled = False

                if utility_data_filtered.empty:
                    logging.warning(
                        'RateStructureDM: No results matched the query.')

                    popup = WarningPopup()
                    popup.popup_text.text = 'No results matched your query.'
                    popup.open()

                # Enable search results selector.
                fade_in_animation(self.utility_select_bx)
                self._populate_utility_selector(utility_data_filtered)
Ejemplo n.º 2
0
    def _query_api_for_rate_structures(self, api_query):
        """Uses OpenEI API to query the rate structures for given EIA ID and populates rate structure RecycleView."""
        ssl_verify, proxy_settings = check_connection_settings()

        attempt_download = True
        n_tries = 0

        while attempt_download:
            n_tries += 1

            if n_tries >= MAX_WHILE_ATTEMPTS:
                logging.warning('RateStructureDM: Hit download retry limit.')
                attempt_download = False
                break

            if App.get_running_app().root.stop.is_set():
                return

            try:
                with requests.Session() as req:
                    http_request = req.get(api_query,
                                           proxies=proxy_settings,
                                           timeout=10,
                                           verify=ssl_verify,
                                           stream=True)
                    if http_request.status_code != requests.codes.ok:
                        http_request.raise_for_status()
                    else:
                        attempt_download = False
            except requests.HTTPError as e:
                logging.error('RateStructureDM: {0}'.format(repr(e)))
                raise requests.ConnectionError
            except requests.exceptions.ProxyError:
                logging.error('RateStructureDM: Could not connect to proxy.')
                raise requests.ConnectionError
            except requests.ConnectionError as e:
                logging.error(
                    'RateStructureDM: Failed to establish a connection to the host server.'
                )
                raise requests.ConnectionError
            except requests.Timeout as e:
                logging.error('RateStructureDM: The connection timed out.')
                raise requests.ConnectionError
            except requests.RequestException as e:
                logging.error('RateStructureDM: {0}'.format(repr(e)))
                raise requests.ConnectionError
            except Exception as e:
                # Something else went wrong.
                logging.error(
                    'RateStructureDM: An unexpected error has occurred. ({0})'.
                    format(repr(e)))
                raise requests.ConnectionError
            else:
                structure_list = http_request.json()['items']

                structure_df = pd.DataFrame.from_records(structure_list)
                structure_df.dropna(subset=['energyratestructure'],
                                    inplace=True)

                # Filter out entries whose energyratestructure array does not contain "rate" terms.
                mask = structure_df['energyratestructure'].apply(lambda x: all(
                    ['rate' in hr.keys() for row in x for hr in row]))
                structure_df = structure_df[mask]

                structure_list = structure_df.to_dict(orient='records')

                # First, sort by effective date.
                # structure_list = sorted(structure_list, key=lambda x: (x['name'], x.get('startdate', np.nan)))
                structure_list = sorted(
                    structure_list,
                    key=lambda x: x.get('startdate', np.nan),
                    reverse=True)

                # Then, sort by name.
                structure_list = sorted(structure_list,
                                        key=lambda x: x['name'])

                # Display name: Name (record['startdate']).
                effective_dates = [
                    '(Effective Date : {0})'.format(
                        dt.datetime.fromtimestamp(
                            record['startdate']).strftime('%m/%d/%Y'))
                    if not np.isnan(record['startdate']) else ''
                    for record in structure_list
                ]

                records = [{
                    'name': record['name'] + ' ' + effective_dates[ix],
                    'record': record
                } for ix, record in enumerate(structure_list, start=0)]
                # records = sorted(records, key=lambda t: t['name'])

                self.rate_structure_rv.data = records
                self.rate_structure_rv.unfiltered_data = records

                logging.info(
                    'RateStructureDM: Retrieved utility rate structures.')
                fade_in_animation(self.rate_structure_select_bx)
            finally:
                self.loading_screen.dismiss()