def add_comment(request, utm_campaign): try: comments = MySQLdb._mysql.escape_string(request.POST['comments']) except: return HttpResponseRedirect(reverse('tests.views.index')) """ Retrieve the report """ ttl = DL.TestTableLoader() row = ttl.get_test_row(utm_campaign) html_string = ttl.get_test_field(row, 'html_report') """ Insert comment into the page html """ new_html = '' lines = html_string.split('\n') now = datetime.datetime.utcnow().__str__() for line in lines: if line == '<!-- Cend -->': line = '<div class="spacer"></div>\n<div class="spacer"></div>\n' + comments + '\n<div class="spacer"></div> --' + now + '\n<!-- Cend -->' new_html = new_html + line + '\n' html_string = new_html html_string = html_string.replace('"', '\\"') # parse the html for <!-- Cbegin --> <!-- Cend --> # add the comment above this """ Update the report """ 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'), \ 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) return HttpResponse(new_html)
def test_summaries(request): """ Initialize TestTableLoader """ ttl = DL.TestTableLoader() test_rows = ttl.get_all_test_rows() """ Process test info / write html """ html = '' for row in test_rows: 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') end_time = ttl.get_test_field(row, 'end_time') try: test_type = FDH._TESTTYPE_VERBOSE_[test_type] except: test_type = 'unknown' pass try: summary_table = ttl.get_test_field( row, 'html_report').split('<!-- SUMMARY TABLE MARKER -->')[1] html = html + '<h1><u>' + test_name + ' -- ' + utm_campaign + '</u></h1><div class="spacer"></div>' \ + '<font size="4"><u>Test Type:</u> ' + test_type + '</font><div class="spacer_small"></div>' \ + '<font size="4"><u>Running from</u> ' + start_time + ' <u>to</u> ' + end_time + '</font><div class="spacer"></div>' \ + summary_table + '<div class="spacer"></div><div class="spacer"></div>' except: pass return render_to_response('tests/test_summaries.html', {'template': html}, context_instance=RequestContext(request))
def show_report(request, utm_campaign): ttl = DL.TestTableLoader() row = ttl.get_test_row(utm_campaign) try: html = row[7] except: html = '<br><br><center><p><b>Was unable to retrieve report</b></p><br><br><a href="/">Home</a></center>' return HttpResponse(html)
def show_campaigns(request, utm_campaign, **kwargs): """ PROCESS POST KWARGS =================== """ err_msg = '' try: err_msg = str(kwargs['kwargs']['err_msg']) except: pass test_type_override = '' try: test_type_override = MySQLdb._mysql.escape_string( request.POST['test_type_override']) if test_type_override == 'Banner': test_type_var = FDH._TESTTYPE_BANNER_ elif test_type_override == 'Landing Page': test_type_var = FDH._TESTTYPE_LP_ elif test_type_override == 'Banner and LP': test_type_var = FDH._TESTTYPE_BANNER_LP_ except: test_type_var = '' pass try: """ Find the earliest and latest page views for a given campaign """ lptl = DL.LandingPageTableLoader() ccrml = DL.CiviCRMLoader() start_time = ccrml.get_earliest_donation(utm_campaign) end_time = ccrml.get_latest_donation(utm_campaign) one_step = lptl.is_one_step(start_time, end_time, utm_campaign) if not (one_step): start_time = lptl.get_earliest_campaign_view(utm_campaign) end_time = lptl.get_latest_campaign_view(utm_campaign) interval = 1 """ Create reporting object to retrieve campaign data and write plots to image repo on disk """ ir = DR.IntervalReporting(was_run=False, use_labels=False, font_size=20, plot_type='line', query_type='campaign', file_path=projSet.__web_home__ + 'campaigns/static/images/') """ Produce analysis on the campaign view data """ ir.run(start_time, end_time, interval, 'views', utm_campaign, {}, one_step=one_step) """ ESTIMATE THE START AND END TIME OF THE CAMPAIGN =============================================== Search for the first instance when more than 10 views are observed over a sampling period """ col_names = ir._data_loader_.get_column_names() views_index = col_names.index('views') ts_index = col_names.index('ts') row_list = list(ir._data_loader_._results_) # copy the query results for row in row_list: if row[views_index] > 100: start_time_est = row[ts_index] break row_list.reverse() for row in row_list: if row[views_index] > 100: end_time_est = row[ts_index] break """ BUILD THE VISUALIZATION FOR THE TEST VIEWS OF THIS CAMAPAIGN ============================================================ """ """ Read the test name """ ttl = DL.TestTableLoader() row = ttl.get_test_row(utm_campaign) test_name = ttl.get_test_field(row, 'test_name') """ Regenerate the data using the estimated start and end times """ ir = DR.IntervalReporting(was_run=False, use_labels=False, font_size=20, plot_type='line', query_type='campaign', file_path=projSet.__web_home__ + 'campaigns/static/images/') ir.run(start_time_est, end_time_est, interval, 'views', utm_campaign, {}, one_step=one_step) """ Determine the type of test (if not overridden) and retrieve the artifacts """ test_type, artifact_name_list = FDH.get_test_type( utm_campaign, start_time, end_time, DL.CampaignReportingLoader(query_type=''), test_type_var) return render_to_response('campaigns/show_campaigns.html', {'utm_campaign' : utm_campaign, 'test_name' : test_name, 'start_time' : start_time_est, 'end_time' : end_time_est, 'one_step' : one_step, \ 'artifacts' : artifact_name_list, 'test_type' : test_type, 'err_msg' : err_msg}, context_instance=RequestContext(request)) except Exception as inst: logging.error('Failed to correctly produce campaign diagnostics.') logging.error(type(inst)) logging.error(inst.args) logging.error(inst) """ Return to the index page with an error """ err_msg = 'There is insufficient data to analyze this campaign: %s. Check to see if the <a href="/LML/">impressions have been loaded</a>. <br><br>ERROR:<br><br>%s' % ( utm_campaign, inst.__str__()) return index(request, kwargs={'err_msg': err_msg})
def index(request, **kwargs): """ PROCESS POST DATA ================= """ if 'err_msg' in kwargs: err_msg = kwargs['err_msg'] else: err_msg = '' try: latest_utc_ts_var = MySQLdb._mysql.escape_string( request.POST['latest_utc_ts'].strip()) earliest_utc_ts_var = MySQLdb._mysql.escape_string( request.POST['earliest_utc_ts'].strip()) if not TP.is_timestamp(earliest_utc_ts_var, 1) or not TP.is_timestamp( earliest_utc_ts_var, 1): raise TypeError if latest_utc_ts_var == '': latest_utc_ts_var = _end_time_ except KeyError: earliest_utc_ts_var = _beginning_time_ latest_utc_ts_var = _end_time_ except TypeError: err_msg = 'Please enter a valid timestamp.' earliest_utc_ts_var = _beginning_time_ latest_utc_ts_var = _end_time_ ttl = DL.TestTableLoader() columns = ttl.get_column_names() test_rows = ttl.get_all_test_rows() """ Build a list of tests -- apply filters """ l = [] utm_campaign_index = ttl.get_test_index('utm_campaign') html_report_index = ttl.get_test_index('html_report') for i in test_rows: test_start_time = ttl.get_test_field(i, 'start_time') new_row = list(i) """ Ensure the timestamp is properly formatted """ if TP.is_timestamp(test_start_time, 2): test_start_time = TP.timestamp_convert_format( test_start_time, 2, 1) new_row[ html_report_index] = '<a href="/tests/report/%s">view</a>' % new_row[ utm_campaign_index] if int(test_start_time) > int(earliest_utc_ts_var) and int( test_start_time) < int(latest_utc_ts_var): l.append(new_row) l.reverse() test_table = DR.DataReporting()._write_html_table( l, columns, use_standard_metric_names=True) return render_to_response('tests/index.html', { 'err_msg': err_msg, 'test_table': test_table }, context_instance=RequestContext(request))
def test(request): try: """ PROCESS POST DATA ================= Escape all user input that can be entered in text fields """ test_name_var = MySQLdb._mysql.escape_string( request.POST['test_name'].strip()) utm_campaign_var = MySQLdb._mysql.escape_string( request.POST['utm_campaign'].strip()) start_time_var = MySQLdb._mysql.escape_string( request.POST['start_time'].strip()) end_time_var = MySQLdb._mysql.escape_string( request.POST['end_time'].strip()) one_step_var = MySQLdb._mysql.escape_string( request.POST['one_step'].strip()) country = MySQLdb._mysql.escape_string(request.POST['iso_filter']) """ Convert timestamp format if necessary """ if TP.is_timestamp(start_time_var, 2): start_time_var = TP.timestamp_convert_format(start_time_var, 2, 1) if TP.is_timestamp(end_time_var, 2): end_time_var = TP.timestamp_convert_format(end_time_var, 2, 1) if cmp(one_step_var, 'True') == 0: one_step_var = True else: one_step_var = False try: test_type_var = MySQLdb._mysql.escape_string( request.POST['test_type']) labels = request.POST['artifacts'] except KeyError: test_type_var, labels = FDH.get_test_type( utm_campaign_var, start_time_var, end_time_var, DL.CampaignReportingLoader( query_type='')) # submit an empty query type labels = labels.__str__() label_dict = dict() label_dict_full = dict() labels = labels[1:-1].split(',') """ Parse the labels """ for i in range(len(labels)): labels[i] = labels[i] label = labels[i].split('\'')[1] label = label.strip() pieces = label.split(' ') label = pieces[0] for j in range(len(pieces) - 1): label = label + '_' + pieces[j + 1] """ Escape the label parameters """ label = MySQLdb._mysql.escape_string(label) label_dict_full[label] = label """ Look at the artifact names and map them into a dict() - Determine if artifacts were chosen by the user """ if request.POST.__contains__('artifacts_chosen'): artifacts_chosen = request.POST.getlist('artifacts_chosen') """ Ensure that only two items are selected """ if len(artifacts_chosen) > 2: raise Exception( 'Please select (checkboxes) exactly two items to test') for elem in artifacts_chosen: esc_elem = MySQLdb._mysql.escape_string(str(elem)) label_dict[esc_elem] = esc_elem else: label_dict = label_dict_full """ Parse the added labels IF they are not empty """ for key in label_dict.keys(): try: if not (request.POST[key] == ''): label_dict[key] = MySQLdb._mysql.escape_string( str(request.POST[key])) else: label_dict[key] = key except: logging.error('Could not find %s in the POST QueryDict.' % key) for key in label_dict_full.keys(): try: if not (request.POST[key] == ''): label_dict_full[key] = MySQLdb._mysql.escape_string( str(request.POST[key])) else: label_dict_full[key] = key except: logging.error('Could not find %s in the POST QueryDict.' % key) """ EXECUTE REPORT GENERATION ========================= setup time parameters determine test metrics execute queries """ sample_interval = 1 start_time_obj = TP.timestamp_to_obj(start_time_var, 1) end_time_obj = TP.timestamp_to_obj(end_time_var, 1) time_diff = end_time_obj - start_time_obj time_diff_min = time_diff.seconds / 60.0 test_interval = int(math.floor(time_diff_min / sample_interval)) # 2 is the interval metric_types = FDH.get_test_type_metrics(test_type_var) metric_types_full = dict() """ Get the full (descriptive) version of the metric names !! FIXME / TODO -- order these properly !! """ for i in range(len(metric_types)): metric_types_full[metric_types[i]] = QD.get_metric_full_name( metric_types[i]) """ USE generate_reporting_objects() TO GENERATE THE REPORT DATA - dependent on test type """ measured_metric, winner, loser, percent_win, confidence, html_table_pm_banner, html_table_pm_lp, html_table_language, html_table \ = generate_reporting_objects(test_name_var, start_time_var, end_time_var, utm_campaign_var, label_dict, label_dict_full, \ sample_interval, test_interval, test_type_var, metric_types, one_step_var, country) winner_var = winner[0] results = list() for index in range(len(winner)): results.append({ 'metric': measured_metric[index], 'winner': winner[index], 'loser': loser[index], 'percent_win': percent_win[index], 'confidence': confidence[index] }) template_var_dict = {'results' : results, \ 'utm_campaign' : utm_campaign_var, 'metric_names_full' : metric_types_full, \ 'summary_table': html_table, 'sample_interval' : sample_interval, \ 'banner_pm_table' : html_table_pm_banner, 'lp_pm_table' : html_table_pm_lp, 'html_table_language' : html_table_language, \ 'start_time' : TP.timestamp_convert_format(start_time_var, 1, 2) , 'end_time' : TP.timestamp_convert_format(end_time_var, 1, 2)} html = render_to_response('tests/results_' + test_type_var + '.html', template_var_dict, context_instance=RequestContext(request)) """ WRITE TO TEST TABLE =================== """ ttl = DL.TestTableLoader() """ Format the html string """ html_string = html.__str__() html_string = html_string.replace('"', '\\"') if ttl.record_exists(utm_campaign=utm_campaign_var): 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, winner=winner_var) else: 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, winner=winner_var) return html except Exception as inst: logging.error('Failed to correctly generate test report.') logging.error(type(inst)) logging.error(inst.args) logging.error(inst) """ Return to the index page with an error """ try: err_msg = 'Test Generation failed for: %s. Check the fields submitted for generation. <br><br>ERROR:<br><br>%s' % ( utm_campaign_var, inst.__str__()) except: err_msg = 'Test Generation failed. Check the fields submitted for generation. <br><br>ERROR:<br><br>%s' % inst.__str__( ) return campaigns_index(request, kwargs={'err_msg': err_msg}) return show_campaigns(request, utm_campaign_var, kwargs={'err_msg': err_msg})