Skip to content

Commit

Permalink
feat: active cpu time (#22331)
Browse files Browse the repository at this point in the history
active cpu time

Co-authored-by: Alexander Spicer <aspicer@surfer-172-30-3-221-hotspot.internet-for-guests.com>
  • Loading branch information
aspicer and Alexander Spicer committed May 16, 2024
1 parent 7446568 commit ad3d6d7
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 2 deletions.
5 changes: 4 additions & 1 deletion frontend/src/queries/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1542,6 +1542,9 @@
"ClickhouseQueryStatus": {
"additionalProperties": false,
"properties": {
"active_cpu_time": {
"type": "integer"
},
"bytes_read": {
"type": "integer"
},
Expand All @@ -1555,7 +1558,7 @@
"type": "integer"
}
},
"required": ["bytes_read", "rows_read", "estimated_rows_total", "time_elapsed"],
"required": ["bytes_read", "rows_read", "estimated_rows_total", "time_elapsed", "active_cpu_time"],
"type": "object"
},
"CohortPropertyFilter": {
Expand Down
1 change: 1 addition & 0 deletions frontend/src/queries/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -932,6 +932,7 @@ export type ClickhouseQueryStatus = {
rows_read: integer
estimated_rows_total: integer
time_elapsed: integer
active_cpu_time: integer
}

export type QueryStatus = {
Expand Down
7 changes: 7 additions & 0 deletions frontend/src/scenes/insights/EmptyStates/EmptyStates.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ export function InsightLoadingStateWithLoadingBar({
}, [insightPollResponse])
const bytesPerSecond = (bytesRead / (secondsElapsed || 1)) * 1000

const cpuUtilization =
(insightPollResponse?.status?.query_progress?.active_cpu_time || 0) /
(insightPollResponse?.status?.query_progress?.time_elapsed || 1) /
10000

return (
<div className="insight-empty-state warning">
<div className="empty-state-inner">
Expand All @@ -130,6 +135,8 @@ export function InsightLoadingStateWithLoadingBar({
{humanFriendlyNumber(rowsRead || 0, 0)} rows
<br />
{humanFileSize(bytesRead || 0)} ({humanFileSize(bytesPerSecond || 0)}/s)
<br />
CPU {humanFriendlyNumber(cpuUtilization, 0)}%
</>
)}
</p>
Expand Down
6 changes: 5 additions & 1 deletion posthog/clickhouse/client/execute_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ def get_query_status(self, show_progress=False) -> QueryStatus:
read_rows,
read_bytes,
total_rows_approx,
elapsed
elapsed,
ProfileEvents['OSCPUVirtualTimeMicroseconds'] as OSCPUVirtualTimeMicroseconds
FROM clusterAllReplicas(posthog, system.processes)
WHERE query_id like %(query_id)s
"""
Expand All @@ -116,6 +117,7 @@ def get_query_status(self, show_progress=False) -> QueryStatus:
"rows_read": noNaNInt(result[2]),
"estimated_rows_total": noNaNInt(result[4]),
"time_elapsed": noNaNInt(result[5]),
"active_cpu_time": noNaNInt(result[6]),
}
for result in results
}
Expand All @@ -127,12 +129,14 @@ def get_query_status(self, show_progress=False) -> QueryStatus:
"rows_read": 0,
"estimated_rows_total": 0,
"time_elapsed": 0,
"active_cpu_time": 0,
}
for single_query_progress in clickhouse_query_progress_dict.values():
query_progress["bytes_read"] += single_query_progress["bytes_read"]
query_progress["rows_read"] += single_query_progress["rows_read"]
query_progress["estimated_rows_total"] += single_query_progress["estimated_rows_total"]
query_progress["time_elapsed"] += single_query_progress["time_elapsed"]
query_progress["active_cpu_time"] += single_query_progress["active_cpu_time"]
query_status.query_progress = ClickhouseQueryStatus(**query_progress)

except Exception as e:
Expand Down
1 change: 1 addition & 0 deletions posthog/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ class ClickhouseQueryStatus(BaseModel):
model_config = ConfigDict(
extra="forbid",
)
active_cpu_time: int
bytes_read: int
estimated_rows_total: int
rows_read: int
Expand Down

0 comments on commit ad3d6d7

Please sign in to comment.