Skip to content

Commit

Permalink
Merge pull request #1523 from frappe/avg-request-duration
Browse files Browse the repository at this point in the history
feat(SiteCharts): Add avg request duration charts
  • Loading branch information
balamurali27 committed Mar 11, 2024
2 parents 4f3334b + 8d26ce4 commit 957f248
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 10 deletions.
16 changes: 16 additions & 0 deletions dashboard/src/views/site/SiteCharts.vue
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@
:chartTheme="requestChartColors"
:loading="$resources.analytics.loading"
/>
<BarChart
class="sm:col-span-2"
title="Average Request Duration by Path"
:key="averageRequestDurationByPathData"
:data="averageRequestDurationByPathData"
unit="seconds"
:chartTheme="requestChartColors"
:loading="$resources.analytics.loading"
/>
<BarChart
class="sm:col-span-2"
title="Slow Logs by Count"
Expand Down Expand Up @@ -205,6 +214,13 @@ export default {
return requestDurationByPath;
},
averageRequestDurationByPathData() {
let averageRequestDurationByPath =
this.$resources.analytics.data?.average_request_duration_by_path;
if (!averageRequestDurationByPath) return;
return averageRequestDurationByPath;
},
slowLogsByCountData() {
let slowLogsByCount = this.$resources.analytics.data?.slow_logs_by_count;
if (!slowLogsByCount) return;
Expand Down
114 changes: 104 additions & 10 deletions press/api/analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ def get(name, timezone, duration="7d"):
request_duration_by_path_data = get_request_by_path(
name, "duration", timezone, timespan, timegrain
)
average_request_duration_by_path_data = get_request_by_path(
name, "average_duration", timezone, timespan, timegrain
)
slow_logs_by_count = get_slow_logs(name, "count", timezone, timespan, timegrain)
slow_logs_by_duration = get_slow_logs(name, "duration", timezone, timespan, timegrain)
job_data = get_usage(name, "job", timezone, timespan, timegrain)
Expand All @@ -64,6 +67,7 @@ def get(name, timezone, duration="7d"):
"request_cpu_time": [{"value": r.duration, "date": r.date} for r in request_data],
"request_count_by_path": request_count_by_path_data,
"request_duration_by_path": request_duration_by_path_data,
"average_request_duration_by_path": average_request_duration_by_path_data,
"slow_logs_by_count": slow_logs_by_count,
"slow_logs_by_duration": slow_logs_by_duration,
"job_count": [{"value": r.count, "date": r.date} for r in job_data],
Expand Down Expand Up @@ -288,10 +292,95 @@ def get_request_by_path(site, query_type, timezone, timespan, timegrain):
},
}

average_duration_query = {
"aggs": {
"method_path": {
"terms": {
"field": "json.request.path",
"order": {"methods>avg": "desc"},
"size": MAX_NO_OF_PATHS,
},
"aggs": {
"methods": {
"filter": {
"bool": {
"filter": [
{"match_phrase": {"json.site": site}},
{
"range": {
"@timestamp": {
"gte": f"now-{timespan}s",
"lte": "now",
}
}
},
],
"must_not": [{"match_phrase": {"json.request.path": "/api/method/ping"}}],
}
},
"aggs": {
"avg": {
"avg": {
"field": "json.duration",
}
}
},
},
"histogram_of_method": {
"date_histogram": {
"field": "@timestamp",
"fixed_interval": f"{timegrain}s",
"time_zone": timezone,
},
"aggs": {
"methods": {
"filter": {
"bool": {
"filter": [
{"match_phrase": {"json.site": site}},
{
"range": {
"@timestamp": {
"gte": f"now-{timespan}s",
"lte": "now",
}
}
},
],
"must_not": [{"match_phrase": {"json.request.path": "/api/method/ping"}}],
}
},
"aggs": {
"avg": {
"avg": {
"field": "json.duration",
}
}
},
},
},
},
},
},
},
"size": 0,
"query": {
"bool": {
"filter": [
{"match_phrase": {"json.site": site}},
{"range": {"@timestamp": {"gte": f"now-{timespan}s", "lte": "now"}}},
],
"must_not": [{"match_phrase": {"json.request.path": "/api/method/ping"}}],
}
},
}

if query_type == "count":
query = count_query
else:
elif query_type == "duration":
query = duration_query
elif query_type == "average_duration":
query = average_duration_query

response = requests.post(url, json=query, auth=("frappe", password)).json()

Expand All @@ -315,7 +404,11 @@ def get_request_by_path(site, query_type, timezone, timespan, timegrain):
data["request_count"]["doc_count"]
if query_type == "count"
else (
data["methods"]["sum"]["value"] / 1000000 if query_type == "duration" else 0
data["methods"]["sum"]["value"] / 1000000
if query_type == "duration"
else data["methods"]["avg"]["value"] / 1000000
if query_type == "average_duration"
else 0
)
)
for data in bucket["histogram_of_method"]["buckets"]
Expand Down Expand Up @@ -492,16 +585,17 @@ def get_slow_logs(site, query_type, timezone, timespan, timegrain):

response = requests.post(url, json=query, auth=("frappe", password)).json()

if not response["aggregations"]["method_path"]["buckets"]:
buckets = []
try:
labels = [
get_datetime(data["key_as_string"]).replace(tzinfo=None)
for data in response["aggregations"]["method_path"]["buckets"][0][
"histogram_of_method"
]["buckets"]
]
except KeyError:
return {"datasets": [], "labels": []}

buckets = []
labels = [
get_datetime(data["key_as_string"]).replace(tzinfo=None)
for data in response["aggregations"]["method_path"]["buckets"][0][
"histogram_of_method"
]["buckets"]
]
for bucket in response["aggregations"]["method_path"]["buckets"]:
buckets.append(
frappe._dict(
Expand Down

0 comments on commit 957f248

Please sign in to comment.