forge-sql-orm 2.1.10 → 2.1.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. package/README.md +356 -263
  2. package/dist/ForgeSQLORM.js +3263 -3226
  3. package/dist/ForgeSQLORM.js.map +1 -1
  4. package/dist/ForgeSQLORM.mjs +3261 -3224
  5. package/dist/ForgeSQLORM.mjs.map +1 -1
  6. package/dist/core/ForgeSQLORM.d.ts +66 -12
  7. package/dist/core/ForgeSQLORM.d.ts.map +1 -1
  8. package/dist/core/ForgeSQLQueryBuilder.d.ts +66 -11
  9. package/dist/core/ForgeSQLQueryBuilder.d.ts.map +1 -1
  10. package/dist/core/SystemTables.d.ts +82 -82
  11. package/dist/utils/cacheUtils.d.ts.map +1 -1
  12. package/dist/utils/forgeDriver.d.ts.map +1 -1
  13. package/dist/utils/forgeDriverProxy.d.ts +6 -2
  14. package/dist/utils/forgeDriverProxy.d.ts.map +1 -1
  15. package/dist/utils/metadataContextUtils.d.ts +5 -2
  16. package/dist/utils/metadataContextUtils.d.ts.map +1 -1
  17. package/dist/utils/sqlUtils.d.ts +72 -1
  18. package/dist/utils/sqlUtils.d.ts.map +1 -1
  19. package/dist/webtriggers/index.d.ts +1 -1
  20. package/dist/webtriggers/index.d.ts.map +1 -1
  21. package/dist/webtriggers/slowQuerySchedulerTrigger.d.ts +67 -0
  22. package/dist/webtriggers/slowQuerySchedulerTrigger.d.ts.map +1 -0
  23. package/package.json +12 -12
  24. package/src/core/ForgeSQLORM.ts +166 -29
  25. package/src/core/ForgeSQLQueryBuilder.ts +65 -11
  26. package/src/core/SystemTables.ts +1 -1
  27. package/src/utils/cacheUtils.ts +26 -3
  28. package/src/utils/forgeDriver.ts +15 -34
  29. package/src/utils/forgeDriverProxy.ts +58 -6
  30. package/src/utils/metadataContextUtils.ts +18 -6
  31. package/src/utils/sqlUtils.ts +241 -2
  32. package/src/webtriggers/index.ts +1 -1
  33. package/src/webtriggers/slowQuerySchedulerTrigger.ts +89 -0
  34. package/dist/webtriggers/topSlowestStatementLastHourTrigger.d.ts +0 -114
  35. package/dist/webtriggers/topSlowestStatementLastHourTrigger.d.ts.map +0 -1
  36. package/src/webtriggers/topSlowestStatementLastHourTrigger.ts +0 -563
