Index: trunk/fundraiser-statistics/fundraiser-scripts/web_reporting/live_results/views.py |
— | — | @@ -0,0 +1,145 @@ |
| 2 | + |
| 3 | +""" |
| 4 | + DJANGO VIEW DEFINITIONS: |
| 5 | + ======================== |
| 6 | + |
| 7 | + Defines the views for ongoing donations. This view interacts with templates serving embedded AJAX plotting to provide the user with a |
| 8 | + a running feed of campaign, banner, and landing page performance based on donations received. |
| 9 | + |
| 10 | + Views: |
| 11 | + |
| 12 | + index -- Queries the fundraiser database for donations and sends the data to the template to be displayed |
| 13 | + |
| 14 | + Helpers: |
| 15 | + |
| 16 | + get_data_lists -- composes the donation query data into a format expected by the template |
| 17 | + combine_data_lists -- combines the separate data sets from different queries into one in a format expected by the template |
| 18 | + |
| 19 | +""" |
| 20 | + |
| 21 | +__author__ = "Ryan Faulkner" |
| 22 | +__revision__ = "$Rev$" |
| 23 | +__date__ = "June 20th, 2011" |
| 24 | + |
| 25 | + |
| 26 | +from django.shortcuts import render_to_response |
| 27 | +from django.http import Http404 |
| 28 | +from django.shortcuts import render_to_response, get_object_or_404 |
| 29 | +from django.template import RequestContext |
| 30 | +from django.http import HttpResponseRedirect, HttpResponse |
| 31 | +from django.core.urlresolvers import reverse |
| 32 | + |
| 33 | +import sys |
| 34 | +import os |
| 35 | +import types |
| 36 | +import re |
| 37 | +import datetime |
| 38 | +import json |
| 39 | +from django.utils import simplejson |
| 40 | + |
| 41 | +import Fundraiser_Tools.classes.Helper as Hlp |
| 42 | +import Fundraiser_Tools.classes.DataReporting as DR |
| 43 | +import Fundraiser_Tools.classes.DataLoader as DL |
| 44 | +import Fundraiser_Tools.classes.DataMapper as DM |
| 45 | +import Fundraiser_Tools.classes.FundraiserDataThreading as FDT |
| 46 | +import Fundraiser_Tools.classes.FundraiserDataHandler as FDH |
| 47 | +import Fundraiser_Tools.classes.TimestampProcessor as TP |
| 48 | +import Fundraiser_Tools.settings as projSet |
| 49 | +import operator |
| 50 | + |
| 51 | + |
| 52 | +""" |
| 53 | + Index page for live results. |
| 54 | +""" |
| 55 | +def index(request): |
| 56 | + |
| 57 | + """ Get the donations for all campaigns over the last n hours """ |
| 58 | + end_time, start_time = TP.timestamps_for_interval(datetime.datetime.now() + datetime.timedelta(hours=4), 1, hours=-4) |
| 59 | + end_time = '20110617000000' |
| 60 | + start_time = '20110616160000' |
| 61 | + |
| 62 | + """ Create a interval loader objects """ |
| 63 | + ir_cmpgn = DR.IntervalReporting(query_type=FDH._QTYPE_CAMPAIGN_ + FDH._QTYPE_TIME_) |
| 64 | + ir_banner = DR.IntervalReporting(query_type=FDH._QTYPE_BANNER_ + FDH._QTYPE_TIME_) |
| 65 | + ir_lp = DR.IntervalReporting(query_type=FDH._QTYPE_LP_ + FDH._QTYPE_TIME_) |
| 66 | + |
| 67 | + """ Execute queries for campaign, banner, and landing page donations """ |
| 68 | + os.chdir(projSet.__project_home__ + '/classes') |
| 69 | + #ir.run('20110603120000', '20110604000000', 2, 'donations', '',[]) |
| 70 | + ir_cmpgn.run(start_time, end_time, 10, 'donations', '',[]) |
| 71 | + ir_banner.run(start_time, end_time, 10, 'donations', '',[]) |
| 72 | + ir_lp.run(start_time, end_time, 10, 'donations', '',[]) |
| 73 | + os.chdir(projSet.__home__) |
| 74 | + |
| 75 | + """ Extract data from interval reporting objects """ |
| 76 | + cmpgn_data_dict = get_data_lists(ir_cmpgn) |
| 77 | + cmpgn_banner_dict = get_data_lists(ir_banner) |
| 78 | + cmpgn_lp_dict = get_data_lists(ir_lp) |
| 79 | + |
| 80 | + """ combine the separate data sets """ |
| 81 | + dict_param = combine_data_lists([cmpgn_data_dict, cmpgn_banner_dict, cmpgn_lp_dict]) |
| 82 | + |
| 83 | + return render_to_response('live_results/index.html', dict_param, context_instance=RequestContext(request)) |
| 84 | + |
| 85 | + |
| 86 | +""" |
| 87 | + |
| 88 | + !! FIXME -- Move to Helper?? !! |
| 89 | + |
| 90 | +""" |
| 91 | +def get_data_lists(ir): |
| 92 | + |
| 93 | + """ Get metrics """ |
| 94 | + data = list() |
| 95 | + labels = '!' |
| 96 | + counts = list() |
| 97 | + max_data = 0 |
| 98 | + |
| 99 | + data_index = 0 |
| 100 | + for key in ir._counts_.keys(): |
| 101 | + |
| 102 | + data.append(list()) |
| 103 | + |
| 104 | + if key == None or key == '': |
| 105 | + labels = labels + 'empty?' |
| 106 | + else: |
| 107 | + labels = labels + key + '?' |
| 108 | + |
| 109 | + counts.append(len(ir._counts_[key])) |
| 110 | + |
| 111 | + for i in range(counts[data_index]): |
| 112 | + data[data_index].append([ir._times_[key][i], ir._counts_[key][i]]) |
| 113 | + if ir._counts_[key][i] > max_data: |
| 114 | + max_data = ir._counts_[key][i] |
| 115 | + |
| 116 | + data_index = data_index + 1 |
| 117 | + |
| 118 | + labels = labels + '!' |
| 119 | + |
| 120 | + return {'num_elems' : data_index, 'counts' : counts, 'labels' : labels, 'data' : data, 'max_data' : max_data} |
| 121 | + |
| 122 | + |
| 123 | +""" |
| 124 | + |
| 125 | + !! FIXME -- Move to Helper?? !! |
| 126 | + |
| 127 | +""" |
| 128 | +def combine_data_lists(dict_list): |
| 129 | + |
| 130 | + try: |
| 131 | + template_keys = dict_list[0].keys() |
| 132 | + num_elems = len(dict_list) |
| 133 | + except: |
| 134 | + print >> sys.stderr, projSet.__web_home__ + '/live_results/views.py: No template data found.' |
| 135 | + return -1 |
| 136 | + |
| 137 | + combined_dict = dict() |
| 138 | + |
| 139 | + for key in template_keys: |
| 140 | + key_list = list() |
| 141 | + for i in range(num_elems): |
| 142 | + key_list.append(dict_list[i][key]) |
| 143 | + combined_dict[key] = key_list |
| 144 | + |
| 145 | + return combined_dict |
| 146 | + |
Index: trunk/fundraiser-statistics/fundraiser-scripts/web_reporting/live_results/__init__.py |
Index: trunk/fundraiser-statistics/fundraiser-scripts/web_reporting/live_results/tests.py |
— | — | @@ -0,0 +1,16 @@ |
| 2 | +""" |
| 3 | +This file demonstrates writing tests using the unittest module. These will pass |
| 4 | +when you run "manage.py test". |
| 5 | + |
| 6 | +Replace this with more appropriate tests for your application. |
| 7 | +""" |
| 8 | + |
| 9 | +from django.test import TestCase |
| 10 | + |
| 11 | + |
| 12 | +class SimpleTest(TestCase): |
| 13 | + def test_basic_addition(self): |
| 14 | + """ |
| 15 | + Tests that 1 + 1 always equals 2. |
| 16 | + """ |
| 17 | + self.assertEqual(1 + 1, 2) |
Index: trunk/fundraiser-statistics/fundraiser-scripts/web_reporting/live_results/models.py |
— | — | @@ -0,0 +1,3 @@ |
| 2 | +from django.db import models |
| 3 | + |
| 4 | +# Create your models here. |
Index: trunk/fundraiser-statistics/fundraiser-scripts/web_reporting/live_results/urls.py |
— | — | @@ -0,0 +1,6 @@ |
| 2 | +from django.conf.urls.defaults import * |
| 3 | + |
| 4 | + |
| 5 | +urlpatterns = patterns('', |
| 6 | + (r'^$', 'live_results.views.index'), |
| 7 | +) |