2023-11-03 11:30:06 -03:00
|
|
|
## Utilities for serializing profiler metrics.
|
2023-11-03 14:51:00 -03:00
|
|
|
import std/algorithm
|
2023-11-03 11:30:06 -03:00
|
|
|
import std/json
|
|
|
|
|
2023-11-02 12:07:15 -03:00
|
|
|
import asyncprofiler
|
|
|
|
|
2023-11-03 11:30:06 -03:00
|
|
|
proc `%`*(o: Duration): JsonNode =
|
|
|
|
%(o.nanoseconds)
|
2023-11-02 12:07:15 -03:00
|
|
|
|
2023-11-03 11:30:06 -03:00
|
|
|
proc `%`*(o: cstring): JsonNode =
|
|
|
|
%($(o))
|
2023-11-02 12:07:15 -03:00
|
|
|
|
2023-11-03 14:51:00 -03:00
|
|
|
proc toJson*(o: MetricsSummary): JsonNode =
|
2023-11-02 12:07:15 -03:00
|
|
|
var rows = newJArray()
|
|
|
|
for (location, metric) in o.pairs:
|
|
|
|
var row = %(metric)
|
|
|
|
row["location"] = %(location[])
|
|
|
|
rows.add(row)
|
|
|
|
|
|
|
|
rows
|
2023-11-03 14:51:00 -03:00
|
|
|
|
|
|
|
proc `%`*(o: MetricsSummary): JsonNode = o.toJson()
|
|
|
|
|
|
|
|
proc sortBy*(jArray: JsonNode, metric: string): JsonNode {.raises: [ref KeyError].} =
|
|
|
|
%(jArray.getElems.sorted(
|
|
|
|
proc (a, b: JsonNode): int {.raises: [ref KeyError].} =
|
|
|
|
cmp(a[metric].getInt, b[metric].getInt),
|
2023-11-06 15:10:13 -03:00
|
|
|
order = SortOrder.Descending))
|