Skip to content
This repository has been archived by the owner on Jul 25, 2018. It is now read-only.

Commit

Permalink
Added comparison to previous day in single day view. Now sending a di…
Browse files Browse the repository at this point in the history
…ct with observation values plus other data rather than actual observation objects. Renamed getter for Observation from observation_type to observation_model to avoid confusion with the observation_type property on the Unit model, which points to a ContentType. Updated templates to refer to observation_model.
  • Loading branch information
dcloud committed Jan 19, 2012
1 parent f4eb673 commit 13578c0
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 20 deletions.
37 changes: 32 additions & 5 deletions dashboard/views.py
Expand Up @@ -121,6 +121,8 @@ def observations_for_day(projects, ordered_units, extra_units, from_datetime):
}
for ordered_unit in ordered_units:
logger.debug('unit in unitcol is {unit}'.format(unit=ordered_unit.slug))
obs_class = ordered_unit.observation_type.model_class()

try:
metric = Metric.objects.get(project=project, unit=ordered_unit)
if metric.is_cumulative:
Expand All @@ -133,7 +135,32 @@ def observations_for_day(projects, ordered_units, extra_units, from_datetime):
else:
try:
proj_obs = metric.related_observations.filter(to_datetime__lte=to_datetime, from_datetime__gte=from_datetime).latest('to_datetime')
obj['observations'].append(proj_obs)
obs_dict = {
'metric': metric,
'observation_model': proj_obs.observation_model,
'data' : proj_obs.data
}
if obs_class is RatioObservation:
obs_dict['antecedent'] = proj_obs.antecedent

if ordered_unit.slug in UNIT_COMPARE_PAST:
past_from_datetime = datetime.datetime.combine(from_datetime-datetime.timedelta(days=1), datetime.time(0,0,0))
past_to_datetime = datetime.datetime.combine(from_datetime-datetime.timedelta(days=1), datetime.time(23,59,59))
try:
past_proj_obs = metric.related_observations.filter(to_datetime__lte=past_to_datetime, from_datetime__gte=past_from_datetime).latest('to_datetime')
logger.debug('ordered_unit {0} in UNIT_COMPARE_PAST'.format(ordered_unit))
if past_proj_obs:
obs_dict['past'] = {
'metric': metric,
'observation_model': past_proj_obs.observation_model,
'data' : past_proj_obs.data
}
if obs_class is RatioObservation:
obs_dict['past']['antecedent'] = past_proj_obs.antecedent
except Exception, e:
pass

obj['observations'].append(obs_dict)
except Exception, e:
logger.debug("No observation for {unit}".format(unit=ordered_unit))
obj['observations'].append(None)
Expand Down Expand Up @@ -169,7 +196,7 @@ def _aggregate_observation_by_class(unit, project, from_datetime, to_datetime):
latest_obs = metric.related_observations.filter(to_datetime__lte=to_datetime).latest('to_datetime')
obs_dict = {
'metric': metric,
'observation_type': unit.observation_type.model,
'observation_model': latest_obs.observation_model,
'data' : latest_obs.data
}
return obs_dict
Expand All @@ -188,7 +215,7 @@ def _aggregate_observation_by_class(unit, project, from_datetime, to_datetime):
obs_dict = {
'metric': metric,
'data': obs_aggregate['value'],
'observation_type': unit.observation_type.model,
'observation_model': unit.observation_type.model,
'observations': obs_qs
}
return obs_dict
Expand All @@ -203,7 +230,7 @@ def _aggregate_observation_by_class(unit, project, from_datetime, to_datetime):
obs_dict = {
'metric': metric,
'antecedent': obs_qs[0].antecedent,
'observation_type': unit.observation_type.model,
'observation_model': unit.observation_type.model,
'data': aggregate_value,
'observations': obs_qs
}
Expand All @@ -213,7 +240,7 @@ def _aggregate_observation_by_class(unit, project, from_datetime, to_datetime):
else:
obs_dict = {
'metric': metric,
'observation_type': unit.observation_type.model,
'observation_model': unit.observation_type.model,
'observations': obs_qs
}
return obs_dict
Expand Down
4 changes: 2 additions & 2 deletions metrics/models.py
Expand Up @@ -138,13 +138,13 @@ def _get_datapoint(self):

data = property(_get_datapoint)

def _get_observationtype(self):
def _get_observationmodel(self):
if self.metric:
if self.metric.unit:
return self.metric.unit.observation_type.model
return None
return None
observation_type = property(_get_observationtype)
observation_model = property(_get_observationmodel)

