添加gorm的clickhouse
This commit is contained in:
parent
802728012f
commit
9087a3a816
5
db/db.go
5
db/db.go
@ -39,9 +39,10 @@ func InitClickHouse(host, port, user, password, database string) (*gorm.DB, erro
|
|||||||
return db, err
|
return db, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func AutoMigrateClickHouse[T any](db *gorm.DB, table *T) error {
|
func AutoMigrateClickHouse(db *gorm.DB, tableOptions string, table any) error {
|
||||||
// 带ClickHouse引擎的迁移
|
// 带ClickHouse引擎的迁移
|
||||||
return db.Set("gorm:table_options", "ENGINE=MergeTree() ORDER BY tuple()").AutoMigrate(table)
|
//return db.Set("gorm:table_options", "ENGINE=MergeTree() ORDER BY tuple()").AutoMigrate(table)
|
||||||
|
return db.Set("gorm:table_options", tableOptions).AutoMigrate(table)
|
||||||
}
|
}
|
||||||
|
|
||||||
func InitClickHouse2(host, port, user, password, database string) (*ClickHouseRepo, error) {
|
func InitClickHouse2(host, port, user, password, database string) (*ClickHouseRepo, error) {
|
||||||
|
@ -5,6 +5,7 @@ import (
|
|||||||
"github.com/fox/fox/log"
|
"github.com/fox/fox/log"
|
||||||
"github.com/wanghuiyt/ding"
|
"github.com/wanghuiyt/ding"
|
||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
// 辅助函数列表
|
// 辅助函数列表
|
||||||
@ -73,3 +74,15 @@ func WrapSafe(fn func(), recoverFunc func()) func() {
|
|||||||
RunSafe(fn, recoverFunc)
|
RunSafe(fn, recoverFunc)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func runSafeWg(wg *sync.WaitGroup, fn func()) {
|
||||||
|
defer wg.Done()
|
||||||
|
defer Recover(nil)
|
||||||
|
fn()
|
||||||
|
}
|
||||||
|
|
||||||
|
// 安全的执行go协程组
|
||||||
|
func GroupGo(wg *sync.WaitGroup, fn func()) {
|
||||||
|
wg.Add(1)
|
||||||
|
go runSafeWg(wg, fn)
|
||||||
|
}
|
||||||
|
@ -1,42 +0,0 @@
|
|||||||
package xrand
|
|
||||||
|
|
||||||
import "errors"
|
|
||||||
|
|
||||||
var ErrWeightRandomBadParam = errors.New("WeightRandom: bad param")
|
|
||||||
|
|
||||||
type randomItem[T any] struct {
|
|
||||||
Weight uint
|
|
||||||
Data T
|
|
||||||
}
|
|
||||||
|
|
||||||
// WeightRandom 加权随机
|
|
||||||
func WeightRandom[W int | uint | int32 | uint32 | int64 | uint64, T any](m map[W]T) (result T, err error) {
|
|
||||||
sum := uint(0)
|
|
||||||
var items []randomItem[T]
|
|
||||||
for weight, data := range m {
|
|
||||||
sum += uint(weight)
|
|
||||||
items = append(items, randomItem[T]{
|
|
||||||
Weight: uint(weight),
|
|
||||||
Data: data,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(items) == 0 || sum == 0 {
|
|
||||||
err = ErrWeightRandomBadParam
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if len(items) == 1 {
|
|
||||||
return items[0].Data, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
r := IntN(int(sum))
|
|
||||||
for _, item := range items {
|
|
||||||
r -= int(item.Weight)
|
|
||||||
if r < 0 {
|
|
||||||
return item.Data, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
err = ErrWeightRandomBadParam
|
|
||||||
return
|
|
||||||
}
|
|
@ -1,25 +0,0 @@
|
|||||||
package xrand
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
"testing"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestWeightRandom(t *testing.T) {
|
|
||||||
stat := make(map[any]int)
|
|
||||||
for i := 1; i <= 10000; i++ {
|
|
||||||
items := map[uint]string{
|
|
||||||
5: "a",
|
|
||||||
15: "b",
|
|
||||||
30: "c",
|
|
||||||
50: "d",
|
|
||||||
}
|
|
||||||
item, err := WeightRandom(items)
|
|
||||||
assert.Nil(t, err)
|
|
||||||
if _, ok := stat[item]; !ok {
|
|
||||||
stat[item] = 0
|
|
||||||
}
|
|
||||||
stat[item]++
|
|
||||||
}
|
|
||||||
t.Log(stat)
|
|
||||||
}
|
|
@ -11,36 +11,36 @@ const (
|
|||||||
|
|
||||||
// 2006-01-02
|
// 2006-01-02
|
||||||
|
|
||||||
func FormatDate(c carbon.Carbon) string {
|
func FormatDate(c *carbon.Carbon) string {
|
||||||
return c.ToDateString()
|
return c.ToDateString()
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2006-55 (55周)
|
// 2006-55 (55周)
|
||||||
|
|
||||||
func FormatWeek(c carbon.Carbon) string {
|
func FormatWeek(c *carbon.Carbon) string {
|
||||||
return c.Format(WeekLayout)
|
return c.Format(WeekLayout)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2006-12
|
// 2006-12
|
||||||
|
|
||||||
func FormatMonth(c carbon.Carbon) string {
|
func FormatMonth(c *carbon.Carbon) string {
|
||||||
return c.Format(MonthLayout)
|
return c.Format(MonthLayout)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2006-04 (4季度)
|
// 2006-04 (4季度)
|
||||||
|
|
||||||
func FormatQuarter(c carbon.Carbon) string {
|
func FormatQuarter(c *carbon.Carbon) string {
|
||||||
return c.Format(QuarterLayout)
|
return c.Format(QuarterLayout)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2006
|
// 2006
|
||||||
|
|
||||||
func FormatYear(c carbon.Carbon) string {
|
func FormatYear(c *carbon.Carbon) string {
|
||||||
return c.Format(YearLayout)
|
return c.Format(YearLayout)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 20060102
|
// 20060102
|
||||||
|
|
||||||
func FormatDateNum(c carbon.Carbon) int {
|
func FormatDateNum(c *carbon.Carbon) int {
|
||||||
return c.Year()*10000 + c.Month()*100 + c.Day()
|
return c.Year()*10000 + c.Month()*100 + c.Day()
|
||||||
}
|
}
|
||||||
|
@ -7,34 +7,34 @@ import (
|
|||||||
// 解析系统时区输出的字符串、时间戳,转为偏移时区
|
// 解析系统时区输出的字符串、时间戳,转为偏移时区
|
||||||
// 注意如果你的时间戳和字符串已经是偏移时区输出的,就不应使用下列解析函数!
|
// 注意如果你的时间戳和字符串已经是偏移时区输出的,就不应使用下列解析函数!
|
||||||
|
|
||||||
func Parse(value string, offsets ...int) (c carbon.Carbon) {
|
func Parse(value string, offsets ...int) (c *carbon.Carbon) {
|
||||||
c = carbon.Parse(value)
|
c = carbon.Parse(value)
|
||||||
c = set(c, offsets...)
|
c = set(c, offsets...)
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
//goland:noinspection GoUnusedExportedFunction
|
//goland:noinspection GoUnusedExportedFunction
|
||||||
func FromUnix(sec int64, offsets ...int) carbon.Carbon {
|
func FromUnix(sec int64, offsets ...int) *carbon.Carbon {
|
||||||
c := carbon.CreateFromTimestamp(sec)
|
c := carbon.CreateFromTimestamp(sec)
|
||||||
c = set(c, offsets...)
|
c = set(c, offsets...)
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
func FromUnixMilli(msec int64, offsets ...int) carbon.Carbon {
|
func FromUnixMilli(msec int64, offsets ...int) *carbon.Carbon {
|
||||||
c := carbon.CreateFromTimestampMilli(msec)
|
c := carbon.CreateFromTimestampMilli(msec)
|
||||||
c = set(c, offsets...)
|
c = set(c, offsets...)
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
//goland:noinspection GoUnusedExportedFunction
|
//goland:noinspection GoUnusedExportedFunction
|
||||||
func FromUnixMicro(msec int64, offsets ...int) carbon.Carbon {
|
func FromUnixMicro(msec int64, offsets ...int) *carbon.Carbon {
|
||||||
c := carbon.CreateFromTimestampMicro(msec)
|
c := carbon.CreateFromTimestampMicro(msec)
|
||||||
c = set(c, offsets...)
|
c = set(c, offsets...)
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
//goland:noinspection GoUnusedExportedFunction
|
//goland:noinspection GoUnusedExportedFunction
|
||||||
func FromUnixNano(msec int64, offsets ...int) carbon.Carbon {
|
func FromUnixNano(msec int64, offsets ...int) *carbon.Carbon {
|
||||||
c := carbon.CreateFromTimestampNano(msec)
|
c := carbon.CreateFromTimestampNano(msec)
|
||||||
c = set(c, offsets...)
|
c = set(c, offsets...)
|
||||||
return c
|
return c
|
||||||
|
@ -147,6 +147,6 @@ func TestIsInDuration(t *testing.T) {
|
|||||||
|
|
||||||
func TestClean(t *testing.T) {
|
func TestClean(t *testing.T) {
|
||||||
_ = t
|
_ = t
|
||||||
_ = TimestampToTime
|
_ = TimestampToTime[int]
|
||||||
_ = FormatDuration
|
_ = FormatDuration[int64]
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user