Skip to content

Commit

Permalink
Correct amount of samples in the chart
Browse files Browse the repository at this point in the history
We need to always show some reasonable amount of samples in the chart.

It was hardcoded to 1000s time windows, when there will be a
several months of data, it will be a great amount of samples.

It needs to be changed to always show some reasonable amount of samples
(I would say 400)

Also exceptions added for unexpected states of dates.
Date_from has been changed to beginning of the day, and date_to to
the end of that day.

Fixes bug 1221566
Change-Id: I60cd0aa0882c5b8890d61b649c312d4f7e9ddf82
  • Loading branch information
Ladas committed Sep 6, 2013
1 parent c6d02d6 commit e19677a
Showing 1 changed file with 46 additions and 6 deletions.
52 changes: 46 additions & 6 deletions openstack_dashboard/dashboards/admin/metering/views.py
Expand Up @@ -51,19 +51,59 @@ def get(self, request, *args, **kwargs):
resource = request.GET.get('resource', 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")
except ValueError:
raise exceptions.NotFound
# 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:
date_from = datetime.now() - timedelta(days=int(date_options))
date_to = datetime.now()
# If some date is missing, just set static window to one day.
period = 3600 * 24

query = [{"field": "metadata.OS-EXT-AZ:availability_zone",
"op": "eq",
Expand Down Expand Up @@ -101,7 +141,7 @@ def get(self, request, *args, **kwargs):

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

series = []
Expand All @@ -123,7 +163,7 @@ def get(self, request, *args, **kwargs):
ceilometer_usage = ceilometer.CeilometerUsage(request)
try:
resources = ceilometer_usage.resources_with_statistics(
query, [meter], period=1000, stats_attr=None,
query, [meter], period=period, stats_attr=None,
additional_query=additional_query)
except Exception:
resources = []
Expand Down

0 comments on commit e19677a

Please sign in to comment.