Skip to content

Commit fd20c0a

Browse files
committed
Pulled latency calculations into a helper function (Closes #20) and updated the latency calc to use a weighted average based on volume (Closes #12).
1 parent 854fd27 commit fd20c0a

File tree

2 files changed

+60
-11
lines changed

2 files changed

+60
-11
lines changed

server.py

+57-10
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,58 @@ def get_env_total(sql_filter):
4040

4141
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()
4242

43-
env_total = 0
43+
env_total = Decimal(0)
4444

4545
for success_total in success_totals:
46-
env_total += success_total.total
46+
env_total += Decimal(success_total.total)
4747

4848
return env_total
4949

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)
5095

5196
############################### Flask Routes ###################################
5297

@@ -56,14 +101,19 @@ def index():
56101

57102
sql_filter = '%prod%'
58103

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()
60105

61-
total_latency = Decimal(0)
106+
# total_latency = Decimal(0)
62107

63-
for latency in all_latency:
64-
total_latency += latency[0]
108+
# for latency in all_latency:
109+
# total_latency += latency[0]
65110

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.
67117

68118
if avg_latency < 200:
69119
overall_status = 'green'
@@ -75,10 +125,7 @@ def index():
75125
overall_status = 'red'
76126
status_icon = 'fa-flash'
77127

78-
# Compare avg_latency to a range of values.
79-
# Based on place in range, choose green/yellow/red icon.
80128
# Pass icon to template.
81-
82129
return render_template("homepage.html", avg_latency=avg_latency, overall_status=overall_status, status_icon=status_icon)
83130

84131

templates/homepage.html

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ <h1>Developer Program Pulse</h1>
1616
</div>
1717
<div class="col-xs-9 text-right">
1818
<div class="huge">{{'%0.0f' % avg_latency|float}} ms</div>
19-
<div>Average Latency</div>
19+
<div>(Weighted) Average Latency</div>
2020
</div>
2121
</div>
2222
</div>
@@ -31,6 +31,8 @@ <h1>Developer Program Pulse</h1>
3131
</div>
3232
</div>
3333

34+
35+
3436
<p>Check out these pages:</p>
3537
<ul>
3638
<li><a href="/env">API Calls by Volume</a></li>

0 commit comments

Comments
 (0)