Ejemplo n.º 1
0
 def create_table_elements(cls, data_list, id_key, **kwargs):
     """Creates a list of objects to be rendered as table by jinja template
     """
     sites_list_request = sites.list_metadata()
     sites_list = sites_list_request.json()
     site_dict = {site['site_id']: site for site in sites_list}
     table_rows = []
     for data in data_list:
         table_row = {}
         site_name = site_dict[data['site_id']]['name']
         site_href = url_for('data_dashboard.site_view',
                             uuid=data['site_id'])
         site_link = f'<a href={site_href}>{site_name}</a>'
         table_row['name'] = data['name']
         table_row['variable'] = data['variable']
         table_row['provider'] = data.get('provider', '')
         table_row['site'] = site_link
         if id_key == 'forecast_id':
             table_row['link'] = url_for('data_dashboard.forecast_view',
                                         uuid=data[id_key])
         else:
             table_row['link'] = url_for('data_dashboard.observation_view',
                                         uuid=data[id_key])
         table_rows.append(table_row)
     return table_rows
Ejemplo n.º 2
0
 def get_pairable_objects(self):
     """Requests the forecasts and observations from
     the api for injecting into the dom as a js variable
     """
     observation_list = observations.list_metadata()
     if self.report_type == 'probabilistic':
         forecast_list = cdf_forecast_groups.list_metadata()
     else:
         forecast_list = forecasts.list_metadata()
     site_list = sites.list_metadata()
     aggregate_list = aggregates.list_metadata()
     for obs in observation_list:
         del obs['extra_parameters']
     for fx in forecast_list:
         del fx['extra_parameters']
     for site in site_list:
         del site['extra_parameters']
     for agg in aggregate_list:
         del agg['extra_parameters']
     return {
         'observations': observation_list,
         'forecasts': forecast_list,
         'sites': site_list,
         'aggregates': aggregate_list
     }
Ejemplo n.º 3
0
    def get_site_table(cls, create=None, **kwargs):
        """Generates an html element containing a table of Sites.

        Parameters
        ----------
        create: {'None', 'observation', 'forecast', 'cdf_forecast_group'}
            If set, Site names will be links to create an object of type
            `create` for the given site.

        Returns
        -------
        string
            The rendered html template, including a table of sites, with search
            bar and 'Create new Site' button.
        """
        site_data_request = sites.list_metadata()
        site_data = site_data_request.json()
        rows = cls.create_site_table_elements(site_data, create, **kwargs)
        if create is None:
            # If the create argument is present, we don't need a "Create
            # Site" button, because we're using the view as a selector for
            # another object's `site` field.
            creation_link = url_for('forms.create_site')
        else:
            creation_link = None
        rendered_table = render_template(cls.site_template,
                                         creation_link=creation_link,
                                         table_rows=rows)
        return rendered_table
Ejemplo n.º 4
0
    def get_site_table(cls):
        """Generates an html element containing a table of Sites.

        Returns
        -------
        rendered_table: string
            The rendered html template, including a table of sites, with search
            bar and 'Create new Site' button.

        site_data: list of dict
            The site metadata list as returned by the api.

        Raises
        ------
        DataRequestException
            If a site_id is passed and the user does not have access
            to that site or some other api error has occurred.
        """
        site_data = sites.list_metadata()
        rows = cls.create_site_table_elements(site_data)
        creation_link = url_for('forms.create_site')
        rendered_table = render_template(cls.site_template,
                                         creation_link=creation_link,
                                         table_rows=rows)
        return rendered_table, site_data
