def check_graphite_data(request, test_id): if not ServerMonitoringData.objects.filter( test_id=test_id, source='graphite', data_resolution_id=1).exists() or not TestData.objects.filter( test_id=test_id, source='graphite', data_resolution_id=1).exists(): result = update_test_graphite_data(test_id) response = { "message": { "text": "Graphite data was updated", "type": "success", "msg_params": { "result": result } } } else: response = { "message": { "text": "Graphite data is already exists", "type": "success", "msg_params": {} } } return JsonResponse(response, safe=False)
def get_compare_tests_aggregate_data(test_id, num_of_tests, order='-test__start_time', source='default'): ''' Compares given test with test_id against num_of_tests previous ''' project = Test.objects.filter(id=test_id).values('project_id') start_time = Test.objects.filter( id=test_id).values('start_time')[0]['start_time'] project_id = project[0]['project_id'] if source == 'default': data = TestData.objects. \ filter(test__start_time__lte=start_time, test__project_id=project_id, test__show=True, source=source).\ annotate(display_name=F('test__display_name')). \ annotate(start_time=F('test__start_time')). \ values('display_name', 'start_time'). \ annotate(average=Sum(RawSQL("((data->>%s)::numeric)", ('avg',))*RawSQL("((data->>%s)::numeric)", ('count',)))/Sum(RawSQL("((data->>%s)::numeric)", ('count',)))). \ annotate(median=Sum(RawSQL("((data->>%s)::numeric)", ('median',))*RawSQL("((data->>%s)::numeric)", ('count',)))/Sum(RawSQL("((data->>%s)::numeric)", ('count',)))). \ order_by(order)[:num_of_tests] elif source == 'graphite': tests = Test.objects.filter( start_time__lte=start_time, project_id=project_id, show=True).values().order_by('-start_time')[:num_of_tests] print tests for t in tests: test_id = t['id'] if not ServerMonitoringData.objects.filter( test_id=test_id, source='graphite').exists() or not TestData.objects.filter( test_id=test_id, source='graphite').exists(): result = update_test_graphite_data(test_id) data = TestData.objects. \ filter(test__start_time__lte=start_time, test__project_id=project_id, test__show=True, source=source).\ annotate(display_name=F('test__display_name')). \ annotate(start_time=F('test__start_time')). \ values('display_name', 'start_time'). \ annotate(average=Avg(RawSQL("((data->>%s)::numeric)", ('avg',)))). \ annotate(median=Avg(RawSQL("((data->>%s)::numeric)", ('median',)))). \ order_by(order).order_by(order)[:num_of_tests] return data
def tests_compare_report(request, test_id_1, test_id_2): ''' Compare current test (test_id_1) with one of the previous ''' report = { 'absense': [], 'higher_response_times': [], 'lower_response_times': [], 'lower_count': [], 'new_action_in_test': [], 'cpu_steal': [], } sp = int( Configuration.objects.get(name='signifficant_actions_compare_percent') .value) if not ServerMonitoringData.objects.filter( test_id=test_id_1, source='graphite').exists(): result = update_test_graphite_data(test_id_1) if not ServerMonitoringData.objects.filter( test_id=test_id_2, source='graphite').exists(): result = update_test_graphite_data(test_id_2) cpu_steal_data = ServerMonitoringData.objects.filter( test_id__in=[test_id_1, test_id_2], source='graphite').annotate( server_name=F('server__server_name'), test_name=F('test__display_name')).values( 'server_name', 'test_name').annotate(cpu_steal=Avg( RawSQL("((data->>%s)::float)", ('CPU_steal', )))) for d in cpu_steal_data: if d['cpu_steal'] > 0: # ?? report['cpu_steal'].append({ 'server_name': d['server_name'], 'test_name': d['test_name'], 'cpu_steal': d['cpu_steal'], "severity": "danger", }) test_1_actions = list( TestActionAggregateData.objects.annotate(url=F('action__url')) .filter(test_id=test_id_1).values('action_id', 'url', 'data')) test_2_actions = list( TestActionAggregateData.objects.annotate(url=F('action__url')) .filter(test_id=test_id_2).values('action_id', 'url', 'data')) for action in test_1_actions: # Check for new actions executed during the last test action_id = action['action_id'] action_url = action['url'] if not TestActionAggregateData.objects.filter( test_id=test_id_2, action_id=action_id).exists( ) and TestActionAggregateData.objects.filter( test_id=test_id_1, action_id=action_id).exists(): report['new_action_in_test'].append({ "action": action_url, "severity": "danger", }) for action in test_2_actions: action_id = action['action_id'] action_url = action['url'] action_data_2 = action['data'] # Check if one of the actions were not executed if not TestActionAggregateData.objects.filter( test_id=test_id_1, action_id=action_id).exists(): report['absense'].append({ "action": action_url, "severity": "danger", }) else: action_data_1 = list( TestActionAggregateData.objects.filter( test_id=test_id_1, action_id=action_id).values('data'))[0][ 'data'] # Student t-criteria Xa = action_data_1['mean'] Xb = action_data_2['mean'] Sa = 0 if action_data_1['std'] is None else action_data_1['std'] Sb = 0 if action_data_2['std'] is None else action_data_2['std'] Na = action_data_1['count'] Nb = action_data_2['count'] # df = Na - 1 + Nb - 1 # Satterthwaite Formula for Degrees of Freedom if Xa > 10 and Xb > 10 and not Sa == 0 and not Sb == 0: df = math.pow( math.pow(Sa, 2) / Na + math.pow(Sb, 2) / Nb, 2) / ( math.pow(math.pow(Sa, 2) / Na, 2) / (Na - 1) + math.pow(math.pow(Sb, 2) / Nb, 2) / (Nb - 1)) if df > 0: t = stats.t.ppf(1 - 0.01, df) logger.debug( 'Action: {0} t: {1} Xa: {2} Xb: {3} Sa: {4} Sb: {5} Na: {6} Nb: {7} df: {8}'. format(action_url, stats.t.ppf(1 - 0.025, df), Xa, Xb, Sa, Sb, Na, Nb, df)) Sab = math.sqrt(((Na - 1) * math.pow(Sa, 2) + (Nb - 1) * math.pow(Sb, 2)) / df) Texp = (math.fabs(Xa - Xb)) / ( Sab * math.sqrt(1 / Na + 1 / Nb)) logger.debug('Action: {0} Texp: {1} Sab: {2}'.format( action_url, Texp, Sab)) if Texp > t: diff_percent = abs(100 - 100 * Xa / Xb) if Xa > Xb: if diff_percent > sp: if diff_percent > 10: severity = "danger" else: severity = "warning" report["higher_response_times"].append({ "action": action_url, "severity": severity, "action_data_1": action_data_1, "action_data_2": action_data_2, }) else: if diff_percent > sp: report["lower_response_times"].append({ "action": action_url, "severity": "success", "action_data_1": action_data_1, "action_data_2": action_data_2, }) if Na / 100 * Nb < 90: report["lower_count"].append({ "action": action_url, "severity": "warning", "action_data_1": action_data_1, "action_data_2": action_data_2, }) return render( request, 'compare_report.html', { 'report': report, 'test_1': Test.objects.get(id=test_id_1), 'test_2': Test.objects.get(id=test_id_2), })