def get(self, request, *args, **kwargs):
        meter = request.GET.get('meter', None)
        meter_name = meter.replace(".", "_")
        date_options = request.GET.get('date_options', None)
        date_from = request.GET.get('date_from', None)
        date_to = request.GET.get('date_to', None)
        stats_attr = request.GET.get('stats_attr', 'avg')

        # TODO(lsmola) all timestamps should probably work with
        # current timezone. And also show the current timezone in chart.
        if (date_options == "other"):
            try:
                if date_from:
                    date_from = datetime.strptime(date_from,
                                                  "%Y-%m-%d")
                else:
                    # TODO(lsmola) there should be probably the date
                    # of the first sample as default, so it correctly
                    # counts the time window. Though I need ordering
                    # and limit of samples to obtain that.
                    pass
                if date_to:
                    date_to = datetime.strptime(date_to,
                                                "%Y-%m-%d")
                    # It return beginning of the day, I want the and of
                    # the day, so i will add one day without a second.
                    date_to = (date_to + timedelta(days=1) -
                               timedelta(seconds=1))
                else:
                    date_to = datetime.now()
            except Exception:
                raise ValueError("The dates haven't been "
                                 "recognized")
        else:
            try:
                date_from = datetime.now() - timedelta(days=int(date_options))
                date_to = datetime.now()
            except Exception:
                raise ValueError("The time delta must be an "
                                 "integer representing days.")

        if date_from and date_to:
            if date_to < date_from:
                # TODO(lsmola) propagate the Value error through Horizon
                # handler to the client with verbose message.
                raise ValueError("Date to must be bigger than date "
                                 "from.")
            # get the time delta in seconds
            delta = date_to - date_from
            if delta.days <= 0:
                # it's one day
                delta_in_seconds = 3600 * 24
            else:
                delta_in_seconds = delta.days * 24 * 3600 + delta.seconds
            # Lets always show 400 samples in the chart. Know that it is
            # maximum amount of samples and it can be lower.
            number_of_samples = 400
            period = delta_in_seconds / number_of_samples
        else:
            # If some date is missing, just set static window to one day.
            period = 3600 * 24

        additional_query = []
        if date_from:
            additional_query += [{'field': 'timestamp',
                                  'op': 'ge',
                                  'value': date_from}]
        if date_to:
            additional_query += [{'field': 'timestamp',
                                  'op': 'le',
                                  'value': date_to}]

        # TODO(lsmola) replace this by logic implemented in I1 in bugs
        # 1226479 and 1226482, this is just a quick fix for RC1
        try:
            meter_list = [m for m in ceilometer.meter_list(request)
                            if m.name == meter]
            unit = meter_list[0].unit
        except Exception:
            unit = ""

        if request.GET.get('group_by', None) == "project":
            try:
                tenants, more = api.keystone.tenant_list(
                    request,
                    domain=None,
                    paginate=True,
                    marker="tenant_marker")
            except Exception:
                tenants = []
                exceptions.handle(request,
                                  _('Unable to retrieve tenant list.'))
            queries = {}
            for tenant in tenants:
                tenant_query = [{
                    "field": "project_id",
                    "op": "eq",
                    "value": tenant.id}]

                queries[tenant.name] = tenant_query

            ceilometer_usage = ceilometer.CeilometerUsage(request)
            resources = ceilometer_usage.resource_aggregates_with_statistics(
                queries, [meter], period=period, stats_attr=None,
                additional_query=additional_query)

            series = self._series_for_meter(resources,
                                            'id',
                                            meter_name,
                                            stats_attr,
                                            unit)
        else:
            query = []

            def filter_by_meter_name(resource):
                """ Function for filtering of the list of resources.

                Will pick the right resources according to currently selected
                meter.
                """
                for link in resource.links:
                    if link['rel'] == meter:
                        # If resource has the currently chosen meter.
                        return True
                return False

            ceilometer_usage = ceilometer.CeilometerUsage(request)
            try:
                resources = ceilometer_usage.resources_with_statistics(
                    query, [meter], period=period, stats_attr=None,
                    additional_query=additional_query,
                    filter_func=filter_by_meter_name)
            except Exception:
                resources = []
                exceptions.handle(request,
                                  _('Unable to retrieve statistics.'))

            series = self._series_for_meter(resources,
                                            'resource_id',
                                            meter_name,
                                            stats_attr,
                                            unit)

        ret = {}
        ret['series'] = series
        ret['settings'] = {}

        return HttpResponse(json.dumps(ret),
            mimetype='application/json')
dataset/ETHPy150Open Havate/havate-openstack/proto-build/gui/horizon/Horizon_GUI/openstack_dashboard/dashboards/admin/metering/views.py/SamplesView.get/original