mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-09-30 14:26:36 +08:00
fb6a3edb89
- Physically purged all legacy internal/ packages, centralized pkg/model/ and pkg/repository/ - Migrated domain models and database repositories into self-contained owner plugins (user, auth, message_gateway, admin, upload, risk_control) - Decoupled cross-plugin interactions via pure core/contracts and typed EventBus - Ensured 100% test coverage pass, zero data races (-race clean), and 0 lint issues in make code-check
49 lines
1.0 KiB
Go
49 lines
1.0 KiB
Go
// Copyright 2026 Arctel.net
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package analytics
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/Rain-kl/Wavelet/pkg/persistence"
|
|
)
|
|
|
|
// BatchInsert writes access logs to ClickHouse using the native batch API.
|
|
func BatchInsert(ctx context.Context, logs []UserAccessLog) error {
|
|
if len(logs) == 0 {
|
|
return nil
|
|
}
|
|
if db.ChConn == nil {
|
|
return fmt.Errorf("clickhouse connection is not initialized")
|
|
}
|
|
|
|
batch, err := db.ChConn.PrepareBatch(ctx, UserAccessLog{}.BatchInsertSQL())
|
|
if err != nil {
|
|
return fmt.Errorf("prepare clickhouse batch: %w", err)
|
|
}
|
|
|
|
for _, logItem := range logs {
|
|
if err := batch.Append(
|
|
logItem.ID,
|
|
logItem.UserID,
|
|
logItem.Path,
|
|
logItem.Method,
|
|
logItem.IP,
|
|
logItem.UserAgent,
|
|
logItem.Headers,
|
|
logItem.Status,
|
|
logItem.Latency,
|
|
logItem.CreatedAt,
|
|
); err != nil {
|
|
return fmt.Errorf("append access log to batch: %w", err)
|
|
}
|
|
}
|
|
|
|
if err := batch.Send(); err != nil {
|
|
return fmt.Errorf("send clickhouse batch: %w", err)
|
|
}
|
|
return nil
|
|
}
|