Thanks Tim. Your reply is quite helpful.


On Jul 30, 6:25 pm, Tim Chase <django.us...@tim.thechases.com> wrote:
> > al_1 = Alert.objects.filter(creation_date__regex=today).values
> > ('dimension1').annotate(Sum('metric1')).order_by('dimension1')
>
> > and
>
> > al_2 = Alert.objects.filter(creation_date__regex=yesterday).values
> > ('dimension1').annotate(Sum('metric1')).order_by('dimension1')
>
> > They are almost the same except that al_1 is for today's data and al_2
> > is for yesterday's data. They summarize data in column "metric1" that
> > is grouped and ordered by "dimension1".
>
> > Sample data from al_1:
>
> > [{'metric1__sum': 0.0, 'dimension1': u'1'}, {'metric1__sum': 14.0,
> [snip]
> > What I need to do is to find a ratio value for each "dimension1"
> > between its today's value and yesterday's value.  For example, for
> > dimension1 = '110085', I need to find 2758.0 / 2658.0 = 1.04.
> > Then I compare this value "1.04" with a threshold and process
> > further.
>
> You can do something like
>
>  >>> map1 = dict((d['dimension1'], d['metric1__sum']) for d in al_1)
>  >>> map2 = dict((d['dimension1'], d['metric1__sum']) for d in al_2)
>  >>> ratios = [(dim, metric/map2[dim]) for dim, metric in
> map1.iteritems() if map2.get(dim, 0) != 0]
>  >>> results = [(dim, ratio) for dim, ratio in ratios if ratio >=
> threshold]
>
> It may lose ordering if that is significant (dicts aren't ordered
> by default), and skips over those where there are problems (items
> don't exist in the opposite map, or the previous value was 0
> making for a divide-by-zero problem).
>
> Things might change a little depending on what you'd expect on
> those edge cases (dim in a1 but not in a2; dim in a2 but not in
> a1; a2.metric = 0)
>
> -tim- Hide quoted text -
>
> - Show quoted text -
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to