class Meta:
ordering = ('-from_datetime',)
Expand Down
10 changes: 5 additions & 5 deletions templates/dashboard/project_detail.html
Expand Up @@ -12,7 +12,7 @@ <h1>{{ object.project.name }}</h1>
</thead>
<tbody>
<tr class='primary'>{% for datap in object.observations %}
<td class='{{ datap.metric.unit.slug }} {{ datap.observation_type }}{% if datap.past %} {% spaceless %}
<td class='{{ datap.metric.unit.slug }} {{ datap.observation_model }}{% if datap.past %} {% spaceless %}
{% if datap.past.data > datap.data %}
decreased
{% else %}{% if datap.past.data < datap.data %}
Expand All @@ -27,7 +27,7 @@ <h1>{{ object.project.name }}</h1>
{% else %}
Unchanged
{% endif %}{% endif %}
{% endspaceless %} from {% include 'dashboard/snippets/_render_observation_value.html' with datap=datap.past %} in the previous period"{% endif %}>
{% endspaceless %} from {% include 'dashboard/snippets/_render_observation_value.html' with datap=datap.past %} in the previous date range"{% endif %}>
{% if datap %}{% spaceless %}{% include "dashboard/snippets/_render_observation.html" %}{% endspaceless %}{% else %}&nbsp;{% endif %}
</td>{% endfor %}
<td>&nbsp;</td>
Expand All @@ -45,9 +45,9 @@ <h2>Annotations</h2>
{% for datap in object.observations %}{% if "observations" in datap %}
<section id="{{ datap.metric.unit.slug }}" class="unit-observations">
<h2>{{ datap.metric.unit.name|title }}</h2>
{% ifnotequal datap.observation_type "objectobservation" %}
{% ifnotequal datap.observation_model "objectobservation" %}
<div class="aggregate-value">
{% ifequal datap.observation_type "ratioobservation" %}
{% ifequal datap.observation_model "ratioobservation" %}
<p class="observation" data-value="{{ datap.data }}">{% spaceless %}{% ifequal datap.data.antecedent.metric.unit.observation_unit "seconds" %}
{{ datap.data|secondstoduration }}
{% else %}
Expand All @@ -59,7 +59,7 @@ <h2>{{ datap.metric.unit.name|title }}</h2>
{% endifequal %}
{% endspaceless %}</p>
{% endifequal %}
{% ifequal datap.observation_type "countobservation" %}
{% ifequal datap.observation_model "countobservation" %}
<p class="observation" data-value="{{ datap.data }}">{% spaceless %}{% ifequal datap.metric.unit.observation_unit "seconds" %}
{{ datap.data|secondstoduration }}
{% else %}
Expand Down
4 changes: 2 additions & 2 deletions templates/dashboard/project_list.html
Expand Up @@ -58,13 +58,13 @@
{% else %}
Unchanged
{% endif %}{% endif %}
{% endspaceless %} from {% include "dashboard/snippets/_render_observation_value.html" with datap=datap.past %} in the previous period"{% endif %}>
{% endspaceless %} from {% include "dashboard/snippets/_render_observation_value.html" with datap=datap.past %} in the previous date range"{% endif %}>
{% spaceless %}{% include "dashboard/snippets/_render_observation.html" %}{% endspaceless %}
</td>{% endfor %}
</tr>{% for datap in object.extra_observations %}{% if datap and datap.data %}
<tr class="secondary {% if forloop.first %}first{% endif %}">
<th colspan=2 scope=row>{{ datap.metric.unit.name }}</th>
<td class='{{ datap.metric.unit.slug }} {{ datap.observation_type }}' colspan={{ numcols|add:"-1" }}>
<td class='{{ datap.metric.unit.slug }} {{ datap.observation_model }}' colspan={{ numcols|add:"-1" }}>
{% spaceless %}{% include "dashboard/snippets/_render_observation.html" with list_slice=":10" %}{% endspaceless %}
</td>
</tr>{% endif %}{% endfor %}{% if object.annotations %}
Expand Down
6 changes: 3 additions & 3 deletions templates/dashboard/snippets/_render_observation.html
Expand Up @@ -3,12 +3,12 @@
{% ifequal datap None %}
<p class="nodata">[no data]</p>
{% else %}
{% ifequal datap.observation_type "objectobservation" %}
{% ifequal datap.observation_model "objectobservation" %}
<ol class="observation">{% for item in datap.data|slice:slice_val %}
<li {% if forloop.last %}class="last"{% endif %}>{{ item.name|urlizetrunc:25 }}&nbsp;{% if item.value %}<span>[{{ item.value }}]</span>{% endif %}</li>{% endfor %}
</ol>
{% endifequal %}
{% ifequal datap.observation_type "ratioobservation" %}
{% ifequal datap.observation_model "ratioobservation" %}
<p class="observation" data-value="{{ datap.data }}">{% spaceless %}{% ifequal datap.antecedent.metric.unit.observation_unit "seconds" %}
{{ datap.data|secondstoduration }}
{% else %}
Expand All @@ -20,7 +20,7 @@
{% endifequal %}
{% endspaceless %}</p>
{% endifequal %}
{% ifequal datap.observation_type "countobservation" %}
{% ifequal datap.observation_model "countobservation" %}
<p class="observation" data-value="{{ datap.data }}">{% spaceless %}{% ifequal datap.metric.unit.observation_unit "seconds" %}
{{ datap.data|secondstoduration }}
{% else %}
Expand Down
6 changes: 3 additions & 3 deletions templates/dashboard/snippets/_render_observation_value.html
@@ -1,7 +1,7 @@
{% load dashboard_filters humanize %}
{% with slice_val=list_slice|default:":5" %}{% spaceless %}
{% ifequal datap.observation_type "objectobservation" %}{{ datap.data }}{% endifequal %}
{% ifequal datap.observation_type "ratioobservation" %}
{% ifequal datap.observation_model "objectobservation" %}{{ datap.data }}{% endifequal %}
{% ifequal datap.observation_model "ratioobservation" %}
{% ifequal datap.antecedent.metric.unit.observation_unit "seconds" %}
{{ datap.data|secondstoduration }}
{% else %}
Expand All @@ -12,7 +12,7 @@
{% endifequal %}
{% endifequal %}
{% endifequal %}
{% ifequal datap.observation_type "countobservation" %}
{% ifequal datap.observation_model "countobservation" %}
{% ifequal datap.metric.unit.observation_unit "seconds" %}
{{ datap.data|secondstoduration }}
{% else %}
Expand Down

0 comments on commit 13578c0

Please sign in to comment.