mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-10-02 06:56:36 +08:00
e52592b16d
用户访问日志可在 ClickHouse、PostgreSQL、SQLite 之间切换。 关闭 ClickHouse 时由主库承接写入与查询;切换任务会冻结写入、复制数据后翻转主库。 启动时校验日志主库与运行配置一致,定期清理按各库保留天数删除过期记录。
61 lines
2.0 KiB
Go
61 lines
2.0 KiB
Go
// Copyright 2026 Arctel.net
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package logstore
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
analyticsmodel "github.com/Rain-kl/Wavelet/internal/model/analytics"
|
|
analyticsrepo "github.com/Rain-kl/Wavelet/internal/repository/analytics"
|
|
"github.com/glebarez/sqlite"
|
|
"github.com/stretchr/testify/require"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func newTestUserAccessStore(t *testing.T) *userAccessLogGormStore {
|
|
t.Helper()
|
|
gdb, err := gorm.Open(sqlite.Open("file:logstore-"+t.Name()+"?mode=memory&cache=shared"), &gorm.Config{})
|
|
require.NoError(t, err)
|
|
require.NoError(t, gdb.AutoMigrate(&analyticsmodel.UserAccessLog{}))
|
|
return newUserAccessLogGormStore(gdb)
|
|
}
|
|
|
|
func TestGormUserAccessLogCountList(t *testing.T) {
|
|
ua := newTestUserAccessStore(t)
|
|
ctx := context.Background()
|
|
now := time.Now().UTC().Truncate(time.Second)
|
|
require.NoError(t, ua.BatchInsert(ctx, []analyticsmodel.UserAccessLog{
|
|
{UserID: 10, Path: "/api/v1/users", Method: "GET", Status: 200, CreatedAt: now},
|
|
{UserID: 20, Path: "/api/v1/admin", Method: "GET", Status: 200, CreatedAt: now},
|
|
{UserID: 10, Path: "/api/v1/other", Method: "POST", Status: 201, CreatedAt: now},
|
|
}))
|
|
|
|
count, err := ua.Count(ctx, analyticsrepo.AccessLogFilter{UserIDs: []uint64{10}, Path: "users"})
|
|
require.NoError(t, err)
|
|
require.Equal(t, uint64(1), count)
|
|
|
|
rows, total, err := ua.List(ctx, analyticsrepo.AccessLogFilter{UserIDs: []uint64{10}, Path: "users"}, 1, 10)
|
|
require.NoError(t, err)
|
|
require.Equal(t, uint64(1), total)
|
|
require.Len(t, rows, 1)
|
|
require.Equal(t, "/api/v1/users", rows[0].Path)
|
|
require.NotZero(t, rows[0].ID)
|
|
}
|
|
|
|
func TestGormUserAccessLogFreeze(t *testing.T) {
|
|
ua := newTestUserAccessStore(t)
|
|
SetConfigReader(func(_ context.Context, key string) (string, error) {
|
|
if key == logMigrationKey {
|
|
return "migrating", nil
|
|
}
|
|
return "", nil
|
|
})
|
|
t.Cleanup(ResetForTest)
|
|
|
|
err := ua.BatchInsert(context.Background(), []analyticsmodel.UserAccessLog{{UserID: 1, CreatedAt: time.Now()}})
|
|
require.ErrorIs(t, err, ErrMigrating)
|
|
}
|