Skip to content

Commit 9988c25

Browse files
committed
migrate responses to TSV's
1 parent 25ef3ae commit 9988c25

File tree

3 files changed

+151
-2
lines changed

3 files changed

+151
-2
lines changed

apps/workers-observability/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
},
1414
"dependencies": {
1515
"@cloudflare/workers-oauth-provider": "0.0.5",
16+
"@fast-csv/format": "^5.0.2",
1617
"@hono/zod-validator": "0.4.3",
1718
"@modelcontextprotocol/sdk": "1.10.2",
1819
"@repo/mcp-common": "workspace:*",

apps/workers-observability/src/tools/observability.ts

Lines changed: 110 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { writeToString } from '@fast-csv/format'
2+
13
import {
24
handleWorkerLogsKeys,
35
handleWorkerLogsValues,
@@ -60,6 +62,103 @@ This tool provides three primary views of your Worker data:
6062
}
6163
try {
6264
const response = await queryWorkersObservability(agent.props.accessToken, accountId, query)
65+
66+
if (query.view === 'calculations') {
67+
let data = 'DISPLAY THE CALCULATIONS IN A GRAPHICAL WAY'
68+
for (const calculation of response?.calculations || []) {
69+
const alias = calculation.alias || calculation.calculation
70+
const aggregates = calculation.aggregates.map((agg) => {
71+
const keys = agg.groups?.reduce(
72+
(acc, group) => {
73+
acc[`${group.key}`] = `${group.value}`
74+
return acc
75+
},
76+
{} as Record<string, string>
77+
)
78+
return {
79+
...keys,
80+
[alias]: agg.value,
81+
}
82+
})
83+
84+
const aggregatesString = await writeToString(aggregates, {
85+
headers: true,
86+
delimiter: '\t',
87+
})
88+
89+
const series = calculation.series.map(({ time, data }) => {
90+
return {
91+
time,
92+
...data.reduce(
93+
(acc, point) => {
94+
const key = point.groups?.reduce((acc, group) => {
95+
return `${acc} * ${group.value}`
96+
}, '')
97+
if (!key) {
98+
return {
99+
...acc,
100+
[alias]: point.value,
101+
}
102+
}
103+
return {
104+
...acc,
105+
key,
106+
[alias]: point.value,
107+
}
108+
},
109+
{} as Record<string, string | number | undefined>
110+
),
111+
}
112+
})
113+
const seriesString = await writeToString(series, { headers: true, delimiter: '\t' })
114+
data = data + '\n' + `## ${alias}`
115+
data = data + '\n' + `### Aggregation`
116+
data = data + '\n' + aggregatesString
117+
data = data + '\n' + `### Series`
118+
data = data + '\n' + seriesString
119+
}
120+
121+
return {
122+
content: [
123+
{
124+
type: 'text',
125+
text: data,
126+
},
127+
],
128+
}
129+
}
130+
131+
if(query.view === 'events') {
132+
const events = response?.events?.events
133+
return {
134+
content: [
135+
{
136+
type: 'text',
137+
text: JSON.stringify(events),
138+
},
139+
],
140+
}
141+
}
142+
143+
if(query.view === 'invocations') {
144+
const invocations = Object.entries(response?.invocations || {}).map(([_, logs]) => {
145+
const invocationLog = logs.find((log) => log.$metadata.type === 'cf-worker-event')
146+
return invocationLog?.$metadata ?? logs[0]?.$metadata
147+
});
148+
149+
const tsv = await writeToString(
150+
invocations,
151+
{ headers: true, delimiter: '\t' }
152+
)
153+
return {
154+
content: [
155+
{
156+
type: 'text',
157+
text: tsv,
158+
},
159+
],
160+
}
161+
}
63162
return {
64163
content: [
65164
{
@@ -110,11 +209,16 @@ This tool provides three primary views of your Worker data:
110209
}
111210
try {
112211
const result = await handleWorkerLogsKeys(agent.props.accessToken, accountId, keysQuery)
212+
213+
const tsv = await writeToString(
214+
result.map((key) => ({ type: key.type, key: key.key })),
215+
{ headers: true, delimiter: '\t' }
216+
)
113217
return {
114218
content: [
115219
{
116220
type: 'text',
117-
text: JSON.stringify(result),
221+
text: tsv,
118222
},
119223
],
120224
}
@@ -155,11 +259,15 @@ This tool provides three primary views of your Worker data:
155259
}
156260
try {
157261
const result = await handleWorkerLogsValues(agent.props.accessToken, accountId, valuesQuery)
262+
const tsv = await writeToString(
263+
result?.map((value) => ({ type: value.type, value: value.value })) || [],
264+
{ headers: true, delimiter: '\t' }
265+
)
158266
return {
159267
content: [
160268
{
161269
type: 'text',
162-
text: JSON.stringify(result),
270+
text: tsv,
163271
},
164272
],
165273
}

pnpm-lock.yaml

Lines changed: 40 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)