Skip to content

Commit

Permalink
feat: add query_time in data_source
Browse files Browse the repository at this point in the history
  • Loading branch information
SongZhen0704 committed Mar 6, 2025
1 parent 8f29a56 commit aa2bd38
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 7 deletions.
2 changes: 1 addition & 1 deletion server/controller/db/metadb/migrator/schema/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ const (
RAW_SQL_ROOT_DIR = "/etc/metadb/schema/rawsql"

DB_VERSION_TABLE = "db_version"
DB_VERSION_EXPECTED = "7.0.1.9"
DB_VERSION_EXPECTED = "7.0.1.10"
)
Original file line number Diff line number Diff line change
Expand Up @@ -2109,6 +2109,7 @@ CREATE TABLE IF NOT EXISTS data_source (
base_data_source_id INTEGER,
`interval_time` INTEGER NOT NULL COMMENT 'uint: s',
retention_time INTEGER NOT NULL COMMENT 'uint: hour',
query_time INTEGER NOT NULL COMMENT 'uint: minute',
summable_metrics_operator CHAR(64),
unsummable_metrics_operator CHAR(64),
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
Expand All @@ -2123,17 +2124,17 @@ set @lcuuid = (select uuid());
INSERT INTO data_source (id, display_name, data_table_collection, base_data_source_id, `interval_time`, retention_time, summable_metrics_operator, unsummable_metrics_operator, lcuuid)
VALUES (2, '网络-指标(分钟级)', 'flow_metrics.network*', 1, 60, 7*24, 'Sum', 'Avg', @lcuuid);
set @lcuuid = (select uuid());
INSERT INTO data_source (id, display_name, data_table_collection, `interval_time`, retention_time, lcuuid)
VALUES (6, '网络-流日志', 'flow_log.l4_flow_log', 0, 3*24, @lcuuid);
INSERT INTO data_source (id, display_name, data_table_collection, `interval_time`, retention_time, query_time, lcuuid)
VALUES (6, '网络-流日志', 'flow_log.l4_flow_log', 0, 3*24, 6*60, @lcuuid);
set @lcuuid = (select uuid());
INSERT INTO data_source (id, display_name, data_table_collection, `interval_time`, retention_time, lcuuid)
VALUES (7, '应用-指标(秒级)', 'flow_metrics.application*', 1, 1*24, @lcuuid);
set @lcuuid = (select uuid());
INSERT INTO data_source (id, display_name, data_table_collection, base_data_source_id, `interval_time`, retention_time, summable_metrics_operator, unsummable_metrics_operator, lcuuid)
VALUES (8, '应用-指标(分钟级)', 'flow_metrics.application*', 7, 60, 7*24, 'Sum', 'Avg', @lcuuid);
set @lcuuid = (select uuid());
INSERT INTO data_source (id, display_name, data_table_collection, `interval_time`, retention_time, lcuuid)
VALUES (9, '应用-调用日志', 'flow_log.l7_flow_log', 0, 3*24, @lcuuid);
INSERT INTO data_source (id, display_name, data_table_collection, `interval_time`, retention_time, query_time, lcuuid)
VALUES (9, '应用-调用日志', 'flow_log.l7_flow_log', 0, 3*24, 6*60, @lcuuid);
set @lcuuid = (select uuid());
INSERT INTO data_source (id, display_name, data_table_collection, `interval_time`, retention_time, lcuuid)
VALUES (10, '网络-TCP 时序数据', 'flow_log.l4_packet', 0, 3*24, @lcuuid);
Expand Down Expand Up @@ -2165,8 +2166,8 @@ set @lcuuid = (select uuid());
INSERT INTO data_source (id, display_name, data_table_collection, `interval_time`, retention_time, lcuuid)
VALUES (19, '网络-网络策略', 'flow_metrics.traffic_policy', 60, 3*24, @lcuuid);
set @lcuuid = (select uuid());
INSERT INTO data_source (id, display_name, data_table_collection, `interval_time`, retention_time, lcuuid)
VALUES (20, '日志-日志数据', 'application_log.log', 1, 30*24, @lcuuid);
INSERT INTO data_source (id, display_name, data_table_collection, `interval_time`, retention_time, query_time, lcuuid)
VALUES (20, '日志-日志数据', 'application_log.log', 1, 30*24, 6*60, @lcuuid);
set @lcuuid = (select uuid());
INSERT INTO data_source (id, display_name, data_table_collection, base_data_source_id, `interval_time`, retention_time, summable_metrics_operator, unsummable_metrics_operator, lcuuid)
VALUES (21, '网络-指标(小时级)', 'flow_metrics.network*', 2, 3600, 30*24, 'Sum', 'Avg', @lcuuid);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
DROP PROCEDURE IF EXISTS AddColumnIfNotExists;

CREATE PROCEDURE AddColumnIfNotExists(
IN tableName VARCHAR(255),
IN colName VARCHAR(255),
IN colType VARCHAR(255),
IN afterCol VARCHAR(255)
)
BEGIN
DECLARE column_count INT;

SELECT COUNT(*)
INTO column_count
FROM information_schema.columns
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = tableName
AND column_name = colName;

IF column_count = 0 THEN
SET @sql = CONCAT('ALTER TABLE ', tableName, ' ADD COLUMN ', colName, ' ', colType, ' AFTER ', afterCol);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END IF;
END;

CALL AddColumnIfNotExists('data_source', 'query_time', "INTEGER NOT NULL COMMENT 'uint: minute'", 'retention_time');

DROP PROCEDURE AddColumnIfNotExists;

DROP PROCEDURE IF EXISTS UpdateQueryTime;

CREATE PROCEDURE UpdateQueryTime(
IN tableName VARCHAR(255)
)
BEGIN
-- 动态生成并执行 UPDATE 语句
SET @update_sql = CONCAT(
'UPDATE data_source ',
'SET query_time = 3600 ',
'WHERE data_table_collection = "', tableName, '"'
);

-- 准备并执行动态 SQL
PREPARE stmt FROM @update_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END;

-- 调用存储过程
CALL UpdateQueryTime('flow_log.l4_flow_log');
CALL UpdateQueryTime('flow_log.l7_flow_log');
CALL UpdateQueryTime('application_log.log');

DROP PROCEDURE UpdateQueryTime;

UPDATE db_version SET version='7.0.1.10';
1 change: 1 addition & 0 deletions server/controller/db/metadb/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ type DataSource struct {
BaseDataSourceID int `gorm:"column:base_data_source_id;type:int" json:"BASE_DATA_SOURCE_ID"`
IntervalTime int `gorm:"column:interval_time;type:int" json:"INTERVAL"`
RetentionTime int `gorm:"column:retention_time;type:int" json:"RETENTION_TIME"` // unit: hour
QueryTime int `gorm:"column:query_time;type:int" json:"QUERY_TIME"` // unit: minute
SummableMetricsOperator string `gorm:"column:summable_metrics_operator;type:char(64)" json:"SUMMABLE_METRICS_OPERATOR"`
UnSummableMetricsOperator string `gorm:"column:unsummable_metrics_operator;type:char(64)" json:"UNSUMMABLE_METRICS_OPERATOR"`
UpdatedAt time.Time `gorm:"column:updated_at" json:"UPDATED_AT"`
Expand Down

0 comments on commit aa2bd38

Please sign in to comment.