forge-sql-orm 2.1.2 → 2.1.4
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.
- package/README.md +205 -0
- package/dist/ForgeSQLORM.js +677 -56
- package/dist/ForgeSQLORM.js.map +1 -1
- package/dist/ForgeSQLORM.mjs +680 -59
- package/dist/ForgeSQLORM.mjs.map +1 -1
- package/dist/core/ForgeSQLQueryBuilder.d.ts +1 -2
- package/dist/core/ForgeSQLQueryBuilder.d.ts.map +1 -1
- package/dist/core/SystemTables.d.ts +5039 -0
- package/dist/core/SystemTables.d.ts.map +1 -1
- package/dist/utils/cacheUtils.d.ts.map +1 -1
- package/dist/utils/forgeDriver.d.ts.map +1 -1
- package/dist/utils/forgeDriverProxy.d.ts.map +1 -1
- package/dist/utils/sqlUtils.d.ts +1 -1
- package/dist/utils/sqlUtils.d.ts.map +1 -1
- package/dist/webtriggers/applyMigrationsWebTrigger.d.ts +1 -1
- package/dist/webtriggers/applyMigrationsWebTrigger.d.ts.map +1 -1
- package/dist/webtriggers/dropMigrationWebTrigger.d.ts.map +1 -1
- package/dist/webtriggers/dropTablesMigrationWebTrigger.d.ts.map +1 -1
- package/dist/webtriggers/fetchSchemaWebTrigger.d.ts.map +1 -1
- package/dist/webtriggers/index.d.ts +1 -0
- package/dist/webtriggers/index.d.ts.map +1 -1
- package/dist/webtriggers/topSlowestStatementLastHourTrigger.d.ts +72 -0
- package/dist/webtriggers/topSlowestStatementLastHourTrigger.d.ts.map +1 -0
- package/package.json +8 -7
- package/src/core/ForgeSQLQueryBuilder.ts +13 -9
- package/src/core/SystemTables.ts +313 -1
- package/src/lib/drizzle/extensions/additionalActions.ts +2 -2
- package/src/utils/cacheContextUtils.ts +2 -2
- package/src/utils/cacheUtils.ts +3 -1
- package/src/utils/forgeDriver.ts +16 -21
- package/src/utils/forgeDriverProxy.ts +10 -3
- package/src/utils/sqlUtils.ts +32 -7
- package/src/webtriggers/applyMigrationsWebTrigger.ts +21 -15
- package/src/webtriggers/dropMigrationWebTrigger.ts +8 -4
- package/src/webtriggers/dropTablesMigrationWebTrigger.ts +8 -4
- package/src/webtriggers/fetchSchemaWebTrigger.ts +7 -3
- package/src/webtriggers/index.ts +1 -0
- package/src/webtriggers/topSlowestStatementLastHourTrigger.ts +305 -0
package/dist/ForgeSQLORM.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { isTable, sql, eq, and, getTableColumns } from "drizzle-orm";
|
|
1
|
+
import { isTable, sql, eq, and, getTableColumns, gte, desc } from "drizzle-orm";
|
|
2
2
|
import { DateTime } from "luxon";
|
|
3
3
|
import { isSQLWrapper } from "drizzle-orm/sql/sql";
|
|
4
4
|
import { AsyncLocalStorage } from "node:async_hooks";
|
|
@@ -7,7 +7,7 @@ import * as crypto from "crypto";
|
|
|
7
7
|
import { kvs, WhereConditions, Filter, FilterConditions } from "@forge/kvs";
|
|
8
8
|
import { sql as sql$1, migrationRunner } from "@forge/sql";
|
|
9
9
|
import { drizzle } from "drizzle-orm/mysql-proxy";
|
|
10
|
-
import { customType, mysqlTable, timestamp, varchar, bigint } from "drizzle-orm/mysql-core";
|
|
10
|
+
import { customType, mysqlTable, timestamp, varchar, bigint, mysqlSchema, longtext, double, boolean, text, int, unionAll } from "drizzle-orm/mysql-core";
|
|
11
11
|
const parseDateTime = (value, format) => {
|
|
12
12
|
let result;
|
|
13
13
|
if (value instanceof Date) {
|
|
@@ -17,11 +17,16 @@ const parseDateTime = (value, format) => {
|
|
|
17
17
|
if (dt.isValid) {
|
|
18
18
|
result = dt.toJSDate();
|
|
19
19
|
} else {
|
|
20
|
-
const
|
|
21
|
-
if (
|
|
22
|
-
result =
|
|
20
|
+
const sqlDt = DateTime.fromSQL(value);
|
|
21
|
+
if (sqlDt.isValid) {
|
|
22
|
+
result = sqlDt.toJSDate();
|
|
23
23
|
} else {
|
|
24
|
-
|
|
24
|
+
const isoDt = DateTime.fromRFC2822(value);
|
|
25
|
+
if (isoDt.isValid) {
|
|
26
|
+
result = isoDt.toJSDate();
|
|
27
|
+
} else {
|
|
28
|
+
result = new Date(value);
|
|
29
|
+
}
|
|
25
30
|
}
|
|
26
31
|
}
|
|
27
32
|
}
|
|
@@ -30,7 +35,7 @@ const parseDateTime = (value, format) => {
|
|
|
30
35
|
}
|
|
31
36
|
return result;
|
|
32
37
|
};
|
|
33
|
-
function formatDateTime(value, format) {
|
|
38
|
+
function formatDateTime(value, format, isTimeStamp) {
|
|
34
39
|
let dt = null;
|
|
35
40
|
if (value instanceof Date) {
|
|
36
41
|
dt = DateTime.fromJSDate(value);
|
|
@@ -58,6 +63,20 @@ function formatDateTime(value, format) {
|
|
|
58
63
|
if (!dt?.isValid) {
|
|
59
64
|
throw new Error("Invalid Date");
|
|
60
65
|
}
|
|
66
|
+
const minDate = DateTime.fromSeconds(1);
|
|
67
|
+
const maxDate = DateTime.fromMillis(2147483647 * 1e3);
|
|
68
|
+
if (isTimeStamp) {
|
|
69
|
+
if (dt < minDate) {
|
|
70
|
+
throw new Error(
|
|
71
|
+
"Atlassian Forge does not support zero or negative timestamps. Allowed range: from '1970-01-01 00:00:01.000000' to '2038-01-19 03:14:07.999999'."
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
if (dt > maxDate) {
|
|
75
|
+
throw new Error(
|
|
76
|
+
"Atlassian Forge does not support timestamps beyond 2038-01-19 03:14:07.999999. Please use a smaller date within the supported range."
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
61
80
|
return dt.toFormat(format);
|
|
62
81
|
}
|
|
63
82
|
function getPrimaryKeys(table) {
|
|
@@ -461,7 +480,9 @@ async function clearExpiredCache(options) {
|
|
|
461
480
|
);
|
|
462
481
|
} finally {
|
|
463
482
|
const duration = DateTime.now().toSeconds() - startTime.toSeconds();
|
|
464
|
-
|
|
483
|
+
if (options?.logRawSqlQuery) {
|
|
484
|
+
console.debug(`Cleared ${totalRecords} expired cache records in ${duration} seconds`);
|
|
485
|
+
}
|
|
465
486
|
}
|
|
466
487
|
}
|
|
467
488
|
async function getFromCache(query, options) {
|
|
@@ -548,7 +569,7 @@ async function saveQueryLocalCacheQuery(query, rows, options) {
|
|
|
548
569
|
};
|
|
549
570
|
if (options.logRawSqlQuery) {
|
|
550
571
|
const q = sql2.toSQL();
|
|
551
|
-
console.
|
|
572
|
+
console.debug(
|
|
552
573
|
`[forge-sql-orm][local-cache][SAVE] Stored result in cache. sql="${q.sql}", params=${JSON.stringify(q.params)}`
|
|
553
574
|
);
|
|
554
575
|
}
|
|
@@ -565,7 +586,7 @@ async function getQueryLocalCacheQuery(query, options) {
|
|
|
565
586
|
if (context.cache[key] && context.cache[key].sql === sql2.toSQL().sql.toLowerCase()) {
|
|
566
587
|
if (options.logRawSqlQuery) {
|
|
567
588
|
const q = sql2.toSQL();
|
|
568
|
-
console.
|
|
589
|
+
console.debug(
|
|
569
590
|
`[forge-sql-orm][local-cache][HIT] Returned cached result. sql="${q.sql}", params=${JSON.stringify(q.params)}`
|
|
570
591
|
);
|
|
571
592
|
}
|
|
@@ -995,28 +1016,23 @@ class ForgeSQLSelectOperations {
|
|
|
995
1016
|
}
|
|
996
1017
|
}
|
|
997
1018
|
const forgeDriver = async (query, params, method) => {
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
sqlStatement.bindParams(...params);
|
|
1003
|
-
}
|
|
1004
|
-
const updateQueryResponseResults = await sqlStatement.execute();
|
|
1005
|
-
let result = updateQueryResponseResults.rows;
|
|
1006
|
-
return { ...result, rows: [result] };
|
|
1007
|
-
} else {
|
|
1008
|
-
const sqlStatement = await sql$1.prepare(query);
|
|
1009
|
-
if (params) {
|
|
1010
|
-
await sqlStatement.bindParams(...params);
|
|
1011
|
-
}
|
|
1012
|
-
const result = await sqlStatement.execute();
|
|
1013
|
-
let rows;
|
|
1014
|
-
rows = result.rows.map((r) => Object.values(r));
|
|
1015
|
-
return { rows };
|
|
1019
|
+
if (method == "execute") {
|
|
1020
|
+
const sqlStatement = sql$1.prepare(query);
|
|
1021
|
+
if (params) {
|
|
1022
|
+
sqlStatement.bindParams(...params);
|
|
1016
1023
|
}
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1024
|
+
const updateQueryResponseResults = await sqlStatement.execute();
|
|
1025
|
+
let result = updateQueryResponseResults.rows;
|
|
1026
|
+
return { ...result, rows: [result] };
|
|
1027
|
+
} else {
|
|
1028
|
+
const sqlStatement = await sql$1.prepare(query);
|
|
1029
|
+
if (params) {
|
|
1030
|
+
await sqlStatement.bindParams(...params);
|
|
1031
|
+
}
|
|
1032
|
+
const result = await sqlStatement.execute();
|
|
1033
|
+
let rows;
|
|
1034
|
+
rows = result.rows.map((r) => Object.values(r));
|
|
1035
|
+
return { rows };
|
|
1020
1036
|
}
|
|
1021
1037
|
};
|
|
1022
1038
|
function injectSqlHints(query, hints) {
|
|
@@ -1053,9 +1069,16 @@ function createForgeDriverProxy(options, logRawSqlQuery) {
|
|
|
1053
1069
|
return async (query, params, method) => {
|
|
1054
1070
|
const modifiedQuery = injectSqlHints(query, options);
|
|
1055
1071
|
if (options && logRawSqlQuery && modifiedQuery !== query) {
|
|
1056
|
-
console.
|
|
1072
|
+
console.debug("injected Hints: " + modifiedQuery);
|
|
1073
|
+
}
|
|
1074
|
+
try {
|
|
1075
|
+
return await forgeDriver(modifiedQuery, params, method);
|
|
1076
|
+
} catch (error) {
|
|
1077
|
+
if (logRawSqlQuery) {
|
|
1078
|
+
console.debug("SQL Error:", JSON.stringify(error));
|
|
1079
|
+
}
|
|
1080
|
+
throw error;
|
|
1057
1081
|
}
|
|
1058
|
-
return forgeDriver(modifiedQuery, params, method);
|
|
1059
1082
|
};
|
|
1060
1083
|
}
|
|
1061
1084
|
const NON_CACHE_CLEARING_ERROR_CODES = ["VALIDATION_ERROR", "CONSTRAINT_ERROR"];
|
|
@@ -1090,8 +1113,8 @@ async function handleSuccessfulExecution(rows, onfulfilled, table, options, isCa
|
|
|
1090
1113
|
if (shouldClearCacheOnError(error)) {
|
|
1091
1114
|
await evictLocalCacheQuery(table, options);
|
|
1092
1115
|
if (isCached) {
|
|
1093
|
-
await clearCache(table, options).catch(() => {
|
|
1094
|
-
console.warn("Ignore cache clear errors");
|
|
1116
|
+
await clearCache(table, options).catch((e) => {
|
|
1117
|
+
console.warn("Ignore cache clear errors", e);
|
|
1095
1118
|
});
|
|
1096
1119
|
} else {
|
|
1097
1120
|
await saveTableIfInsideCacheContext(table);
|
|
@@ -2553,10 +2576,10 @@ const forgeDateTimeString = customType({
|
|
|
2553
2576
|
return "datetime";
|
|
2554
2577
|
},
|
|
2555
2578
|
toDriver(value) {
|
|
2556
|
-
return formatDateTime(value, "yyyy-
|
|
2579
|
+
return formatDateTime(value, "yyyy-MM-dd' 'HH:mm:ss.SSS", false);
|
|
2557
2580
|
},
|
|
2558
2581
|
fromDriver(value) {
|
|
2559
|
-
const format = "yyyy-
|
|
2582
|
+
const format = "yyyy-MM-dd' 'HH:mm:ss.SSS";
|
|
2560
2583
|
return parseDateTime(value, format);
|
|
2561
2584
|
}
|
|
2562
2585
|
});
|
|
@@ -2565,10 +2588,10 @@ const forgeTimestampString = customType({
|
|
|
2565
2588
|
return "timestamp";
|
|
2566
2589
|
},
|
|
2567
2590
|
toDriver(value) {
|
|
2568
|
-
return formatDateTime(value, "yyyy-
|
|
2591
|
+
return formatDateTime(value, "yyyy-MM-dd' 'HH:mm:ss.SSS", true);
|
|
2569
2592
|
},
|
|
2570
2593
|
fromDriver(value) {
|
|
2571
|
-
const format = "yyyy-
|
|
2594
|
+
const format = "yyyy-MM-dd' 'HH:mm:ss.SSS";
|
|
2572
2595
|
return parseDateTime(value, format);
|
|
2573
2596
|
}
|
|
2574
2597
|
});
|
|
@@ -2577,10 +2600,10 @@ const forgeDateString = customType({
|
|
|
2577
2600
|
return "date";
|
|
2578
2601
|
},
|
|
2579
2602
|
toDriver(value) {
|
|
2580
|
-
return formatDateTime(value, "yyyy-
|
|
2603
|
+
return formatDateTime(value, "yyyy-MM-dd", false);
|
|
2581
2604
|
},
|
|
2582
2605
|
fromDriver(value) {
|
|
2583
|
-
const format = "yyyy-
|
|
2606
|
+
const format = "yyyy-MM-dd";
|
|
2584
2607
|
return parseDateTime(value, format);
|
|
2585
2608
|
}
|
|
2586
2609
|
});
|
|
@@ -2589,7 +2612,7 @@ const forgeTimeString = customType({
|
|
|
2589
2612
|
return "time";
|
|
2590
2613
|
},
|
|
2591
2614
|
toDriver(value) {
|
|
2592
|
-
return formatDateTime(value, "HH:mm:ss.SSS");
|
|
2615
|
+
return formatDateTime(value, "HH:mm:ss.SSS", false);
|
|
2593
2616
|
},
|
|
2594
2617
|
fromDriver(value) {
|
|
2595
2618
|
return parseDateTime(value, "HH:mm:ss.SSS");
|
|
@@ -2600,6 +2623,435 @@ const migrations = mysqlTable("__migrations", {
|
|
|
2600
2623
|
name: varchar("name", { length: 255 }).notNull(),
|
|
2601
2624
|
migratedAt: timestamp("migratedAt").defaultNow().notNull()
|
|
2602
2625
|
});
|
|
2626
|
+
const informationSchema = mysqlSchema("information_schema");
|
|
2627
|
+
const slowQuery = informationSchema.table("SLOW_QUERY", {
|
|
2628
|
+
time: timestamp("Time", { fsp: 6, mode: "string" }).notNull(),
|
|
2629
|
+
// Timestamp when the slow query was recorded
|
|
2630
|
+
txnStartTs: bigint("Txn_start_ts", { mode: "bigint", unsigned: true }),
|
|
2631
|
+
// Transaction start timestamp (TSO)
|
|
2632
|
+
user: varchar("User", { length: 64 }),
|
|
2633
|
+
// User executing the query
|
|
2634
|
+
host: varchar("Host", { length: 64 }),
|
|
2635
|
+
// Host from which the query originated
|
|
2636
|
+
connId: bigint("Conn_ID", { mode: "bigint", unsigned: true }),
|
|
2637
|
+
// Connection ID
|
|
2638
|
+
sessionAlias: varchar("Session_alias", { length: 64 }),
|
|
2639
|
+
// Session alias
|
|
2640
|
+
execRetryCount: bigint("Exec_retry_count", { mode: "bigint", unsigned: true }),
|
|
2641
|
+
// Number of retries during execution
|
|
2642
|
+
execRetryTime: double("Exec_retry_time"),
|
|
2643
|
+
// Time spent in retries
|
|
2644
|
+
queryTime: double("Query_time"),
|
|
2645
|
+
// Total execution time
|
|
2646
|
+
parseTime: double("Parse_time"),
|
|
2647
|
+
// Time spent parsing SQL
|
|
2648
|
+
compileTime: double("Compile_time"),
|
|
2649
|
+
// Time spent compiling query plan
|
|
2650
|
+
rewriteTime: double("Rewrite_time"),
|
|
2651
|
+
// Time spent rewriting query
|
|
2652
|
+
preprocSubqueries: bigint("Preproc_subqueries", { mode: "bigint", unsigned: true }),
|
|
2653
|
+
// Number of subqueries preprocessed
|
|
2654
|
+
preprocSubqueriesTime: double("Preproc_subqueries_time"),
|
|
2655
|
+
// Time spent preprocessing subqueries
|
|
2656
|
+
optimizeTime: double("Optimize_time"),
|
|
2657
|
+
// Time spent in optimizer
|
|
2658
|
+
waitTs: double("Wait_TS"),
|
|
2659
|
+
// Wait time for getting TSO
|
|
2660
|
+
prewriteTime: double("Prewrite_time"),
|
|
2661
|
+
// Time spent in prewrite phase
|
|
2662
|
+
waitPrewriteBinlogTime: double("Wait_prewrite_binlog_time"),
|
|
2663
|
+
// Time waiting for binlog prewrite
|
|
2664
|
+
commitTime: double("Commit_time"),
|
|
2665
|
+
// Commit duration
|
|
2666
|
+
getCommitTsTime: double("Get_commit_ts_time"),
|
|
2667
|
+
// Time waiting for commit TSO
|
|
2668
|
+
commitBackoffTime: double("Commit_backoff_time"),
|
|
2669
|
+
// Backoff time during commit
|
|
2670
|
+
backoffTypes: varchar("Backoff_types", { length: 64 }),
|
|
2671
|
+
// Types of backoff occurred
|
|
2672
|
+
resolveLockTime: double("Resolve_lock_time"),
|
|
2673
|
+
// Time resolving locks
|
|
2674
|
+
localLatchWaitTime: double("Local_latch_wait_time"),
|
|
2675
|
+
// Time waiting on local latch
|
|
2676
|
+
writeKeys: bigint("Write_keys", { mode: "bigint" }),
|
|
2677
|
+
// Number of keys written
|
|
2678
|
+
writeSize: bigint("Write_size", { mode: "bigint" }),
|
|
2679
|
+
// Amount of data written
|
|
2680
|
+
prewriteRegion: bigint("Prewrite_region", { mode: "bigint" }),
|
|
2681
|
+
// Regions involved in prewrite
|
|
2682
|
+
txnRetry: bigint("Txn_retry", { mode: "bigint" }),
|
|
2683
|
+
// Transaction retry count
|
|
2684
|
+
copTime: double("Cop_time"),
|
|
2685
|
+
// Time spent in coprocessor
|
|
2686
|
+
processTime: double("Process_time"),
|
|
2687
|
+
// Processing time
|
|
2688
|
+
waitTime: double("Wait_time"),
|
|
2689
|
+
// Wait time in TiKV
|
|
2690
|
+
backoffTime: double("Backoff_time"),
|
|
2691
|
+
// Backoff wait time
|
|
2692
|
+
lockKeysTime: double("LockKeys_time"),
|
|
2693
|
+
// Time spent waiting for locks
|
|
2694
|
+
requestCount: bigint("Request_count", { mode: "bigint", unsigned: true }),
|
|
2695
|
+
// Total number of requests
|
|
2696
|
+
totalKeys: bigint("Total_keys", { mode: "bigint", unsigned: true }),
|
|
2697
|
+
// Total keys scanned
|
|
2698
|
+
processKeys: bigint("Process_keys", { mode: "bigint", unsigned: true }),
|
|
2699
|
+
// Keys processed
|
|
2700
|
+
rocksdbDeleteSkippedCount: bigint("Rocksdb_delete_skipped_count", {
|
|
2701
|
+
mode: "bigint",
|
|
2702
|
+
unsigned: true
|
|
2703
|
+
}),
|
|
2704
|
+
// RocksDB delete skips
|
|
2705
|
+
rocksdbKeySkippedCount: bigint("Rocksdb_key_skipped_count", { mode: "bigint", unsigned: true }),
|
|
2706
|
+
// RocksDB key skips
|
|
2707
|
+
rocksdbBlockCacheHitCount: bigint("Rocksdb_block_cache_hit_count", {
|
|
2708
|
+
mode: "bigint",
|
|
2709
|
+
unsigned: true
|
|
2710
|
+
}),
|
|
2711
|
+
// RocksDB block cache hits
|
|
2712
|
+
rocksdbBlockReadCount: bigint("Rocksdb_block_read_count", { mode: "bigint", unsigned: true }),
|
|
2713
|
+
// RocksDB block reads
|
|
2714
|
+
rocksdbBlockReadByte: bigint("Rocksdb_block_read_byte", { mode: "bigint", unsigned: true }),
|
|
2715
|
+
// RocksDB block read bytes
|
|
2716
|
+
db: varchar("DB", { length: 64 }),
|
|
2717
|
+
// Database name
|
|
2718
|
+
indexNames: varchar("Index_names", { length: 100 }),
|
|
2719
|
+
// Indexes used
|
|
2720
|
+
isInternal: boolean("Is_internal"),
|
|
2721
|
+
// Whether the query is internal
|
|
2722
|
+
digest: varchar("Digest", { length: 64 }),
|
|
2723
|
+
// SQL digest hash
|
|
2724
|
+
stats: varchar("Stats", { length: 512 }),
|
|
2725
|
+
// Stats used during planning
|
|
2726
|
+
copProcAvg: double("Cop_proc_avg"),
|
|
2727
|
+
// Coprocessor average processing time
|
|
2728
|
+
copProcP90: double("Cop_proc_p90"),
|
|
2729
|
+
// Coprocessor 90th percentile processing time
|
|
2730
|
+
copProcMax: double("Cop_proc_max"),
|
|
2731
|
+
// Coprocessor max processing time
|
|
2732
|
+
copProcAddr: varchar("Cop_proc_addr", { length: 64 }),
|
|
2733
|
+
// Coprocessor address for processing
|
|
2734
|
+
copWaitAvg: double("Cop_wait_avg"),
|
|
2735
|
+
// Coprocessor average wait time
|
|
2736
|
+
copWaitP90: double("Cop_wait_p90"),
|
|
2737
|
+
// Coprocessor 90th percentile wait time
|
|
2738
|
+
copWaitMax: double("Cop_wait_max"),
|
|
2739
|
+
// Coprocessor max wait time
|
|
2740
|
+
copWaitAddr: varchar("Cop_wait_addr", { length: 64 }),
|
|
2741
|
+
// Coprocessor address for wait
|
|
2742
|
+
memMax: bigint("Mem_max", { mode: "bigint" }),
|
|
2743
|
+
// Max memory usage
|
|
2744
|
+
diskMax: bigint("Disk_max", { mode: "bigint" }),
|
|
2745
|
+
// Max disk usage
|
|
2746
|
+
kvTotal: double("KV_total"),
|
|
2747
|
+
// Total KV request time
|
|
2748
|
+
pdTotal: double("PD_total"),
|
|
2749
|
+
// Total PD request time
|
|
2750
|
+
backoffTotal: double("Backoff_total"),
|
|
2751
|
+
// Total backoff time
|
|
2752
|
+
writeSqlResponseTotal: double("Write_sql_response_total"),
|
|
2753
|
+
// SQL response write time
|
|
2754
|
+
resultRows: bigint("Result_rows", { mode: "bigint" }),
|
|
2755
|
+
// Rows returned
|
|
2756
|
+
warnings: longtext("Warnings"),
|
|
2757
|
+
// Warnings during execution
|
|
2758
|
+
backoffDetail: varchar("Backoff_Detail", { length: 4096 }),
|
|
2759
|
+
// Detailed backoff info
|
|
2760
|
+
prepared: boolean("Prepared"),
|
|
2761
|
+
// Whether query was prepared
|
|
2762
|
+
succ: boolean("Succ"),
|
|
2763
|
+
// Success flag
|
|
2764
|
+
isExplicitTxn: boolean("IsExplicitTxn"),
|
|
2765
|
+
// Whether explicit transaction
|
|
2766
|
+
isWriteCacheTable: boolean("IsWriteCacheTable"),
|
|
2767
|
+
// Whether wrote to cache table
|
|
2768
|
+
planFromCache: boolean("Plan_from_cache"),
|
|
2769
|
+
// Plan was from cache
|
|
2770
|
+
planFromBinding: boolean("Plan_from_binding"),
|
|
2771
|
+
// Plan was from binding
|
|
2772
|
+
hasMoreResults: boolean("Has_more_results"),
|
|
2773
|
+
// Query returned multiple results
|
|
2774
|
+
resourceGroup: varchar("Resource_group", { length: 64 }),
|
|
2775
|
+
// Resource group name
|
|
2776
|
+
requestUnitRead: double("Request_unit_read"),
|
|
2777
|
+
// RU consumed for read
|
|
2778
|
+
requestUnitWrite: double("Request_unit_write"),
|
|
2779
|
+
// RU consumed for write
|
|
2780
|
+
timeQueuedByRc: double("Time_queued_by_rc"),
|
|
2781
|
+
// Time queued by resource control
|
|
2782
|
+
tidbCpuTime: double("Tidb_cpu_time"),
|
|
2783
|
+
// TiDB CPU time
|
|
2784
|
+
tikvCpuTime: double("Tikv_cpu_time"),
|
|
2785
|
+
// TiKV CPU time
|
|
2786
|
+
plan: longtext("Plan"),
|
|
2787
|
+
// Query execution plan
|
|
2788
|
+
planDigest: varchar("Plan_digest", { length: 128 }),
|
|
2789
|
+
// Plan digest hash
|
|
2790
|
+
binaryPlan: longtext("Binary_plan"),
|
|
2791
|
+
// Binary execution plan
|
|
2792
|
+
prevStmt: longtext("Prev_stmt"),
|
|
2793
|
+
// Previous statement in session
|
|
2794
|
+
query: longtext("Query")
|
|
2795
|
+
// Original SQL query
|
|
2796
|
+
});
|
|
2797
|
+
const createClusterStatementsSummarySchema = () => ({
|
|
2798
|
+
instance: varchar("INSTANCE", { length: 64 }),
|
|
2799
|
+
// TiDB/TiKV instance address
|
|
2800
|
+
summaryBeginTime: timestamp("SUMMARY_BEGIN_TIME", { mode: "string" }).notNull(),
|
|
2801
|
+
// Begin time of this summary window
|
|
2802
|
+
summaryEndTime: timestamp("SUMMARY_END_TIME", { mode: "string" }).notNull(),
|
|
2803
|
+
// End time of this summary window
|
|
2804
|
+
stmtType: varchar("STMT_TYPE", { length: 64 }).notNull(),
|
|
2805
|
+
// Statement type (e.g., Select/Insert/Update)
|
|
2806
|
+
schemaName: varchar("SCHEMA_NAME", { length: 64 }),
|
|
2807
|
+
// Current schema name
|
|
2808
|
+
digest: varchar("DIGEST", { length: 64 }),
|
|
2809
|
+
// SQL digest (normalized hash)
|
|
2810
|
+
digestText: text("DIGEST_TEXT").notNull(),
|
|
2811
|
+
// Normalized SQL text
|
|
2812
|
+
tableNames: text("TABLE_NAMES"),
|
|
2813
|
+
// Involved table names
|
|
2814
|
+
indexNames: text("INDEX_NAMES"),
|
|
2815
|
+
// Used index names
|
|
2816
|
+
sampleUser: varchar("SAMPLE_USER", { length: 64 }),
|
|
2817
|
+
// Sampled user who executed the statements
|
|
2818
|
+
execCount: bigint("EXEC_COUNT", { mode: "bigint", unsigned: true }).notNull(),
|
|
2819
|
+
// Total executions
|
|
2820
|
+
sumErrors: int("SUM_ERRORS", { unsigned: true }).notNull(),
|
|
2821
|
+
// Sum of errors
|
|
2822
|
+
sumWarnings: int("SUM_WARNINGS", { unsigned: true }).notNull(),
|
|
2823
|
+
// Sum of warnings
|
|
2824
|
+
sumLatency: bigint("SUM_LATENCY", { mode: "bigint", unsigned: true }).notNull(),
|
|
2825
|
+
// Sum of latency (ns)
|
|
2826
|
+
maxLatency: bigint("MAX_LATENCY", { mode: "bigint", unsigned: true }).notNull(),
|
|
2827
|
+
// Max latency (ns)
|
|
2828
|
+
minLatency: bigint("MIN_LATENCY", { mode: "bigint", unsigned: true }).notNull(),
|
|
2829
|
+
// Min latency (ns)
|
|
2830
|
+
avgLatency: bigint("AVG_LATENCY", { mode: "bigint", unsigned: true }).notNull(),
|
|
2831
|
+
// Avg latency (ns)
|
|
2832
|
+
avgParseLatency: bigint("AVG_PARSE_LATENCY", { mode: "bigint", unsigned: true }).notNull(),
|
|
2833
|
+
// Avg parse time (ns)
|
|
2834
|
+
maxParseLatency: bigint("MAX_PARSE_LATENCY", { mode: "bigint", unsigned: true }).notNull(),
|
|
2835
|
+
// Max parse time (ns)
|
|
2836
|
+
avgCompileLatency: bigint("AVG_COMPILE_LATENCY", { mode: "bigint", unsigned: true }).notNull(),
|
|
2837
|
+
// Avg compile time (ns)
|
|
2838
|
+
maxCompileLatency: bigint("MAX_COMPILE_LATENCY", { mode: "bigint", unsigned: true }).notNull(),
|
|
2839
|
+
// Max compile time (ns)
|
|
2840
|
+
sumCopTaskNum: bigint("SUM_COP_TASK_NUM", { mode: "bigint", unsigned: true }).notNull(),
|
|
2841
|
+
// Total number of cop tasks
|
|
2842
|
+
maxCopProcessTime: bigint("MAX_COP_PROCESS_TIME", { mode: "bigint", unsigned: true }).notNull(),
|
|
2843
|
+
// Max TiKV coprocessor processing time (ns)
|
|
2844
|
+
maxCopProcessAddress: varchar("MAX_COP_PROCESS_ADDRESS", { length: 256 }),
|
|
2845
|
+
// Address of cop task with max processing time
|
|
2846
|
+
maxCopWaitTime: bigint("MAX_COP_WAIT_TIME", { mode: "bigint", unsigned: true }).notNull(),
|
|
2847
|
+
// Max TiKV coprocessor wait time (ns)
|
|
2848
|
+
maxCopWaitAddress: varchar("MAX_COP_WAIT_ADDRESS", { length: 256 }),
|
|
2849
|
+
// Address of cop task with max wait time
|
|
2850
|
+
avgProcessTime: bigint("AVG_PROCESS_TIME", { mode: "bigint", unsigned: true }).notNull(),
|
|
2851
|
+
// Avg TiKV processing time (ns)
|
|
2852
|
+
maxProcessTime: bigint("MAX_PROCESS_TIME", { mode: "bigint", unsigned: true }).notNull(),
|
|
2853
|
+
// Max TiKV processing time (ns)
|
|
2854
|
+
avgWaitTime: bigint("AVG_WAIT_TIME", { mode: "bigint", unsigned: true }).notNull(),
|
|
2855
|
+
// Avg TiKV wait time (ns)
|
|
2856
|
+
maxWaitTime: bigint("MAX_WAIT_TIME", { mode: "bigint", unsigned: true }).notNull(),
|
|
2857
|
+
// Max TiKV wait time (ns)
|
|
2858
|
+
avgBackoffTime: bigint("AVG_BACKOFF_TIME", { mode: "bigint", unsigned: true }).notNull(),
|
|
2859
|
+
// Avg backoff time before retry (ns)
|
|
2860
|
+
maxBackoffTime: bigint("MAX_BACKOFF_TIME", { mode: "bigint", unsigned: true }).notNull(),
|
|
2861
|
+
// Max backoff time before retry (ns)
|
|
2862
|
+
avgTotalKeys: bigint("AVG_TOTAL_KEYS", { mode: "bigint", unsigned: true }).notNull(),
|
|
2863
|
+
// Avg scanned keys
|
|
2864
|
+
maxTotalKeys: bigint("MAX_TOTAL_KEYS", { mode: "bigint", unsigned: true }).notNull(),
|
|
2865
|
+
// Max scanned keys
|
|
2866
|
+
avgProcessedKeys: bigint("AVG_PROCESSED_KEYS", { mode: "bigint", unsigned: true }).notNull(),
|
|
2867
|
+
// Avg processed keys
|
|
2868
|
+
maxProcessedKeys: bigint("MAX_PROCESSED_KEYS", { mode: "bigint", unsigned: true }).notNull(),
|
|
2869
|
+
// Max processed keys
|
|
2870
|
+
avgRocksdbDeleteSkippedCount: double("AVG_ROCKSDB_DELETE_SKIPPED_COUNT").notNull(),
|
|
2871
|
+
// Avg RocksDB deletes skipped
|
|
2872
|
+
maxRocksdbDeleteSkippedCount: int("MAX_ROCKSDB_DELETE_SKIPPED_COUNT", {
|
|
2873
|
+
unsigned: true
|
|
2874
|
+
}).notNull(),
|
|
2875
|
+
// Max RocksDB deletes skipped
|
|
2876
|
+
avgRocksdbKeySkippedCount: double("AVG_ROCKSDB_KEY_SKIPPED_COUNT").notNull(),
|
|
2877
|
+
// Avg RocksDB keys skipped
|
|
2878
|
+
maxRocksdbKeySkippedCount: int("MAX_ROCKSDB_KEY_SKIPPED_COUNT", { unsigned: true }).notNull(),
|
|
2879
|
+
// Max RocksDB keys skipped
|
|
2880
|
+
avgRocksdbBlockCacheHitCount: double("AVG_ROCKSDB_BLOCK_CACHE_HIT_COUNT").notNull(),
|
|
2881
|
+
// Avg RocksDB block cache hits
|
|
2882
|
+
maxRocksdbBlockCacheHitCount: int("MAX_ROCKSDB_BLOCK_CACHE_HIT_COUNT", {
|
|
2883
|
+
unsigned: true
|
|
2884
|
+
}).notNull(),
|
|
2885
|
+
// Max RocksDB block cache hits
|
|
2886
|
+
avgRocksdbBlockReadCount: double("AVG_ROCKSDB_BLOCK_READ_COUNT").notNull(),
|
|
2887
|
+
// Avg RocksDB block reads
|
|
2888
|
+
maxRocksdbBlockReadCount: int("MAX_ROCKSDB_BLOCK_READ_COUNT", { unsigned: true }).notNull(),
|
|
2889
|
+
// Max RocksDB block reads
|
|
2890
|
+
avgRocksdbBlockReadByte: double("AVG_ROCKSDB_BLOCK_READ_BYTE").notNull(),
|
|
2891
|
+
// Avg RocksDB block read bytes
|
|
2892
|
+
maxRocksdbBlockReadByte: int("MAX_ROCKSDB_BLOCK_READ_BYTE", { unsigned: true }).notNull(),
|
|
2893
|
+
// Max RocksDB block read bytes
|
|
2894
|
+
avgPrewriteTime: bigint("AVG_PREWRITE_TIME", { mode: "bigint", unsigned: true }).notNull(),
|
|
2895
|
+
// Avg prewrite phase time (ns)
|
|
2896
|
+
maxPrewriteTime: bigint("MAX_PREWRITE_TIME", { mode: "bigint", unsigned: true }).notNull(),
|
|
2897
|
+
// Max prewrite phase time (ns)
|
|
2898
|
+
avgCommitTime: bigint("AVG_COMMIT_TIME", { mode: "bigint", unsigned: true }).notNull(),
|
|
2899
|
+
// Avg commit phase time (ns)
|
|
2900
|
+
maxCommitTime: bigint("MAX_COMMIT_TIME", { mode: "bigint", unsigned: true }).notNull(),
|
|
2901
|
+
// Max commit phase time (ns)
|
|
2902
|
+
avgGetCommitTsTime: bigint("AVG_GET_COMMIT_TS_TIME", {
|
|
2903
|
+
mode: "bigint",
|
|
2904
|
+
unsigned: true
|
|
2905
|
+
}).notNull(),
|
|
2906
|
+
// Avg get commit_ts time (ns)
|
|
2907
|
+
maxGetCommitTsTime: bigint("MAX_GET_COMMIT_TS_TIME", {
|
|
2908
|
+
mode: "bigint",
|
|
2909
|
+
unsigned: true
|
|
2910
|
+
}).notNull(),
|
|
2911
|
+
// Max get commit_ts time (ns)
|
|
2912
|
+
avgCommitBackoffTime: bigint("AVG_COMMIT_BACKOFF_TIME", {
|
|
2913
|
+
mode: "bigint",
|
|
2914
|
+
unsigned: true
|
|
2915
|
+
}).notNull(),
|
|
2916
|
+
// Avg backoff during commit (ns)
|
|
2917
|
+
maxCommitBackoffTime: bigint("MAX_COMMIT_BACKOFF_TIME", {
|
|
2918
|
+
mode: "bigint",
|
|
2919
|
+
unsigned: true
|
|
2920
|
+
}).notNull(),
|
|
2921
|
+
// Max backoff during commit (ns)
|
|
2922
|
+
avgResolveLockTime: bigint("AVG_RESOLVE_LOCK_TIME", {
|
|
2923
|
+
mode: "bigint",
|
|
2924
|
+
unsigned: true
|
|
2925
|
+
}).notNull(),
|
|
2926
|
+
// Avg resolve lock time (ns)
|
|
2927
|
+
maxResolveLockTime: bigint("MAX_RESOLVE_LOCK_TIME", {
|
|
2928
|
+
mode: "bigint",
|
|
2929
|
+
unsigned: true
|
|
2930
|
+
}).notNull(),
|
|
2931
|
+
// Max resolve lock time (ns)
|
|
2932
|
+
avgLocalLatchWaitTime: bigint("AVG_LOCAL_LATCH_WAIT_TIME", {
|
|
2933
|
+
mode: "bigint",
|
|
2934
|
+
unsigned: true
|
|
2935
|
+
}).notNull(),
|
|
2936
|
+
// Avg local latch wait (ns)
|
|
2937
|
+
maxLocalLatchWaitTime: bigint("MAX_LOCAL_LATCH_WAIT_TIME", {
|
|
2938
|
+
mode: "bigint",
|
|
2939
|
+
unsigned: true
|
|
2940
|
+
}).notNull(),
|
|
2941
|
+
// Max local latch wait (ns)
|
|
2942
|
+
avgWriteKeys: double("AVG_WRITE_KEYS").notNull(),
|
|
2943
|
+
// Avg number of written keys
|
|
2944
|
+
maxWriteKeys: bigint("MAX_WRITE_KEYS", { mode: "bigint", unsigned: true }).notNull(),
|
|
2945
|
+
// Max written keys
|
|
2946
|
+
avgWriteSize: double("AVG_WRITE_SIZE").notNull(),
|
|
2947
|
+
// Avg written bytes
|
|
2948
|
+
maxWriteSize: bigint("MAX_WRITE_SIZE", { mode: "bigint", unsigned: true }).notNull(),
|
|
2949
|
+
// Max written bytes
|
|
2950
|
+
avgPrewriteRegions: double("AVG_PREWRITE_REGIONS").notNull(),
|
|
2951
|
+
// Avg regions in prewrite
|
|
2952
|
+
maxPrewriteRegions: int("MAX_PREWRITE_REGIONS", { unsigned: true }).notNull(),
|
|
2953
|
+
// Max regions in prewrite
|
|
2954
|
+
avgTxnRetry: double("AVG_TXN_RETRY").notNull(),
|
|
2955
|
+
// Avg transaction retry count
|
|
2956
|
+
maxTxnRetry: int("MAX_TXN_RETRY", { unsigned: true }).notNull(),
|
|
2957
|
+
// Max transaction retry count
|
|
2958
|
+
sumExecRetry: bigint("SUM_EXEC_RETRY", { mode: "bigint", unsigned: true }).notNull(),
|
|
2959
|
+
// Sum of execution retries (pessimistic)
|
|
2960
|
+
sumExecRetryTime: bigint("SUM_EXEC_RETRY_TIME", { mode: "bigint", unsigned: true }).notNull(),
|
|
2961
|
+
// Sum time of execution retries (ns)
|
|
2962
|
+
sumBackoffTimes: bigint("SUM_BACKOFF_TIMES", { mode: "bigint", unsigned: true }).notNull(),
|
|
2963
|
+
// Sum of backoff retries
|
|
2964
|
+
backoffTypes: varchar("BACKOFF_TYPES", { length: 1024 }),
|
|
2965
|
+
// Backoff types with counts
|
|
2966
|
+
avgMem: bigint("AVG_MEM", { mode: "bigint", unsigned: true }).notNull(),
|
|
2967
|
+
// Avg memory used (bytes)
|
|
2968
|
+
maxMem: bigint("MAX_MEM", { mode: "bigint", unsigned: true }).notNull(),
|
|
2969
|
+
// Max memory used (bytes)
|
|
2970
|
+
avgDisk: bigint("AVG_DISK", { mode: "bigint", unsigned: true }).notNull(),
|
|
2971
|
+
// Avg disk used (bytes)
|
|
2972
|
+
maxDisk: bigint("MAX_DISK", { mode: "bigint", unsigned: true }).notNull(),
|
|
2973
|
+
// Max disk used (bytes)
|
|
2974
|
+
avgKvTime: bigint("AVG_KV_TIME", { mode: "bigint", unsigned: true }).notNull(),
|
|
2975
|
+
// Avg time spent in TiKV (ns)
|
|
2976
|
+
avgPdTime: bigint("AVG_PD_TIME", { mode: "bigint", unsigned: true }).notNull(),
|
|
2977
|
+
// Avg time spent in PD (ns)
|
|
2978
|
+
avgBackoffTotalTime: bigint("AVG_BACKOFF_TOTAL_TIME", {
|
|
2979
|
+
mode: "bigint",
|
|
2980
|
+
unsigned: true
|
|
2981
|
+
}).notNull(),
|
|
2982
|
+
// Avg total backoff time (ns)
|
|
2983
|
+
avgWriteSqlRespTime: bigint("AVG_WRITE_SQL_RESP_TIME", {
|
|
2984
|
+
mode: "bigint",
|
|
2985
|
+
unsigned: true
|
|
2986
|
+
}).notNull(),
|
|
2987
|
+
// Avg write SQL response time (ns)
|
|
2988
|
+
avgTidbCpuTime: bigint("AVG_TIDB_CPU_TIME", { mode: "bigint", unsigned: true }).notNull(),
|
|
2989
|
+
// Avg TiDB CPU time (ns)
|
|
2990
|
+
avgTikvCpuTime: bigint("AVG_TIKV_CPU_TIME", { mode: "bigint", unsigned: true }).notNull(),
|
|
2991
|
+
// Avg TiKV CPU time (ns)
|
|
2992
|
+
maxResultRows: bigint("MAX_RESULT_ROWS", { mode: "bigint" }).notNull(),
|
|
2993
|
+
// Max number of result rows
|
|
2994
|
+
minResultRows: bigint("MIN_RESULT_ROWS", { mode: "bigint" }).notNull(),
|
|
2995
|
+
// Min number of result rows
|
|
2996
|
+
avgResultRows: bigint("AVG_RESULT_ROWS", { mode: "bigint" }).notNull(),
|
|
2997
|
+
// Avg number of result rows
|
|
2998
|
+
prepared: boolean("PREPARED").notNull(),
|
|
2999
|
+
// Whether statements are prepared
|
|
3000
|
+
avgAffectedRows: double("AVG_AFFECTED_ROWS").notNull(),
|
|
3001
|
+
// Avg affected rows
|
|
3002
|
+
firstSeen: timestamp("FIRST_SEEN", { mode: "string" }).notNull(),
|
|
3003
|
+
// First time statements observed
|
|
3004
|
+
lastSeen: timestamp("LAST_SEEN", { mode: "string" }).notNull(),
|
|
3005
|
+
// Last time statements observed
|
|
3006
|
+
planInCache: boolean("PLAN_IN_CACHE").notNull(),
|
|
3007
|
+
// Whether last stmt hit plan cache
|
|
3008
|
+
planCacheHits: bigint("PLAN_CACHE_HITS", { mode: "bigint" }).notNull(),
|
|
3009
|
+
// Number of plan cache hits
|
|
3010
|
+
planInBinding: boolean("PLAN_IN_BINDING").notNull(),
|
|
3011
|
+
// Whether matched bindings
|
|
3012
|
+
querySampleText: text("QUERY_SAMPLE_TEXT"),
|
|
3013
|
+
// Sampled original SQL
|
|
3014
|
+
prevSampleText: text("PREV_SAMPLE_TEXT"),
|
|
3015
|
+
// Sampled previous SQL before commit
|
|
3016
|
+
planDigest: varchar("PLAN_DIGEST", { length: 64 }),
|
|
3017
|
+
// Plan digest hash
|
|
3018
|
+
plan: text("PLAN"),
|
|
3019
|
+
// Sampled textual plan
|
|
3020
|
+
binaryPlan: text("BINARY_PLAN"),
|
|
3021
|
+
// Sampled binary plan
|
|
3022
|
+
charset: varchar("CHARSET", { length: 64 }),
|
|
3023
|
+
// Sampled charset
|
|
3024
|
+
collation: varchar("COLLATION", { length: 64 }),
|
|
3025
|
+
// Sampled collation
|
|
3026
|
+
planHint: varchar("PLAN_HINT", { length: 64 }),
|
|
3027
|
+
// Sampled plan hint
|
|
3028
|
+
maxRequestUnitRead: double("MAX_REQUEST_UNIT_READ").notNull(),
|
|
3029
|
+
// Max RU cost (read)
|
|
3030
|
+
avgRequestUnitRead: double("AVG_REQUEST_UNIT_READ").notNull(),
|
|
3031
|
+
// Avg RU cost (read)
|
|
3032
|
+
maxRequestUnitWrite: double("MAX_REQUEST_UNIT_WRITE").notNull(),
|
|
3033
|
+
// Max RU cost (write)
|
|
3034
|
+
avgRequestUnitWrite: double("AVG_REQUEST_UNIT_WRITE").notNull(),
|
|
3035
|
+
// Avg RU cost (write)
|
|
3036
|
+
maxQueuedRcTime: bigint("MAX_QUEUED_RC_TIME", { mode: "bigint", unsigned: true }).notNull(),
|
|
3037
|
+
// Max queued time waiting for RU (ns)
|
|
3038
|
+
avgQueuedRcTime: bigint("AVG_QUEUED_RC_TIME", { mode: "bigint", unsigned: true }).notNull(),
|
|
3039
|
+
// Avg queued time waiting for RU (ns)
|
|
3040
|
+
resourceGroup: varchar("RESOURCE_GROUP", { length: 64 }),
|
|
3041
|
+
// Bound resource group name
|
|
3042
|
+
planCacheUnqualified: bigint("PLAN_CACHE_UNQUALIFIED", { mode: "bigint" }).notNull(),
|
|
3043
|
+
// Times not eligible for plan cache
|
|
3044
|
+
planCacheUnqualifiedLastReason: text("PLAN_CACHE_UNQUALIFIED_LAST_REASON")
|
|
3045
|
+
// Last reason of plan cache ineligibility
|
|
3046
|
+
});
|
|
3047
|
+
const clusterStatementsSummaryHistory = informationSchema.table(
|
|
3048
|
+
"CLUSTER_STATEMENTS_SUMMARY_HISTORY",
|
|
3049
|
+
createClusterStatementsSummarySchema()
|
|
3050
|
+
);
|
|
3051
|
+
const clusterStatementsSummary = informationSchema.table(
|
|
3052
|
+
"CLUSTER_STATEMENTS_SUMMARY",
|
|
3053
|
+
createClusterStatementsSummarySchema()
|
|
3054
|
+
);
|
|
2603
3055
|
async function getTables() {
|
|
2604
3056
|
const tables = await sql$1.executeDDL("SHOW TABLES");
|
|
2605
3057
|
return tables.rows.flatMap((tableInfo) => Object.values(tableInfo));
|
|
@@ -2610,7 +3062,7 @@ async function dropSchemaMigrations() {
|
|
|
2610
3062
|
const tables = await getTables();
|
|
2611
3063
|
const dropStatements = generateDropTableStatements(tables, { sequence: true, table: true });
|
|
2612
3064
|
for (const statement of dropStatements) {
|
|
2613
|
-
console.
|
|
3065
|
+
console.debug(`execute DDL: ${statement}`);
|
|
2614
3066
|
await sql$1.executeDDL(statement);
|
|
2615
3067
|
}
|
|
2616
3068
|
return getHttpResponse(
|
|
@@ -2618,8 +3070,8 @@ async function dropSchemaMigrations() {
|
|
|
2618
3070
|
"⚠️ All data in these tables has been permanently deleted. This operation cannot be undone."
|
|
2619
3071
|
);
|
|
2620
3072
|
} catch (error) {
|
|
2621
|
-
|
|
2622
|
-
|
|
3073
|
+
const errorMessage = error?.debug?.sqlMessage ?? error?.debug?.message ?? error.message ?? "Unknown error occurred";
|
|
3074
|
+
console.error(errorMessage);
|
|
2623
3075
|
return getHttpResponse(500, errorMessage);
|
|
2624
3076
|
}
|
|
2625
3077
|
}
|
|
@@ -2635,7 +3087,13 @@ const applySchemaMigrations = async (migration) => {
|
|
|
2635
3087
|
const successfulMigrations = await migrations2.run();
|
|
2636
3088
|
console.info("Migrations applied:", successfulMigrations);
|
|
2637
3089
|
const migrationList = await migrationRunner.list();
|
|
2638
|
-
|
|
3090
|
+
let migrationHistory = "No migrations found";
|
|
3091
|
+
if (Array.isArray(migrationList) && migrationList.length > 0) {
|
|
3092
|
+
const sortedMigrations = migrationList.toSorted(
|
|
3093
|
+
(a, b) => a.migratedAt.getTime() - b.migratedAt.getTime()
|
|
3094
|
+
);
|
|
3095
|
+
migrationHistory = sortedMigrations.map((y) => `${y.id}, ${y.name}, ${y.migratedAt.toUTCString()}`).join("\n");
|
|
3096
|
+
}
|
|
2639
3097
|
console.info("Migrations history:\nid, name, migrated_at\n", migrationHistory);
|
|
2640
3098
|
return {
|
|
2641
3099
|
headers: { "Content-Type": ["application/json"] },
|
|
@@ -2644,17 +3102,13 @@ const applySchemaMigrations = async (migration) => {
|
|
|
2644
3102
|
body: "Migrations successfully executed"
|
|
2645
3103
|
};
|
|
2646
3104
|
} catch (error) {
|
|
2647
|
-
|
|
2648
|
-
|
|
2649
|
-
} catch (e) {
|
|
2650
|
-
console.trace("Error stringify:", e);
|
|
2651
|
-
console.error("Error during migration:", error);
|
|
2652
|
-
}
|
|
3105
|
+
const errorMessage = error?.cause?.context?.debug?.sqlMessage ?? error?.cause?.context?.debug?.message ?? error?.debug?.context?.sqlMessage ?? error?.debug?.context?.message ?? error.message ?? "Unknown error occurred";
|
|
3106
|
+
console.error("Error during migration:", errorMessage);
|
|
2653
3107
|
return {
|
|
2654
3108
|
headers: { "Content-Type": ["application/json"] },
|
|
2655
3109
|
statusCode: 500,
|
|
2656
3110
|
statusText: "Internal Server Error",
|
|
2657
|
-
body: error instanceof Error ?
|
|
3111
|
+
body: error instanceof Error ? errorMessage : "Unknown error during migration"
|
|
2658
3112
|
};
|
|
2659
3113
|
}
|
|
2660
3114
|
};
|
|
@@ -2665,8 +3119,8 @@ async function fetchSchemaWebTrigger() {
|
|
|
2665
3119
|
const sqlStatements = wrapWithForeignKeyChecks(createTableStatements);
|
|
2666
3120
|
return getHttpResponse(200, sqlStatements.join(";\n"));
|
|
2667
3121
|
} catch (error) {
|
|
2668
|
-
|
|
2669
|
-
|
|
3122
|
+
const errorMessage = error?.debug?.sqlMessage ?? error?.debug?.message ?? error.message ?? "Unknown error occurred";
|
|
3123
|
+
console.error(errorMessage);
|
|
2670
3124
|
return getHttpResponse(500, errorMessage);
|
|
2671
3125
|
}
|
|
2672
3126
|
}
|
|
@@ -2693,7 +3147,7 @@ async function dropTableSchemaMigrations() {
|
|
|
2693
3147
|
const tables = await getTables();
|
|
2694
3148
|
const dropStatements = generateDropTableStatements(tables, { sequence: false, table: true });
|
|
2695
3149
|
for (const statement of dropStatements) {
|
|
2696
|
-
console.
|
|
3150
|
+
console.debug(`execute DDL: ${statement}`);
|
|
2697
3151
|
await sql$1.executeDDL(statement);
|
|
2698
3152
|
}
|
|
2699
3153
|
return getHttpResponse(
|
|
@@ -2701,8 +3155,8 @@ async function dropTableSchemaMigrations() {
|
|
|
2701
3155
|
"⚠️ All data in these tables has been permanently deleted. This operation cannot be undone."
|
|
2702
3156
|
);
|
|
2703
3157
|
} catch (error) {
|
|
2704
|
-
|
|
2705
|
-
|
|
3158
|
+
const errorMessage = error?.debug?.sqlMessage ?? error?.debug?.message ?? error.message ?? "Unknown error occurred";
|
|
3159
|
+
console.error(errorMessage);
|
|
2706
3160
|
return getHttpResponse(500, errorMessage);
|
|
2707
3161
|
}
|
|
2708
3162
|
}
|
|
@@ -2745,6 +3199,169 @@ const clearCacheSchedulerTrigger = async (options) => {
|
|
|
2745
3199
|
};
|
|
2746
3200
|
}
|
|
2747
3201
|
};
|
|
3202
|
+
const topSlowestStatementLastHourTrigger = async (orm, warnThresholdMs = 300, memoryThresholdBytes = 8 * 1024 * 1024) => {
|
|
3203
|
+
const nsToMs = (v) => {
|
|
3204
|
+
const n = Number(v);
|
|
3205
|
+
return Number.isFinite(n) ? n / 1e6 : NaN;
|
|
3206
|
+
};
|
|
3207
|
+
const bytesToMB = (v) => {
|
|
3208
|
+
const n = Number(v);
|
|
3209
|
+
return Number.isFinite(n) ? n / (1024 * 1024) : NaN;
|
|
3210
|
+
};
|
|
3211
|
+
const jsonSafeStringify = (value) => JSON.stringify(value, (_k, v) => typeof v === "bigint" ? v.toString() : v);
|
|
3212
|
+
const TOP_N = 1;
|
|
3213
|
+
try {
|
|
3214
|
+
const summaryHistory = clusterStatementsSummaryHistory;
|
|
3215
|
+
const summary = clusterStatementsSummary;
|
|
3216
|
+
const selectShape = (t) => ({
|
|
3217
|
+
digest: t.digest,
|
|
3218
|
+
stmtType: t.stmtType,
|
|
3219
|
+
schemaName: t.schemaName,
|
|
3220
|
+
execCount: t.execCount,
|
|
3221
|
+
avgLatencyNs: t.avgLatency,
|
|
3222
|
+
maxLatencyNs: t.maxLatency,
|
|
3223
|
+
minLatencyNs: t.minLatency,
|
|
3224
|
+
avgProcessTimeNs: t.avgProcessTime,
|
|
3225
|
+
avgWaitTimeNs: t.avgWaitTime,
|
|
3226
|
+
avgBackoffTimeNs: t.avgBackoffTime,
|
|
3227
|
+
avgTotalKeys: t.avgTotalKeys,
|
|
3228
|
+
firstSeen: t.firstSeen,
|
|
3229
|
+
lastSeen: t.lastSeen,
|
|
3230
|
+
planInCache: t.planInCache,
|
|
3231
|
+
planCacheHits: t.planCacheHits,
|
|
3232
|
+
digestText: t.digestText,
|
|
3233
|
+
plan: t.plan,
|
|
3234
|
+
avgMemBytes: t.avgMem,
|
|
3235
|
+
maxMemBytes: t.maxMem
|
|
3236
|
+
});
|
|
3237
|
+
const lastHourFilterHistory = gte(
|
|
3238
|
+
summaryHistory.summaryEndTime,
|
|
3239
|
+
sql`DATE_SUB(NOW(), INTERVAL 1 HOUR)`
|
|
3240
|
+
);
|
|
3241
|
+
const lastHourFilterSummary = gte(
|
|
3242
|
+
summary.summaryEndTime,
|
|
3243
|
+
sql`DATE_SUB(NOW(), INTERVAL 1 HOUR)`
|
|
3244
|
+
);
|
|
3245
|
+
const qHistory = orm.getDrizzleQueryBuilder().select(selectShape(summaryHistory)).from(summaryHistory).where(lastHourFilterHistory);
|
|
3246
|
+
const qSummary = orm.getDrizzleQueryBuilder().select(selectShape(summary)).from(summary).where(lastHourFilterSummary);
|
|
3247
|
+
const combined = unionAll(qHistory, qSummary).as("combined");
|
|
3248
|
+
const thresholdNs = Math.floor(warnThresholdMs * 1e6);
|
|
3249
|
+
const grouped = orm.getDrizzleQueryBuilder().select({
|
|
3250
|
+
digest: combined.digest,
|
|
3251
|
+
stmtType: combined.stmtType,
|
|
3252
|
+
schemaName: combined.schemaName,
|
|
3253
|
+
execCount: sql`SUM(${combined.execCount})`.as("execCount"),
|
|
3254
|
+
avgLatencyNs: sql`MAX(${combined.avgLatencyNs})`.as("avgLatencyNs"),
|
|
3255
|
+
maxLatencyNs: sql`MAX(${combined.maxLatencyNs})`.as("maxLatencyNs"),
|
|
3256
|
+
minLatencyNs: sql`MIN(${combined.minLatencyNs})`.as("minLatencyNs"),
|
|
3257
|
+
avgProcessTimeNs: sql`MAX(${combined.avgProcessTimeNs})`.as("avgProcessTimeNs"),
|
|
3258
|
+
avgWaitTimeNs: sql`MAX(${combined.avgWaitTimeNs})`.as("avgWaitTimeNs"),
|
|
3259
|
+
avgBackoffTimeNs: sql`MAX(${combined.avgBackoffTimeNs})`.as("avgBackoffTimeNs"),
|
|
3260
|
+
avgMemBytes: sql`MAX(${combined.avgMemBytes})`.as("avgMemBytes"),
|
|
3261
|
+
maxMemBytes: sql`MAX(${combined.maxMemBytes})`.as("maxMemBytes"),
|
|
3262
|
+
avgTotalKeys: sql`MAX(${combined.avgTotalKeys})`.as("avgTotalKeys"),
|
|
3263
|
+
firstSeen: sql`MIN(${combined.firstSeen})`.as("firstSeen"),
|
|
3264
|
+
lastSeen: sql`MAX(${combined.lastSeen})`.as("lastSeen"),
|
|
3265
|
+
planInCache: sql`MAX(${combined.planInCache})`.as("planInCache"),
|
|
3266
|
+
planCacheHits: sql`SUM(${combined.planCacheHits})`.as("planCacheHits"),
|
|
3267
|
+
// Prefer a non-empty sample text/plan via MAX; acceptable for de-dup
|
|
3268
|
+
digestText: sql`MAX(${combined.digestText})`.as("digestText"),
|
|
3269
|
+
plan: sql`MAX(${combined.plan})`.as("plan")
|
|
3270
|
+
}).from(combined).where(
|
|
3271
|
+
sql`COALESCE(${combined.digest}, '') <> '' AND COALESCE(${combined.digestText}, '') <> '' AND COALESCE(${combined.stmtType}, '') NOT IN ('Use','Set','Show')`
|
|
3272
|
+
).groupBy(combined.digest, combined.stmtType, combined.schemaName).as("grouped");
|
|
3273
|
+
const rows = await orm.getDrizzleQueryBuilder().select({
|
|
3274
|
+
digest: grouped.digest,
|
|
3275
|
+
stmtType: grouped.stmtType,
|
|
3276
|
+
schemaName: grouped.schemaName,
|
|
3277
|
+
execCount: grouped.execCount,
|
|
3278
|
+
avgLatencyNs: grouped.avgLatencyNs,
|
|
3279
|
+
maxLatencyNs: grouped.maxLatencyNs,
|
|
3280
|
+
minLatencyNs: grouped.minLatencyNs,
|
|
3281
|
+
avgProcessTimeNs: grouped.avgProcessTimeNs,
|
|
3282
|
+
avgWaitTimeNs: grouped.avgWaitTimeNs,
|
|
3283
|
+
avgBackoffTimeNs: grouped.avgBackoffTimeNs,
|
|
3284
|
+
avgMemBytes: grouped.avgMemBytes,
|
|
3285
|
+
maxMemBytes: grouped.maxMemBytes,
|
|
3286
|
+
avgTotalKeys: grouped.avgTotalKeys,
|
|
3287
|
+
firstSeen: grouped.firstSeen,
|
|
3288
|
+
lastSeen: grouped.lastSeen,
|
|
3289
|
+
planInCache: grouped.planInCache,
|
|
3290
|
+
planCacheHits: grouped.planCacheHits,
|
|
3291
|
+
digestText: grouped.digestText,
|
|
3292
|
+
plan: grouped.plan
|
|
3293
|
+
}).from(grouped).where(
|
|
3294
|
+
sql`${grouped.avgLatencyNs} > ${thresholdNs} OR ${grouped.avgMemBytes} > ${memoryThresholdBytes}`
|
|
3295
|
+
).orderBy(desc(grouped.avgLatencyNs)).limit(formatLimitOffset(TOP_N));
|
|
3296
|
+
const formatted = rows.map((r, i) => ({
|
|
3297
|
+
rank: i + 1,
|
|
3298
|
+
// 1-based rank in the top N
|
|
3299
|
+
digest: r.digest,
|
|
3300
|
+
stmtType: r.stmtType,
|
|
3301
|
+
schemaName: r.schemaName,
|
|
3302
|
+
execCount: r.execCount,
|
|
3303
|
+
avgLatencyMs: nsToMs(r.avgLatencyNs),
|
|
3304
|
+
// Convert ns to ms for readability
|
|
3305
|
+
maxLatencyMs: nsToMs(r.maxLatencyNs),
|
|
3306
|
+
minLatencyMs: nsToMs(r.minLatencyNs),
|
|
3307
|
+
avgProcessTimeMs: nsToMs(r.avgProcessTimeNs),
|
|
3308
|
+
avgWaitTimeMs: nsToMs(r.avgWaitTimeNs),
|
|
3309
|
+
avgBackoffTimeMs: nsToMs(r.avgBackoffTimeNs),
|
|
3310
|
+
avgMemMB: bytesToMB(r.avgMemBytes),
|
|
3311
|
+
maxMemMB: bytesToMB(r.maxMemBytes),
|
|
3312
|
+
avgMemBytes: r.avgMemBytes,
|
|
3313
|
+
maxMemBytes: r.maxMemBytes,
|
|
3314
|
+
avgTotalKeys: r.avgTotalKeys,
|
|
3315
|
+
firstSeen: r.firstSeen,
|
|
3316
|
+
lastSeen: r.lastSeen,
|
|
3317
|
+
planInCache: r.planInCache,
|
|
3318
|
+
planCacheHits: r.planCacheHits,
|
|
3319
|
+
digestText: r.digestText,
|
|
3320
|
+
plan: r.plan
|
|
3321
|
+
}));
|
|
3322
|
+
for (const f of formatted) {
|
|
3323
|
+
console.warn(
|
|
3324
|
+
`${f.rank}. ${f.stmtType} avg=${f.avgLatencyMs?.toFixed?.(2)}ms max=${f.maxLatencyMs?.toFixed?.(2)}ms mem≈${f.avgMemMB?.toFixed?.(2)}MB(max ${f.maxMemMB?.toFixed?.(2)}MB) exec=${f.execCount}
|
|
3325
|
+
digest=${f.digest}
|
|
3326
|
+
sql=${(f.digestText || "").slice(0, 300)}${f.digestText && f.digestText.length > 300 ? "…" : ""}`
|
|
3327
|
+
);
|
|
3328
|
+
if (f.plan) {
|
|
3329
|
+
console.warn(` full plan:
|
|
3330
|
+
${f.plan}`);
|
|
3331
|
+
}
|
|
3332
|
+
}
|
|
3333
|
+
return {
|
|
3334
|
+
headers: { "Content-Type": ["application/json"] },
|
|
3335
|
+
statusCode: 200,
|
|
3336
|
+
statusText: "OK",
|
|
3337
|
+
body: jsonSafeStringify({
|
|
3338
|
+
success: true,
|
|
3339
|
+
window: "last_1h",
|
|
3340
|
+
top: TOP_N,
|
|
3341
|
+
warnThresholdMs,
|
|
3342
|
+
memoryThresholdBytes,
|
|
3343
|
+
rows: formatted,
|
|
3344
|
+
generatedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
3345
|
+
})
|
|
3346
|
+
};
|
|
3347
|
+
} catch (error) {
|
|
3348
|
+
console.error(
|
|
3349
|
+
"Error in topSlowestStatementLastHourTrigger:",
|
|
3350
|
+
error?.cause?.context?.debug?.sqlMessage ?? error?.cause ?? error
|
|
3351
|
+
);
|
|
3352
|
+
return {
|
|
3353
|
+
headers: { "Content-Type": ["application/json"] },
|
|
3354
|
+
statusCode: 500,
|
|
3355
|
+
statusText: "Internal Server Error",
|
|
3356
|
+
body: jsonSafeStringify({
|
|
3357
|
+
success: false,
|
|
3358
|
+
message: "Failed to fetch or log slow queries",
|
|
3359
|
+
error: error?.cause?.context?.debug?.sqlMessage ?? error?.cause?.message,
|
|
3360
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
3361
|
+
})
|
|
3362
|
+
};
|
|
3363
|
+
}
|
|
3364
|
+
};
|
|
2748
3365
|
const getHttpResponse = (statusCode, body) => {
|
|
2749
3366
|
let statusText = "";
|
|
2750
3367
|
if (statusCode === 200) {
|
|
@@ -2765,6 +3382,8 @@ export {
|
|
|
2765
3382
|
applyFromDriverTransform,
|
|
2766
3383
|
applySchemaMigrations,
|
|
2767
3384
|
clearCacheSchedulerTrigger,
|
|
3385
|
+
clusterStatementsSummary,
|
|
3386
|
+
clusterStatementsSummaryHistory,
|
|
2768
3387
|
ForgeSQLORM as default,
|
|
2769
3388
|
dropSchemaMigrations,
|
|
2770
3389
|
dropTableSchemaMigrations,
|
|
@@ -2787,6 +3406,8 @@ export {
|
|
|
2787
3406
|
migrations,
|
|
2788
3407
|
nextVal,
|
|
2789
3408
|
parseDateTime,
|
|
2790
|
-
patchDbWithSelectAliased
|
|
3409
|
+
patchDbWithSelectAliased,
|
|
3410
|
+
slowQuery,
|
|
3411
|
+
topSlowestStatementLastHourTrigger
|
|
2791
3412
|
};
|
|
2792
3413
|
//# sourceMappingURL=ForgeSQLORM.mjs.map
|