Ejemplo n.º 5
0
    def get_sites_and_observations(self):
        """Returns a dict of observations mapping uuid to an observation
        dict with nested site.

        Parameters
        ----------
        aggregate_metadata: dict
            The metadata of the aggregate used for filtering applicable
            observations.
        """
        sites_list = sites.list_metadata()
        observations_list = observations.list_metadata()

        # Remove observations with greater interval length
        observations_list = list(
            filter(
                lambda x: x['interval_length'] <= self.metadata[
                    'interval_length'], observations_list))

        # Remove observations with different variables
        observations_list = list(
            filter(lambda x: x['variable'] == self.metadata['variable'],
                   observations_list))

        # Remove observations that exist in the aggregate and do not
        # have an effective_until set.
        effective_observations = [
            obs['observation_id'] for obs in self.metadata['observations']
            if obs['effective_until'] is None
        ]
        observations_list = list(
            filter(lambda x: x['observation_id'] not in effective_observations,
                   observations_list))

        # Finally remove extra parameters, which may cause templating
        # issues.
        for obs in observations_list:
            del obs['extra_parameters']
        for site in sites_list:
            del site['extra_parameters']
        site_dict = {site['site_id']: site for site in sites_list}
        for obs in observations_list:
            obs['site'] = site_dict.get(obs['site_id'], None)
        return observations_list
Ejemplo n.º 6
0
 def create_cdf_forecast_elements(cls, data_list, **kwargs):
     sites_list_request = sites.list_metadata()
     sites_list = sites_list_request.json()
     site_dict = {site['site_id']: site for site in sites_list}
     table_rows = []
     for data in data_list:
         table_row = {}
         site_name = site_dict[data['site_id']]['name']
         site_href = url_for('data_dashboard.site_view',
                             uuid=data['site_id'])
         site_link = f'<a href={site_href}>{site_name}</a>'
         table_row['name'] = data['name']
         table_row['variable'] = data['variable']
         table_row['provider'] = data.get('provider', '')
         table_row['site'] = site_link
         table_row['link'] = url_for(
             'data_dashboard.cdf_forecast_group_view',
             uuid=data['forecast_id'])
         table_rows.append(table_row)
     return table_rows
Ejemplo n.º 7
0
    def create_table_elements(cls, data_list, id_key, view_name):
        """Creates a list of objects to be rendered as table by jinja template.
        This method handles types with a reference to a site or aggregate.
        Types are Observation, Forecast, and CDF Forecast Group.

        Parameters
        ----------
        data_list: list of dicts
            List of metadata dictionaries as returned from the API.
        id_key: str
            The name of the UUID of the metadata. e.g. `observation_id`
        view_name: str
            The dashboard view that handles displaying a single object of the
            given type. e.g. `data_dashboard.observation_view`.

        Returns
        -------
        list of dict
            A list of dictionaries with the following keys for displaying as a
            table.

            name:     Name of the forecast/observation.
            variable: Variable recorded by this object.
            provider: Name of the organization tha created the object
            location: A link displaying the site or aggregate name and linking
                      to its page on the dashboard. If the site or aggregate
                      cannot be read, this is set to Site/Aggregate
                      unavailable.
            link:     The URL for viewing the full metadata and timeseries of
                      the object.
        """
        sites_list = sites.list_metadata()
        location_dict = {site['site_id']: site for site in sites_list}
        if id_key != 'observation_id':
            no_location_text = 'Site/Aggregate unavailable'
            aggregates_list = aggregates.list_metadata()
            location_dict.update(
                {agg['aggregate_id']: agg
                 for agg in aggregates_list})
        else:
            no_location_text = 'Site unavailable'
        table_rows = []
        for data in data_list:
            table_row = {}
            if data.get('site_id') is not None:
                location_id = data['site_id']
                location_view_name = 'data_dashboard.site_view'
            elif data.get('aggregate_id') is not None:
                location_id = data['aggregate_id']
                location_view_name = 'data_dashboard.aggregate_view'
            else:
                location_id = None
            location = location_dict.get(location_id)
            if location is not None:
                location_name = location['name']
                location_href = url_for(location_view_name, uuid=location_id)
                location_link = (f'<a href="{location_href}">'
                                 f'{location_name}</a>')
            else:
                location_link = no_location_text
            table_row['name'] = data['name']
            table_row['variable'] = data['variable']
            table_row['provider'] = data.get('provider', '')
            table_row['location'] = location_link
            table_row['link'] = url_for(view_name, uuid=data[id_key])
            table_rows.append(table_row)
        return table_rows