diff --git a/db/db.go b/db/db.go index 5438bf2..271175d 100644 --- a/db/db.go +++ b/db/db.go @@ -39,9 +39,10 @@ func InitClickHouse(host, port, user, password, database string) (*gorm.DB, erro return db, err } -func AutoMigrateClickHouse[T any](db *gorm.DB, table *T) error { +func AutoMigrateClickHouse(db *gorm.DB, tableOptions string, table any) error { // 带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) { diff --git a/ksync/ksync.go b/ksync/ksync.go index 6e685f0..7d15db6 100644 --- a/ksync/ksync.go +++ b/ksync/ksync.go @@ -5,6 +5,7 @@ import ( "github.com/fox/fox/log" "github.com/wanghuiyt/ding" "runtime/debug" + "sync" ) // 辅助函数列表 @@ -73,3 +74,15 @@ func WrapSafe(fn func(), recoverFunc func()) func() { 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) +} diff --git a/xrand/weight.go b/xrand/weight.go deleted file mode 100644 index ef9a453..0000000 --- a/xrand/weight.go +++ /dev/null @@ -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 -} diff --git a/xrand/weight_test.go b/xrand/weight_test.go deleted file mode 100644 index 36b6392..0000000 --- a/xrand/weight_test.go +++ /dev/null @@ -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) -} diff --git a/xtime/format.go b/xtime/format.go index bd7a5af..2e600cd 100644 --- a/xtime/format.go +++ b/xtime/format.go @@ -11,36 +11,36 @@ const ( // 2006-01-02 -func FormatDate(c carbon.Carbon) string { +func FormatDate(c *carbon.Carbon) string { return c.ToDateString() } // 2006-55 (55周) -func FormatWeek(c carbon.Carbon) string { +func FormatWeek(c *carbon.Carbon) string { return c.Format(WeekLayout) } // 2006-12 -func FormatMonth(c carbon.Carbon) string { +func FormatMonth(c *carbon.Carbon) string { return c.Format(MonthLayout) } // 2006-04 (4季度) -func FormatQuarter(c carbon.Carbon) string { +func FormatQuarter(c *carbon.Carbon) string { return c.Format(QuarterLayout) } // 2006 -func FormatYear(c carbon.Carbon) string { +func FormatYear(c *carbon.Carbon) string { return c.Format(YearLayout) } // 20060102 -func FormatDateNum(c carbon.Carbon) int { +func FormatDateNum(c *carbon.Carbon) int { return c.Year()*10000 + c.Month()*100 + c.Day() } diff --git a/xtime/parser.go b/xtime/parser.go index 33ebd41..5278407 100644 --- a/xtime/parser.go +++ b/xtime/parser.go @@ -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 = set(c, offsets...) return c } //goland:noinspection GoUnusedExportedFunction -func FromUnix(sec int64, offsets ...int) carbon.Carbon { +func FromUnix(sec int64, offsets ...int) *carbon.Carbon { c := carbon.CreateFromTimestamp(sec) c = set(c, offsets...) return c } -func FromUnixMilli(msec int64, offsets ...int) carbon.Carbon { +func FromUnixMilli(msec int64, offsets ...int) *carbon.Carbon { c := carbon.CreateFromTimestampMilli(msec) c = set(c, offsets...) return c } //goland:noinspection GoUnusedExportedFunction -func FromUnixMicro(msec int64, offsets ...int) carbon.Carbon { +func FromUnixMicro(msec int64, offsets ...int) *carbon.Carbon { c := carbon.CreateFromTimestampMicro(msec) c = set(c, offsets...) return c } //goland:noinspection GoUnusedExportedFunction -func FromUnixNano(msec int64, offsets ...int) carbon.Carbon { +func FromUnixNano(msec int64, offsets ...int) *carbon.Carbon { c := carbon.CreateFromTimestampNano(msec) c = set(c, offsets...) return c diff --git a/xtime/timex_test.go b/xtime/timex_test.go index 922e5e8..3f3d98e 100644 --- a/xtime/timex_test.go +++ b/xtime/timex_test.go @@ -147,6 +147,6 @@ func TestIsInDuration(t *testing.T) { func TestClean(t *testing.T) { _ = t - _ = TimestampToTime - _ = FormatDuration + _ = TimestampToTime[int] + _ = FormatDuration[int64] }