mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-09-29 05:56:38 +08:00
699e95f12c
Result: {"status":"keep","total_issues":56,"golint_canonicalheader":8,"golint_errname":1,"golint_errorlint":12,"golint_forcetypeassert":3,"golint_gosec":0,"golint_intrange":3,"golint_modernize":5,"golint_nilnil":3,"golint_perfsprint":0,"golint_prealloc":3,"golint_recvcheck":7,"golint_usestdlibvars":3,"golint_wastedassign":7,"golint_total":55,"eslint_problems":1,"eslint_errors":0,"eslint_warnings":1,"tsc_errors":0,"measure_s":47}
51 lines
1.1 KiB
Go
51 lines
1.1 KiB
Go
// Copyright 2026 Arctel.net
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package analytics
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
|
|
db "github.com/Rain-kl/Wavelet/internal/infra/persistence"
|
|
analyticsmodel "github.com/Rain-kl/Wavelet/internal/model/analytics"
|
|
)
|
|
|
|
// BatchInsert writes access logs to ClickHouse using the native batch API.
|
|
func BatchInsert(ctx context.Context, logs []analyticsmodel.UserAccessLog) error {
|
|
if len(logs) == 0 {
|
|
return nil
|
|
}
|
|
if db.ChConn == nil {
|
|
return errors.New("clickhouse connection is not initialized")
|
|
}
|
|
|
|
batch, err := db.ChConn.PrepareBatch(ctx, analyticsmodel.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
|
|
}
|