Initial commit
This commit is contained in:
75
scripts/seed.go
Normal file
75
scripts/seed.go
Normal file
@@ -0,0 +1,75 @@
|
||||
package scripts
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/uptrace/bun"
|
||||
"go.uber.org/zap"
|
||||
|
||||
"git.derfenix.pro/fenix/protect_trans_info/application/repository"
|
||||
)
|
||||
|
||||
func SeedData(ctx context.Context, db *bun.DB, logger *zap.Logger) error {
|
||||
now := time.Now().Add(-24 * time.Hour)
|
||||
|
||||
const bufSize = 500
|
||||
buf := make([]repository.ConnLog, 0, bufSize)
|
||||
|
||||
for i := 0; i < 2_000_000; i++ {
|
||||
buf = append(buf, repository.ConnLog{
|
||||
UserID: uint64(rand.Int63n(10000)),
|
||||
IP: randomIP(),
|
||||
TS: now.Add(time.Second * time.Duration(i)),
|
||||
})
|
||||
|
||||
if len(buf) == bufSize {
|
||||
_, err := db.NewInsert().Model(&buf).Exec(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("insert bulk: %w", err)
|
||||
}
|
||||
logger.Sugar().Infof("insert %d items", len(buf))
|
||||
|
||||
buf = make([]repository.ConnLog, 0, bufSize)
|
||||
}
|
||||
}
|
||||
|
||||
if len(buf) > 0 {
|
||||
_, err := db.NewInsert().Model(&buf).Exec(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("insert bulk: %w", err)
|
||||
}
|
||||
logger.Sugar().Infof("insert %d items", len(buf))
|
||||
}
|
||||
|
||||
fixedDup := []repository.ConnLog{
|
||||
repository.ConnLog{
|
||||
UserID: 88888,
|
||||
IP: "127.0.0.1",
|
||||
TS: now.Add(time.Second * time.Duration(888)),
|
||||
},
|
||||
repository.ConnLog{
|
||||
UserID: 99999,
|
||||
IP: "127.0.0.1",
|
||||
TS: now.Add(time.Second * time.Duration(999)),
|
||||
},
|
||||
}
|
||||
|
||||
_, err := db.NewInsert().Model(&fixedDup).Exec(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("insert fixed dup: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func randomIP() string {
|
||||
buf := make([]byte, 4)
|
||||
binary.LittleEndian.PutUint32(buf, rand.Uint32())
|
||||
|
||||
return net.IP(buf).String()
|
||||
}
|
||||
Reference in New Issue
Block a user