@@ -0,0 +1,89 @@
1
+ import { getHttpResponse, TriggerResponse } from "./index";
2
+ import { slowQueryPerHours } from "../utils/sqlUtils";
3
+ import { ForgeSqlOperation } from "../core/ForgeSQLQueryBuilder";
4
+
5
+ /**
6
+ * Scheduler trigger for analyzing slow queries from the last specified number of hours.
7
+ *
8
+ * This trigger analyzes slow queries from TiDB's slow query log system table and provides
9
+ * detailed performance information including SQL query text, memory usage, execution time,
10
+ * and execution plans. It's designed to be used as a scheduled trigger in Atlassian Forge
11
+ * to monitor query performance over time.
12
+ *
13
+ * The function queries the slow query system table to find queries executed within the
14
+ * specified time window and logs detailed performance information to the console. Results
15
+ * are limited to the top 50 slow queries to prevent excessive output.
16
+ *
17
+ * @param forgeSQLORM - The ForgeSQL operation instance for database access
18
+ * @param options - Configuration options for the slow query analysis
19
+ * @param options.hours - Number of hours to look back for slow queries (default: 1)
20
+ * @param options.timeout - Timeout in milliseconds for the query execution (default: 2000ms)
21
+ *
22
+ * @returns Promise<TriggerResponse<string>> - HTTP response with JSON stringified query results or error message
23
+ *
24
+ * @example
25
+ * ```typescript
26
+ * import ForgeSQL, { slowQuerySchedulerTrigger } from "forge-sql-orm";
27
+ *
28
+ * const forgeSQL = new ForgeSQL();
29
+ *
30
+ * // Basic usage with default options (1 hour, 2000ms timeout)
31
+ * export const slowQueryTrigger = () =>
32
+ * slowQuerySchedulerTrigger(forgeSQL, { hours: 1, timeout: 2000 });
33
+ *
34
+ * // Analyze slow queries from the last 6 hours with extended timeout
35
+ * export const sixHourSlowQueryTrigger = () =>
36
+ * slowQuerySchedulerTrigger(forgeSQL, { hours: 6, timeout: 5000 });
37
+ *
38
+ * // Analyze slow queries from the last 24 hours
39
+ * export const dailySlowQueryTrigger = () =>
40
+ * slowQuerySchedulerTrigger(forgeSQL, { hours: 24, timeout: 3000 });
41
+ * ```
42
+ *
43
+ * @example
44
+ * ```yaml
45
+ * # manifest.yml configuration
46
+ * scheduledTrigger:
47
+ * - key: slow-query-trigger
48
+ * function: slowQueryTrigger
49
+ * interval: hour
50
+ *
51
+ * function:
52
+ * - key: slowQueryTrigger
53
+ * handler: index.slowQueryTrigger
54
+ * ```
55
+ *
56
+ * @remarks
57
+ * - Results are automatically logged to the Forge Developer Console via `console.warn()`
58
+ * - The function returns up to 50 slow queries to prevent excessive logging
59
+ * - Transient timeouts are usually fine; repeated timeouts indicate the diagnostic query itself is slow
60
+ * - This trigger is best used with hourly intervals to catch slow queries in a timely manner
61
+ * - Error responses return HTTP 500 with error details
62
+ *
63
+ * @see {@link slowQueryPerHours} - The underlying function that performs the actual query analysis
64
+ */
65
+ export async function slowQuerySchedulerTrigger(
66
+ forgeSQLORM: ForgeSqlOperation,
67
+ options: {
68
+ hours: number;
69
+ timeout: number;
70
+ },
71
+ ): Promise<TriggerResponse<string>> {
72
+ try {
73
+ return getHttpResponse<string>(
74
+ 200,
75
+ JSON.stringify(
76
+ await slowQueryPerHours(forgeSQLORM, options?.hours ?? 1, options?.timeout ?? 3000),
77
+ ),
78
+ );
79
+ } catch (error: any) {
80
+ const errorMessage =
81
+ error?.debug?.sqlMessage ??
82
+ error?.debug?.message ??
83
+ error.message ??
84
+ "Unknown error occurred";
85
+ // eslint-disable-next-line no-console
86
+ console.error(errorMessage);
87
+ return getHttpResponse<string>(500, errorMessage);
88
+ }
89
+ }
@@ -1,114 +0,0 @@
1
- import { ForgeSqlOperation } from "../core/ForgeSQLQueryBuilder";
2
- import { OperationType } from "../utils/requestTypeContextUtils";
3
- interface TriggerOptions {
4
- warnThresholdMs?: number;
5
- memoryThresholdBytes?: number;
6
- showPlan?: boolean;
7
- operationType?: OperationType;
8
- topN?: number;
9
- hours?: number;
10
- tables?: "SUMMARY_AND_HISTORY" | "CLUSTER_SUMMARY_AND_HISTORY";
11
- }
12
- interface TriggerResponse {
13
- headers: {
14
- "Content-Type": string[];
15
- };
16
- statusCode: number;
17
- statusText?: string;
18
- body: string;
19
- }
20
- /**
21
- * Performance monitoring scheduler trigger for Atlassian Forge SQL.
22
- *
23
- * This trigger analyzes query performance from the last hour and identifies slow or memory-intensive queries
24
- * that exceed configurable thresholds. It's designed specifically for Atlassian Forge's 16 MiB memory limit
25
- * and provides detailed insights for query optimization.
26
- *
27
- * ## Key Features
28
- * - **Memory-focused monitoring**: Primary focus on memory usage with configurable thresholds
29
- * - **Atlassian 16 MiB limit awareness**: Designed specifically for Forge SQL's memory constraints
30
- * - **Execution plan analysis**: Shows detailed query plans to help optimize memory consumption
31
- * - **Configurable thresholds**: Set custom memory usage and latency thresholds
32
- * - **Automatic filtering**: Excludes system queries (`Use`, `Set`, `Show`) and empty queries
33
- * - **Scheduled monitoring**: Run automatically on configurable intervals
34
- *
35
- * ## OR Logic Thresholds
36
- * Statements are included if they exceed **EITHER** threshold:
37
- * - `avgLatencyMs > warnThresholdMs` **OR**
38
- * - `avgMemBytes > memoryThresholdBytes`
39
- *
40
- * ## Configuration Tips
41
- * - **Memory-only monitoring**: Set `warnThresholdMs` to 10000ms (effectively disabled)
42
- * - **Latency-only monitoring**: Set `memoryThresholdBytes` to 16MB (16 * 1024 * 1024) (effectively disabled)
43
- * - **Combined monitoring**: Use both thresholds for comprehensive monitoring
44
- * - **Conservative monitoring**: 4MB warning (25% of 16MB limit)
45
- * - **Default monitoring**: 8MB warning (50% of 16MB limit)
46
- * - **Aggressive monitoring**: 12MB warning (75% of 16MB limit)
47
- *
48
- * ## Exclusions
49
- * - Statements with empty `digestText` or `digest`
50
- * - Service statements (`Use`, `Set`, `Show`)
51
- * - Queries that don't exceed either threshold
52
- *
53
- * @param orm - ForgeSQL ORM instance (required)
54
- * @param options - Configuration options
55
- * @param options.warnThresholdMs - Milliseconds threshold for latency monitoring (default: 300ms)
56
- * @param options.memoryThresholdBytes - Bytes threshold for memory usage monitoring (default: 8MB)
57
- * @param options.showPlan - Whether to include execution plan in logs (default: false)
58
- * @param options.operationType - Operation type context for query execution (default: "DML")
59
- * @param options.topN - Number of top slow queries to return (default: 1)
60
- * @param options.hours - Number of hours to look back (default: 1)
61
- * @param options.tables - Table configuration to use (default: "CLUSTER_SUMMARY_AND_HISTORY")
62
- * @returns Promise<TriggerResponse> - HTTP response with query results or error
63
- *
64
- * @example
65
- * ```typescript
66
- * import ForgeSQL, { topSlowestStatementLastHourTrigger } from "forge-sql-orm";
67
- *
68
- * const forgeSQL = new ForgeSQL();
69
- *
70
- * // Default thresholds: 300ms latency OR 8MB memory
71
- * export const performanceTrigger = () =>
72
- * topSlowestStatementLastHourTrigger(forgeSQL);
73
- *
74
- * // Conservative memory monitoring: 4MB threshold
75
- * export const conservativeTrigger = () =>
76
- * topSlowestStatementLastHourTrigger(forgeSQL, {
77
- * memoryThresholdBytes: 4 * 1024 * 1024
78
- * });
79
- *
80
- * // Memory-only monitoring: 4MB threshold (latency effectively disabled)
81
- * export const memoryOnlyTrigger = () =>
82
- * topSlowestStatementLastHourTrigger(forgeSQL, {
83
- * warnThresholdMs: 10000,
84
- * memoryThresholdBytes: 4 * 1024 * 1024
85
- * });
86
- *
87
- * // With execution plan in logs
88
- * export const withPlanTrigger = () =>
89
- * topSlowestStatementLastHourTrigger(forgeSQL, { showPlan: true });
90
- * ```
91
- *
92
- * @example
93
- * ```yaml
94
- * # manifest.yml configuration
95
- * scheduledTrigger:
96
- * - key: performance-trigger
97
- * function: performanceTrigger
98
- * interval: hour
99
- *
100
- * function:
101
- * - key: performanceTrigger
102
- * handler: index.performanceTrigger
103
- * ```
104
- */
105
- /**
106
- * Main scheduler trigger function to log the single slowest SQL statement from the last hour.
107
- *
108
- * @param orm - ForgeSQL ORM instance (required)
109
- * @param options - Configuration options
110
- * @returns Promise<TriggerResponse> - HTTP response with query results or error
111
- */
112
- export declare const topSlowestStatementLastHourTrigger: (orm: ForgeSqlOperation, options?: TriggerOptions) => Promise<TriggerResponse>;
113
- export {};
114
- //# sourceMappingURL=topSlowestStatementLastHourTrigger.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"topSlowestStatementLastHourTrigger.d.ts","sourceRoot":"","sources":["../../src/webtriggers/topSlowestStatementLastHourTrigger.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,8BAA8B,CAAC;AASjE,OAAO,EAAE,aAAa,EAAE,MAAM,kCAAkC,CAAC;AAcjE,UAAU,cAAc;IACtB,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,oBAAoB,CAAC,EAAE,MAAM,CAAC;IAC9B,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,aAAa,CAAC,EAAE,aAAa,CAAC;IAC9B,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,MAAM,CAAC,EAAE,qBAAqB,GAAG,6BAA6B,CAAC;CAChE;AA2BD,UAAU,eAAe;IACvB,OAAO,EAAE;QAAE,cAAc,EAAE,MAAM,EAAE,CAAA;KAAE,CAAC;IACtC,UAAU,EAAE,MAAM,CAAC;IACnB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,IAAI,EAAE,MAAM,CAAC;CACd;AAgWD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoFG;AACH;;;;;;GAMG;AACH,eAAO,MAAM,kCAAkC,GAC7C,KAAK,iBAAiB,EACtB,UAAU,cAAc,KACvB,OAAO,CAAC,eAAe,CAoDzB,CAAC"}