|
| 1 | +import { writeToString } from '@fast-csv/format' |
| 2 | + |
1 | 3 | import { |
2 | 4 | handleWorkerLogsKeys, |
3 | 5 | handleWorkerLogsValues, |
@@ -60,6 +62,100 @@ This tool provides three primary views of your Worker data: |
60 | 62 | } |
61 | 63 | try { |
62 | 64 | const response = await queryWorkersObservability(agent.props.accessToken, accountId, query) |
| 65 | + |
| 66 | + if (query.view === 'calculations') { |
| 67 | + let data = '' |
| 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(invocations, { headers: true, delimiter: '\t' }) |
| 150 | + return { |
| 151 | + content: [ |
| 152 | + { |
| 153 | + type: 'text', |
| 154 | + text: tsv, |
| 155 | + }, |
| 156 | + ], |
| 157 | + } |
| 158 | + } |
63 | 159 | return { |
64 | 160 | content: [ |
65 | 161 | { |
@@ -110,11 +206,16 @@ This tool provides three primary views of your Worker data: |
110 | 206 | } |
111 | 207 | try { |
112 | 208 | const result = await handleWorkerLogsKeys(agent.props.accessToken, accountId, keysQuery) |
| 209 | + |
| 210 | + const tsv = await writeToString( |
| 211 | + result.map((key) => ({ type: key.type, key: key.key })), |
| 212 | + { headers: true, delimiter: '\t' } |
| 213 | + ) |
113 | 214 | return { |
114 | 215 | content: [ |
115 | 216 | { |
116 | 217 | type: 'text', |
117 | | - text: JSON.stringify(result), |
| 218 | + text: tsv, |
118 | 219 | }, |
119 | 220 | ], |
120 | 221 | } |
@@ -155,11 +256,15 @@ This tool provides three primary views of your Worker data: |
155 | 256 | } |
156 | 257 | try { |
157 | 258 | const result = await handleWorkerLogsValues(agent.props.accessToken, accountId, valuesQuery) |
| 259 | + const tsv = await writeToString( |
| 260 | + result?.map((value) => ({ type: value.type, value: value.value })) || [], |
| 261 | + { headers: true, delimiter: '\t' } |
| 262 | + ) |
158 | 263 | return { |
159 | 264 | content: [ |
160 | 265 | { |
161 | 266 | type: 'text', |
162 | | - text: JSON.stringify(result), |
| 267 | + text: tsv, |
163 | 268 | }, |
164 | 269 | ], |
165 | 270 | } |
|
0 commit comments