diff --git a/cmd/app.go b/cmd/app.go index a45a06b..49ef207 100644 --- a/cmd/app.go +++ b/cmd/app.go @@ -471,7 +471,7 @@ func registerScheduledWork() { const taskResultRetentionDays = 30 func cleanupScheduledData() { - before := time.Now().Add(-24 * time.Hour * taskResultRetentionDays) + before := time.Now().UTC().Add(-24 * time.Hour * taskResultRetentionDays) if err := tasks.ClearTaskResultsByTimeBefore(before); err != nil { log.Printf("Failed to clean expired task results: %v", err) } @@ -484,7 +484,7 @@ func compactMetricStore(ctx context.Context) { compactCtx, cancel := context.WithTimeout(ctx, 15*time.Minute) defer cancel() - written, err := metricstore.Compact(compactCtx, time.Now()) + written, err := metricstore.Compact(compactCtx, time.Now().UTC()) if errors.Is(err, metricstore.ErrCompactInProgress) { return } diff --git a/database/accounts/accounts.go b/database/accounts/accounts.go index 12e481c..49ef99c 100644 --- a/database/accounts/accounts.go +++ b/database/accounts/accounts.go @@ -100,8 +100,8 @@ func CreateDefaultAdminAccount() (username, passwd string, err error) { Username: username, Passwd: hashedPassword, SSOID: "", - CreatedAt: models.FromTime(time.Now()), - UpdatedAt: models.FromTime(time.Now()), + CreatedAt: time.Now().UTC(), + UpdatedAt: time.Now().UTC(), } err = db.Create(&user).Error @@ -171,7 +171,7 @@ func UpdateUser(uuid string, name, password, sso_type *string) error { if sso_type != nil { updates["sso_type"] = *sso_type } - updates["updated_at"] = time.Now() + updates["updated_at"] = time.Now().UTC() err := db.Model(&models.User{}).Where("uuid = ?", uuid).Updates(updates).Error if err != nil { return err diff --git a/database/accounts/sessions.go b/database/accounts/sessions.go index a019aee..f5b4c84 100644 --- a/database/accounts/sessions.go +++ b/database/accounts/sessions.go @@ -33,11 +33,11 @@ func CreateSession(uuid string, expires int, userAgent, ip, login_method string) sessionRecord := models.Session{ UUID: uuid, Session: session, - Expires: models.FromTime(time.Now().Add(time.Duration(expires) * time.Second)), + Expires: time.Now().UTC().Add(time.Duration(expires) * time.Second), UserAgent: userAgent, Ip: ip, LoginMethod: login_method, - LatestOnline: models.FromTime(time.Now()), + LatestOnline: time.Now().UTC(), } go func() { LoginNotification, _ := config.GetAs[bool](config.LoginNotificationKey, false) @@ -50,7 +50,7 @@ func CreateSession(uuid string, expires int, userAgent, ip, login_method string) } messageSender.SendEvent(models.EventMessage{ Event: messageevent.Login, - Time: time.Now(), + Time: time.Now().UTC(), Message: fmt.Sprintf("%s: %s (%s)\n%s", login_method, ip, loc, userAgent), Emoji: "🔑", }) @@ -73,7 +73,7 @@ func GetSession(session string) (uuid string, err error) { return "", err } - if time.Now().After(sessionRecord.Expires.ToTime()) { + if time.Now().UTC().After(sessionRecord.Expires) { // 会话已过期,删除它 _ = DeleteSession(session) return "", errors.New("session expired") @@ -114,7 +114,7 @@ func DeleteAllSessions() error { func UpdateLatest(session, useragent, ip string) error { db := dbcore.GetDBInstance() return db.Model(&models.Session{}).Where("session = ?", session).Updates(map[string]interface{}{ - "latest_online": time.Now(), + "latest_online": time.Now().UTC(), "latest_user_agent": useragent, "latest_ip": ip, }).Error @@ -122,7 +122,7 @@ func UpdateLatest(session, useragent, ip string) error { func RemoveExpiredSessions() error { db := dbcore.GetDBInstance() - result := db.Where("expires < ?", time.Now()).Delete(&models.Session{}) + result := db.Where("expires < ?", time.Now().UTC()).Delete(&models.Session{}) if result.Error != nil { return result.Error } diff --git a/database/auditlog/log.go b/database/auditlog/log.go index 211bd4e..b20bff7 100644 --- a/database/auditlog/log.go +++ b/database/auditlog/log.go @@ -9,14 +9,13 @@ import ( ) func Log(ip, uuid, message, msgType string) { - now := time.Now() db := dbcore.GetDBInstance() logEntry := &models.Log{ IP: ip, UUID: uuid, Message: message, MsgType: msgType, - Time: models.FromTime(now), + Time: time.Now().UTC(), } db.Create(logEntry) } @@ -28,7 +27,7 @@ func EventLog(eventType, message string) { // Delete logs older than 30 days func RemoveOldLogs() { db := dbcore.GetDBInstance() - threshold := time.Now().AddDate(0, 0, -30) + threshold := time.Now().UTC().AddDate(0, 0, -30) if err := db.Where("time < ?", threshold).Delete(&models.Log{}).Error; err != nil { log.Println("Failed to remove old logs:", err) } diff --git a/database/clients/client.go b/database/clients/client.go index 14dd1a1..818afe0 100644 --- a/database/clients/client.go +++ b/database/clients/client.go @@ -43,7 +43,7 @@ func SaveClientInfo(update map[string]interface{}) error { return fmt.Errorf("no fields to update") } - update["updated_at"] = time.Now() + update["updated_at"] = time.Now().UTC() toFloat64 := func(value interface{}) (float64, bool) { switch typed := value.(type) { @@ -138,8 +138,8 @@ func CreateClient() (clientUUID, token string, err error) { UUID: clientUUID, Token: token, Name: "client_" + clientUUID[0:8], - CreatedAt: models.FromTime(time.Now()), - UpdatedAt: models.FromTime(time.Now()), + CreatedAt: time.Now().UTC(), + UpdatedAt: time.Now().UTC(), } err = db.Create(&client).Error @@ -163,8 +163,8 @@ func CreateClientWithName(name string) (clientUUID, token string, err error) { UUID: clientUUID, Token: token, Name: name, - CreatedAt: models.FromTime(time.Now()), - UpdatedAt: models.FromTime(time.Now()), + CreatedAt: time.Now().UTC(), + UpdatedAt: time.Now().UTC(), } err = db.Create(&client).Error @@ -236,8 +236,30 @@ func SaveClient(updates map[string]interface{}) error { } } } + if value, exists := updates["expired_at"]; exists { + switch typed := value.(type) { + case nil: + updates["expired_at"] = nil + case time.Time: + updates["expired_at"] = typed.UTC() + case *time.Time: + if typed == nil { + updates["expired_at"] = nil + } else { + updates["expired_at"] = typed.UTC() + } + case string: + stamp, err := time.Parse(time.RFC3339Nano, typed) + if err != nil { + return fmt.Errorf("expired_at must be an RFC3339 timestamp with a timezone: %w", err) + } + updates["expired_at"] = stamp.UTC() + default: + return fmt.Errorf("expired_at must be an RFC3339 timestamp with a timezone") + } + } - updates["updated_at"] = time.Now() + updates["updated_at"] = time.Now().UTC() err := db.Model(&models.Client{}).Where("uuid = ?", clientUUID).Updates(updates).Error if err != nil { diff --git a/database/clipboard/clipboard.go b/database/clipboard/clipboard.go index c657a31..e1fa9d1 100644 --- a/database/clipboard/clipboard.go +++ b/database/clipboard/clipboard.go @@ -1,6 +1,8 @@ package clipboard import ( + "time" + "github.com/komari-monitor/komari/database/dbcore" "github.com/komari-monitor/komari/database/models" ) @@ -8,6 +10,8 @@ import ( // CreateClipboard 创建剪贴板记录 func CreateClipboard(cb *models.Clipboard) error { db := dbcore.GetDBInstance() + cb.CreatedAt = time.Now().UTC() + cb.UpdatedAt = cb.CreatedAt return db.Create(cb).Error } @@ -24,6 +28,8 @@ func GetClipboardByID(id int) (*models.Clipboard, error) { // UpdateClipboardFields 更新剪贴板记录 func UpdateClipboardFields(id int, fields map[string]interface{}) error { db := dbcore.GetDBInstance() + delete(fields, "created_at") + fields["updated_at"] = time.Now().UTC() return db.Model(&models.Clipboard{}).Where("id = ?", id).Updates(fields).Error } diff --git a/database/dbcore/dbcore.go b/database/dbcore/dbcore.go index 51ccc61..c6ac85b 100644 --- a/database/dbcore/dbcore.go +++ b/database/dbcore/dbcore.go @@ -244,7 +244,7 @@ func backupOnVersionUpgrade() { log.Printf("[upgrade-backup] failed to create backup dir: %v", err) return } - tsName := time.Now().Format("20060102-150405") + tsName := time.Now().UTC().Format("20060102-150405") bakPath := filepath.Join("./backup", fmt.Sprintf("upgrade-%s.zip", tsName)) backupZipPath := filepath.Join(".", "data", "backup.zip") if zipErr := zipDirectoryExcluding("./data", bakPath, map[string]struct{}{backupZipPath: {}}); zipErr != nil { @@ -328,7 +328,7 @@ func doInitialize() error { if err := os.MkdirAll("./backup", 0755); err != nil { log.Printf("[restore] failed to create backup dir: %v", err) } else { - tsName := time.Now().Format("20060102-150405") + tsName := time.Now().UTC().Format("20060102-150405") bakPath := filepath.Join("./backup", fmt.Sprintf("%s.zip", tsName)) if zipErr := zipDirectoryExcluding("./data", bakPath, map[string]struct{}{backupZipPath: {}}); zipErr != nil { log.Printf("[restore] failed to zip current data: %v", zipErr) @@ -372,7 +372,8 @@ func doInitialize() error { } logConfig := &gorm.Config{ - Logger: logutil.NewGormLogger(), + Logger: logutil.NewGormLogger(), + NowFunc: func() time.Time { return time.Now().UTC() }, } // 根据数据库类型选择不同的连接方式 diff --git a/database/metricstore/metricstore.go b/database/metricstore/metricstore.go index cbfd5fd..67ed045 100644 --- a/database/metricstore/metricstore.go +++ b/database/metricstore/metricstore.go @@ -564,7 +564,7 @@ func WritePingRecord(ctx context.Context, rec models.PingRecord) error { return fmt.Errorf("metric store not enabled") } - ts := rec.Time.ToTime() + ts := rec.Time entityID := rec.Client tags := map[string]string{ "task_id": fmt.Sprintf("%d", rec.TaskId), @@ -611,7 +611,7 @@ func GetRecordsByTime(ctx context.Context, start, end time.Time) ([]models.Recor return nil, fmt.Errorf("metric store not enabled") } - interval := recordSeriesInterval(s, start, end, time.Now()) + interval := recordSeriesInterval(s, start, end, time.Now().UTC()) entityIDs, err := listRecordEntityIDs(ctx, s, start, end, interval) if err != nil { return nil, err @@ -634,7 +634,7 @@ type recordSeriesKey struct { } func getRecordsByClientAndTimeFromSeries(ctx context.Context, s *metric.Store, clientUUID string, start, end time.Time) ([]models.Record, error) { - now := time.Now() + now := time.Now().UTC() interval := recordSeriesInterval(s, start, end, now) recordMap := make(map[recordSeriesKey]*models.Record) @@ -662,7 +662,7 @@ func getRecordsByClientAndTimeFromSeries(ctx context.Context, s *metric.Store, c if recordMap[key] == nil { recordMap[key] = &models.Record{ Client: entityID, - Time: models.FromTime(point.Bucket), + Time: point.Bucket.UTC(), } } applyRecordMetricValue(recordMap[key], metricName, point.Value) @@ -779,7 +779,7 @@ func sortRecords(records []models.Record) { if records[i].Client != records[j].Client { return records[i].Client < records[j].Client } - return records[i].Time.ToTime().Before(records[j].Time.ToTime()) + return records[i].Time.Before(records[j].Time) }) } @@ -826,7 +826,7 @@ func GetGPURecordsByClientAndTime(ctx context.Context, clientUUID string, start, if recordMap[key] == nil { recordMap[key] = &models.GPURecord{ Client: clientUUID, - Time: models.FromTime(p.Timestamp), + Time: p.Timestamp.UTC(), DeviceIndex: deviceIndex, DeviceName: deviceName, } @@ -882,13 +882,13 @@ func GetPingRecords(ctx context.Context, clientUUID string, taskID int, start, e } interval := pingQueryInterval(end.Sub(start), 4000) - interval = s.CompatibleSeriesInterval(start, time.Now(), interval) + interval = s.CompatibleSeriesInterval(start, time.Now().UTC(), interval) points, err := s.Series(ctx, metric.AggregateQuery{ Query: query, Aggregation: metric.AggLast, Interval: interval, PreserveSeries: true, - }, time.Now()) + }, time.Now().UTC()) if err != nil { return nil, err } @@ -905,12 +905,12 @@ func GetPingRecords(ctx context.Context, clientUUID string, taskID int, start, e records = append(records, models.PingRecord{ Client: p.EntityID, TaskId: taskIDVal, - Time: models.FromTime(p.Bucket), + Time: p.Bucket.UTC(), Value: int(p.Value), }) } sort.Slice(records, func(i, j int) bool { - return records[i].Time.ToTime().After(records[j].Time.ToTime()) + return records[i].Time.After(records[j].Time) }) return records, nil @@ -932,7 +932,7 @@ func pingQueryInterval(rangeDuration time.Duration, maxPoints int) time.Duration // farFuture 返回一个足够远的未来时间,用于以 DeleteBefore 语义清空某指标的全部数据。 func farFuture() time.Time { - return time.Now().Add(24 * 365 * time.Hour) + return time.Now().UTC().Add(24 * 365 * time.Hour) } // DeleteAllRecords 删除所有负载/系统类记录(保留指标定义,不含 ping)。 diff --git a/database/metricstore/metricstore_test.go b/database/metricstore/metricstore_test.go index 92daf0c..3339fe7 100644 --- a/database/metricstore/metricstore_test.go +++ b/database/metricstore/metricstore_test.go @@ -419,7 +419,7 @@ func TestGetRecordsByClientAndTimeReadsRollupsAfterRawCompaction(t *testing.T) { ts := now.Add(-time.Hour) rec := models.Record{ Client: "node-a", - Time: models.FromTime(ts), + Time: ts, Cpu: 42.5, Ram: 123456, RamTotal: 999999, diff --git a/database/metricstore/migration_store.go b/database/metricstore/migration_store.go index 4a71f61..c11f1ab 100644 --- a/database/metricstore/migration_store.go +++ b/database/metricstore/migration_store.go @@ -182,7 +182,7 @@ func metricTimeBounds(ctx context.Context, src *metric.Store, name string) (time wide := metric.Query{ MetricName: name, Start: time.Unix(0, 0), - End: time.Now().Add(24 * time.Hour), + End: time.Now().UTC().Add(24 * time.Hour), } asc := wide diff --git a/database/metricstore/report.go b/database/metricstore/report.go index c4edef5..d365ab1 100644 --- a/database/metricstore/report.go +++ b/database/metricstore/report.go @@ -154,6 +154,7 @@ func WriteReport(ctx context.Context, report v1.Report) (v1.Report, error) { if report.UpdatedAt.IsZero() { return v1.Report{}, fmt.Errorf("report receive time is required") } + report.UpdatedAt = report.UpdatedAt.UTC() if GetStore() == nil { return v1.Report{}, fmt.Errorf("metric store not enabled") } @@ -295,7 +296,6 @@ func writeReportBatch(ctx context.Context, reports []v1.Report) ([]v1.Report, er values.initialized = true } - report.UpdatedAt = report.UpdatedAt.UTC() if !values.timestamp.IsZero() && !report.UpdatedAt.After(values.timestamp) { report.UpdatedAt = values.timestamp.Add(time.Nanosecond) } @@ -435,7 +435,7 @@ func GetLatestTrafficBefore(ctx context.Context, entityIDs []string, before time } result[entityID] = models.Record{ Client: entityID, - Time: models.FromTime(before.UTC().Add(-time.Nanosecond)), + Time: before.UTC().Add(-time.Nanosecond), NetTotalUp: up, NetTotalDown: down, } diff --git a/database/metricstore/report_test.go b/database/metricstore/report_test.go index 5b8c77a..891edeb 100644 --- a/database/metricstore/report_test.go +++ b/database/metricstore/report_test.go @@ -221,6 +221,39 @@ func TestTrafficCounterDelta(t *testing.T) { } } +func TestWriteReportNormalizesReceiveTimeToUTC(t *testing.T) { + ctx := context.Background() + s := useReportTestStore(t, nil) + local := time.FixedZone("UTC+8", 8*60*60) + receiveTime := time.Date(2026, 7, 17, 9, 30, 0, 123456789, local) + report := v1.Report{ + UUID: "utc-report", + UpdatedAt: receiveTime, + CPU: v1.CPUReport{Usage: 10}, + Network: v1.NetworkReport{TotalUp: 1, TotalDown: 2}, + } + + saved, err := WriteReport(ctx, report) + if err != nil { + t.Fatalf("write report: %v", err) + } + if !saved.UpdatedAt.Equal(receiveTime) || saved.UpdatedAt.Location() != time.UTC { + t.Fatalf("saved receive time = %s (%s), want UTC", saved.UpdatedAt, saved.UpdatedAt.Location()) + } + points, err := s.Query(ctx, metric.Query{ + MetricName: MetricCPU, + EntityID: report.UUID, + Start: receiveTime.Add(-time.Nanosecond), + End: receiveTime.Add(time.Nanosecond), + }) + if err != nil { + t.Fatalf("query stored point: %v", err) + } + if len(points) != 1 || points[0].Timestamp.Location() != time.UTC || points[0].Timestamp.Nanosecond() != 123456789 { + t.Fatalf("stored points = %#v, want one UTC nanosecond point", points) + } +} + func assertMetricValues(t *testing.T, s *metric.Store, metricName, entityID string, start, end time.Time, want []float64) { t.Helper() points, err := s.Query(context.Background(), metric.Query{ diff --git a/database/metricstore/store_migration.go b/database/metricstore/store_migration.go index 43b685d..a0e53dd 100644 --- a/database/metricstore/store_migration.go +++ b/database/metricstore/store_migration.go @@ -149,7 +149,7 @@ func StartStoreMigration(sourceDriver, sourceDSN string) error { SourceDSN: maskDSN(srcCfg.DSN), TargetDriver: string(ResolveDriverFromConfig(cfg.Driver, cfg.DSN)), TargetDSN: maskDSN(cfg.DSN), - StartTime: time.Now(), + StartTime: time.Now().UTC(), } storeMigMu.Unlock() @@ -211,7 +211,7 @@ func finishStoreMigration(status string, err error) { storeMigMu.Lock() defer storeMigMu.Unlock() storeMigProgress.Status = status - storeMigProgress.EndTime = time.Now() + storeMigProgress.EndTime = time.Now().UTC() if err != nil { storeMigProgress.Error = err.Error() } diff --git a/database/models/UTCTime.go b/database/models/UTCTime.go deleted file mode 100644 index c368772..0000000 --- a/database/models/UTCTime.go +++ /dev/null @@ -1,121 +0,0 @@ -package models - -import ( - "database/sql/driver" - "fmt" - "log" - "os" - "strings" - "sync" - "time" -) - -// LocalTime is a custom time type for GORM to handle time.Time correctly. -// It ensures that all times are stored and retrieved based on the application's configured timezone. -type LocalTime time.Time - -// Value implements the driver.Valuer interface. -// It converts UTCTime to a TEXT format that the database understands. -// The time is formatted as a string in the application's local timezone, without timezone information. -func (t LocalTime) Value() (driver.Value, error) { - if time.Time(t).IsZero() { - return nil, nil - } - return time.Time(t).In(GetAppLocation()).Format("2006-01-02 15:04:05.0000000"), nil -} - -// Scan implements the sql.Scanner interface. -// It scans a value from the database into a UTCTime object. -func (t *LocalTime) Scan(v interface{}) error { - if v == nil { - *t = LocalTime(time.Time{}) - return nil - } - - loc := GetAppLocation() - - switch val := v.(type) { - case time.Time: - // CRITICAL FIX: When the driver reads a timezone-less string (e.g., "16:00:00"), - // it often incorrectly assumes it's UTC. We must correct this by re-interpreting - // the date and clock values in the application's actual timezone. - year, month, day := val.Date() - hour, min, sec := val.Clock() - nanosec := val.Nanosecond() - *t = LocalTime(time.Date(year, month, day, hour, min, sec, nanosec, loc)) - return nil - case []byte: - return t.parseTime(string(val), loc) - case string: - return t.parseTime(val, loc) - default: - return fmt.Errorf("UTCTime scan source was not string, []byte or time.Time: %T (%v)", v, v) - } -} - -// parseTime handles parsing a string into UTCTime. -func (t *LocalTime) parseTime(timeStr string, loc *time.Location) error { - timeStr = strings.TrimSpace(timeStr) - if timeStr == "" { - *t = LocalTime(time.Time{}) - return nil - } - - layouts := []string{ - time.RFC3339Nano, time.RFC3339, - "2006-01-02 15:04:05.0000000-07:00", "2006-01-02 15:04:05-07:00", - "2006-01-02 15:04:05.0000000", "2006-01-02 15:04:05", "2006-01-02", - } - - for _, layout := range layouts { - if parsedTime, err := time.ParseInLocation(layout, timeStr, loc); err == nil { - *t = LocalTime(parsedTime) - return nil - } - } - return fmt.Errorf("unable to parse time string '%s' into UTCTime", timeStr) -} - -// MarshalJSON implements the json.Marshaler interface. -// Serializes UTCTime to JSON in RFC3339 format with the correct timezone offset. -func (t LocalTime) MarshalJSON() ([]byte, error) { - if time.Time(t).IsZero() { - return []byte("null"), nil - } - formattedTime := time.Time(t).In(GetAppLocation()).Format(time.RFC3339) - return []byte(fmt.Sprintf(`"%s"`, formattedTime)), nil -} - -// ToTime converts UTCTime to Go's native time.Time type. -func (t LocalTime) ToTime() time.Time { return time.Time(t) } - -// FromTime converts Go's native time.Time type to UTCTime. -func FromTime(t time.Time) LocalTime { return LocalTime(t) } - -// Now returns the current time in the application's configured location. -func Now() LocalTime { return LocalTime(time.Now().In(GetAppLocation())) } - -var ( - appLocation *time.Location - locationOnce sync.Once -) - -// GetAppLocation retrieves the application's global timezone from the "TZ" environment variable. -func GetAppLocation() *time.Location { - locationOnce.Do(func() { - tz := os.Getenv("TZ") - if tz == "" { - tz = "UTC" - } - loc, err := time.LoadLocation(tz) - if err != nil { - log.Printf("Warning: Failed to load timezone '%s', falling back to UTC. Error: %v", tz, err) - appLocation = time.UTC - } else { - appLocation = loc - } - time.Local = appLocation - log.Printf("Application timezone is set to '%s'.", appLocation.String()) - }) - return appLocation -} diff --git a/database/models/clipboard.go b/database/models/clipboard.go index 06f94fd..e8618e6 100644 --- a/database/models/clipboard.go +++ b/database/models/clipboard.go @@ -1,11 +1,13 @@ package models +import "time" + type Clipboard struct { Id int `json:"id" gorm:"primaryKey;autoIncrement;unique"` Text string `json:"text" gorm:"type:longtext"` Name string `json:"name" gorm:"type:varchar(255)"` Weight int `json:"weight" gorm:"type:int"` Remark string `json:"remark" gorm:"type:text"` - CreatedAt LocalTime `json:"created_at"` - UpdatedAt LocalTime `json:"updated_at"` + CreatedAt time.Time `json:"created_at"` + UpdatedAt time.Time `json:"updated_at"` } diff --git a/database/models/log.go b/database/models/log.go index 1683cad..26fd7bf 100644 --- a/database/models/log.go +++ b/database/models/log.go @@ -1,10 +1,12 @@ package models +import "time" + type Log struct { ID uint `json:"id,omitempty" gorm:"primaryKey;autoIncrement"` IP string `json:"ip" gorm:"type:varchar(45);"` // IPv4 or IPv6 UUID string `json:"uuid" gorm:"type:varchar(36);"` Message string `json:"message" gorm:"type:text;not null"` MsgType string `json:"msg_type" gorm:"type:varchar(20);not null;index:idx_logs_msg_type_time,priority:1"` - Time LocalTime `json:"time" gorm:"autoCreateTime;not null;index:idx_logs_time;index:idx_logs_msg_type_time,priority:2"` + Time time.Time `json:"time" gorm:"autoCreateTime;not null;index:idx_logs_time;index:idx_logs_msg_type_time,priority:2"` } diff --git a/database/models/models.go b/database/models/models.go index 8b00c98..6adf847 100644 --- a/database/models/models.go +++ b/database/models/models.go @@ -4,43 +4,44 @@ import ( "database/sql/driver" "encoding/json" "fmt" + "time" ) // Client represents a registered client device type Client struct { - UUID string `json:"uuid,omitempty" gorm:"type:varchar(36);primaryKey"` - Token string `json:"token,omitempty" gorm:"type:varchar(255);unique;not null"` - Name string `json:"name" gorm:"type:varchar(100)"` - CpuName string `json:"cpu_name" gorm:"type:varchar(100)"` - Virtualization string `json:"virtualization" gorm:"type:varchar(50)"` - Arch string `json:"arch" gorm:"type:varchar(50)"` - CpuCores int `json:"cpu_cores" gorm:"type:int"` - CpuPhysicalCores int `json:"cpu_physical_cores" gorm:"type:int"` - OS string `json:"os" gorm:"type:varchar(100)"` - KernelVersion string `json:"kernel_version" gorm:"type:varchar(100)"` - GpuName string `json:"gpu_name" gorm:"type:varchar(100)"` - IPv4 string `json:"ipv4,omitempty" gorm:"type:varchar(100)"` - IPv6 string `json:"ipv6,omitempty" gorm:"type:varchar(100)"` - Region string `json:"region" gorm:"type:varchar(100)"` - Remark string `json:"remark,omitempty" gorm:"type:longtext"` - PublicRemark string `json:"public_remark,omitempty" gorm:"type:longtext"` - MemTotal int64 `json:"mem_total" gorm:"type:bigint"` - SwapTotal int64 `json:"swap_total" gorm:"type:bigint"` - DiskTotal int64 `json:"disk_total" gorm:"type:bigint"` - Version string `json:"version,omitempty" gorm:"type:varchar(100)"` - Weight int `json:"weight" gorm:"type:int"` - Price float64 `json:"price"` - BillingCycle int `json:"billing_cycle"` - AutoRenewal bool `json:"auto_renewal" gorm:"default:false"` // 是否自动续费 - Currency string `json:"currency" gorm:"type:varchar(20);default:'$'"` - ExpiredAt LocalTime `json:"expired_at" gorm:"type:timestamp"` - Group string `json:"group" gorm:"type:varchar(100)"` - Tags string `json:"tags" gorm:"type:text"` // split by ';' - Hidden bool `json:"hidden" gorm:"default:false"` - TrafficLimit int64 `json:"traffic_limit" gorm:"type:bigint"` - TrafficLimitType string `json:"traffic_limit_type" gorm:"type:varchar(10);default:'max'"` // 流量阈值类型:sum max min up down - CreatedAt LocalTime `json:"created_at"` - UpdatedAt LocalTime `json:"updated_at"` + UUID string `json:"uuid,omitempty" gorm:"type:varchar(36);primaryKey"` + Token string `json:"token,omitempty" gorm:"type:varchar(255);unique;not null"` + Name string `json:"name" gorm:"type:varchar(100)"` + CpuName string `json:"cpu_name" gorm:"type:varchar(100)"` + Virtualization string `json:"virtualization" gorm:"type:varchar(50)"` + Arch string `json:"arch" gorm:"type:varchar(50)"` + CpuCores int `json:"cpu_cores" gorm:"type:int"` + CpuPhysicalCores int `json:"cpu_physical_cores" gorm:"type:int"` + OS string `json:"os" gorm:"type:varchar(100)"` + KernelVersion string `json:"kernel_version" gorm:"type:varchar(100)"` + GpuName string `json:"gpu_name" gorm:"type:varchar(100)"` + IPv4 string `json:"ipv4,omitempty" gorm:"type:varchar(100)"` + IPv6 string `json:"ipv6,omitempty" gorm:"type:varchar(100)"` + Region string `json:"region" gorm:"type:varchar(100)"` + Remark string `json:"remark,omitempty" gorm:"type:longtext"` + PublicRemark string `json:"public_remark,omitempty" gorm:"type:longtext"` + MemTotal int64 `json:"mem_total" gorm:"type:bigint"` + SwapTotal int64 `json:"swap_total" gorm:"type:bigint"` + DiskTotal int64 `json:"disk_total" gorm:"type:bigint"` + Version string `json:"version,omitempty" gorm:"type:varchar(100)"` + Weight int `json:"weight" gorm:"type:int"` + Price float64 `json:"price"` + BillingCycle int `json:"billing_cycle"` + AutoRenewal bool `json:"auto_renewal" gorm:"default:false"` // 是否自动续费 + Currency string `json:"currency" gorm:"type:varchar(20);default:'$'"` + ExpiredAt *time.Time `json:"expired_at" gorm:"type:timestamp"` + Group string `json:"group" gorm:"type:varchar(100)"` + Tags string `json:"tags" gorm:"type:text"` // split by ';' + Hidden bool `json:"hidden" gorm:"default:false"` + TrafficLimit int64 `json:"traffic_limit" gorm:"type:bigint"` + TrafficLimitType string `json:"traffic_limit_type" gorm:"type:varchar(10);default:'max'"` // 流量阈值类型:sum max min up down + CreatedAt time.Time `json:"created_at"` + UpdatedAt time.Time `json:"updated_at"` } // User represents an authenticated user @@ -52,8 +53,8 @@ type User struct { SSOID string `json:"sso_id" gorm:"type:varchar(100)"` // OAuth provider's user ID TwoFactor string `json:"two_factor,omitempty" gorm:"type:varchar(255)"` // 2FA secret Sessions []Session `json:"sessions,omitempty" gorm:"foreignKey:UUID;references:UUID;constraint:OnDelete:CASCADE,OnUpdate:CASCADE"` - CreatedAt LocalTime `json:"created_at"` - UpdatedAt LocalTime `json:"updated_at"` + CreatedAt time.Time `json:"created_at"` + UpdatedAt time.Time `json:"updated_at"` } // Session manages user sessions @@ -63,17 +64,17 @@ type Session struct { UserAgent string `json:"user_agent" gorm:"type:text"` Ip string `json:"ip" gorm:"type:varchar(100)"` LoginMethod string `json:"login_method" gorm:"type:varchar(50)"` - LatestOnline LocalTime `json:"latest_online" gorm:"type:timestamp"` + LatestOnline time.Time `json:"latest_online" gorm:"type:timestamp"` LatestUserAgent string `json:"latest_user_agent" gorm:"type:text"` LatestIp string `json:"latest_ip" gorm:"type:varchar(100)"` - Expires LocalTime `json:"expires" gorm:"not null"` - CreatedAt LocalTime `json:"created_at"` + Expires time.Time `json:"expires" gorm:"not null"` + CreatedAt time.Time `json:"created_at"` } // Record logs client metrics over time type Record struct { Client string `json:"client" gorm:"type:varchar(36);index"` - Time LocalTime `json:"time" gorm:"index"` + Time time.Time `json:"time" gorm:"index"` Cpu float32 `json:"cpu" gorm:"type:decimal(5,2)"` // e.g., 75.50% Gpu float32 `json:"gpu" gorm:"type:decimal(5,2)"` Ram int64 `json:"ram" gorm:"type:bigint"` @@ -99,7 +100,7 @@ type Record struct { // GPURecord logs individual GPU metrics over time type GPURecord struct { Client string `json:"client" gorm:"type:varchar(36);index"` // 客户端UUID - Time LocalTime `json:"time" gorm:"index"` // 记录时间 + Time time.Time `json:"time" gorm:"index"` // 记录时间 DeviceIndex int `json:"device_index" gorm:"index"` // GPU设备索引 (0,1,2...) DeviceName string `json:"device_name" gorm:"type:varchar(100)"` // GPU型号 MemTotal int64 `json:"mem_total" gorm:"type:bigint"` // 显存总量(字节) diff --git a/database/models/notification.go b/database/models/notification.go index 3d03060..701c57c 100644 --- a/database/models/notification.go +++ b/database/models/notification.go @@ -1,13 +1,15 @@ package models +import "time" + // Notification 定义了通知相关的数据库模型 type OfflineNotification struct { Client string `json:"client" gorm:"type:varchar(36);not null;index;unique;constraint:OnDelete:CASCADE,OnUpdate:CASCADE;foreignKey:client;references:UUID"` ClientInfo Client `json:"client_info,omitempty" gorm:"foreignKey:Client;references:UUID"` Enable bool `json:"enable" gorm:"type:boolean;default:false"` //Cooldown int `json:"cooldown" gorm:"type:int;not null;default:1800"` // 冷却时间(秒),默认 30 分钟 - GracePeriod int `json:"grace_period" gorm:"type:int;not null;default:180"` // 宽限期(秒),默认 3 分钟 - LastNotified LocalTime `json:"last_notified"` // 上次通知时间 + GracePeriod int `json:"grace_period" gorm:"type:int;not null;default:180"` // 宽限期(秒),默认 3 分钟 + LastNotified *time.Time `json:"last_notified"` // 上次通知时间 } // LoadNotification 定义了基于资源占用达标时间比的负载通知规则 @@ -19,7 +21,7 @@ type LoadNotification struct { Threshold float32 `json:"threshold" gorm:"type:decimal(5,2);not null;default:80.00"` // 阈值百分比 Ratio float32 `json:"ratio" gorm:"type:decimal(5,2);not null;default:0.80"` // 达标时间比 Interval int `json:"interval" gorm:"type:int;not null;default:15"` // 监测间隔(分钟) - LastNotified LocalTime `json:"last_notified"` // 上次通知时间 + LastNotified *time.Time `json:"last_notified"` // 上次通知时间 } // TrafficReportNotification 定义了流量定时报告的数据库模型 @@ -27,7 +29,7 @@ type TrafficReportNotification struct { Client string `json:"client" gorm:"type:varchar(36);not null;index;unique;constraint:OnDelete:CASCADE,OnUpdate:CASCADE;foreignKey:client;references:UUID"` ClientInfo Client `json:"client_info,omitempty" gorm:"foreignKey:Client;references:UUID"` Enable bool `json:"enable" gorm:"type:boolean;default:false"` - Daily bool `json:"daily" gorm:"type:boolean;default:false"` // 日报 - Weekly bool `json:"weekly" gorm:"type:boolean;default:false"` // 周报 + Daily bool `json:"daily" gorm:"type:boolean;default:false"` // 日报 + Weekly bool `json:"weekly" gorm:"type:boolean;default:false"` // 周报 Monthly bool `json:"monthly" gorm:"type:boolean;default:false"` // 月报 } diff --git a/database/models/pingTask.go b/database/models/pingTask.go index 506d331..791b629 100644 --- a/database/models/pingTask.go +++ b/database/models/pingTask.go @@ -1,11 +1,13 @@ package models +import "time" + type PingRecord struct { Client string `json:"client" gorm:"type:varchar(36);not null;index"` ClientInfo Client `json:"client_info" gorm:"foreignKey:Client;references:UUID;constraint:OnDelete:CASCADE,OnUpdate:CASCADE"` TaskId uint `json:"task_id" gorm:"not null;index"` Task PingTask `json:"task" gorm:"foreignKey:TaskId;references:Id;constraint:OnDelete:CASCADE,OnUpdate:CASCADE;"` - Time LocalTime `json:"time" gorm:"index;not null"` + Time time.Time `json:"time" gorm:"index;not null"` Value int `json:"value" gorm:"type:int;not null"` // Ping 值,单位毫秒 } diff --git a/database/models/task.go b/database/models/task.go index dc32fcc..43e4c40 100644 --- a/database/models/task.go +++ b/database/models/task.go @@ -1,5 +1,7 @@ package models +import "time" + type Task struct { TaskId string `json:"task_id" gorm:"type:varchar(36);primaryKey;unique"` Clients StringArray `json:"clients" gorm:"type:longtext"` @@ -13,6 +15,6 @@ type TaskResult struct { ClientInfo Client `json:"client_info" gorm:"foreignKey:Client;references:UUID;constraint:OnDelete:CASCADE,OnUpdate:CASCADE"` Result string `json:"result" gorm:"type:longtext"` ExitCode *int `json:"exit_code" gorm:"type:int"` - FinishedAt *LocalTime `json:"finished_at" gorm:"type:timestamp"` - CreatedAt LocalTime `json:"created_at" gorm:"type:timestamp"` + FinishedAt *time.Time `json:"finished_at" gorm:"type:timestamp"` + CreatedAt time.Time `json:"created_at" gorm:"type:timestamp"` } diff --git a/database/models/time_test.go b/database/models/time_test.go new file mode 100644 index 0000000..4d7f907 --- /dev/null +++ b/database/models/time_test.go @@ -0,0 +1,77 @@ +package models + +import ( + "encoding/json" + "fmt" + "strings" + "testing" + "time" + + "gorm.io/driver/sqlite" + "gorm.io/gorm" + "gorm.io/gorm/logger" +) + +func TestTimeFieldsStoreAndSerializeAsUTCWithNanoseconds(t *testing.T) { + dsn := fmt.Sprintf("file:%s?mode=memory&cache=shared", strings.ReplaceAll(t.Name(), "/", "_")) + db, err := gorm.Open(sqlite.Open(dsn), &gorm.Config{ + Logger: logger.Default.LogMode(logger.Silent), + NowFunc: func() time.Time { return time.Now().UTC() }, + }) + if err != nil { + t.Fatalf("open database: %v", err) + } + if err := db.AutoMigrate(&Log{}); err != nil { + t.Fatalf("migrate log: %v", err) + } + + stamp := time.Date(2026, 7, 17, 1, 2, 3, 123456789, time.UTC) + entry := Log{IP: "127.0.0.1", UUID: "test", Message: "test", MsgType: "info", Time: stamp} + if err := db.Create(&entry).Error; err != nil { + t.Fatalf("create log: %v", err) + } + var loaded Log + if err := db.First(&loaded, entry.ID).Error; err != nil { + t.Fatalf("load log: %v", err) + } + if !loaded.Time.Equal(stamp) || loaded.Time.Nanosecond() != stamp.Nanosecond() { + t.Fatalf("loaded time = %s, want %s", loaded.Time, stamp) + } + + encoded, err := json.Marshal(loaded) + if err != nil { + t.Fatalf("marshal log: %v", err) + } + if !strings.Contains(string(encoded), `"time":"2026-07-17T01:02:03.123456789Z"`) { + t.Fatalf("unexpected JSON: %s", encoded) + } +} + +func TestNullableTimeFieldReadsAndSerializesNull(t *testing.T) { + dsn := fmt.Sprintf("file:%s?mode=memory&cache=shared", strings.ReplaceAll(t.Name(), "/", "_")) + db, err := gorm.Open(sqlite.Open(dsn), &gorm.Config{Logger: logger.Default.LogMode(logger.Silent)}) + if err != nil { + t.Fatalf("open database: %v", err) + } + if err := db.AutoMigrate(&Client{}); err != nil { + t.Fatalf("migrate client: %v", err) + } + if err := db.Exec(`INSERT INTO clients (uuid, token, expired_at) VALUES (?, ?, NULL)`, "nullable", "token").Error; err != nil { + t.Fatalf("insert client: %v", err) + } + + var client Client + if err := db.First(&client, "uuid = ?", "nullable").Error; err != nil { + t.Fatalf("load client: %v", err) + } + if client.ExpiredAt != nil { + t.Fatalf("expired_at = %s, want nil", client.ExpiredAt) + } + encoded, err := json.Marshal(client) + if err != nil { + t.Fatalf("marshal client: %v", err) + } + if !strings.Contains(string(encoded), `"expired_at":null`) { + t.Fatalf("unexpected JSON: %s", encoded) + } +} diff --git a/database/tasks/tasks.go b/database/tasks/tasks.go index 86c682e..fa129e0 100644 --- a/database/tasks/tasks.go +++ b/database/tasks/tasks.go @@ -26,7 +26,7 @@ func CreateTask(taskId string, clients []string, command string) error { Result: "", ExitCode: nil, FinishedAt: nil, - CreatedAt: models.FromTime(time.Now()), + CreatedAt: time.Now().UTC(), }) } if len(taskResults) > 0 { @@ -72,17 +72,17 @@ func GetTaskResultsByTaskId(taskId string) ([]models.TaskResult, error) { } return results, nil } -func SaveTaskResult(taskId, clientId, result string, exitCode int, timestamp models.LocalTime) error { +func SaveTaskResult(taskId, clientId, result string, exitCode int, timestamp time.Time) error { return dbcore.GetDBInstance(). Model(&models.TaskResult{}). Where("task_id = ? AND client = ?", taskId, clientId). Updates(map[string]interface{}{ "result": result, "exit_code": exitCode, - "finished_at": timestamp, + "finished_at": timestamp.UTC(), }).Error } func ClearTaskResultsByTimeBefore(before time.Time) error { - return dbcore.GetDBInstance().Where("created_at < ?", before.Format(time.RFC3339)).Delete(&models.TaskResult{}).Error + return dbcore.GetDBInstance().Where("created_at < ?", before.UTC()).Delete(&models.TaskResult{}).Error } diff --git a/database/tasks/tasks_test.go b/database/tasks/tasks_test.go new file mode 100644 index 0000000..043f439 --- /dev/null +++ b/database/tasks/tasks_test.go @@ -0,0 +1,42 @@ +package tasks + +import ( + "testing" + "time" + + "github.com/komari-monitor/komari/cmd/flags" + "github.com/komari-monitor/komari/database/dbcore" + "github.com/komari-monitor/komari/database/models" +) + +func TestClearTaskResultsByTimeBeforeUsesUTCTimeValue(t *testing.T) { + flags.DatabaseType = flags.DatabaseTypeSQLite + flags.DatabaseFile = "file:task_cleanup_time?mode=memory&cache=shared" + db := dbcore.GetDBInstance() + + taskID := "task-cleanup-time" + if err := db.Create(&models.Task{TaskId: taskID, Command: "true"}).Error; err != nil { + t.Fatalf("create task: %v", err) + } + cutoff := time.Date(2026, 7, 17, 12, 0, 0, 123456789, time.UTC) + results := []models.TaskResult{ + {TaskId: taskID, Client: "old", CreatedAt: cutoff.Add(-time.Nanosecond)}, + {TaskId: taskID, Client: "boundary", CreatedAt: cutoff}, + {TaskId: taskID, Client: "new", CreatedAt: cutoff.Add(time.Nanosecond)}, + } + if err := db.Create(&results).Error; err != nil { + t.Fatalf("create task results: %v", err) + } + + localCutoff := cutoff.In(time.FixedZone("UTC+8", 8*60*60)) + if err := ClearTaskResultsByTimeBefore(localCutoff); err != nil { + t.Fatalf("clear task results: %v", err) + } + var remaining []models.TaskResult + if err := db.Where("task_id = ?", taskID).Order("created_at").Find(&remaining).Error; err != nil { + t.Fatalf("load remaining results: %v", err) + } + if len(remaining) != 2 || remaining[0].Client != "boundary" || remaining[1].Client != "new" { + t.Fatalf("remaining results = %#v, want boundary and new", remaining) + } +} diff --git a/pkg/corn/corn.go b/pkg/corn/corn.go index 20397c4..8dbf8ee 100644 --- a/pkg/corn/corn.go +++ b/pkg/corn/corn.go @@ -40,7 +40,7 @@ type cronSchedule struct { } func (s cronSchedule) Next(t time.Time) time.Time { - next := t.Truncate(time.Second).Add(time.Second) + next := t.UTC().Truncate(time.Second).Add(time.Second) limit := next.Add(366 * 24 * time.Hour) for next.Before(limit) { if s.match(next) { @@ -52,6 +52,7 @@ func (s cronSchedule) Next(t time.Time) time.Time { } func (s cronSchedule) match(t time.Time) bool { + t = t.In(time.Local) _, okSecond := s.seconds[t.Second()] _, okMinute := s.minutes[t.Minute()] _, okHour := s.hours[t.Hour()] diff --git a/pkg/corn/corn_test.go b/pkg/corn/corn_test.go new file mode 100644 index 0000000..8060df4 --- /dev/null +++ b/pkg/corn/corn_test.go @@ -0,0 +1,35 @@ +package corn + +import ( + "testing" + "time" +) + +func TestCronScheduleUsesSystemLocalWallClock(t *testing.T) { + originalLocal := time.Local + time.Local = time.FixedZone("UTC+8", 8*60*60) + t.Cleanup(func() { time.Local = originalLocal }) + + schedule, err := Parse("0 0 9 * * *") + if err != nil { + t.Fatalf("parse schedule: %v", err) + } + after := time.Date(2026, 7, 17, 0, 30, 0, 0, time.UTC) + want := time.Date(2026, 7, 17, 1, 0, 0, 0, time.UTC) + if got := schedule.Next(after); !got.Equal(want) { + t.Fatalf("next run = %s, want %s", got, want) + } else if got.Location() != time.UTC { + t.Fatalf("next run location = %s, want UTC", got.Location()) + } +} + +func TestEverySchedulePreservesElapsedDuration(t *testing.T) { + schedule, err := Parse("@every 90s") + if err != nil { + t.Fatalf("parse schedule: %v", err) + } + after := time.Now() + if got := schedule.Next(after); got.Sub(after) != 90*time.Second { + t.Fatalf("interval = %s, want 90s", got.Sub(after)) + } +} diff --git a/pkg/migrations/legacy.go b/pkg/migrations/legacy.go index 2b0fe57..b76d2d2 100644 --- a/pkg/migrations/legacy.go +++ b/pkg/migrations/legacy.go @@ -4,27 +4,27 @@ import "time" // ClientInfo is the legacy table shape for migrating pre-Client model data. type ClientInfo struct { - UUID string `json:"uuid,omitempty" gorm:"type:varchar(36);primaryKey;foreignKey:ClientUUID;references:UUID;constraint:OnDelete:CASCADE"` - Name string `json:"name" gorm:"type:varchar(100);not null"` - CpuName string `json:"cpu_name" gorm:"type:varchar(100)"` - Virtualization string `json:"virtualization" gorm:"type:varchar(50)"` - Arch string `json:"arch" gorm:"type:varchar(50)"` - CpuCores int `json:"cpu_cores" gorm:"type:int"` - OS string `json:"os" gorm:"type:varchar(100)"` - GpuName string `json:"gpu_name" gorm:"type:varchar(100)"` - IPv4 string `json:"ipv4,omitempty" gorm:"type:varchar(100)"` - IPv6 string `json:"ipv6,omitempty" gorm:"type:varchar(100)"` - Region string `json:"region" gorm:"type:varchar(100)"` - Remark string `json:"remark,omitempty" gorm:"type:longtext"` - PublicRemark string `json:"public_remark,omitempty" gorm:"type:longtext"` - MemTotal int64 `json:"mem_total" gorm:"type:bigint"` - SwapTotal int64 `json:"swap_total" gorm:"type:bigint"` - DiskTotal int64 `json:"disk_total" gorm:"type:bigint"` - Version string `json:"version,omitempty" gorm:"type:varchar(100)"` - Weight int `json:"weight" gorm:"type:int"` - Price float64 `json:"price"` - BillingCycle int `json:"billing_cycle"` - ExpiredAt time.Time `json:"expired_at" gorm:"type:timestamp"` + UUID string `json:"uuid,omitempty" gorm:"type:varchar(36);primaryKey;foreignKey:ClientUUID;references:UUID;constraint:OnDelete:CASCADE"` + Name string `json:"name" gorm:"type:varchar(100);not null"` + CpuName string `json:"cpu_name" gorm:"type:varchar(100)"` + Virtualization string `json:"virtualization" gorm:"type:varchar(50)"` + Arch string `json:"arch" gorm:"type:varchar(50)"` + CpuCores int `json:"cpu_cores" gorm:"type:int"` + OS string `json:"os" gorm:"type:varchar(100)"` + GpuName string `json:"gpu_name" gorm:"type:varchar(100)"` + IPv4 string `json:"ipv4,omitempty" gorm:"type:varchar(100)"` + IPv6 string `json:"ipv6,omitempty" gorm:"type:varchar(100)"` + Region string `json:"region" gorm:"type:varchar(100)"` + Remark string `json:"remark,omitempty" gorm:"type:longtext"` + PublicRemark string `json:"public_remark,omitempty" gorm:"type:longtext"` + MemTotal int64 `json:"mem_total" gorm:"type:bigint"` + SwapTotal int64 `json:"swap_total" gorm:"type:bigint"` + DiskTotal int64 `json:"disk_total" gorm:"type:bigint"` + Version string `json:"version,omitempty" gorm:"type:varchar(100)"` + Weight int `json:"weight" gorm:"type:int"` + Price float64 `json:"price"` + BillingCycle int `json:"billing_cycle"` + ExpiredAt *time.Time `json:"expired_at" gorm:"type:timestamp"` CreatedAt time.Time UpdatedAt time.Time } diff --git a/pkg/migrations/legacy_monitoring.go b/pkg/migrations/legacy_monitoring.go index 460d3e1..df64925 100644 --- a/pkg/migrations/legacy_monitoring.go +++ b/pkg/migrations/legacy_monitoring.go @@ -63,8 +63,8 @@ type LegacyMonitoringDeleted struct { } type legacyMonitoringBounds struct { - Oldest models.LocalTime `gorm:"column:oldest"` - Newest models.LocalTime `gorm:"column:newest"` + Oldest string `gorm:"column:oldest"` + Newest string `gorm:"column:newest"` } type legacyBatchProgress func(table string, rows, points int64) @@ -154,8 +154,14 @@ func InspectLegacyMonitoring(db *gorm.DB) (LegacyMonitoringSummary, error) { if err := db.Table(table).Select("MIN(time) AS oldest, MAX(time) AS newest").Scan(&bounds).Error; err != nil { return summary, fmt.Errorf("inspect legacy %s time range: %w", table, err) } - tableOldest := bounds.Oldest.ToTime() - tableNewest := bounds.Newest.ToTime() + tableOldest, err := parseLegacyTimestamp(bounds.Oldest, time.UTC) + if err != nil { + return summary, fmt.Errorf("parse legacy %s oldest time: %w", table, err) + } + tableNewest, err := parseLegacyTimestamp(bounds.Newest, time.UTC) + if err != nil { + return summary, fmt.Errorf("parse legacy %s newest time: %w", table, err) + } if oldest.IsZero() || (!tableOldest.IsZero() && tableOldest.Before(oldest)) { oldest = tableOldest } @@ -175,7 +181,7 @@ func InspectLegacyMonitoring(db *gorm.DB) (LegacyMonitoringSummary, error) { summary.NewestAt = &newestCopy } if !oldest.IsZero() && !newest.IsZero() { - retentionEnd := time.Now() + retentionEnd := time.Now().UTC() if newest.After(retentionEnd) { retentionEnd = newest } @@ -203,7 +209,7 @@ func DeleteLegacyMonitoringBefore(db *gorm.DB, cutoff time.Time) (LegacyMonitori if !tx.Migrator().HasTable(table) { continue } - result := tx.Exec("DELETE FROM "+table+" WHERE time < ?", models.FromTime(cutoff)) + result := tx.Exec("DELETE FROM "+table+" WHERE time < ?", cutoff.UTC()) if result.Error != nil { return fmt.Errorf("delete legacy %s before cutoff: %w", table, result.Error) } @@ -478,7 +484,7 @@ func migrateLegacyPingRecordTable(ctx context.Context, s *metric.Store, db *gorm } func recordToPoints(rec models.Record) []metric.Point { - ts := rec.Time.ToTime() + ts := rec.Time entityID := rec.Client return []metric.Point{ {MetricName: metricstore.MetricCPU, EntityID: entityID, Timestamp: ts, Value: float64(rec.Cpu)}, @@ -504,7 +510,7 @@ func recordToPoints(rec models.Record) []metric.Point { } func gpuRecordToPoints(rec models.GPURecord) []metric.Point { - ts := rec.Time.ToTime() + ts := rec.Time tags := map[string]string{ "device_index": fmt.Sprintf("%d", rec.DeviceIndex), "device_name": rec.DeviceName, @@ -518,7 +524,7 @@ func gpuRecordToPoints(rec models.GPURecord) []metric.Point { } func pingRecordToPoints(rec models.PingRecord) []metric.Point { - ts := rec.Time.ToTime() + ts := rec.Time tags := map[string]string{"task_id": fmt.Sprintf("%d", rec.TaskId)} loss := 0.0 if rec.Value < 0 { diff --git a/pkg/migrations/legacy_monitoring_test.go b/pkg/migrations/legacy_monitoring_test.go index 4e9aaea..97eadec 100644 --- a/pkg/migrations/legacy_monitoring_test.go +++ b/pkg/migrations/legacy_monitoring_test.go @@ -35,19 +35,19 @@ func TestLegacyMonitoringTablesMigratedByOneShotMigration(t *testing.T) { } base := time.Date(2026, 7, 8, 23, 42, 0, 0, time.UTC) - if err := mainDB.Create(&models.Record{Client: "client-a", Time: models.FromTime(base), Cpu: 12.5, Ram: 2048}).Error; err != nil { + if err := mainDB.Create(&models.Record{Client: "client-a", Time: base, Cpu: 12.5, Ram: 2048}).Error; err != nil { t.Fatalf("seed records: %v", err) } - if err := mainDB.Table("records_long_term").Create(&models.Record{Client: "client-a", Time: models.FromTime(base.Add(time.Minute)), Cpu: 22.5, Ram: 4096}).Error; err != nil { + if err := mainDB.Table("records_long_term").Create(&models.Record{Client: "client-a", Time: base.Add(time.Minute), Cpu: 22.5, Ram: 4096}).Error; err != nil { t.Fatalf("seed records_long_term: %v", err) } - if err := mainDB.Create(&models.GPURecord{Client: "client-a", Time: models.FromTime(base), DeviceIndex: 0, DeviceName: "GPU 0", MemUsed: 1024, MemTotal: 2048, Utilization: 67, Temperature: 55}).Error; err != nil { + if err := mainDB.Create(&models.GPURecord{Client: "client-a", Time: base, DeviceIndex: 0, DeviceName: "GPU 0", MemUsed: 1024, MemTotal: 2048, Utilization: 67, Temperature: 55}).Error; err != nil { t.Fatalf("seed gpu_records: %v", err) } - if err := mainDB.Create(&models.PingRecord{Client: "client-a", TaskId: 7, Time: models.FromTime(base), Value: 36}).Error; err != nil { + if err := mainDB.Create(&models.PingRecord{Client: "client-a", TaskId: 7, Time: base, Value: 36}).Error; err != nil { t.Fatalf("seed ping_records: %v", err) } - if err := mainDB.Create(&models.PingRecord{Client: "client-a", TaskId: 7, Time: models.FromTime(base.Add(30 * time.Second)), Value: -1}).Error; err != nil { + if err := mainDB.Create(&models.PingRecord{Client: "client-a", TaskId: 7, Time: base.Add(30 * time.Second), Value: -1}).Error; err != nil { t.Fatalf("seed loss ping_records: %v", err) } summary, err := InspectLegacyMonitoring(mainDB) @@ -173,8 +173,8 @@ func TestDeleteLegacyMonitoringBeforeOnlyRemovesOldHistory(t *testing.T) { } cutoff := time.Date(2026, 7, 10, 0, 0, 0, 0, time.UTC) - oldTime := models.FromTime(cutoff.Add(-time.Hour)) - newTime := models.FromTime(cutoff.Add(time.Hour)) + oldTime := cutoff.Add(-time.Hour) + newTime := cutoff.Add(time.Hour) for _, record := range []models.Record{{Client: "client-a", Time: oldTime}, {Client: "client-a", Time: newTime}} { if err := db.Create(&record).Error; err != nil { t.Fatalf("seed load record: %v", err) diff --git a/pkg/migrations/migrations.go b/pkg/migrations/migrations.go index 958e949..9ce6c1a 100644 --- a/pkg/migrations/migrations.go +++ b/pkg/migrations/migrations.go @@ -39,8 +39,8 @@ type legacyModelConfig struct { ExpireNotificationLeadDays int `json:"expire_notification_lead_days" gorm:"default:7"` LoginNotification bool `json:"login_notification" gorm:"default:false"` TrafficLimitPercentage float64 `json:"traffic_limit_percentage" gorm:"default:80.00"` - CreatedAt models.LocalTime - UpdatedAt models.LocalTime + CreatedAt time.Time + UpdatedAt time.Time } func (legacyModelConfig) TableName() string { @@ -97,6 +97,9 @@ func Run(ctx Context) error { } legacyConfigTable := hasLegacyConfigTable(db) + if err := migrateLegacyTimestampColumns(db); err != nil { + return err + } if legacyConfigTable { if err := migrateLegacyOidcConfig(db); err != nil { @@ -106,7 +109,6 @@ func Run(ctx Context) error { return err } } - if err := migrateLegacyClientInfo(db); err != nil { return err } @@ -127,6 +129,9 @@ func Run(ctx Context) error { if err := migrateRemovedCompatibilityConfig(db); err != nil { return err } + if err := markTimestampMigrationDone(db); err != nil { + return fmt.Errorf("mark UTC timestamp migration done: %w", err) + } return nil } @@ -402,7 +407,7 @@ func migrateLegacyClientInfo(db *gorm.DB) error { client.Weight = info.Weight client.Price = info.Price client.BillingCycle = info.BillingCycle - client.ExpiredAt = models.FromTime(info.ExpiredAt) + client.ExpiredAt = info.ExpiredAt if err := db.Save(&client).Error; err != nil { return fmt.Errorf("update Client record %s: %w", info.UUID, err) } diff --git a/pkg/migrations/migrations_test.go b/pkg/migrations/migrations_test.go index 81f4c68..2a5f6dd 100644 --- a/pkg/migrations/migrations_test.go +++ b/pkg/migrations/migrations_test.go @@ -143,7 +143,7 @@ func TestRunPreservesVersion120RuntimeShape(t *testing.T) { ); err != nil { t.Fatalf("migrate 1.2.0 runtime shape: %v", err) } - now := models.FromTime(time.Now()) + now := time.Now().UTC() if err := db.Create(&models.Client{UUID: "client-a", Token: "token-a", CreatedAt: now, UpdatedAt: now}).Error; err != nil { t.Fatalf("seed client: %v", err) } @@ -268,7 +268,7 @@ func TestRunExpandsLegacyPingAllClientsTasks(t *testing.T) { `).Error; err != nil { t.Fatalf("create legacy ping_tasks: %v", err) } - now := models.FromTime(time.Now()) + now := time.Now().UTC() clients := []models.Client{ {UUID: "client-a", Token: "token-a", CreatedAt: now, UpdatedAt: now}, {UUID: "client-b", Token: "token-b", CreatedAt: now, UpdatedAt: now}, diff --git a/pkg/migrations/timestamp.go b/pkg/migrations/timestamp.go new file mode 100644 index 0000000..7b4e303 --- /dev/null +++ b/pkg/migrations/timestamp.go @@ -0,0 +1,242 @@ +package migrations + +import ( + "database/sql" + "fmt" + "log" + "math" + "os" + "strconv" + "strings" + "time" + + appconfig "github.com/komari-monitor/komari/pkg/config" + "gorm.io/gorm" + "gorm.io/gorm/clause" +) + +const timestampUTCMigrationKey = "internal_timestamp_utc_migrated" + +type timestampColumn struct { + table string + column string +} + +var legacyTimestampColumns = [...]timestampColumn{ + {table: "clients", column: "expired_at"}, + {table: "clients", column: "created_at"}, + {table: "clients", column: "updated_at"}, + {table: "client_infos", column: "expired_at"}, + {table: "client_infos", column: "created_at"}, + {table: "client_infos", column: "updated_at"}, + {table: "users", column: "created_at"}, + {table: "users", column: "updated_at"}, + {table: "sessions", column: "latest_online"}, + {table: "sessions", column: "expires"}, + {table: "sessions", column: "created_at"}, + {table: "logs", column: "time"}, + {table: "clipboards", column: "created_at"}, + {table: "clipboards", column: "updated_at"}, + {table: "offline_notifications", column: "last_notified"}, + {table: "load_notifications", column: "last_notified"}, + {table: "task_results", column: "finished_at"}, + {table: "task_results", column: "created_at"}, + {table: "records", column: "time"}, + {table: "records_long_term", column: "time"}, + {table: "gpu_records", column: "time"}, + {table: "ping_records", column: "time"}, + {table: "configs", column: "created_at"}, + {table: "configs", column: "updated_at"}, +} + +var legacyTimestampLayouts = [...]string{ + "2006-01-02 15:04:05.999999999-07:00", + "2006-01-02 15:04:05-07:00", + "2006-01-02 15:04:05.999999999", + "2006-01-02 15:04:05", + "2006-01-02T15:04:05.999999999", + "2006-01-02T15:04:05", + "2006-01-02", +} + +type timestampRow struct { + rowID int64 + value sql.NullString +} + +// migrateLegacyTimestampColumns makes old offset-free SQLite values +// unambiguous before any current model scans them as time.Time. +func migrateLegacyTimestampColumns(db *gorm.DB) error { + if timestampMigrationDone(db) { + return nil + } + + location := legacyTimestampLocation() + var converted int64 + err := db.Transaction(func(tx *gorm.DB) error { + for _, target := range legacyTimestampColumns { + if !tx.Migrator().HasTable(target.table) || !tx.Migrator().HasColumn(target.table, target.column) { + continue + } + count, err := migrateTimestampColumn(tx, target, location) + if err != nil { + return err + } + converted += count + } + return nil + }) + if err != nil { + return err + } + if converted > 0 { + log.Printf("Converted %d legacy timestamp values to explicit UTC", converted) + } + return nil +} + +func migrateTimestampColumn(db *gorm.DB, target timestampColumn, location *time.Location) (int64, error) { + table := quoteSQLiteIdentifier(target.table) + column := quoteSQLiteIdentifier(target.column) + var converted int64 + var lastRowID int64 + + for { + query := fmt.Sprintf( + "SELECT rowid, CAST(%s AS TEXT) FROM %s WHERE rowid > ? AND %s IS NOT NULL ORDER BY rowid LIMIT 1000", + column, table, column, + ) + rows, err := db.Raw(query, lastRowID).Rows() + if err != nil { + return converted, fmt.Errorf("read legacy timestamp %s.%s: %w", target.table, target.column, err) + } + + batch := make([]timestampRow, 0, 1000) + for rows.Next() { + var row timestampRow + if err := rows.Scan(&row.rowID, &row.value); err != nil { + rows.Close() + return converted, fmt.Errorf("scan legacy timestamp %s.%s: %w", target.table, target.column, err) + } + batch = append(batch, row) + lastRowID = row.rowID + } + if err := rows.Err(); err != nil { + rows.Close() + return converted, fmt.Errorf("iterate legacy timestamp %s.%s: %w", target.table, target.column, err) + } + rows.Close() + if len(batch) == 0 { + break + } + + for _, row := range batch { + raw := strings.TrimSpace(row.value.String) + if !row.value.Valid || raw == "" { + continue + } + stamp, err := parseLegacyTimestamp(raw, location) + if err != nil { + return converted, fmt.Errorf("convert legacy timestamp %s.%s rowid=%d: %w", target.table, target.column, row.rowID, err) + } + update := fmt.Sprintf("UPDATE %s SET %s = ? WHERE rowid = ?", table, column) + if err := db.Exec(update, stamp.UTC(), row.rowID).Error; err != nil { + return converted, fmt.Errorf("write UTC timestamp %s.%s rowid=%d: %w", target.table, target.column, row.rowID, err) + } + converted++ + } + if len(batch) < 1000 { + break + } + } + return converted, nil +} + +func parseLegacyTimestamp(value string, location *time.Location) (time.Time, error) { + raw := strings.TrimSpace(value) + if raw == "" { + return time.Time{}, fmt.Errorf("timestamp is empty") + } + if location == nil { + location = time.UTC + } + if stamp, err := time.Parse(time.RFC3339Nano, raw); err == nil { + return stamp.UTC(), nil + } + for _, layout := range legacyTimestampLayouts { + if stamp, err := time.ParseInLocation(layout, raw, location); err == nil { + return stamp.UTC(), nil + } + } + epoch, err := strconv.ParseInt(raw, 10, 64) + if err != nil { + return time.Time{}, fmt.Errorf("unsupported legacy timestamp %q", value) + } + return legacyEpochTime(epoch), nil +} + +func legacyEpochTime(value int64) time.Time { + abs := value + if abs < 0 { + if abs == math.MinInt64 { + return time.Unix(0, value).UTC() + } + abs = -abs + } + switch { + case abs >= 1e17: + return time.Unix(0, value).UTC() + case abs >= 1e14: + return time.Unix(0, value*int64(time.Microsecond)).UTC() + case abs >= 1e11: + return time.UnixMilli(value).UTC() + default: + return time.Unix(value, 0).UTC() + } +} + +// legacyTimestampLocation is intentionally migration-only. The old custom +// type used TZ and defaulted to UTC; current runtime code uses system time.Local. +func legacyTimestampLocation() *time.Location { + name := strings.TrimSpace(os.Getenv("TZ")) + if name == "" { + return time.UTC + } + location, err := time.LoadLocation(name) + if err != nil { + log.Printf("Legacy timezone %q cannot be loaded; interpreting old timestamps as UTC: %v", name, err) + return time.UTC + } + return location +} + +func timestampMigrationDone(db *gorm.DB) bool { + if !db.Migrator().HasTable(&appconfig.ConfigItem{}) || hasLegacyConfigTable(db) { + return false + } + var item appconfig.ConfigItem + if err := db.Where("key = ?", timestampUTCMigrationKey).First(&item).Error; err != nil { + return false + } + return item.Value == "true" +} + +func markTimestampMigrationDone(db *gorm.DB) error { + if hasLegacyConfigTable(db) { + return fmt.Errorf("new config item table is unavailable") + } + if !db.Migrator().HasTable(&appconfig.ConfigItem{}) { + if err := db.AutoMigrate(&appconfig.ConfigItem{}); err != nil { + return fmt.Errorf("create config item table: %w", err) + } + } + item := appconfig.ConfigItem{Key: timestampUTCMigrationKey, Value: "true"} + return db.Clauses(clause.OnConflict{ + Columns: []clause.Column{{Name: "key"}}, + DoUpdates: clause.AssignmentColumns([]string{"value"}), + }).Create(&item).Error +} + +func quoteSQLiteIdentifier(value string) string { + return `"` + strings.ReplaceAll(value, `"`, `""`) + `"` +} diff --git a/pkg/migrations/timestamp_test.go b/pkg/migrations/timestamp_test.go new file mode 100644 index 0000000..b1a2b07 --- /dev/null +++ b/pkg/migrations/timestamp_test.go @@ -0,0 +1,99 @@ +package migrations + +import ( + "fmt" + "strings" + "testing" + "time" + + "gorm.io/driver/sqlite" + "gorm.io/gorm" + "gorm.io/gorm/logger" +) + +func TestLegacyTimestampMigrationDefaultsToUTC(t *testing.T) { + t.Setenv("TZ", "") + db := openTimestampMigrationDB(t) + insertLegacyLogTime(t, db, "2026-07-17 09:30:00.1234567") + + if err := migrateLegacyTimestampColumns(db); err != nil { + t.Fatalf("migrate timestamps: %v", err) + } + want := time.Date(2026, 7, 17, 9, 30, 0, 123456700, time.UTC) + assertMigratedLogTime(t, db, want) +} + +func TestLegacyTimestampMigrationUsesLegacyTZ(t *testing.T) { + t.Setenv("TZ", "Asia/Shanghai") + db := openTimestampMigrationDB(t) + insertLegacyLogTime(t, db, "2026-07-17 09:30:00.123456789") + + if err := migrateLegacyTimestampColumns(db); err != nil { + t.Fatalf("migrate timestamps: %v", err) + } + want := time.Date(2026, 7, 17, 1, 30, 0, 123456789, time.UTC) + assertMigratedLogTime(t, db, want) +} + +func TestParseLegacyTimestampAcceptsHistoricalEpochUnits(t *testing.T) { + stamp := time.Date(2026, 7, 17, 1, 30, 0, 123456789, time.UTC) + tests := map[string]struct { + value string + want time.Time + }{ + "seconds": {value: fmt.Sprint(stamp.Unix()), want: stamp.Truncate(time.Second)}, + "milliseconds": {value: fmt.Sprint(stamp.UnixMilli()), want: stamp.Truncate(time.Millisecond)}, + "microseconds": {value: fmt.Sprint(stamp.UnixMicro()), want: stamp.Truncate(time.Microsecond)}, + "nanoseconds": {value: fmt.Sprint(stamp.UnixNano()), want: stamp}, + } + for name, test := range tests { + t.Run(name, func(t *testing.T) { + got, err := parseLegacyTimestamp(test.value, time.UTC) + if err != nil { + t.Fatalf("parse legacy timestamp: %v", err) + } + if !got.Equal(test.want) { + t.Fatalf("parsed timestamp = %s, want %s", got, test.want) + } + }) + } +} + +func openTimestampMigrationDB(t *testing.T) *gorm.DB { + t.Helper() + dsn := fmt.Sprintf("file:%s?mode=memory&cache=shared", strings.ReplaceAll(t.Name(), "/", "_")) + db, err := gorm.Open(sqlite.Open(dsn), &gorm.Config{Logger: logger.Default.LogMode(logger.Silent)}) + if err != nil { + t.Fatalf("open database: %v", err) + } + if err := db.Exec(`CREATE TABLE logs (id INTEGER PRIMARY KEY, time TIMESTAMP)`).Error; err != nil { + t.Fatalf("create logs table: %v", err) + } + return db +} + +func insertLegacyLogTime(t *testing.T, db *gorm.DB, value string) { + t.Helper() + if err := db.Exec(`INSERT INTO logs (time) VALUES (?)`, value).Error; err != nil { + t.Fatalf("insert legacy timestamp: %v", err) + } +} + +func assertMigratedLogTime(t *testing.T, db *gorm.DB, want time.Time) { + t.Helper() + var got time.Time + if err := db.Raw(`SELECT time FROM logs LIMIT 1`).Scan(&got).Error; err != nil { + t.Fatalf("read migrated timestamp: %v", err) + } + if !got.Equal(want) || got.Nanosecond() != want.Nanosecond() { + t.Fatalf("migrated time = %s, want %s", got, want) + } + + var raw string + if err := db.Raw(`SELECT CAST(time AS TEXT) FROM logs LIMIT 1`).Scan(&raw).Error; err != nil { + t.Fatalf("read raw migrated timestamp: %v", err) + } + if !strings.HasSuffix(raw, "+00:00") { + t.Fatalf("stored timestamp %q has no explicit UTC offset", raw) + } +} diff --git a/pkg/timeutil/timeutil.go b/pkg/timeutil/timeutil.go new file mode 100644 index 0000000..09be37d --- /dev/null +++ b/pkg/timeutil/timeutil.go @@ -0,0 +1,32 @@ +package timeutil + +import ( + "time" +) + +// SameSystemDate compares calendar dates in the operating system timezone. +func SameSystemDate(left, right time.Time) bool { + left = left.In(time.Local) + right = right.In(time.Local) + ly, lm, ld := left.Date() + ry, rm, rd := right.Date() + return ly == ry && lm == rm && ld == rd +} + +// FormatSystemDate formats an instant as a system-local calendar date. +func FormatSystemDate(value time.Time) string { + if value.IsZero() { + return "" + } + return value.In(time.Local).Format("2006-01-02") +} + +// SystemDateDistance returns the number of local calendar boundaries between +// two instants, independent of daylight-saving day length. +func SystemDateDistance(from, to time.Time) int { + fy, fm, fd := from.In(time.Local).Date() + ty, tm, td := to.In(time.Local).Date() + fromDate := time.Date(fy, fm, fd, 0, 0, 0, 0, time.UTC) + toDate := time.Date(ty, tm, td, 0, 0, 0, 0, time.UTC) + return int(toDate.Sub(fromDate) / (24 * time.Hour)) +} diff --git a/pkg/timeutil/timeutil_test.go b/pkg/timeutil/timeutil_test.go new file mode 100644 index 0000000..36b42b9 --- /dev/null +++ b/pkg/timeutil/timeutil_test.go @@ -0,0 +1,30 @@ +package timeutil + +import ( + "testing" + "time" +) + +func TestSystemDateHelpersUseSystemLocal(t *testing.T) { + originalLocal := time.Local + time.Local = time.FixedZone("UTC+8", 8*60*60) + t.Cleanup(func() { time.Local = originalLocal }) + + lateUTC := time.Date(2026, 7, 17, 16, 30, 0, 0, time.UTC) + earlyUTC := time.Date(2026, 7, 18, 1, 30, 0, 0, time.UTC) + if !SameSystemDate(lateUTC, earlyUTC) { + t.Fatal("instants on the same system-local date should match") + } + if got := FormatSystemDate(lateUTC); got != "2026-07-18" { + t.Fatalf("formatted system date = %q, want 2026-07-18", got) + } + if got := SystemDateDistance(lateUTC.AddDate(0, 0, -2), lateUTC); got != 2 { + t.Fatalf("system date distance = %d, want 2", got) + } +} + +func TestFormatSystemDateReturnsEmptyForZeroTime(t *testing.T) { + if got := FormatSystemDate(time.Time{}); got != "" { + t.Fatalf("formatted zero time = %q, want empty", got) + } +} diff --git a/protocol/v2/jsonrpc.go b/protocol/v2/jsonrpc.go index 2325a57..f13920b 100644 --- a/protocol/v2/jsonrpc.go +++ b/protocol/v2/jsonrpc.go @@ -1,6 +1,10 @@ package v2 -import v1 "github.com/komari-monitor/komari/protocol/v1" +import ( + "time" + + v1 "github.com/komari-monitor/komari/protocol/v1" +) const ( Version = "2.0" @@ -31,11 +35,11 @@ type Response struct { } type Event struct { - ID string `json:"id"` - Method string `json:"method"` - Params any `json:"params,omitempty"` - CreatedAt string `json:"created_at,omitempty"` - ExpiresAt string `json:"expires_at,omitempty"` + ID string `json:"id"` + Method string `json:"method"` + Params any `json:"params,omitempty"` + CreatedAt time.Time `json:"created_at"` + ExpiresAt time.Time `json:"expires_at"` } type RPCError struct { @@ -54,10 +58,10 @@ type BasicInfoParams struct { } type PingResultParams struct { - TaskID uint `json:"task_id"` - PingType string `json:"ping_type"` - Value int `json:"value"` - FinishedAt string `json:"finished_at"` + TaskID uint `json:"task_id"` + PingType string `json:"ping_type"` + Value int `json:"value"` + FinishedAt time.Time `json:"finished_at"` } type PullParams struct { diff --git a/protocol/v2/networktest.go b/protocol/v2/networktest.go index 64b6d3d..f0eeadb 100644 --- a/protocol/v2/networktest.go +++ b/protocol/v2/networktest.go @@ -1,6 +1,9 @@ package v2 -import "encoding/json" +import ( + "encoding/json" + "time" +) const ( MethodNetworkTestNextTrace = "networkTest.nextTrace" @@ -59,8 +62,8 @@ type NextTraceResult struct { TargetHost string `json:"target_host"` IPFamily IPFamily `json:"ip_family"` Protocol TraceProtocol `json:"protocol"` - StartedAt string `json:"started_at"` - FinishedAt string `json:"finished_at"` + StartedAt time.Time `json:"started_at"` + FinishedAt time.Time `json:"finished_at"` DurationMs int `json:"duration_ms"` OK bool `json:"ok"` Error string `json:"error"` @@ -102,11 +105,11 @@ type MeshTraceParams struct { } type MeshTraceAccepted struct { - JobID string `json:"job_id"` - Status string `json:"status"` - TotalPairs int `json:"total_pairs"` - AcceptedAt string `json:"accepted_at"` - PollIntervalMs int `json:"poll_interval_ms"` + JobID string `json:"job_id"` + Status string `json:"status"` + TotalPairs int `json:"total_pairs"` + AcceptedAt time.Time `json:"accepted_at"` + PollIntervalMs int `json:"poll_interval_ms"` } type MeshTraceJobSnapshot struct { @@ -117,8 +120,8 @@ type MeshTraceJobSnapshot struct { Failed int `json:"failed"` Running int `json:"running"` Results []NextTraceResult `json:"results"` - StartedAt string `json:"started_at"` - UpdatedAt string `json:"updated_at"` - FinishedAt string `json:"finished_at"` + StartedAt time.Time `json:"started_at"` + UpdatedAt time.Time `json:"updated_at"` + FinishedAt *time.Time `json:"finished_at"` Error string `json:"error"` } diff --git a/protocol/v2/networktest_test.go b/protocol/v2/networktest_test.go index f8408d5..17c21db 100644 --- a/protocol/v2/networktest_test.go +++ b/protocol/v2/networktest_test.go @@ -5,6 +5,7 @@ import ( "reflect" "strings" "testing" + "time" ) func TestNetworkTestMethodConstants(t *testing.T) { @@ -63,6 +64,7 @@ func TestNextTraceParamsJSON(t *testing.T) { } func TestNextTraceResultJSON(t *testing.T) { + startedAt := time.Date(2026, 6, 5, 10, 0, 0, 0, time.UTC) result := NextTraceResult{ TaskID: "trace-1", SourceID: "agent-a", @@ -70,8 +72,8 @@ func TestNextTraceResultJSON(t *testing.T) { TargetHost: "203.0.113.10", IPFamily: IPFamilyIPv4, Protocol: TraceProtocolICMP, - StartedAt: "2026-06-05T10:00:00Z", - FinishedAt: "2026-06-05T10:00:01Z", + StartedAt: startedAt, + FinishedAt: startedAt.Add(time.Second), DurationMs: 1000, OK: true, Error: "", @@ -169,6 +171,7 @@ func TestMeshTraceParamsJSON(t *testing.T) { } func TestMeshTraceJobSnapshotJSON(t *testing.T) { + startedAt := time.Date(2026, 6, 5, 10, 0, 0, 0, time.UTC) snapshot := MeshTraceJobSnapshot{ JobID: "mesh-1", Status: "running", @@ -184,8 +187,8 @@ func TestMeshTraceJobSnapshotJSON(t *testing.T) { TargetHost: "203.0.113.10", IPFamily: IPFamilyIPv4, Protocol: TraceProtocolUDP, - StartedAt: "2026-06-05T10:00:00Z", - FinishedAt: "2026-06-05T10:00:01Z", + StartedAt: startedAt, + FinishedAt: startedAt.Add(time.Second), DurationMs: 1000, OK: false, Error: "timeout", @@ -197,9 +200,9 @@ func TestMeshTraceJobSnapshotJSON(t *testing.T) { Truncated: false, }, }, - StartedAt: "2026-06-05T10:00:00Z", - UpdatedAt: "2026-06-05T10:00:01Z", - FinishedAt: "", + StartedAt: startedAt, + UpdatedAt: startedAt.Add(time.Second), + FinishedAt: nil, Error: "", } diff --git a/utils/log/gin.go b/utils/log/gin.go index 4a404b8..d5c3942 100644 --- a/utils/log/gin.go +++ b/utils/log/gin.go @@ -54,7 +54,7 @@ func GinLogger() gin.HandlerFunc { level = slog.LevelInfo } - r := slog.NewRecord(time.Now(), level, msg, 0) + r := slog.NewRecord(time.Now().UTC(), level, msg, 0) r.AddAttrs(slog.String("_group", "GIN")) // 添加分组标识 handler.Handle(c.Request.Context(), r) } @@ -67,7 +67,7 @@ func GinRecovery() gin.HandlerFunc { if err := recover(); err != nil { msg := fmt.Sprintf("panic recovered: %v | %s %s", err, c.Request.Method, c.Request.URL.Path) handler := slog.Default().Handler() - r := slog.NewRecord(time.Now(), slog.LevelError, msg, 0) + r := slog.NewRecord(time.Now().UTC(), slog.LevelError, msg, 0) r.AddAttrs(slog.String("_group", "GIN")) handler.Handle(c.Request.Context(), r) c.AbortWithStatus(500) diff --git a/utils/log/gorm.go b/utils/log/gorm.go index 6644b9b..380cf69 100644 --- a/utils/log/gorm.go +++ b/utils/log/gorm.go @@ -74,19 +74,19 @@ func (l *GormLogger) Trace(ctx context.Context, begin time.Time, fc func() (stri case err != nil && l.LogLevel >= gormlogger.Error && (!errors.Is(err, gorm.ErrRecordNotFound) || !l.IgnoreRecordNotFoundError): msg := fmt.Sprintf("[%.3fms] [rows:%d] %s | ERROR: %v %s", float64(elapsed.Nanoseconds())/1e6, rows, sql, err, Gray("(%s)", fileWithLineNum)) - r := slog.NewRecord(time.Now(), slog.LevelError, msg, 0) + r := slog.NewRecord(time.Now().UTC(), slog.LevelError, msg, 0) r.AddAttrs(slog.String("_group", "GORM")) handler.Handle(ctx, r) case elapsed > l.SlowThreshold && l.SlowThreshold != 0 && l.LogLevel >= gormlogger.Warn: msg := fmt.Sprintf("[%.3fms] [rows:%d] %s | SLOW QUERY %s", float64(elapsed.Nanoseconds())/1e6, rows, sql, Gray("(%s)", fileWithLineNum)) - r := slog.NewRecord(time.Now(), slog.LevelWarn, msg, 0) + r := slog.NewRecord(time.Now().UTC(), slog.LevelWarn, msg, 0) r.AddAttrs(slog.String("_group", "GORM")) handler.Handle(ctx, r) case l.LogLevel >= gormlogger.Info: msg := fmt.Sprintf("[%.3fms] [rows:%d] %s %s", float64(elapsed.Nanoseconds())/1e6, rows, sql, Gray("(%s)", fileWithLineNum)) - r := slog.NewRecord(time.Now(), slog.LevelDebug, msg, 0) + r := slog.NewRecord(time.Now().UTC(), slog.LevelDebug, msg, 0) r.AddAttrs(slog.String("_group", "GORM")) handler.Handle(ctx, r) } diff --git a/utils/log/log.go b/utils/log/log.go index 86ce9e7..04c3995 100644 --- a/utils/log/log.go +++ b/utils/log/log.go @@ -63,7 +63,7 @@ func (h *LogHandler) Handle(_ context.Context, r slog.Record) error { line = f.Line } - timeStr := r.Time.Format("2006/01/02 15:04:05") + timeStr := r.Time.UTC().Format("2006/01/02 15:04:05") // 检查是否有分组信息(从 attributes 中获取) group := h.group @@ -169,7 +169,7 @@ func (w *writerAdapter) Write(p []byte) (n int, err error) { var pcs [1]uintptr runtime.Callers(4, pcs[:]) // skip [Callers, Write, log.Output, log.Printf/etc] - r := slog.NewRecord(time.Now(), w.level, msg, pcs[0]) + r := slog.NewRecord(time.Now().UTC(), w.level, msg, pcs[0]) return len(p), w.handler.Handle(context.Background(), r) } diff --git a/utils/messageSender/email/email.go b/utils/messageSender/email/email.go index bb76e43..5b33616 100644 --- a/utils/messageSender/email/email.go +++ b/utils/messageSender/email/email.go @@ -178,6 +178,7 @@ func (e *EmailSender) SendTextMessage(message, title string) error { } // Compose headers + now := time.Now().UTC() headers := []string{ "To: " + strings.Join(rcptHeaderParts, ", "), "From: " + senderHeader, @@ -185,8 +186,8 @@ func (e *EmailSender) SendTextMessage(message, title string) error { "MIME-Version: 1.0", "Content-Type: " + contentType, "Content-Transfer-Encoding: quoted-printable", - "Date: " + time.Now().Format(time.RFC1123Z), - fmt.Sprintf("Message-ID: <%d@%s>", time.Now().UnixNano(), e.Addition.Host), + "Date: " + now.Format(time.RFC1123Z), + fmt.Sprintf("Message-ID: <%d@%s>", now.UnixNano(), e.Addition.Host), } fullMsg := []byte(strings.Join(headers, "\r\n") + "\r\n\r\n" + bodyBuf.String()) diff --git a/utils/messageSender/javascript/javascript.go b/utils/messageSender/javascript/javascript.go index c923353..f6dfe88 100644 --- a/utils/messageSender/javascript/javascript.go +++ b/utils/messageSender/javascript/javascript.go @@ -262,7 +262,7 @@ func (j *JavaScriptSender) fallbackToTextMessage(event models.EventMessage) erro event.Emoji, event.Emoji, event.Emoji, event.Event, event.Message, - event.Time.Format(time.RFC3339)) + event.Time.UTC().Format(time.RFC3339Nano)) // 添加客户端信息 if len(event.Clients) > 0 { @@ -279,7 +279,7 @@ func (j *JavaScriptSender) fallbackToTextMessage(event models.EventMessage) erro event.Event, clientNames, event.Message, - event.Time.Format(time.RFC3339)) + event.Time.UTC().Format(time.RFC3339Nano)) } return j.SendTextMessage(message, event.Event) diff --git a/utils/messageSender/loader_test.go b/utils/messageSender/loader_test.go index 4868c1b..af6c4c3 100644 --- a/utils/messageSender/loader_test.go +++ b/utils/messageSender/loader_test.go @@ -2,10 +2,22 @@ package messageSender import ( "testing" + "time" + "github.com/komari-monitor/komari/database/models" "github.com/komari-monitor/komari/utils/messageSender/factory" ) +func TestParseTemplateFormatsEventTimeAsUTCNanoseconds(t *testing.T) { + local := time.FixedZone("UTC+8", 8*60*60) + eventTime := time.Date(2026, 7, 17, 9, 30, 0, 123456789, local) + got := parseTemplate("{{time}}", models.EventMessage{Time: eventTime}) + want := "2026-07-17T01:30:00.123456789Z" + if got != want { + t.Fatalf("formatted event time = %q, want %q", got, want) + } +} + func Test(t *testing.T) { senders := factory.GetAllMessageSenders() if len(senders) == 0 { diff --git a/utils/messageSender/sender.go b/utils/messageSender/sender.go index 8e184ba..eb42837 100644 --- a/utils/messageSender/sender.go +++ b/utils/messageSender/sender.go @@ -107,6 +107,11 @@ func SendEvent(event models.EventMessage) error { if CurrentProvider() == nil { return fmt.Errorf("message sender provider is not initialized") } + if event.Time.IsZero() { + event.Time = time.Now().UTC() + } else { + event.Time = event.Time.UTC() + } var err error cfg, err := config.GetMany(map[string]any{ config.NotificationEnabledKey: false, @@ -165,7 +170,7 @@ func parseTemplate(messageTemplate string, event models.EventMessage) string { replaceMap := map[string]string{ "{{event}}": event.Event, "{{client}}": joinedClients, - "{{time}}": event.Time.Format(time.RFC3339), + "{{time}}": event.Time.UTC().Format(time.RFC3339Nano), "{{message}}": event.Message, "{{emoji}}": event.Emoji, } diff --git a/utils/notifier/expire.go b/utils/notifier/expire.go index d505fac..69ba771 100644 --- a/utils/notifier/expire.go +++ b/utils/notifier/expire.go @@ -2,13 +2,13 @@ package notifier import ( "fmt" - "math" "time" "github.com/komari-monitor/komari/database/clients" "github.com/komari-monitor/komari/database/models" messageevent "github.com/komari-monitor/komari/database/models/messageEvent" "github.com/komari-monitor/komari/pkg/config" + "github.com/komari-monitor/komari/pkg/timeutil" "github.com/komari-monitor/komari/utils/messageSender" "github.com/komari-monitor/komari/utils/renewal" ) @@ -31,7 +31,7 @@ func CheckExpire() { return } - checkTime := time.Now() + checkTime := time.Now().UTC() // 过期提醒检查(仅当启用过期通知时) if cfg[config.ExpireNotificationEnabledKey].(bool) { @@ -45,17 +45,19 @@ func CheckExpire() { var clientLeadToExpire []clientToExpireInfo for _, client := range clients_all { - clientExpireTime := client.ExpiredAt.ToTime() + if client.ExpiredAt == nil { + continue + } + clientExpireTime := client.ExpiredAt.UTC() if clientExpireTime.Before(checkTime) { continue } - notificationThreshold := checkTime.Add(time.Duration(notificationLeadDays) * 24 * time.Hour) + notificationThreshold := checkTime.In(time.Local).AddDate(0, 0, notificationLeadDays).UTC() if clientExpireTime.Before(notificationThreshold) || clientExpireTime.Equal(notificationThreshold) { - remainingDuration := clientExpireTime.Sub(checkTime) - daysLeft := int(math.Ceil(remainingDuration.Hours() / 24)) + daysLeft := timeutil.SystemDateDistance(checkTime, clientExpireTime) clientLeadToExpire = append(clientLeadToExpire, clientToExpireInfo{ Name: client.Name, @@ -71,7 +73,7 @@ func CheckExpire() { } messageSender.SendEvent(models.EventMessage{ Event: messageevent.Expire, - Time: time.Now(), + Time: time.Now().UTC(), Message: message, Emoji: "⏳", }) diff --git a/utils/notifier/load.go b/utils/notifier/load.go index e504c14..2848258 100644 --- a/utils/notifier/load.go +++ b/utils/notifier/load.go @@ -64,7 +64,7 @@ func executeLoadNotificationTask(task models.LoadNotification) { return } - now := time.Now() + now := time.Now().UTC() windowStart := now.Add(-time.Duration(task.Interval) * time.Minute) overloadClients := make([]string, 0) for _, clientUUID := range task.Clients { @@ -86,13 +86,13 @@ func executeLoadNotificationTask(task models.LoadNotification) { // shouldSkipNotification 检查是否应该跳过通知(冷却期检查) func shouldSkipNotification(task models.LoadNotification) bool { - if task.LastNotified.ToTime().IsZero() { + if task.LastNotified == nil || task.LastNotified.IsZero() { return false } // 计算冷却期(使用 interval 作为冷却期) cooldownPeriod := time.Duration(task.Interval) * time.Minute - timeSinceLastNotified := time.Since(task.LastNotified.ToTime()) + timeSinceLastNotified := time.Since(*task.LastNotified) return timeSinceLastNotified < cooldownPeriod } @@ -214,7 +214,7 @@ func sendLoadNotification(clientUUIDs []string, task models.LoadNotification) { messageSender.SendEvent(models.EventMessage{ Event: messageevent.Alert, Clients: ex_clients, - Time: time.Now(), + Time: time.Now().UTC(), Emoji: "⚠️", Message: task.Name, }) @@ -224,7 +224,7 @@ func sendLoadNotification(clientUUIDs []string, task models.LoadNotification) { // updateLastNotified 更新最后通知时间 func updateLastNotified(taskId uint, notifyTime time.Time) { db := dbcore.GetDBInstance() - if err := db.Model(&models.LoadNotification{}).Where("id = ?", taskId).Update("last_notified", notifyTime).Error; err != nil { + if err := db.Model(&models.LoadNotification{}).Where("id = ?", taskId).Update("last_notified", notifyTime.UTC()).Error; err != nil { log.Printf("Failed to update last_notified for task %d: %v", taskId, err) } } diff --git a/utils/notifier/offline.go b/utils/notifier/offline.go index 02d6cf1..f4e875b 100644 --- a/utils/notifier/offline.go +++ b/utils/notifier/offline.go @@ -71,7 +71,7 @@ func OfflineNotification(clientID string, endedConnectionID int64) { gracePeriod = 5 * time.Minute // 默认宽限期 } - now := time.Now() + now := time.Now().UTC() state := getOrInitState(clientID) state.mu.Lock() @@ -111,7 +111,7 @@ func OfflineNotification(clientID string, endedConnectionID int64) { if err := messageSender.SendEvent(models.EventMessage{ Event: messageevent.Offline, Clients: []models.Client{client}, - Time: time.Now(), + Time: time.Now().UTC(), //Message: msg, Emoji: "🔴", }); err != nil { @@ -121,7 +121,7 @@ func OfflineNotification(clientID string, endedConnectionID int64) { // 更新数据库中的最后通知时间 db := dbcore.GetDBInstance() - if err := db.Model(&models.OfflineNotification{}).Where("client = ?", clientID).Update("last_notified", now).Error; err != nil { + if err := db.Model(&models.OfflineNotification{}).Where("client = ?", clientID).Update("last_notified", now.UTC()).Error; err != nil { log.Printf("Failed to update last_notified for client %s: %v", clientID, err) } }(now, endedConnectionID) @@ -180,7 +180,7 @@ func OnlineNotification(clientID string, connectionID int64) { if err := messageSender.SendEvent(models.EventMessage{ Event: messageevent.Online, Clients: []models.Client{client}, - Time: time.Now(), + Time: time.Now().UTC(), //Message: msg, Emoji: "🟢", }); err != nil { diff --git a/utils/notifier/traffic.go b/utils/notifier/traffic.go index 0daf9ee..0b708b9 100644 --- a/utils/notifier/traffic.go +++ b/utils/notifier/traffic.go @@ -98,7 +98,7 @@ func CheckTraffic() { _ = messageSender.SendEvent(models.EventMessage{ Event: "Traffic", Clients: []models.Client{c}, - Time: time.Now(), + Time: time.Now().UTC(), Emoji: "⚠️", Message: msg, }) diff --git a/utils/notifier/traffic_report.go b/utils/notifier/traffic_report.go index 268bc91..4d1547b 100644 --- a/utils/notifier/traffic_report.go +++ b/utils/notifier/traffic_report.go @@ -53,44 +53,27 @@ func sendTrafficReport(daily, weekly, monthly bool) { } db := dbcore.GetDBInstance() - now := time.Now() + now := time.Now().UTC() - // 计算时间范围 - var start, end time.Time var eventType, label, suffix string switch { case daily: - yesterday := now.AddDate(0, 0, -1) - start = time.Date(yesterday.Year(), yesterday.Month(), yesterday.Day(), 0, 0, 0, 0, yesterday.Location()) - end = time.Date(yesterday.Year(), yesterday.Month(), yesterday.Day(), 23, 59, 59, 0, yesterday.Location()) eventType = messageevent.DReport label = "daily" suffix = "昨日流量" case weekly: - weekday := int(now.Weekday()) - if weekday == 0 { - weekday = 7 - } - lastMonday := now.AddDate(0, 0, -(weekday-1)-7) - lastSunday := lastMonday.AddDate(0, 0, 6) - start = time.Date(lastMonday.Year(), lastMonday.Month(), lastMonday.Day(), 0, 0, 0, 0, lastMonday.Location()) - end = time.Date(lastSunday.Year(), lastSunday.Month(), lastSunday.Day(), 23, 59, 59, 0, lastSunday.Location()) eventType = messageevent.WReport label = "weekly" suffix = "上周流量" case monthly: - firstOfThisMonth := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location()) - firstOfLastMonth := firstOfThisMonth.AddDate(0, -1, 0) - lastDayOfLastMonth := firstOfThisMonth.Add(-time.Second) - start = firstOfLastMonth - end = lastDayOfLastMonth eventType = messageevent.MReport label = "monthly" suffix = "上个月流量" default: return } + start, end := previousTrafficReportRange(now, label) // 查询所有启用该类型报告的服务器配置 var notifications []models.TrafficReportNotification @@ -170,6 +153,33 @@ func sendTrafficReport(daily, weekly, monthly bool) { } } +func previousTrafficReportRange(now time.Time, period string) (time.Time, time.Time) { + localNow := now.In(time.Local) + var startLocal, endLocal time.Time + + switch period { + case "daily": + yesterday := localNow.AddDate(0, 0, -1) + startLocal = time.Date(yesterday.Year(), yesterday.Month(), yesterday.Day(), 0, 0, 0, 0, time.Local) + endLocal = startLocal.AddDate(0, 0, 1) + case "weekly": + weekday := int(localNow.Weekday()) + if weekday == 0 { + weekday = 7 + } + lastMonday := localNow.AddDate(0, 0, -(weekday-1)-7) + startLocal = time.Date(lastMonday.Year(), lastMonday.Month(), lastMonday.Day(), 0, 0, 0, 0, time.Local) + endLocal = startLocal.AddDate(0, 0, 7) + case "monthly": + endLocal = time.Date(localNow.Year(), localNow.Month(), 1, 0, 0, 0, 0, time.Local) + startLocal = endLocal.AddDate(0, -1, 0) + default: + return time.Time{}, time.Time{} + } + + return startLocal.UTC(), endLocal.Add(-time.Nanosecond).UTC() +} + // getClientTrafficInRange 查询某客户端在指定时间段内的流量增量。 // // 历史监控数据已完全迁移到 metric store,这里从 metric store 读取区间内记录并 @@ -192,7 +202,7 @@ func getClientTrafficInRange(clientUUID string, trafficType string, start, end t }) } sort.Slice(records, func(i, j int) bool { - return records[i].Time.ToTime().Before(records[j].Time.ToTime()) + return records[i].Time.Before(records[j].Time) }) // 计算增量基线(区间开始前最后一条累计流量) @@ -214,7 +224,7 @@ func getClientTrafficInRange(clientUUID string, trafficType string, start, end t } type trafficDeltaRecord struct { - Time models.LocalTime + Time time.Time NetTotalUp int64 NetTotalDown int64 TrafficUp int64 diff --git a/utils/notifier/traffic_report_test.go b/utils/notifier/traffic_report_test.go index 6a8f78c..d9fccc8 100644 --- a/utils/notifier/traffic_report_test.go +++ b/utils/notifier/traffic_report_test.go @@ -2,10 +2,68 @@ package notifier import ( "testing" + "time" "github.com/stretchr/testify/assert" ) +func TestPreviousTrafficReportRangesUseSystemTimezone(t *testing.T) { + originalLocal := time.Local + time.Local = time.FixedZone("UTC+8", 8*60*60) + t.Cleanup(func() { time.Local = originalLocal }) + + now := time.Date(2026, 6, 30, 16, 0, 0, 0, time.UTC) // 2026-07-01 00:00 local + tests := []struct { + period string + wantStart time.Time + wantEnd time.Time + }{ + { + period: "daily", + wantStart: time.Date(2026, 6, 29, 16, 0, 0, 0, time.UTC), + wantEnd: time.Date(2026, 6, 30, 16, 0, 0, 0, time.UTC).Add(-time.Nanosecond), + }, + { + period: "weekly", + wantStart: time.Date(2026, 6, 21, 16, 0, 0, 0, time.UTC), + wantEnd: time.Date(2026, 6, 28, 16, 0, 0, 0, time.UTC).Add(-time.Nanosecond), + }, + { + period: "monthly", + wantStart: time.Date(2026, 5, 31, 16, 0, 0, 0, time.UTC), + wantEnd: time.Date(2026, 6, 30, 16, 0, 0, 0, time.UTC).Add(-time.Nanosecond), + }, + } + + for _, test := range tests { + t.Run(test.period, func(t *testing.T) { + start, end := previousTrafficReportRange(now, test.period) + if !start.Equal(test.wantStart) || !end.Equal(test.wantEnd) { + t.Fatalf("range = [%s, %s], want [%s, %s]", start, end, test.wantStart, test.wantEnd) + } + if start.Location() != time.UTC || end.Location() != time.UTC { + t.Fatalf("range locations = [%s, %s], want UTC", start.Location(), end.Location()) + } + }) + } +} + +func TestPreviousDailyTrafficReportRangeHandlesDST(t *testing.T) { + location, err := time.LoadLocation("America/New_York") + if err != nil { + t.Skipf("timezone database unavailable: %v", err) + } + originalLocal := time.Local + time.Local = location + t.Cleanup(func() { time.Local = originalLocal }) + + now := time.Date(2026, 3, 9, 4, 0, 0, 0, time.UTC) // 2026-03-09 00:00 EDT + start, end := previousTrafficReportRange(now, "daily") + if got := end.Add(time.Nanosecond).Sub(start); got != 23*time.Hour { + t.Fatalf("DST day duration = %s, want 23h", got) + } +} + func TestSumTrafficDeltasUsesStoredDeltas(t *testing.T) { records := []trafficDeltaRecord{ {TrafficUp: 30, TrafficDown: 40, NetTotalUp: 130, NetTotalDown: 240}, diff --git a/utils/renewal/renewal.go b/utils/renewal/renewal.go index 04a2ae3..e514d1a 100644 --- a/utils/renewal/renewal.go +++ b/utils/renewal/renewal.go @@ -8,6 +8,7 @@ import ( "github.com/komari-monitor/komari/database/clients" "github.com/komari-monitor/komari/database/models" messageevent "github.com/komari-monitor/komari/database/models/messageEvent" + "github.com/komari-monitor/komari/pkg/timeutil" "github.com/komari-monitor/komari/utils/messageSender" agent_runtime "github.com/komari-monitor/komari/web/agent" ) @@ -27,9 +28,12 @@ func CheckAndAutoRenewal(client models.Client) { if _, ok := agent_runtime.GetConnectedClients()[client.UUID]; !ok { return } + if client.ExpiredAt == nil { + return + } - clientExpireTime := client.ExpiredAt.ToTime() - checkTime := time.Now() + clientExpireTime := client.ExpiredAt.UTC() + checkTime := time.Now().UTC() // 如果到期时间小于0002年,跳过 if clientExpireTime.Year() < 2 { @@ -37,10 +41,11 @@ func CheckAndAutoRenewal(client models.Client) { } // 检查是否已过期或当天过期 - if clientExpireTime.Before(checkTime) || clientExpireTime.Format("2006-01-02") == checkTime.Format("2006-01-02") { + if clientExpireTime.Before(checkTime) || timeutil.SameSystemDate(clientExpireTime, checkTime) { // 计算过期时间距离创建时间的总天数,判断是否为长期账单 - now := time.Now() - hundredYearsFromNow := now.AddDate(100, 0, 0) + now := checkTime + localNow := now.In(time.Local) + hundredYearsFromNow := localNow.AddDate(100, 0, 0).UTC() // 如果过期时间超过当前时间100年,视为长期/一次性账单,不续费 if clientExpireTime.After(hundredYearsFromNow) { @@ -54,9 +59,9 @@ func CheckAndAutoRenewal(client models.Client) { billingCycle := client.BillingCycle // 如果服务器的过期时间太早了,那么直接设置为从当前时间算的下一个到期时间 - baseTime := clientExpireTime - if clientExpireTime.Before(now.AddDate(0, 0, -30)) { // 过期时间超过30天前 - baseTime = now + baseTime := clientExpireTime.In(time.Local) + if clientExpireTime.Before(localNow.AddDate(0, 0, -30).UTC()) { // 过期时间超过30天前 + baseTime = localNow } if billingCycle >= 27 && billingCycle <= 32 { @@ -88,7 +93,7 @@ func CheckAndAutoRenewal(client models.Client) { // 更新客户端过期时间 updates := map[string]interface{}{ "uuid": client.UUID, - "expired_at": models.FromTime(newExpireTime), + "expired_at": newExpireTime.UTC(), } err := clients.SaveClient(updates) @@ -103,14 +108,14 @@ func CheckAndAutoRenewal(client models.Client) { //}) auditlog.EventLog("renewal", fmt.Sprintf("Auto-renewed client: %s until %s", - client.Name, newExpireTime.Format("2006-01-02"))) + client.Name, timeutil.FormatSystemDate(newExpireTime))) messageSender.SendEvent(models.EventMessage{ Event: messageevent.Renew, Clients: []models.Client{client}, - Time: time.Now(), + Time: time.Now().UTC(), Emoji: "🔄", - Message: fmt.Sprintf("• %s until %s\n", client.Name, newExpireTime.Format("2006-01-02")), + Message: fmt.Sprintf("• %s until %s\n", client.Name, timeutil.FormatSystemDate(newExpireTime)), }) } } diff --git a/web/agent/connections.go b/web/agent/connections.go index 0474831..df4b513 100644 --- a/web/agent/connections.go +++ b/web/agent/connections.go @@ -143,6 +143,8 @@ func RecordReport(report v1.Report) { } if report.UpdatedAt.IsZero() { report.UpdatedAt = time.Now().UTC() + } else { + report.UpdatedAt = report.UpdatedAt.UTC() } mu.Lock() defer mu.Unlock() diff --git a/web/agent/v2_events.go b/web/agent/v2_events.go index 9306898..c5829af 100644 --- a/web/agent/v2_events.go +++ b/web/agent/v2_events.go @@ -75,7 +75,7 @@ func IsAgentOnline(uuid string) bool { } func EnqueueV2Event(uuid, method string, params any) v2.Event { - now := time.Now() + now := time.Now().UTC() ttl := v2EventTTL if method == v2.MethodAgentPing { ttl = v2PingEventTTL @@ -84,8 +84,8 @@ func EnqueueV2Event(uuid, method string, params any) v2.Event { ID: newV2EventID(), Method: method, Params: params, - CreatedAt: now.Format(time.RFC3339Nano), - ExpiresAt: now.Add(ttl).Format(time.RFC3339Nano), + CreatedAt: now, + ExpiresAt: now.Add(ttl), } v2EventMu.Lock() @@ -165,15 +165,14 @@ func pruneExpiredV2EventsLocked(q *v2EventQueue) { if len(q.events) == 0 { return } - now := time.Now() + now := time.Now().UTC() filtered := q.events[:0] for _, event := range q.events { - if event.ExpiresAt == "" { + if event.ExpiresAt.IsZero() { filtered = append(filtered, event) continue } - expiresAt, err := time.Parse(time.RFC3339Nano, event.ExpiresAt) - if err != nil || expiresAt.After(now) { + if event.ExpiresAt.After(now) { filtered = append(filtered, event) } } diff --git a/web/api/admin/download.go b/web/api/admin/download.go index c1d7162..666db4a 100644 --- a/web/api/admin/download.go +++ b/web/api/admin/download.go @@ -193,11 +193,12 @@ func DownloadBackup(c *gin.Context) { } // 5) 追加备份标记文件(放在 zip 根目录) - markupContent := "此文件为 Komari 备份标记文件,请勿删除。\nThis is a Komari backup markup file, please do not delete.\n\n备份时间 / Backup Time: " + time.Now().Format(time.RFC3339) + now := time.Now().UTC() + markupContent := "此文件为 Komari 备份标记文件,请勿删除。\nThis is a Komari backup markup file, please do not delete.\n\n备份时间 / Backup Time: " + now.Format(time.RFC3339Nano) markupWriter, err := zipWriter.CreateHeader(&zip.FileHeader{ Name: "komari-backup-markup", Method: zip.Deflate, - Modified: time.Now(), + Modified: now, }) if err != nil { api.RespondError(c, http.StatusInternalServerError, fmt.Sprintf("Error creating backup markup file: %v", err)) diff --git a/web/api/client/ingest.go b/web/api/client/ingest.go index eeb5cef..733911d 100644 --- a/web/api/client/ingest.go +++ b/web/api/client/ingest.go @@ -46,11 +46,11 @@ func ingestBasicInfo(uuid string, info map[string]interface{}, fallbackIP string } // ingestPingResult 保存一条 ping 探测结果。 -func ingestPingResult(uuid string, taskID uint, value int, finishedAt time.Time) { - tasks.SavePingRecord(models.PingRecord{ +func ingestPingResult(uuid string, taskID uint, value int) error { + return tasks.SavePingRecord(models.PingRecord{ Client: uuid, TaskId: taskID, Value: value, - Time: models.FromTime(finishedAt), + Time: time.Now().UTC(), }) } diff --git a/web/api/client/report.go b/web/api/client/report.go index 79011cf..f59a233 100644 --- a/web/api/client/report.go +++ b/web/api/client/report.go @@ -245,17 +245,18 @@ func processMessage(conn *connection.SafeConn, message []byte, uuid string) { } case "ping_result": var reqBody struct { - PingTaskID uint `json:"task_id"` - PingResult int `json:"value"` - PingType string `json:"ping_type"` - FinishedAt time.Time `json:"finished_at"` + PingTaskID uint `json:"task_id"` + PingResult int `json:"value"` + PingType string `json:"ping_type"` } err = json.Unmarshal(message, &reqBody) if err != nil { conn.WriteJSON(gin.H{"status": "error", "error": "Invalid ping result format"}) return } - ingestPingResult(uuid, reqBody.PingTaskID, reqBody.PingResult, reqBody.FinishedAt) + if err := ingestPingResult(uuid, reqBody.PingTaskID, reqBody.PingResult); err != nil { + conn.WriteJSON(gin.H{"status": "error", "error": "Failed to save ping result"}) + } default: log.Printf("Unknown message type: %s", msgType.Type) conn.WriteJSON(gin.H{"status": "error", "error": "Unknown message type"}) diff --git a/web/api/client/report_v2.go b/web/api/client/report_v2.go index 84d48f9..72dc6c3 100644 --- a/web/api/client/report_v2.go +++ b/web/api/client/report_v2.go @@ -72,13 +72,9 @@ func handleV2RPC(uuid string, req v2.Request, allowWait bool) v2.Response { if err := bindV2Params(req.Params, ¶ms); err != nil { return v2.Error(req.ID, -32602, "invalid ping result params", err.Error()) } - finishedAt := time.Now() - if params.FinishedAt != "" { - if t, err := time.Parse(time.RFC3339Nano, params.FinishedAt); err == nil { - finishedAt = t - } + if err := ingestPingResult(uuid, params.TaskID, params.Value); err != nil { + return v2.Error(req.ID, -32000, "failed to save ping result", err.Error()) } - ingestPingResult(uuid, params.TaskID, params.Value, finishedAt) return v2.Success(req.ID, gin.H{"status": "success"}) case v2.MethodAgentPull: var params v2.PullParams diff --git a/web/api/client/uploadBasicInfo_test.go b/web/api/client/uploadBasicInfo_test.go index 527e77c..3cbede9 100644 --- a/web/api/client/uploadBasicInfo_test.go +++ b/web/api/client/uploadBasicInfo_test.go @@ -50,7 +50,7 @@ func TestV2BasicInfoFillsRegionFromGeoIP(t *testing.T) { }) clientUUID := "client-v2-geoip" - now := models.FromTime(time.Now()) + now := time.Now().UTC() if err := db.Create(&models.Client{ UUID: clientUUID, Token: "token-v2-geoip", diff --git a/web/api/public/mjpeg.go b/web/api/public/mjpeg.go index a596e56..1828a87 100644 --- a/web/api/public/mjpeg.go +++ b/web/api/public/mjpeg.go @@ -298,7 +298,7 @@ func downloadFont() { return } downloading = true - downloadProgress = &DownloadProgress{StartTime: time.Now()} + downloadProgress = &DownloadProgress{StartTime: time.Now().UTC()} downloadMutex.Unlock() defer func() { @@ -780,7 +780,7 @@ func renderStatusTable(ctx context.Context, lang string, tzOffset *int, fontErr // 页脚 y += 10 - now := formatTimeWithOffset(time.Now(), tzOffset) + now := formatTimeWithOffset(time.Now().UTC(), tzOffset) drawCenteredString(img, lp.LastUpdate+now, imageWidth/2, y+footerFontSize, color.Gray{100}, normalFace) y += 25 @@ -944,7 +944,7 @@ func renderStatusTableWithBasicFont(ctx context.Context, lang string, tzOffset * // 页脚 y += 10 - now := formatTimeWithOffset(time.Now(), tzOffset) + now := formatTimeWithOffset(time.Now().UTC(), tzOffset) drawCenteredStringBasic(img, lp.LastUpdate+now, imageWidth/2, y+footerFontSize, color.Gray{100}) y += 25 @@ -1059,6 +1059,10 @@ func fetchNodeData(ctx context.Context) ([]NodeInfo, map[string]NodeStatus, erro } func nodeInfoFromClient(client models.Client) NodeInfo { + var expiredAt time.Time + if client.ExpiredAt != nil { + expiredAt = client.ExpiredAt.UTC() + } return NodeInfo{ UUID: client.UUID, Name: client.Name, @@ -1069,7 +1073,7 @@ func nodeInfoFromClient(client models.Client) NodeInfo { Price: client.Price, BillingCycle: client.BillingCycle, Currency: client.Currency, - ExpiredAt: client.ExpiredAt.ToTime(), + ExpiredAt: expiredAt, AutoRenewal: client.AutoRenewal, Hidden: client.Hidden, Weight: client.Weight, @@ -1122,11 +1126,12 @@ func parseNodeInfo(data map[string]interface{}) NodeInfo { if v, ok := data["disk_total"].(float64); ok { node.DiskTotal = int64(v) } - // 解析 expired_at if v, ok := data["expired_at"].(string); ok { - if t, err := time.Parse(time.RFC3339, v); err == nil { + if t, err := time.Parse(time.RFC3339Nano, v); err == nil { node.ExpiredAt = t } + } else if v, ok := data["expired_at"].(time.Time); ok { + node.ExpiredAt = v.UTC() } return node } @@ -1395,7 +1400,7 @@ func formatPriceL(price float64, cycle int, currency string, lp langPack) string } func formatRemainingL(expiredAt time.Time, autoRenewal bool, lp langPack) string { - now := time.Now() + now := time.Now().UTC() if expiredAt.IsZero() || expiredAt.Year() > 2200 { return lp.LongTerm } diff --git a/web/rpc/jsonrpc/admin.metric.go b/web/rpc/jsonrpc/admin.metric.go index d98f164..57f71c9 100644 --- a/web/rpc/jsonrpc/admin.metric.go +++ b/web/rpc/jsonrpc/admin.metric.go @@ -5,6 +5,7 @@ import ( "encoding/json" "errors" "strings" + "time" "github.com/komari-monitor/komari/database/auditlog" "github.com/komari-monitor/komari/database/metricstore" @@ -42,8 +43,8 @@ type metricDefinitionResponse struct { Unit string `json:"unit,omitempty"` RetentionDays int `json:"retention_days"` Metadata map[string]string `json:"metadata,omitempty"` - CreatedAt any `json:"created_at,omitempty"` - UpdatedAt any `json:"updated_at,omitempty"` + CreatedAt time.Time `json:"created_at"` + UpdatedAt time.Time `json:"updated_at"` } func metricDescriptionValue(raw string) any { diff --git a/web/rpc/jsonrpc/admin.system.go b/web/rpc/jsonrpc/admin.system.go index da2ce7e..44264d6 100644 --- a/web/rpc/jsonrpc/admin.system.go +++ b/web/rpc/jsonrpc/admin.system.go @@ -155,7 +155,7 @@ func adminExec(ctx context.Context, req *rpc.JsonRpcRequest) (any, *rpc.JsonRpcE auditlog.Log(ip, actor, "REC, task id: "+taskId, "warn") if len(offlineClients) > 0 { for _, uuid := range offlineClients { - tasks.SaveTaskResult(taskId, uuid, "Client offline!", -1, models.FromTime(time.Now())) + tasks.SaveTaskResult(taskId, uuid, "Client offline!", -1, time.Now().UTC()) } } return map[string]any{ @@ -168,6 +168,7 @@ func adminExec(ctx context.Context, req *rpc.JsonRpcRequest) (any, *rpc.JsonRpcE func adminTestSendMessage(_ context.Context, _ *rpc.JsonRpcRequest) (any, *rpc.JsonRpcError) { err := messageSender.SendEvent(models.EventMessage{ Event: "Test", + Time: time.Now().UTC(), Message: "This is a test message from Komari.", }) if err != nil { diff --git a/web/rpc/jsonrpc/client.go b/web/rpc/jsonrpc/client.go index 146fbec..f0b6db2 100644 --- a/web/rpc/jsonrpc/client.go +++ b/web/rpc/jsonrpc/client.go @@ -45,10 +45,9 @@ func clientUploadPingResult(ctx context.Context, req *rpc.JsonRpcRequest) (any, return nil, rpc.MakeError(rpc.InvalidParams, "client_uuid not found", nil) } var params struct { - TaskID uint `json:"task_id"` - Value int `json:"value"` - PingType string `json:"ping_type"` - FinishedAt time.Time `json:"finished_at"` + TaskID uint `json:"task_id"` + Value int `json:"value"` + PingType string `json:"ping_type"` } if err := req.BindParams(¶ms); err != nil { return nil, rpc.MakeError(rpc.InvalidParams, "Invalid request: "+err.Error(), nil) @@ -57,7 +56,7 @@ func clientUploadPingResult(ctx context.Context, req *rpc.JsonRpcRequest) (any, Client: uuid, TaskId: params.TaskID, Value: params.Value, - Time: models.FromTime(params.FinishedAt), + Time: time.Now().UTC(), } if err := tasks.SavePingRecord(record); err != nil { return nil, rpc.MakeError(rpc.InternalError, "Failed to save ping result: "+err.Error(), nil) @@ -71,15 +70,14 @@ func clientTaskResult(ctx context.Context, req *rpc.JsonRpcRequest) (any, *rpc.J return nil, rpc.MakeError(rpc.InvalidParams, "Invalid or missing token", nil) } var params struct { - TaskId string `json:"task_id"` - Result string `json:"result"` - ExitCode int `json:"exit_code"` - FinishedAt time.Time `json:"finished_at"` + TaskId string `json:"task_id"` + Result string `json:"result"` + ExitCode int `json:"exit_code"` } if err := req.BindParams(¶ms); err != nil { return nil, rpc.MakeError(rpc.InvalidParams, "Invalid request", nil) } - if err := tasks.SaveTaskResult(params.TaskId, uuid, params.Result, params.ExitCode, models.FromTime(params.FinishedAt)); err != nil { + if err := tasks.SaveTaskResult(params.TaskId, uuid, params.Result, params.ExitCode, time.Now().UTC()); err != nil { return nil, rpc.MakeError(rpc.InternalError, "Failed to update task result: "+err.Error(), nil) } return map[string]any{"status": "success", "message": "Task result updated successfully"}, nil diff --git a/web/rpc/jsonrpc/common.go b/web/rpc/jsonrpc/common.go index 0ad3c55..596000f 100644 --- a/web/rpc/jsonrpc/common.go +++ b/web/rpc/jsonrpc/common.go @@ -58,7 +58,7 @@ func getPingStatsForNode(uuid string, pingTasks []models.PingTask) map[string]pi pingStatsCache.Set(key, empty, cache.DefaultExpiration) return empty } - end := time.Now() + end := time.Now().UTC() start := end.Add(-1 * time.Hour) recs, err := tasks.GetPingRecords(uuid, -1, start, end) if err != nil || len(recs) == 0 { @@ -105,7 +105,7 @@ func getPingStatsForNode(uuid string, pingTasks []models.PingTask) map[string]pi if r.Value > maxLat { maxLat = r.Value } - ts := r.Time.ToTime() + ts := r.Time if latestTs.IsZero() || ts.After(latestTs) { latestTs = ts latest = r.Value @@ -320,7 +320,7 @@ func getNodesLatestStatus(ctx context.Context, req *rpc.JsonRpcRequest) (any, *r type recordLike struct { Client string `json:"client"` - Time models.LocalTime `json:"time"` + Time time.Time `json:"time"` Cpu float32 `json:"cpu"` Gpu float32 `json:"gpu"` Ram int64 `json:"ram"` @@ -357,7 +357,7 @@ func getNodesLatestStatus(ctx context.Context, req *rpc.JsonRpcRequest) (any, *r stats := getPingStatsForNode(uuid, pingTasks) rl := recordLike{ Client: uuid, - Time: models.FromTime(rep.UpdatedAt), + Time: rep.UpdatedAt, Cpu: float32(rep.CPU.Usage), Gpu: 0, Ram: rep.Ram.Used, @@ -497,25 +497,25 @@ func getNodeRecentStatus(ctx context.Context, req *rpc.JsonRpcRequest) (any, *rp // 扁平化为 { count, records: [] } type flatRecord struct { - Client string `json:"client"` - Time models.LocalTime `json:"time"` - Cpu float32 `json:"cpu"` - Gpu float32 `json:"gpu"` - Ram int64 `json:"ram"` - RamTotal int64 `json:"ram_total"` - Swap int64 `json:"swap"` - SwapTotal int64 `json:"swap_total"` - Load float32 `json:"load"` - Temp float32 `json:"temp"` - Disk int64 `json:"disk"` - DiskTotal int64 `json:"disk_total"` - NetIn int64 `json:"net_in"` - NetOut int64 `json:"net_out"` - NetTotalUp int64 `json:"net_total_up"` - NetTotalDown int64 `json:"net_total_down"` - Process int `json:"process"` - Connections int `json:"connections"` - ConnectionsUdp int `json:"connections_udp"` + Client string `json:"client"` + Time time.Time `json:"time"` + Cpu float32 `json:"cpu"` + Gpu float32 `json:"gpu"` + Ram int64 `json:"ram"` + RamTotal int64 `json:"ram_total"` + Swap int64 `json:"swap"` + SwapTotal int64 `json:"swap_total"` + Load float32 `json:"load"` + Temp float32 `json:"temp"` + Disk int64 `json:"disk"` + DiskTotal int64 `json:"disk_total"` + NetIn int64 `json:"net_in"` + NetOut int64 `json:"net_out"` + NetTotalUp int64 `json:"net_total_up"` + NetTotalDown int64 `json:"net_total_down"` + Process int `json:"process"` + Connections int `json:"connections"` + ConnectionsUdp int `json:"connections_udp"` } resp := struct { @@ -534,7 +534,7 @@ func getNodeRecentStatus(ctx context.Context, req *rpc.JsonRpcRequest) (any, *rp for _, r := range reports { fr := flatRecord{ Client: params.UUID, - Time: models.FromTime(r.UpdatedAt), + Time: r.UpdatedAt, Cpu: float32(r.CPU.Usage), Gpu: 0, Ram: r.Ram.Used, diff --git a/web/rpc/jsonrpc/common.record.go b/web/rpc/jsonrpc/common.record.go index e313b67..6094a02 100644 --- a/web/rpc/jsonrpc/common.record.go +++ b/web/rpc/jsonrpc/common.record.go @@ -20,16 +20,18 @@ func init() { func getRecords(ctx context.Context, req *rpc.JsonRpcRequest) (any, *rpc.JsonRpcError) { meta := rpc.MetaFromContext(ctx) var params struct { - Type string `json:"type"` // "load" | "ping"; default "load" - UUID string `json:"uuid"` // client uuid; empty = all clients - Hours int `json:"hours"` // time window in hours; default 1 if start/end not provided - Start string `json:"start"` // RFC3339 start time (optional) - End string `json:"end"` // RFC3339 end time (optional) - LoadType string `json:"load_type"` // for type=load: cpu|gpu|ram|swap|load|temp|disk|network|process|connections|all - TaskID int `json:"task_id"` // for type=ping: optional task id; -1 or omitted means all - MaxCount int `json:"maxCount"` // max number of points; -1 unlimited; default 4000 + Type string `json:"type"` // "load" | "ping"; default "load" + UUID string `json:"uuid"` // client uuid; empty = all clients + Hours int `json:"hours"` // time window in hours; default 1 if start/end not provided + Start *time.Time `json:"start"` // RFC3339 with an explicit timezone (optional) + End *time.Time `json:"end"` // RFC3339 with an explicit timezone (optional) + LoadType string `json:"load_type"` // for type=load: cpu|gpu|ram|swap|load|temp|disk|network|process|connections|all + TaskID int `json:"task_id"` // for type=ping: optional task id; -1 or omitted means all + MaxCount int `json:"maxCount"` // max number of points; -1 unlimited; default 4000 + } + if err := req.BindParams(¶ms); err != nil { + return nil, rpc.MakeError(rpc.InvalidParams, "Invalid request body: "+err.Error(), nil) } - req.BindParams(¶ms) // defaults if params.Type == "" { @@ -37,33 +39,25 @@ func getRecords(ctx context.Context, req *rpc.JsonRpcRequest) (any, *rpc.JsonRpc } // parse time window var startTime, endTime time.Time - if params.Start != "" || params.End != "" { + if params.Start != nil || params.End != nil { // allow partial: missing end means now - var err error - if params.End == "" { - endTime = time.Now() + if params.End == nil { + endTime = time.Now().UTC() } else { - endTime, err = time.Parse(time.RFC3339, params.End) - if err != nil { - return nil, rpc.MakeError(rpc.InvalidParams, "Invalid end time", params.End) - } + endTime = params.End.UTC() } - if params.Start == "" { + if params.Start == nil { // default to 1 hour before end startTime = endTime.Add(-1 * time.Hour) } else { - start, err := time.Parse(time.RFC3339, params.Start) - if err != nil { - return nil, rpc.MakeError(rpc.InvalidParams, "Invalid start time", params.Start) - } - startTime = start + startTime = params.Start.UTC() } } else { hours := params.Hours if hours <= 0 { hours = 1 // default 1 hour } - endTime = time.Now() + endTime = time.Now().UTC() startTime = endTime.Add(-time.Duration(hours) * time.Hour) } @@ -122,7 +116,7 @@ func getRecords(ctx context.Context, req *rpc.JsonRpcRequest) (any, *rpc.JsonRpc groupsMeta := make([]allocationGroup[string], 0, len(grouped)) for name := range grouped { arr := grouped[name] - sort.Slice(arr, func(i, j int) bool { return arr[i].Time.ToTime().Before(arr[j].Time.ToTime()) }) + sort.Slice(arr, func(i, j int) bool { return arr[i].Time.Before(arr[j].Time) }) grouped[name] = arr l := len(arr) total += l @@ -141,9 +135,9 @@ func getRecords(ctx context.Context, req *rpc.JsonRpcRequest) (any, *rpc.JsonRpc Count int `json:"count"` Records map[string][]flatRecord `json:"records"` LoadType string `json:"load_type"` - From models.LocalTime `json:"from"` - To models.LocalTime `json:"to"` - }{Count: total, Records: grouped, LoadType: params.LoadType, From: models.FromTime(startTime), To: models.FromTime(endTime)}, nil + From time.Time `json:"from"` + To time.Time `json:"to"` + }{Count: total, Records: grouped, LoadType: params.LoadType, From: startTime.UTC(), To: endTime.UTC()}, nil } // default: return full records, grouped by client grouped := make(map[string][]models.Record) @@ -154,7 +148,7 @@ func getRecords(ctx context.Context, req *rpc.JsonRpcRequest) (any, *rpc.JsonRpc groupsMeta := make([]allocationGroup[string], 0, len(grouped)) for name := range grouped { arr := grouped[name] - sort.Slice(arr, func(i, j int) bool { return arr[i].Time.ToTime().Before(arr[j].Time.ToTime()) }) + sort.Slice(arr, func(i, j int) bool { return arr[i].Time.Before(arr[j].Time) }) grouped[name] = arr l := len(arr) total += l @@ -171,9 +165,9 @@ func getRecords(ctx context.Context, req *rpc.JsonRpcRequest) (any, *rpc.JsonRpc return struct { Count int `json:"count"` Records map[string][]models.Record `json:"records"` - From models.LocalTime `json:"from"` - To models.LocalTime `json:"to"` - }{Count: total, Records: grouped, From: models.FromTime(startTime), To: models.FromTime(endTime)}, nil + From time.Time `json:"from"` + To time.Time `json:"to"` + }{Count: total, Records: grouped, From: startTime.UTC(), To: endTime.UTC()}, nil case "ping": taskId := params.TaskID @@ -197,10 +191,10 @@ func getRecords(ctx context.Context, req *rpc.JsonRpcRequest) (any, *rpc.JsonRpc } type RecordsResp struct { - TaskId uint `json:"task_id,omitempty"` - Time models.LocalTime `json:"time"` - Value int `json:"value"` - Client string `json:"client,omitempty"` + TaskId uint `json:"task_id,omitempty"` + Time time.Time `json:"time"` + Value int `json:"value"` + Client string `json:"client,omitempty"` } type ClientBasicInfo struct { Client string `json:"client"` @@ -213,11 +207,11 @@ func getRecords(ctx context.Context, req *rpc.JsonRpcRequest) (any, *rpc.JsonRpc BasicInfo []ClientBasicInfo `json:"basic_info,omitempty"` Records []RecordsResp `json:"records"` Tasks []map[string]any `json:"tasks"` - From models.LocalTime `json:"from"` - To models.LocalTime `json:"to"` + From time.Time `json:"from"` + To time.Time `json:"to"` } - response := &Resp{Count: 0, Records: []RecordsResp{}, From: models.FromTime(startTime), To: models.FromTime(endTime)} + response := &Resp{Count: 0, Records: []RecordsResp{}, From: startTime.UTC(), To: endTime.UTC()} // stats per client clientStats := make(map[string]struct { @@ -316,7 +310,7 @@ func getRecords(ctx context.Context, req *rpc.JsonRpcRequest) (any, *rpc.JsonRpc maxLat = r.Value } // track latest non-negative value - ts := r.Time.ToTime() + ts := r.Time if latestTs.IsZero() || ts.After(latestTs) { latestTs = ts latestVal = r.Value @@ -402,7 +396,7 @@ func getRecords(ctx context.Context, req *rpc.JsonRpcRequest) (any, *rpc.JsonRpc // sort each group by time for taskId := range taskGroups { sort.Slice(taskGroups[taskId], func(i, j int) bool { - return taskGroups[taskId][i].Time.ToTime().Before(taskGroups[taskId][j].Time.ToTime()) + return taskGroups[taskId][i].Time.Before(taskGroups[taskId][j].Time) }) } @@ -428,7 +422,7 @@ func getRecords(ctx context.Context, req *rpc.JsonRpcRequest) (any, *rpc.JsonRpc response.Count = len(response.Records) // sort by time asc sort.Slice(response.Records, func(i, j int) bool { - return response.Records[i].Time.ToTime().Before(response.Records[j].Time.ToTime()) + return response.Records[i].Time.Before(response.Records[j].Time) }) return response, nil default: @@ -588,26 +582,26 @@ func sampleEvenly[T any](in []T, k int) []T { // flatRecord is a projection used when load_type is specified. type flatRecord struct { - Client string `json:"client"` - Time models.LocalTime `json:"time"` - Cpu *float32 `json:"cpu,omitempty"` - Gpu *float32 `json:"gpu,omitempty"` - Ram *int64 `json:"ram,omitempty"` - RamTotal *int64 `json:"ram_total,omitempty"` - Swap *int64 `json:"swap,omitempty"` - SwapTotal *int64 `json:"swap_total,omitempty"` - Load *float32 `json:"load,omitempty"` - Temp *float32 `json:"temp,omitempty"` - Disk *int64 `json:"disk,omitempty"` - DiskTotal *int64 `json:"disk_total,omitempty"` - NetIn *int64 `json:"net_in,omitempty"` - NetOut *int64 `json:"net_out,omitempty"` - NetTotalUp *int64 `json:"net_total_up,omitempty"` - NetTotalDown *int64 `json:"net_total_down,omitempty"` - Process *int `json:"process,omitempty"` - Connections *int `json:"connections,omitempty"` - ConnectionsUdp *int `json:"connections_udp,omitempty"` - Uptime *int64 `json:"uptime,omitempty"` + Client string `json:"client"` + Time time.Time `json:"time"` + Cpu *float32 `json:"cpu,omitempty"` + Gpu *float32 `json:"gpu,omitempty"` + Ram *int64 `json:"ram,omitempty"` + RamTotal *int64 `json:"ram_total,omitempty"` + Swap *int64 `json:"swap,omitempty"` + SwapTotal *int64 `json:"swap_total,omitempty"` + Load *float32 `json:"load,omitempty"` + Temp *float32 `json:"temp,omitempty"` + Disk *int64 `json:"disk,omitempty"` + DiskTotal *int64 `json:"disk_total,omitempty"` + NetIn *int64 `json:"net_in,omitempty"` + NetOut *int64 `json:"net_out,omitempty"` + NetTotalUp *int64 `json:"net_total_up,omitempty"` + NetTotalDown *int64 `json:"net_total_down,omitempty"` + Process *int `json:"process,omitempty"` + Connections *int `json:"connections,omitempty"` + ConnectionsUdp *int `json:"connections_udp,omitempty"` + Uptime *int64 `json:"uptime,omitempty"` } func filterRecordsByLoadType(recs []models.Record, loadType string) []flatRecord { diff --git a/web/rpc/jsonrpc/public.go b/web/rpc/jsonrpc/public.go index 9455099..ecba553 100644 --- a/web/rpc/jsonrpc/public.go +++ b/web/rpc/jsonrpc/public.go @@ -161,7 +161,8 @@ func publicGetRecordsByUUID(ctx context.Context, req *rpc.JsonRpcRequest) (any, if !validLoadTypes[params.LoadType] { return nil, rpc.MakeError(rpc.InvalidParams, "Invalid load_type parameter", nil) } - clientRecords, err := records.GetRecordsByClientAndTime(params.UUID, time.Now().Add(-time.Duration(hoursInt)*time.Hour), time.Now()) + now := time.Now().UTC() + clientRecords, err := records.GetRecordsByClientAndTime(params.UUID, now.Add(-time.Duration(hoursInt)*time.Hour), now) if err != nil { return nil, rpc.MakeError(rpc.InternalError, "Failed to fetch records: "+err.Error(), nil) } @@ -178,7 +179,7 @@ func publicGetRecordsByUUID(ctx context.Context, req *rpc.JsonRpcRequest) (any, } } if params.LoadType == "" || params.LoadType == "all" || params.LoadType == "gpu" { - gpuRecords, err := records.GetGPURecordsByClientAndTime(params.UUID, time.Now().Add(-time.Duration(hoursInt)*time.Hour), time.Now()) + gpuRecords, err := records.GetGPURecordsByClientAndTime(params.UUID, now.Add(-time.Duration(hoursInt)*time.Hour), now) if err == nil && len(gpuRecords) > 0 { gpuDevices := make(map[string]any) for _, record := range gpuRecords { @@ -295,10 +296,10 @@ func publicGetPingRecords(ctx context.Context, req *rpc.JsonRpcRequest) (any, *r isLogin := isLoginFromCtx(ctx) type recordsResp struct { - TaskId uint `json:"task_id,omitempty"` - Time string `json:"time"` - Value int `json:"value"` - Client string `json:"client,omitempty"` + TaskId uint `json:"task_id,omitempty"` + Time time.Time `json:"time"` + Value int `json:"value"` + Client string `json:"client,omitempty"` } type clientBasicInfo struct { Client string `json:"client"` @@ -336,7 +337,7 @@ func publicGetPingRecords(ctx context.Context, req *rpc.JsonRpcRequest) (any, *r if err != nil { hoursInt = 4 } - endTime := time.Now() + endTime := time.Now().UTC() startTime := endTime.Add(-time.Duration(hoursInt) * time.Hour) taskId := -1 @@ -359,7 +360,7 @@ func publicGetPingRecords(ctx context.Context, req *rpc.JsonRpcRequest) (any, *r if r.Client != "" && !isLogin && hiddenMap[r.Client] { continue } - rec := recordsResp{Time: r.Time.ToTime().Format(time.RFC3339), Value: r.Value, Client: r.Client, TaskId: r.TaskId} + rec := recordsResp{Time: r.Time.UTC(), Value: r.Value, Client: r.Client, TaskId: r.TaskId} stats := clientStats[r.Client] stats.total++ if r.Value < 0 { diff --git a/web/rpc/jsonrpc/public.metric.go b/web/rpc/jsonrpc/public.metric.go index 93fed34..d627197 100644 --- a/web/rpc/jsonrpc/public.metric.go +++ b/web/rpc/jsonrpc/public.metric.go @@ -33,11 +33,11 @@ type publicMetricQueryParams struct { EntityID string `json:"entity_id"` EntityIDs []string `json:"entity_ids"` - Start any `json:"start"` - StartTime any `json:"start_time"` - End any `json:"end"` - EndTime any `json:"end_time"` - Hours any `json:"hours"` + Start *time.Time `json:"start"` + StartTime *time.Time `json:"start_time"` + End *time.Time `json:"end"` + EndTime *time.Time `json:"end_time"` + Hours float64 `json:"hours"` Tags map[string]string `json:"tags"` @@ -61,7 +61,7 @@ type publicMetricQueryParams struct { } type publicMetricPoint struct { - Time string `json:"time"` + Time time.Time `json:"time"` Value *float64 `json:"value"` Count int `json:"count,omitempty"` Tags map[string]string `json:"tags,omitempty"` @@ -92,11 +92,11 @@ type publicPingMetricStatsParams struct { TaskID any `json:"task_id"` TaskIDs []any `json:"task_ids"` - Start any `json:"start"` - StartTime any `json:"start_time"` - End any `json:"end"` - EndTime any `json:"end_time"` - Hours any `json:"hours"` + Start *time.Time `json:"start"` + StartTime *time.Time `json:"start_time"` + End *time.Time `json:"end"` + EndTime *time.Time `json:"end_time"` + Hours float64 `json:"hours"` MaxPoints int `json:"max_points"` DownsamplePoints int `json:"downsample_points"` @@ -124,8 +124,8 @@ type publicPingMetricTaskStats struct { } type publicPingMetricStatsResponse struct { - Start string `json:"start"` - End string `json:"end"` + Start time.Time `json:"start"` + End time.Time `json:"end"` IntervalSeconds float64 `json:"interval_seconds,omitempty"` Stats []publicPingMetricTaskStats `json:"stats"` Count int `json:"count"` @@ -167,15 +167,9 @@ func publicQueryMetrics(ctx context.Context, req *rpc.JsonRpcRequest) (any, *rpc return nil, rpc.MakeError(rpc.InvalidParams, "metric_keys is required", nil) } - end, err := parseMetricQueryTimeOrDefault(firstNonNil(params.End, params.EndTime), time.Now().UTC()) - if err != nil { - return nil, rpc.MakeError(rpc.InvalidParams, "Invalid end time: "+err.Error(), nil) - } + end := metricQueryTimeOrDefault(firstMetricQueryTime(params.End, params.EndTime), time.Now().UTC()) startFallback := end.Add(-metricQueryHours(params.Hours)) - start, err := parseMetricQueryTimeOrDefault(firstNonNil(params.Start, params.StartTime), startFallback) - if err != nil { - return nil, rpc.MakeError(rpc.InvalidParams, "Invalid start time: "+err.Error(), nil) - } + start := metricQueryTimeOrDefault(firstMetricQueryTime(params.Start, params.StartTime), startFallback) if !end.After(start) { return nil, rpc.MakeError(rpc.InvalidParams, "end must be after start", nil) } @@ -243,7 +237,7 @@ func publicQueryMetrics(ctx context.Context, req *rpc.JsonRpcRequest) (any, *rpc if metricDownsample { item.DownsampleAlgorithm = string(algorithm) - now := time.Now() + now := time.Now().UTC() interval := metricDownsampleInterval(end.Sub(start), maxPoints) interval = store.CompatibleSeriesInterval(start, now, interval) item.IntervalSeconds = interval.Seconds() @@ -259,7 +253,7 @@ func publicQueryMetrics(ctx context.Context, req *rpc.JsonRpcRequest) (any, *rpc item.Points = make([]publicMetricPoint, 0, len(points)) for _, point := range points { item.Points = append(item.Points, publicMetricPoint{ - Time: point.Bucket.Format(time.RFC3339Nano), + Time: point.Bucket.UTC(), Value: publicRawMetricValue(point.MetricName, point.Value, metricFillEmpty), Count: point.Count, Tags: point.Tags, @@ -273,7 +267,7 @@ func publicQueryMetrics(ctx context.Context, req *rpc.JsonRpcRequest) (any, *rpc item.Points = make([]publicMetricPoint, 0, len(points)) for _, point := range points { item.Points = append(item.Points, publicMetricPoint{ - Time: point.Timestamp.Format(time.RFC3339Nano), + Time: point.Timestamp.UTC(), Value: publicRawMetricValue(metricKey, point.Value, metricFillEmpty), Tags: point.Tags, Labels: point.Labels, @@ -290,8 +284,8 @@ func publicQueryMetrics(ctx context.Context, req *rpc.JsonRpcRequest) (any, *rpc } return map[string]any{ - "start": start.Format(time.RFC3339Nano), - "end": end.Format(time.RFC3339Nano), + "start": start.UTC(), + "end": end.UTC(), "server_downsample_default": downsample, "default_points": defaultMetricQueryPoints, "series": series, @@ -305,15 +299,9 @@ func publicGetPingMetricStats(ctx context.Context, req *rpc.JsonRpcRequest) (any return nil, rpc.MakeError(rpc.InvalidParams, "Invalid request body: "+err.Error(), nil) } - end, err := parseMetricQueryTimeOrDefault(firstNonNil(params.End, params.EndTime), time.Now().UTC()) - if err != nil { - return nil, rpc.MakeError(rpc.InvalidParams, "Invalid end time: "+err.Error(), nil) - } + end := metricQueryTimeOrDefault(firstMetricQueryTime(params.End, params.EndTime), time.Now().UTC()) startFallback := end.Add(-metricQueryHours(params.Hours)) - start, err := parseMetricQueryTimeOrDefault(firstNonNil(params.Start, params.StartTime), startFallback) - if err != nil { - return nil, rpc.MakeError(rpc.InvalidParams, "Invalid start time: "+err.Error(), nil) - } + start := metricQueryTimeOrDefault(firstMetricQueryTime(params.Start, params.StartTime), startFallback) if !end.After(start) { return nil, rpc.MakeError(rpc.InvalidParams, "end must be after start", nil) } @@ -325,8 +313,8 @@ func publicGetPingMetricStats(ctx context.Context, req *rpc.JsonRpcRequest) (any } if len(entityIDs) == 0 { return publicPingMetricStatsResponse{ - Start: start.Format(time.RFC3339Nano), - End: end.Format(time.RFC3339Nano), + Start: start.UTC(), + End: end.UTC(), Stats: []publicPingMetricTaskStats{}, Count: 0, }, nil @@ -354,7 +342,7 @@ func publicGetPingMetricStats(ctx context.Context, req *rpc.JsonRpcRequest) (any if maxPoints <= 0 { maxPoints = defaultMetricQueryPoints } - now := time.Now() + now := time.Now().UTC() interval := metricDownsampleInterval(end.Sub(start), maxPoints) interval = store.CompatibleSeriesInterval(start, now, interval) @@ -376,8 +364,8 @@ func publicGetPingMetricStats(ctx context.Context, req *rpc.JsonRpcRequest) (any }) return publicPingMetricStatsResponse{ - Start: start.Format(time.RFC3339Nano), - End: end.Format(time.RFC3339Nano), + Start: start.UTC(), + End: end.UTC(), IntervalSeconds: interval.Seconds(), Stats: stats, Count: len(stats), @@ -448,13 +436,9 @@ func adaptiveFillPublicMetricSeries(series publicMetricSeries, start, end time.T pointTimes := make([]time.Time, len(series.Points)) deltas := make([]time.Duration, 0, len(series.Points)) for i, point := range series.Points { - parsed, err := time.Parse(time.RFC3339Nano, point.Time) - if err != nil { - return series - } - pointTimes[i] = parsed + pointTimes[i] = point.Time if i > 0 { - delta := parsed.Sub(pointTimes[i-1]) + delta := point.Time.Sub(pointTimes[i-1]) if delta > 0 { deltas = append(deltas, delta) } @@ -478,7 +462,7 @@ func adaptiveFillPublicMetricSeries(series publicMetricSeries, start, end time.T nullPoint := func(at time.Time) publicMetricPoint { return publicMetricPoint{ - Time: at.UTC().Format(time.RFC3339Nano), + Time: at.UTC(), Value: nil, Tags: series.Tags, } @@ -587,7 +571,7 @@ func normalizeStringList(groups ...[]string) []string { return out } -func firstNonNil(values ...any) any { +func firstMetricQueryTime(values ...*time.Time) *time.Time { for _, value := range values { if value != nil { return value @@ -596,82 +580,14 @@ func firstNonNil(values ...any) any { return nil } -func parseMetricQueryTimeOrDefault(value any, fallback time.Time) (time.Time, error) { +func metricQueryTimeOrDefault(value *time.Time, fallback time.Time) time.Time { if value == nil { - return fallback.UTC(), nil - } - switch v := value.(type) { - case string: - raw := strings.TrimSpace(v) - if raw == "" { - return fallback.UTC(), nil - } - if ts, err := time.Parse(time.RFC3339Nano, raw); err == nil { - return ts.UTC(), nil - } - if ts, err := time.Parse("2006-01-02 15:04:05", raw); err == nil { - return ts.UTC(), nil - } - n, err := strconv.ParseFloat(raw, 64) - if err != nil { - return time.Time{}, fmt.Errorf("unsupported time format %q", raw) - } - return metricUnixNumberToTime(n), nil - case float64: - return metricUnixNumberToTime(v), nil - case float32: - return metricUnixNumberToTime(float64(v)), nil - case int: - return metricUnixNumberToTime(float64(v)), nil - case int64: - return metricUnixNumberToTime(float64(v)), nil - case jsonNumber: - n, err := strconv.ParseFloat(v.String(), 64) - if err != nil { - return time.Time{}, err - } - return metricUnixNumberToTime(n), nil - default: - return time.Time{}, fmt.Errorf("unsupported time value type %T", value) + return fallback.UTC() } + return value.UTC() } -type jsonNumber interface { - String() string -} - -func metricUnixNumberToTime(value float64) time.Time { - if math.Abs(value) >= 1e17 { - return time.Unix(0, int64(value)).UTC() - } - if math.Abs(value) >= 1e14 { - return time.Unix(0, int64(value)*int64(time.Microsecond)).UTC() - } - if math.Abs(value) >= 1e11 { - return time.UnixMilli(int64(value)).UTC() - } - return time.Unix(int64(value), 0).UTC() -} - -func metricQueryHours(value any) time.Duration { - if value == nil { - return 4 * time.Hour - } - var hours float64 - switch v := value.(type) { - case string: - parsed, err := strconv.ParseFloat(strings.TrimSpace(v), 64) - if err != nil { - return 4 * time.Hour - } - hours = parsed - case float64: - hours = v - case int: - hours = float64(v) - default: - return 4 * time.Hour - } +func metricQueryHours(hours float64) time.Duration { if hours <= 0 { return 4 * time.Hour } @@ -954,6 +870,10 @@ func normalizePingMetricTaskIDs(taskID any, taskIDs []any) map[string]bool { return out } +type jsonNumber interface { + String() string +} + func aggregatePointCount(points []metric.AggregatePoint) int { total := 0 for _, point := range points { diff --git a/web/rpc/jsonrpc/public_metric_test.go b/web/rpc/jsonrpc/public_metric_test.go index e2c9686..074bae5 100644 --- a/web/rpc/jsonrpc/public_metric_test.go +++ b/web/rpc/jsonrpc/public_metric_test.go @@ -10,16 +10,46 @@ import ( "github.com/komari-monitor/komari/database/metricstore" "github.com/komari-monitor/komari/database/models" "github.com/komari-monitor/komari/pkg/metric" + "github.com/komari-monitor/komari/pkg/rpc" ) +func TestMetricQueryParamsRequireRFC3339Time(t *testing.T) { + tests := []struct { + name string + value any + wantErr bool + }{ + {name: "RFC3339", value: "2026-07-17T09:30:00.123456789+08:00"}, + {name: "offset free", value: "2026-07-17 09:30:00.123456789", wantErr: true}, + {name: "Unix number", value: float64(1_752_720_600), wantErr: true}, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + req := &rpc.JsonRpcRequest{Params: map[string]any{"start": test.value}} + var params publicMetricQueryParams + err := req.BindParams(¶ms) + if (err != nil) != test.wantErr { + t.Fatalf("BindParams() error = %v, wantErr %v", err, test.wantErr) + } + if err == nil { + want := time.Date(2026, 7, 17, 1, 30, 0, 123456789, time.UTC) + if params.Start == nil || !params.Start.Equal(want) { + t.Fatalf("start = %v, want %s", params.Start, want) + } + } + }) + } +} + func TestSplitPublicMetricSeriesKeepsTagSeries(t *testing.T) { + baseTime := time.Date(2026, 6, 18, 0, 0, 0, 0, time.UTC) base := publicMetricSeries{ MetricKey: "gpu.device.usage", EntityID: "node-a", Points: []publicMetricPoint{ - {Time: "2026-06-18T00:00:00Z", Value: publicMetricValue(10), Count: 2, Tags: map[string]string{"device_index": "0"}}, - {Time: "2026-06-18T00:00:00Z", Value: publicMetricValue(80), Count: 2, Tags: map[string]string{"device_index": "1"}}, - {Time: "2026-06-18T00:01:00Z", Value: publicMetricValue(20), Count: 2, Tags: map[string]string{"device_index": "0"}}, + {Time: baseTime, Value: publicMetricValue(10), Count: 2, Tags: map[string]string{"device_index": "0"}}, + {Time: baseTime, Value: publicMetricValue(80), Count: 2, Tags: map[string]string{"device_index": "1"}}, + {Time: baseTime.Add(time.Minute), Value: publicMetricValue(20), Count: 2, Tags: map[string]string{"device_index": "0"}}, }, } @@ -39,12 +69,13 @@ func TestSplitPublicMetricSeriesKeepsTagSeries(t *testing.T) { } func TestPublicMetricJSONIncludesOnlyTags(t *testing.T) { + pointTime := time.Date(2026, 6, 18, 0, 0, 0, 123456789, time.UTC) payload, err := json.Marshal(publicMetricSeries{ MetricKey: "ping.loss", EntityID: "node-a", Tags: map[string]string{"task_id": "7"}, Points: []publicMetricPoint{{ - Time: "2026-06-18T00:00:00Z", + Time: pointTime, Value: publicMetricValue(0), Tags: map[string]string{"task_id": "7"}, }}, @@ -59,6 +90,9 @@ func TestPublicMetricJSONIncludesOnlyTags(t *testing.T) { if strings.Contains(text, `"tag":`) { t.Fatalf("legacy tag field should not be serialized: %s", text) } + if !strings.Contains(text, `"time":"2026-06-18T00:00:00.123456789Z"`) { + t.Fatalf("metric time is not UTC RFC3339Nano: %s", text) + } } func TestAdaptiveFillPublicMetricSeriesUsesObservedInterval(t *testing.T) { @@ -72,7 +106,7 @@ func TestAdaptiveFillPublicMetricSeriesUsesObservedInterval(t *testing.T) { } for i := 0; i < 10; i++ { series.Points = append(series.Points, publicMetricPoint{ - Time: base.Add(time.Duration(i) * time.Minute).Format(time.RFC3339Nano), + Time: base.Add(time.Duration(i) * time.Minute), Value: publicMetricValue(float64(i)), }) } @@ -100,10 +134,10 @@ func TestAdaptiveFillPublicMetricSeriesAddsCompactGapsAndBounds(t *testing.T) { IntervalSeconds: 1, Tags: tags, Points: []publicMetricPoint{ - {Time: base.Format(time.RFC3339Nano), Value: publicMetricValue(10)}, - {Time: base.Add(time.Minute).Format(time.RFC3339Nano), Value: publicMetricValue(20)}, - {Time: base.Add(2 * time.Minute).Format(time.RFC3339Nano), Value: publicMetricValue(30)}, - {Time: base.Add(4 * time.Minute).Format(time.RFC3339Nano), Value: publicMetricValue(40)}, + {Time: base, Value: publicMetricValue(10)}, + {Time: base.Add(time.Minute), Value: publicMetricValue(20)}, + {Time: base.Add(2 * time.Minute), Value: publicMetricValue(30)}, + {Time: base.Add(4 * time.Minute), Value: publicMetricValue(40)}, }, } start := base.Add(-30 * time.Second) @@ -115,10 +149,10 @@ func TestAdaptiveFillPublicMetricSeriesAddsCompactGapsAndBounds(t *testing.T) { if len(got.Points) != 7 { t.Fatalf("expected four values, one gap and two bounds, got %#v", got.Points) } - wantNullTimes := map[string]bool{ - start.Format(time.RFC3339Nano): true, - base.Add(3 * time.Minute).Format(time.RFC3339Nano): true, - end.Format(time.RFC3339Nano): true, + wantNullTimes := map[time.Time]bool{ + start: true, + base.Add(3 * time.Minute): true, + end: true, } for _, point := range got.Points { if point.Value != nil { diff --git a/web/update/update.go b/web/update/update.go index 9aa4d9c..13a73a2 100644 --- a/web/update/update.go +++ b/web/update/update.go @@ -53,7 +53,7 @@ type Controller struct { } type cleanupRequest struct { - Before string `json:"before"` + Before time.Time `json:"before"` } type startRequest struct { @@ -138,12 +138,12 @@ func (c *Controller) cleanup(ctx *gin.Context) { api.RespondError(ctx, http.StatusBadRequest, err.Error()) return } - cutoff, err := time.Parse(time.RFC3339, request.Before) - if err != nil { - api.RespondError(ctx, http.StatusBadRequest, "before must be an RFC3339 timestamp") + if request.Before.IsZero() { + api.RespondError(ctx, http.StatusBadRequest, "before is required and must be an RFC3339 timestamp with a timezone") return } - if cutoff.After(time.Now()) { + cutoff := request.Before.UTC() + if cutoff.After(time.Now().UTC()) { api.RespondError(ctx, http.StatusBadRequest, "cleanup cutoff cannot be in the future") return }