Index: trunk/fundraiser-statistics/fundraiser-scripts/web_reporting/tests/views.py |
— | — | @@ -1,4 +1,4 @@ |
2 | | -from django.shortcuts import render_to_response |
| 2 | +from django.shortcuts import render_to_response, redirect |
3 | 3 | from django.http import Http404 |
4 | 4 | from django.shortcuts import render_to_response, get_object_or_404 |
5 | 5 | from django.template import RequestContext |
— | — | @@ -6,38 +6,38 @@ |
7 | 7 | from django.core.urlresolvers import reverse |
8 | 8 | |
9 | 9 | import sys |
10 | | -# sys.path.append('/home/rfaulkner/trunk/projects/') |
11 | | -# sys.path.append('/home/rfaulkner/trunk/projects/Fundraiser_Tools/classes') |
12 | | - |
13 | 10 | import math |
14 | 11 | import os |
| 12 | +import getopt |
| 13 | +import re |
| 14 | +import datetime |
15 | 15 | |
16 | 16 | import Fundraiser_Tools.classes.Helper as Hlp |
17 | 17 | import Fundraiser_Tools.classes.DataLoader as DL |
| 18 | +import Fundraiser_Tools.classes.DataReporting as DR |
| 19 | +import Fundraiser_Tools.classes.FundraiserDataHandler as FDH |
18 | 20 | import Fundraiser_Tools.classes.TimestampProcessor as TP |
19 | 21 | import Fundraiser_Tools.settings as projSet |
20 | | -import Fundraiser_Tools.build_report as br |
21 | 22 | import operator |
22 | 23 | |
23 | 24 | |
24 | 25 | """ |
25 | | - <description> |
| 26 | + Index page for tests ... lists all existing tests and allows a new test to be run |
| 27 | +""" |
| 28 | +def index(request): |
26 | 29 | |
27 | | - INPUT: |
| 30 | + ttl = DL.TestTableLoader() |
| 31 | + test_rows = ttl.get_all_test_rows() |
28 | 32 | |
29 | | - RETURN: |
| 33 | + return render_to_response('tests/index.html', {'test_rows' : test_rows}, context_instance=RequestContext(request)) |
30 | 34 | |
31 | | -""" |
32 | | -def index(request): |
33 | | - return render_to_response('tests/index.html', context_instance=RequestContext(request)) |
34 | 35 | |
| 36 | + |
| 37 | + |
| 38 | + |
35 | 39 | """ |
36 | | - <description> |
| 40 | + Executes a test, builds a report, and adds it to the test table |
37 | 41 | |
38 | | - INPUT: |
39 | | - |
40 | | - RETURN: |
41 | | - |
42 | 42 | """ |
43 | 43 | def test(request): |
44 | 44 | |
— | — | @@ -51,18 +51,30 @@ |
52 | 52 | utm_campaign_var = request.POST['utm_campaign'] |
53 | 53 | start_time_var = request.POST['start_time'] |
54 | 54 | end_time_var = request.POST['end_time'] |
55 | | - test_type = request.POST['test_type'] |
56 | 55 | |
57 | | - labels = request.POST['artifacts'][1:-1].split(',') |
| 56 | + try: |
| 57 | + test_type_var = request.POST['test_type'] |
| 58 | + labels = request.POST['artifacts'] |
| 59 | + |
| 60 | + |
| 61 | + except KeyError as e: |
| 62 | + |
| 63 | + os.chdir(projSet.__project_home__ + '/Fundraiser_Tools/classes') |
| 64 | + test_type_var, labels = FDH.get_test_type(utm_campaign_var, start_time_var, end_time_var) |
| 65 | + os.chdir(projSet.__project_home__ + '/Fundraiser_Tools/web_reporting') |
| 66 | + |
| 67 | + labels = labels.__str__() |
| 68 | + |
| 69 | + labels = labels[1:-1].split(',') |
58 | 70 | label_dict = dict() |
59 | | - |
| 71 | + |
60 | 72 | for i in range(len(labels)): |
61 | 73 | label = labels[i].split('\'')[1] |
62 | 74 | label = label.strip() |
63 | 75 | label_dict[label] = label |
64 | | - |
65 | | - except: |
66 | 76 | |
| 77 | + except KeyError as e: |
| 78 | + """ flag an error here for the user """ |
67 | 79 | return HttpResponseRedirect(reverse('tests.views.index')) |
68 | 80 | # pass |
69 | 81 | |
— | — | @@ -81,22 +93,22 @@ |
82 | 94 | |
83 | 95 | os.chdir(projSet.__project_home__ + '/Fundraiser_Tools/web_reporting') |
84 | 96 | |
85 | | - metric_names = Hlp.get_test_type_metrics(test_type) |
| 97 | + metric_types = FDH.get_test_type_metrics(test_type_var) |
86 | 98 | |
87 | | - if test_type == 'banner': |
| 99 | + if test_type_var == FDH._TESTTYPE_BANNER_: |
88 | 100 | |
89 | | - winner_dpi, percent_win_dpi, conf_dpi, winner_api, percent_win_api, conf_api = br.auto_gen(test_name=test_name_var, start_time_var, end_time_var, utm_campaign_var, label_dict, 2, test_interval) |
| 101 | + winner_dpi, percent_win_dpi, conf_dpi, winner_api, percent_win_api, conf_api = auto_gen(test_name_var, start_time_var, end_time_var, utm_campaign_var, label_dict, 2, test_interval, test_type_var, metric_types) |
90 | 102 | |
91 | | - html = render_to_response('tests/results.html', {'winner' : winner_dpi, 'percent_win_dpi' : '%.2f' % percent_win_dpi, 'percent_win_api' : '%.2f' % percent_win_api, 'conf_dpi' : conf_dpi, 'conf_api' : conf_api, 'utm_campaign' : utm_campaign_var, \ |
92 | | - 'metric_names' : metric_names}) |
93 | | - elif test_type == 'LP': |
| 103 | + html = render_to_response('tests/results_' + FDH._TESTTYPE_BANNER_ + '.html', {'winner' : winner_dpi, 'percent_win_dpi' : '%.2f' % percent_win_dpi, 'percent_win_api' : '%.2f' % percent_win_api, 'conf_dpi' : conf_dpi, 'conf_api' : conf_api, 'utm_campaign' : utm_campaign_var, \ |
| 104 | + 'metric_names' : metric_types}, context_instance=RequestContext(request)) |
| 105 | + elif test_type_var == FDH._TESTTYPE_LP_: |
94 | 106 | |
95 | | - winner_dpi, percent_win_dpi, conf_dpi, winner_api, percent_win_api, conf_api = br.auto_gen(test_name=test_name_var, start_time_var, end_time_var, utm_campaign_var, label_dict, 2, test_interval) |
| 107 | + winner_dpv, percent_win_dpv, conf_dpv, winner_apv, percent_win_apv, conf_apv = auto_gen(test_name_var, start_time_var, end_time_var, utm_campaign_var, label_dict, 2, test_interval, test_type_var, metric_types) |
96 | 108 | |
97 | | - html = render_to_response('tests/results.html', {'winner' : winner_dpi, 'percent_win_dpi' : '%.2f' % percent_win_dpi, 'percent_win_api' : '%.2f' % percent_win_api, 'conf_dpi' : conf_dpi, 'conf_api' : conf_api, 'utm_campaign' : utm_campaign_var, \ |
98 | | - 'metric_names' : metric_names}) |
99 | | - |
100 | | - """ Write to test table !! MODIFY -- handle updates !! """ |
| 109 | + html = render_to_response('tests/results_' + FDH._TESTTYPE_LP_ + '.html', {'winner' : winner_dpv, 'percent_win_dpv' : '%.2f' % percent_win_dpv, 'percent_win_apv' : '%.2f' % percent_win_apv, 'conf_dpv' : conf_dpv, 'conf_apv' : conf_apv, 'utm_campaign' : utm_campaign_var, \ |
| 110 | + 'metric_names' : metric_types}, context_instance=RequestContext(request)) |
| 111 | + |
| 112 | + """ Write to test table """ |
101 | 113 | |
102 | 114 | ttl = DL.TestTableLoader() |
103 | 115 | |
— | — | @@ -104,17 +116,168 @@ |
105 | 117 | html_string = html.__str__() |
106 | 118 | html_string = html_string.replace('"', '\\"') |
107 | 119 | html_string = Hlp.stringify(html_string) |
| 120 | + |
| 121 | +# Strips out whitespace |
| 122 | +# html_string_parts = html_string.split() |
| 123 | +# html_string = '' |
| 124 | +# for i in html_string_parts: |
| 125 | +# html_string = html_string + i |
108 | 126 | |
109 | | - html_string_parts = html_string.split() |
110 | | - html_string = '' |
111 | | - for i in html_string_parts: |
112 | | - html_string = html_string + i |
113 | | - |
114 | 127 | if ttl.record_exists(utm_campaign=utm_campaign_var): |
115 | | - ttl.update_test_row(test_name=test_name_var,utm_campaign=utm_campaign_var,start_time=start_time_var,end_time=end_time_var,html_report=html_string) |
| 128 | + ttl.update_test_row(test_name=test_name_var,test_type=test_type_var,utm_campaign=utm_campaign_var,start_time=start_time_var,end_time=end_time_var,html_report=html_string) |
116 | 129 | else: |
117 | | - ttl.insert_row(test_name=test_name_var,utm_campaign=utm_campaign_var,start_time=start_time_var,end_time=end_time_var,html_report=html_string) |
| 130 | + ttl.insert_row(test_name=test_name_var,test_type=test_type_var,utm_campaign=utm_campaign_var,start_time=start_time_var,end_time=end_time_var,html_report=html_string) |
118 | 131 | |
119 | 132 | return html |
120 | 133 | |
121 | | - |
\ No newline at end of file |
| 134 | + |
| 135 | +""" |
| 136 | + |
| 137 | + Helper method for 'test' view which generates a report |
| 138 | + |
| 139 | + INPUT: |
| 140 | + RETURN: |
| 141 | + |
| 142 | +""" |
| 143 | +def auto_gen(test_name, start_time, end_time, campaign, labels, sample_interval, test_interval, test_type, metric_types): |
| 144 | + |
| 145 | + # e.g. labels = {'Static banner':'20101227_JA061_US','Fading banner':'20101228_JAFader_US'} |
| 146 | + |
| 147 | + os.chdir('/home/rfaulkner/trunk/projects/Fundraiser_Tools/classes') |
| 148 | + |
| 149 | + use_labels_var = True |
| 150 | + if len(labels) == 0: |
| 151 | + use_labels_var = False |
| 152 | + |
| 153 | + """ Build reporting objects """ |
| 154 | + ir = DR.IntervalReporting(use_labels=use_labels_var,font_size=20,plot_type='step',file_path=projSet.__web_home__ + 'tests/static/images/') |
| 155 | + ir_cmpgn = DR.IntervalReporting(use_labels=False,font_size=20,plot_type='line',data_loader='campaign',file_path=projSet.__web_home__ + 'tests/static/images/') |
| 156 | + cr = DR.ConfidenceReporting(use_labels=use_labels_var,font_size=20,plot_type='line',hyp_test='t_test',file_path=projSet.__web_home__ + 'tests/static/images/') |
| 157 | + |
| 158 | + """ generate interval reporting plots """ |
| 159 | + |
| 160 | + |
| 161 | + # print 'Generating interval plots ...\n' |
| 162 | + for metric in metric_types: |
| 163 | + if test_type == FDH._TESTTYPE_BANNER_: |
| 164 | + ir.run(start_time, end_time, sample_interval, 'banner', metric, campaign, labels.keys()) |
| 165 | + if test_type == FDH._TESTTYPE_LP_: |
| 166 | + ir.run(start_time, end_time, sample_interval, 'LP', metric, campaign, labels.keys()) |
| 167 | + |
| 168 | + # print 'Generating campaign plots...\n' |
| 169 | + ir_cmpgn.run(start_time, end_time, sample_interval, 'campaign', 'views', campaign, []) |
| 170 | + ir_cmpgn.run(start_time, end_time, sample_interval, 'campaign', 'donations', campaign, []) |
| 171 | + |
| 172 | + """ generate confidence reporting plots """ |
| 173 | + # print 'Executing hypothesis testing ...\n' |
| 174 | + # cr.run('Fader VS Static','report_banner_confidence','don_per_imp','20101228JA075',{'Static banner':'20101227_JA061_US','Fading banner':'20101228_JAFader_US'},'20101229141000','20101229155000',2,10) |
| 175 | + if test_type == FDH._TESTTYPE_BANNER_: |
| 176 | + winner_dpi, percent_increase_dpi, confidence_dpi = cr.run(test_name,'report_banner_confidence','don_per_imp',campaign, labels, start_time, end_time, sample_interval,test_interval) |
| 177 | + winner_api, percent_increase_api, confidence_api = cr.run(test_name,'report_banner_confidence','amt50_per_imp',campaign, labels, start_time, end_time, sample_interval,test_interval) |
| 178 | + elif test_type == FDH._TESTTYPE_LP_: |
| 179 | + winner_dpi, percent_increase_dpi, confidence_dpi = cr.run(test_name,'report_LP_confidence','don_per_view',campaign, labels, start_time, end_time, sample_interval,test_interval) |
| 180 | + winner_api, percent_increase_api, confidence_api = cr.run(test_name,'report_LP_confidence','amt50_per_view',campaign, labels, start_time, end_time, sample_interval,test_interval) |
| 181 | + |
| 182 | + """ compose HTML """ |
| 183 | + |
| 184 | + os.chdir('/home/rfaulkner/trunk/projects/Fundraiser_Tools/classes/tests/') |
| 185 | + |
| 186 | + |
| 187 | + f = open('auto_report.html', 'w') |
| 188 | + |
| 189 | + html_script = '' |
| 190 | + |
| 191 | + html_script = html_script + '\n<html>\n<head>\n<title>Big Ass Reportin\'</title>' |
| 192 | + |
| 193 | + html_script = html_script + '</head>\n<body>\n<h1>Test Report</h1>\n<br>\n' |
| 194 | + |
| 195 | + html_script = html_script + '<h3><u>Interval Reporting</u></h3>\n' |
| 196 | + |
| 197 | + html_script = html_script + '<OBJECT WIDTH="1000" HEIGHT="600" data="' + campaign + '_banner_' + metric_types[0] + '.png" type="image/png">\n<p>.</p>\n</OBJECT><br>\n' |
| 198 | + html_script = html_script + '<OBJECT WIDTH="1000" HEIGHT="600" data="' + campaign + '_banner_' + metric_types[1] + '.png" type="image/png">\n<p>.</p>\n</OBJECT><br>\n' |
| 199 | + html_script = html_script + '<OBJECT WIDTH="1000" HEIGHT="600" data="' + campaign + '_banner_' + metric_types[2] + '.png" type="image/png">\n<p>.</p>\n</OBJECT><br>\n' |
| 200 | + html_script = html_script + '<OBJECT WIDTH="1000" HEIGHT="600" data="' + campaign + '_banner_' + metric_types[3] + '.png" type="image/png">\n<p>.</p>\n</OBJECT><br>\n' |
| 201 | + html_script = html_script + '<OBJECT WIDTH="1000" HEIGHT="600" data="' + campaign + '_campaign_views' + '.png" type="image/png">\n<p>.</p>\n</OBJECT><br>\n' |
| 202 | + html_script = html_script + '<OBJECT WIDTH="1000" HEIGHT="600" data="' + campaign + '_campaign_donations' + '.png" type="image/png">\n<p>.</p>\n</OBJECT><br>\n' |
| 203 | + |
| 204 | + html_script = html_script + '<h3><u>Confidence Reporting</u></h3>\n' |
| 205 | + |
| 206 | + html_script = html_script + '<OBJECT WIDTH="1000" HEIGHT="600" data="' + campaign + '_conf_don_per_imp' + '.png" type="image/png">\n<p>.</p>\n</OBJECT><br>\n' |
| 207 | + html_script = html_script + '<OBJECT WIDTH="1000" HEIGHT="600" data="' + campaign + '_conf_amt50_per_imp' + '.png" type="image/png">\n<p>.</p>\n</OBJECT><br>\n' |
| 208 | + |
| 209 | + """ !! MODIFY -- THIS currently doesn't look great !! |
| 210 | + |
| 211 | + f_test_results_1 = open(campaign + '_conf_don_per_imp' + '.txt', 'r') |
| 212 | + f_test_results_2 = open(campaign + '_conf_amt50_per_imp' + '.txt', 'r') |
| 213 | + |
| 214 | + data_results_1 = '' |
| 215 | + line = f_test_results_1.readline() |
| 216 | + while (line): |
| 217 | + data_results_1 = data_results_1 + line + '<br>' |
| 218 | + line = f_test_results_1.readline() |
| 219 | + |
| 220 | + data_results_2 = '' |
| 221 | + line = f_test_results_2.readline() |
| 222 | + while (line): |
| 223 | + data_results_2 = data_results_2 + line + '<br>' |
| 224 | + line = f_test_results_2.readline() |
| 225 | + |
| 226 | + html_script = html_script + data_results_1 + '<br><br>' |
| 227 | + html_script = html_script + data_results_2 |
| 228 | + |
| 229 | + """ |
| 230 | + |
| 231 | + html_script = html_script + '</body></html>\n' |
| 232 | + |
| 233 | + # print html_script |
| 234 | + |
| 235 | + f.write(html_script) |
| 236 | + |
| 237 | + f.close() |
| 238 | + |
| 239 | + return [winner_dpi, percent_increase_dpi, confidence_dpi, winner_api, percent_increase_api, confidence_api] |
| 240 | + |
| 241 | + |
| 242 | +""" |
| 243 | + Inserts a comment into an existing report |
| 244 | + |
| 245 | +""" |
| 246 | +def add_comment(request, utm_campaign): |
| 247 | + |
| 248 | + try: |
| 249 | + comments = request.POST['comments'] |
| 250 | + |
| 251 | + except: |
| 252 | + return HttpResponseRedirect(reverse('tests.views.index')) |
| 253 | + |
| 254 | + """ Retrieve the report """ |
| 255 | + ttl = DL.TestTableLoader() |
| 256 | + row = ttl.get_test_row(utm_campaign) |
| 257 | + html_string = ttl.get_test_field(row, 'html_report') |
| 258 | + |
| 259 | + """ Insert comment """ |
| 260 | + new_html = '' |
| 261 | + lines = html_string.split('\n') |
| 262 | + now = datetime.datetime.utcnow().__str__() |
| 263 | + |
| 264 | + for line in lines: |
| 265 | + |
| 266 | + if line == '<!-- Cend -->': |
| 267 | + line = '\n<br>' + comments + '\n<br><br> --' + now + '<br>\n' + '<!-- Cend -->' |
| 268 | + |
| 269 | + new_html = new_html + line + '\n' |
| 270 | + |
| 271 | + html_string = new_html |
| 272 | + html_string = html_string.replace('"', '\\"') |
| 273 | + html_string = Hlp.stringify(html_string) |
| 274 | + |
| 275 | + # parse the html for <!-- Cbegin --> <!-- Cend --> |
| 276 | + # add the comment above this |
| 277 | + |
| 278 | + """ Update the report """ |
| 279 | + ttl.update_test_row(test_name=ttl.get_test_field(row, 'test_name'), test_type=ttl.get_test_field(row, 'test_type'), utm_campaign=ttl.get_test_field(row, 'utm_campaign'), start_time=ttl.get_test_field(row, 'start_time'), \ |
| 280 | + end_time=ttl.get_test_field(row, 'end_time'), winner=ttl.get_test_field(row, 'winner'), is_conclusive=ttl.get_test_field(row, 'is_conclusive'), html_report=html_string) |
| 281 | + |
| 282 | + |
| 283 | + return HttpResponse(new_html) |
| 284 | + |
Index: trunk/fundraiser-statistics/fundraiser-scripts/web_reporting/tests/urls.py |
— | — | @@ -3,5 +3,7 @@ |
4 | 4 | |
5 | 5 | urlpatterns = patterns('', |
6 | 6 | (r'^$', 'tests.views.index'), |
7 | | - (r'^build_test$', 'tests.views.test') |
| 7 | + (r'^build_test$', 'tests.views.test'), |
| 8 | + (r'^report/(?P<utm_campaign>[a-zA-Z0-9_]+)$', 'campaigns.views.show_report'), |
| 9 | + (r'^report/comment/(?P<utm_campaign>[a-zA-Z0-9_]+)$', 'tests.views.add_comment'), |
8 | 10 | ) |