From d67220aa5e95c345f2310cef2304c33092ddeade Mon Sep 17 00:00:00 2001 From: zhx Date: Mon, 25 May 2026 13:41:18 +0800 Subject: [PATCH] Fix first user metric aggregate insert --- internal/storage/batch_sql.go | 12 ++++++++---- internal/storage/user_metrics.go | 18 +++++++++++------- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/internal/storage/batch_sql.go b/internal/storage/batch_sql.go index 34933f7..1057452 100644 --- a/internal/storage/batch_sql.go +++ b/internal/storage/batch_sql.go @@ -107,8 +107,10 @@ func (r *MySQLRepository) applyDailyUserGroup(ctx context.Context, tx *sql.Tx, k _, err = tx.ExecContext(ctx, ` INSERT INTO country_dashboard_daily_metric ( stat_date, date_number, sys_origin, country_code, country_name, refreshed_at -) VALUES (?, ?, ?, ?, ?, NOW()) -ON DUPLICATE KEY UPDATE `+column+` = `+column+` + ?, country_name = VALUES(country_name), refreshed_at = NOW() + , `+column+` +) VALUES (?, ?, ?, ?, ?, NOW(), ?) +ON DUPLICATE KEY UPDATE `+column+` = `+column+` + VALUES(`+column+`), + country_name = VALUES(country_name), refreshed_at = NOW() `, key.day, dateNumber(key.day), key.sysOrigin, key.countryCode, key.countryName, inserted) return err } @@ -127,8 +129,10 @@ func (r *MySQLRepository) applyPeriodUserGroup(ctx context.Context, tx *sql.Tx, INSERT INTO country_dashboard_period_metric ( period_type, period_key, stat_timezone, period_name, period_start_date, period_end_date, sys_origin, country_code, country_name, refreshed_at -) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, NOW()) -ON DUPLICATE KEY UPDATE `+column+` = `+column+` + ?, country_name = VALUES(country_name), refreshed_at = NOW() + , `+column+` +) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), ?) +ON DUPLICATE KEY UPDATE `+column+` = `+column+` + VALUES(`+column+`), + country_name = VALUES(country_name), refreshed_at = NOW() `, key.periodType, key.periodKey, key.statTimezone, key.periodName, ptrOrNil(key.startDate), ptrOrNil(key.endDate), key.sysOrigin, key.countryCode, key.countryName, inserted) return err diff --git a/internal/storage/user_metrics.go b/internal/storage/user_metrics.go index fff8e8b..2719566 100644 --- a/internal/storage/user_metrics.go +++ b/internal/storage/user_metrics.go @@ -23,8 +23,10 @@ func (r *MySQLRepository) applyDailyUserMetric(ctx context.Context, tx *sql.Tx, _, err = tx.ExecContext(ctx, ` INSERT INTO country_dashboard_daily_metric ( stat_date, date_number, sys_origin, country_code, country_name, refreshed_at -) VALUES (?, ?, ?, ?, ?, NOW()) -ON DUPLICATE KEY UPDATE `+column+` = `+column+` + 1, country_name = VALUES(country_name), refreshed_at = NOW() + , `+column+` +) VALUES (?, ?, ?, ?, ?, NOW(), 1) +ON DUPLICATE KEY UPDATE `+column+` = `+column+` + VALUES(`+column+`), + country_name = VALUES(country_name), refreshed_at = NOW() `, day, dashboard.DateNumber(day), c.SysOrigin, c.Country.Code, c.Country.Name) return err } @@ -45,8 +47,10 @@ func (r *MySQLRepository) applyPeriodUserMetric(ctx context.Context, tx *sql.Tx, INSERT INTO country_dashboard_period_metric ( period_type, period_key, stat_timezone, period_name, period_start_date, period_end_date, sys_origin, country_code, country_name, refreshed_at -) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, NOW()) -ON DUPLICATE KEY UPDATE `+column+` = `+column+` + 1, country_name = VALUES(country_name), refreshed_at = NOW() + , `+column+` +) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), 1) +ON DUPLICATE KEY UPDATE `+column+` = `+column+` + VALUES(`+column+`), + country_name = VALUES(country_name), refreshed_at = NOW() `, window.Type, window.Key, statTimezone, window.Name, window.StartDate, window.EndDate, c.SysOrigin, c.Country.Code, c.Country.Name) return err @@ -129,9 +133,9 @@ INSERT IGNORE INTO country_dashboard_game_period_user_metric ( _, err = tx.ExecContext(ctx, ` INSERT INTO country_dashboard_game_period_metric ( period_type, period_key, stat_timezone, period_name, period_start_date, period_end_date, - sys_origin, country_code, country_name, game_provider, game_id, game_name, refreshed_at -) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW()) -ON DUPLICATE KEY UPDATE user_count = user_count + 1, country_name = VALUES(country_name), + sys_origin, country_code, country_name, game_provider, game_id, game_name, user_count, refreshed_at +) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 1, NOW()) +ON DUPLICATE KEY UPDATE user_count = user_count + VALUES(user_count), country_name = VALUES(country_name), game_name = VALUES(game_name), refreshed_at = NOW() `, window.Type, window.Key, statTimezone, window.Name, window.StartDate, window.EndDate, c.SysOrigin, c.Country.Code, c.Country.Name, c.GameProvider, c.GameID, c.GameName)