The best way to control Claude Code, Codex and more from any computer or phone
| 1 | // @effect-diagnostics globalDate:off - vendored upstream protocol, not an Effect service. |
| 2 | export class Observations { |
| 3 | request_bytes = 0; |
| 4 | d1_rows_read = 0; |
| 5 | d1_rows_written = 0; |
| 6 | d1_ms = 0; |
| 7 | d1_queries = 0; |
| 8 | d1_storage_bytes = 0; |
| 9 | r2_operations = 0; |
| 10 | |
| 11 | toJSON() { |
| 12 | return { |
| 13 | request_bytes: this.request_bytes, |
| 14 | d1_rows_read: this.d1_rows_read, |
| 15 | d1_rows_written: this.d1_rows_written, |
| 16 | d1_ms: this.d1_ms, |
| 17 | d1_queries: this.d1_queries, |
| 18 | d1_storage_bytes: this.d1_storage_bytes, |
| 19 | r2_operations: this.r2_operations, |
| 20 | }; |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | export async function measured<T extends D1Result | D1Result[]>( |
| 25 | operation: Promise<T>, |
| 26 | stats?: Observations, |
| 27 | ): Promise<T> { |
| 28 | const start = Date.now(); |
| 29 | try { |
| 30 | const result = await operation; |
| 31 | if (stats) |
| 32 | for (const item of Array.isArray(result) ? result : [result]) { |
| 33 | stats.d1_rows_read += item.meta.rows_read; |
| 34 | stats.d1_rows_written += item.meta.rows_written; |
| 35 | stats.d1_storage_bytes = item.meta.size_after; |
| 36 | stats.d1_queries++; |
| 37 | } |
| 38 | return result; |
| 39 | } finally { |
| 40 | if (stats) stats.d1_ms += Date.now() - start; |
| 41 | } |
| 42 | } |