@@ -40,13 +40,58 @@ def get_env_total(sql_filter):
40
40
41
41
success_totals = db .session .query (db .func .sum (Agg_Request .success_count ).label ('total' )).filter (Agg_Request .call_code .like (sql_filter )).group_by (Agg_Request .aggr_id ).all ()
42
42
43
- env_total = 0
43
+ env_total = Decimal ( 0 )
44
44
45
45
for success_total in success_totals :
46
- env_total += success_total .total
46
+ env_total += Decimal ( success_total .total )
47
47
48
48
return env_total
49
49
50
+ def calc_call_volume (sql_filter ):
51
+
52
+ sql_filter = sql_filter
53
+
54
+ env_agg_requests = get_agg_request (sql_filter )
55
+ env_total = get_env_total (sql_filter )
56
+
57
+ env_call_volumes = {}
58
+ for agg_request in env_agg_requests :
59
+ env_call_volumes [agg_request .call_code ] = Decimal (agg_request .success_count ) / env_total
60
+
61
+ return env_call_volumes
62
+
63
+
64
+ def get_weighted_avg_latency (sql_filter ):
65
+
66
+ sql_filter = sql_filter
67
+
68
+ # Get the latency for each call. Returns a list.
69
+ all_latency = db .session .query (Agg_Request .avg_response_time ).filter (Agg_Request .call_code .like (sql_filter )).all ()
70
+
71
+ # Intitialize the total_latency variable
72
+ total_latency = Decimal (0 )
73
+
74
+ # Iterate through each item in the list and add it to the total
75
+ # for latency in all_latency:
76
+ # total_latency += latency[0]
77
+
78
+ # Get the volume percent for each call
79
+ # Returns a dictionary
80
+ env_call_volumes = calc_call_volume (sql_filter )
81
+
82
+ # Multiply the latency by the volume percent for each call
83
+ # Add those together and divide by the number of calls
84
+ # Return the weighted latency
85
+
86
+ weighted_avg_latency = Decimal (0 )
87
+
88
+ for key in env_call_volumes :
89
+ for latency in all_latency [0 ]:
90
+ weighted_avg_latency += (env_call_volumes [key ] * latency ) / Decimal (len (all_latency ))
91
+
92
+ return weighted_avg_latency
93
+
94
+ # avg_latency = total_latency / len(all_latency)
50
95
51
96
############################### Flask Routes ###################################
52
97
@@ -56,14 +101,19 @@ def index():
56
101
57
102
sql_filter = '%prod%'
58
103
59
- all_latency = db .session .query (Agg_Request .avg_response_time ).filter (Agg_Request .call_code .like (sql_filter )).all ()
104
+ # all_latency = db.session.query(Agg_Request.avg_response_time).filter(Agg_Request.call_code.like(sql_filter)).all()
60
105
61
- total_latency = Decimal (0 )
106
+ # total_latency = Decimal(0)
62
107
63
- for latency in all_latency :
64
- total_latency += latency [0 ]
108
+ # for latency in all_latency:
109
+ # total_latency += latency[0]
65
110
66
- avg_latency = total_latency / len (all_latency )
111
+ # avg_latency = total_latency / len(all_latency)
112
+
113
+ avg_latency = get_weighted_avg_latency (sql_filter )
114
+
115
+ # Compare avg_latency to a range of values.
116
+ # Based on place in range, choose green/yellow/red icon.
67
117
68
118
if avg_latency < 200 :
69
119
overall_status = 'green'
@@ -75,10 +125,7 @@ def index():
75
125
overall_status = 'red'
76
126
status_icon = 'fa-flash'
77
127
78
- # Compare avg_latency to a range of values.
79
- # Based on place in range, choose green/yellow/red icon.
80
128
# Pass icon to template.
81
-
82
129
return render_template ("homepage.html" , avg_latency = avg_latency , overall_status = overall_status , status_icon = status_icon )
83
130
84
131
0 commit comments