chore: run telemetry cleanup on page start
This commit is contained in:
@@ -427,6 +427,9 @@ export default {
|
||||
telemetryErrorHandler: null,
|
||||
telemetryPromiseHandler: null,
|
||||
telemetrySlowApiThreshold: 5000,
|
||||
telemetryRetentionDays: 365,
|
||||
telemetryCleanupBatchSize: 2000,
|
||||
telemetryCleanupRunning: false,
|
||||
telemetryApiSuccessSampleRate: 0,
|
||||
telemetryQueue: [],
|
||||
telemetrySending: false,
|
||||
@@ -672,6 +675,8 @@ export default {
|
||||
this.telemetryReady = true
|
||||
this.telemetryEnv = config.telemetryEnv || config.env || (envFromBuild === 'development' ? 'dev' : 'prod')
|
||||
this.telemetryAppVersion = config.telemetryAppVersion || config.appVersion || this.telemetryAppVersion
|
||||
this.telemetryRetentionDays = Number(config.telemetryRetentionDays) || this.telemetryRetentionDays
|
||||
this.telemetryCleanupBatchSize = Number(config.telemetryCleanupBatchSize) || this.telemetryCleanupBatchSize
|
||||
this.telemetrySessionId = this.getTelemetryStorageId('qinganTelemetrySessionId', 'sess')
|
||||
this.telemetryPageTraceId = this.generateTelemetryId('trace')
|
||||
this.telemetryStartAt = Date.now()
|
||||
@@ -715,6 +720,7 @@ export default {
|
||||
}
|
||||
window.addEventListener('error', this.telemetryErrorHandler, true)
|
||||
window.addEventListener('unhandledrejection', this.telemetryPromiseHandler)
|
||||
this.cleanupTelemetryLogs()
|
||||
this.reportTelemetry('page_start', 'frontend_event', 'info', {
|
||||
title: this.titleText || '',
|
||||
mode: this.currentMode,
|
||||
@@ -866,6 +872,25 @@ export default {
|
||||
this.telemetryThrottleMap[key] = now
|
||||
return true
|
||||
},
|
||||
cleanupTelemetryLogs() {
|
||||
if (this.telemetryCleanupRunning) return
|
||||
this.telemetryCleanupRunning = true
|
||||
const params = [
|
||||
['@retention_days', String(this.telemetryRetentionDays), 'string', '0'],
|
||||
['@batch_size', String(this.telemetryCleanupBatchSize), 'string', '0'],
|
||||
['@return_result', '1', 'string', '0']
|
||||
]
|
||||
const data = this.CreateData(this.telemetryWriteType, 'TV_前端埋点事件日志_清理', params)
|
||||
this.ExecDatabase(data)
|
||||
.catch((error) => {
|
||||
if (this.canReportTelemetryEvent('telemetry_cleanup_fail', 30000)) {
|
||||
console.warn('TV_前端埋点事件日志_清理失败', error)
|
||||
}
|
||||
})
|
||||
.then(() => {
|
||||
this.telemetryCleanupRunning = false
|
||||
})
|
||||
},
|
||||
reportTelemetry(eventName, eventType = 'frontend_event', eventLevel = 'info', eventData = {}, options = {}) {
|
||||
if (!this.telemetryReady && eventName !== 'page_start') {
|
||||
return
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
1. 创建前端埋点日志表 dbo.TV_前端埋点事件日志
|
||||
2. 创建写入存储过程 dbo.TV_前端埋点事件_写入
|
||||
3. 创建清理存储过程 dbo.TV_前端埋点事件日志_清理
|
||||
4. 写入时自动小批量清理 365 天以前的数据
|
||||
4. 前端页面启动时调用清理过程,小批量清理 365 天以前的数据
|
||||
*/
|
||||
|
||||
IF OBJECT_ID(N'dbo.TV_前端埋点事件日志', N'U') IS NULL
|
||||
@@ -126,13 +126,6 @@ BEGIN
|
||||
TRY_CONVERT(INT, NULLIF(@duration_ms, N'')), @success_bit, NULLIF(@error_code, N''), NULLIF(@error_message, N'')
|
||||
);
|
||||
|
||||
DECLARE @insert_id BIGINT = CAST(SCOPE_IDENTITY() AS BIGINT);
|
||||
|
||||
EXEC dbo.[TV_前端埋点事件日志_清理]
|
||||
@retention_days = 365,
|
||||
@batch_size = 2000,
|
||||
@return_result = 0;
|
||||
|
||||
SELECT @insert_id AS ID, N'1' AS result, N'写入成功' AS message;
|
||||
SELECT CAST(SCOPE_IDENTITY() AS BIGINT) AS ID, N'1' AS result, N'写入成功' AS message;
|
||||
END;
|
||||
GO
|
||||
|
||||
Reference in New Issue
Block a user