-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathutils.go
More file actions
975 lines (894 loc) · 25 KB
/
Copy pathutils.go
File metadata and controls
975 lines (894 loc) · 25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
/*===----------- utils.go - tracking utility written in go -------------===
*
*
* This file is licensed under the Apache 2 License. See LICENSE for details.
*
* Copyright (c) 2018 Andrew Grosser. All Rights Reserved.
*
* `...
* yNMMh`
* dMMMh`
* dMMMh`
* dMMMh`
* dMMMd`
* dMMMm.
* dMMMm.
* dMMMm. /hdy.
* ohs+` yMMMd. yMMM-
* .mMMm. yMMMm. oMMM/
* :MMMd` sMMMN. oMMMo
* +MMMd` oMMMN. oMMMy
* sMMMd` /MMMN. oMMMh
* sMMMd` /MMMN- oMMMd
* oMMMd` :NMMM- oMMMd
* /MMMd` -NMMM- oMMMm
* :MMMd` .mMMM- oMMMm`
* -NMMm. `mMMM: oMMMm`
* .mMMm. dMMM/ +MMMm`
* `hMMm. hMMM/ /MMMm`
* yMMm. yMMM/ /MMMm`
* oMMm. oMMMo -MMMN.
* +MMm. +MMMo .MMMN-
* +MMm. /MMMo .NMMN-
* ` +MMm. -MMMs .mMMN: `.-.
* /hys:` +MMN- -NMMy `hMMN: .yNNy
* :NMMMy` sMMM/ .NMMy yMMM+-dMMMo
* +NMMMh-hMMMo .mMMy +MMMmNMMMh`
* /dMMMNNMMMs .dMMd -MMMMMNm+`
* .+mMMMMMN: .mMMd `NMNmh/`
* `/yhhy: `dMMd /+:`
* `hMMm`
* `hMMm.
* .mMMm:
* :MMMd-
* -NMMh.
* ./:.
*
*===----------------------------------------------------------------------===
*/
package main
import (
"archive/zip"
"crypto/sha1"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
"hash/fnv"
"io"
"net"
"net/http"
"os"
"os/user"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/gocql/gocql"
"github.com/google/uuid"
goja4h "github.com/lum8rjack/go-ja4h"
)
// zeroUUID is a constant representing the zero UUID (all zeros)
// Used as default value when UUID is nil to prevent ClickHouse binding panics
var zeroUUID = uuid.MustParse("00000000-0000-0000-0000-000000000000")
// normalizeUUIDString accepts common UUID shapes and returns the canonical
// lower-case 8-4-4-4-12 dashed form. Returns "" for empty input. Returns the
// original string when it does not match a recognized shape so that uuid.Parse
// can still produce a useful error.
//
// Production cookies store UUIDs without dashes (32-hex). Go's uuid.Parse
// only accepts the dashed form, so without this helper every dashless UUID
// falls back to the zero UUID in parseUUID().
func normalizeUUIDString(s string) string {
s = strings.TrimSpace(s)
if s == "" {
return ""
}
// Already canonical 8-4-4-4-12
if len(s) == 36 && s[8] == '-' && s[13] == '-' && s[18] == '-' && s[23] == '-' {
return strings.ToLower(s)
}
// 32-hex, no dashes
if len(s) == 32 {
if _, err := hex.DecodeString(s); err == nil {
return strings.ToLower(s[0:8] + "-" + s[8:12] + "-" + s[12:16] + "-" + s[16:20] + "-" + s[20:])
}
}
// Braced {8-4-4-4-12}
if len(s) == 38 && s[0] == '{' && s[37] == '}' {
return normalizeUUIDString(s[1:37])
}
return s
}
// parseUUIDString is the single entry point for parsing untrusted UUID strings
// across the tracker. It applies normalizeUUIDString first so dashless/braced
// inputs succeed instead of silently becoming zero UUIDs.
func parseUUIDString(s string) (uuid.UUID, error) {
return uuid.Parse(normalizeUUIDString(s))
}
// //////////////////////////////////////
// hash
// //////////////////////////////////////
func hash(s string) uint32 {
h := fnv.New32a()
h.Write([]byte(s))
return h.Sum32()
}
func sha(s string) string {
hasher := sha1.New()
hasher.Write([]byte(s))
return base64.URLEncoding.EncodeToString(hasher.Sum(nil))
}
func shasum256(s string) string {
hasher := sha256.New()
hasher.Write([]byte(s))
return fmt.Sprintf("%x", hasher.Sum(nil))
}
// //////////////////////////////////////
// filterUrl
// matchGroup is a 1 indexed array (0 is default last)
// returns last match if no group, or group
// //////////////////////////////////////
func filterUrl(c *Configuration, s *string, matchGroup *int) error {
matches := regexFilterUrl.FindStringSubmatch(*s)
mi := len(matches)
if matchGroup == nil || *matchGroup == 0 {
//Take the last one by default
if mi > 0 {
*s = matches[mi-1]
return nil
}
} else {
if mi > *matchGroup {
*s = matches[*matchGroup]
return nil
}
}
//Fallback
filterUrlAppendix(s)
return fmt.Errorf("Mismatch Regex (Url Filter)")
}
func filterUrlAppendix(s *string) error {
if s != nil {
i := strings.Index(*s, "?")
if i > -1 {
*s = (*s)[:i]
}
}
return nil
}
func filterUrlPrefix(s *string) error {
if s != nil {
*s = strings.ToLower(*s)
i := strings.Index(*s, "https://")
if i > -1 {
*s = (*s)[i+6:]
return nil
}
i = strings.Index(*s, "http://")
if i > -1 {
*s = (*s)[i+5:]
}
}
return nil
}
func cleanInterfaceString(i interface{}) error {
s := &i
if temp, ok := (*s).(string); ok {
*s = strings.ToLower(strings.TrimSpace(temp))
}
return nil
}
func ensureInterfaceString(i interface{}) error {
s := &i
if _, ok := (*s).(string); !ok {
if s != nil {
*s = fmt.Sprintf("%v", *s)
}
}
return nil
}
func cleanString(s *string) error {
if s != nil && *s != "" {
*s = strings.ToLower(strings.TrimSpace(*s))
}
return nil
}
func upperString(s *string) error {
if s != nil && *s != "" {
*s = strings.ToUpper(strings.TrimSpace(*s))
}
return nil
}
func FixedLengthNumberString(length int, str string) string {
verb := fmt.Sprintf("%%%d.%ds", length, length)
return strings.Replace(fmt.Sprintf(verb, str), " ", "0", -1)
}
// //////////////////////////////////////
// cacheDir in /tmp for SSL
// //////////////////////////////////////
func cacheDir() (dir string) {
if u, _ := user.Current(); u != nil {
dir = filepath.Join(os.TempDir(), "cache-golang-autocert-"+u.Username)
//dir = filepath.Join(".", "cache-golang-autocert-"+u.Username)
fmt.Println("Saving cache-go-lang-autocert-u.username to: ", dir)
if err := os.MkdirAll(dir, 0700); err == nil {
return dir
}
}
return ""
}
// getRequestHostInfo returns comprehensive host information from various request headers
func getRequestHostInfo(r *http.Request) map[string]string {
hostInfo := map[string]string{
"request_host": r.Header.Get("host"),
"x_host": r.Header.Get("x-host"),
"forwarded_host": r.Header.Get("x-forwarded-host"),
"original_host": r.Header.Get("x-original-host"),
"client_ip": r.Header.Get("x-real-ip"),
"server_ip": r.Header.Get("server-ip"),
"forwarded_for": r.Header.Get("x-forwarded-for"),
}
// Filter out empty values
filtered := make(map[string]string)
for k, v := range hostInfo {
if v != "" {
filtered[k] = v
}
}
return filtered
}
// getPrimaryHost returns the most reliable host identifier for hhash calculation
// Order of precedence:
// 1. x-original-host (original client request host before any proxying)
// 2. host (standard host header)
// 3. x-forwarded-host (if behind a trusted proxy)
// 4. x-host (fallback custom header)
// NOTE: x-real-ip and x-forwarded-for are IP addresses, not hostnames.
// Using them here caused unique hhash per visitor, exploding partition count.
// IP-based identification is handled separately by getIP() → iphash.
func getPrimaryHost(r *http.Request) string {
headers := r.Header
// Only use actual hostname headers, not IP headers
for _, h := range []string{"x-original-host", "host", "x-forwarded-host", "x-host"} {
if v := headers.Get(h); v != "" {
return cleanHost(v)
}
}
return ""
return ""
}
// cleanHost removes port from host if present and returns clean hostname/IP
func cleanHost(host string) string {
if addr, _, err := net.SplitHostPort(host); err != nil {
return strings.ToLower(host)
} else {
return strings.ToLower(addr)
}
}
func getIP(r *http.Request) string {
// Enhanced IP detection with multiple header fallbacks
// Order of precedence:
// 1. x-real-ip (most reliable, set by trusted proxy)
// 2. x-forwarded-for (first IP in chain)
// 3. x-client-ip (some proxies use this)
// 4. cf-connecting-ip (Cloudflare)
// 5. x-forwarded (standard format)
// 6. RemoteAddr (fallback)
headers := r.Header
// Try x-real-ip first (most reliable)
if ip := headers.Get("x-real-ip"); ip != "" {
return cleanIP(ip)
}
// Try x-forwarded-for (take first IP in chain)
if forwardedFor := headers.Get("x-forwarded-for"); forwardedFor != "" {
if ips := strings.Split(forwardedFor, ","); len(ips) > 0 {
firstIP := strings.TrimSpace(ips[0])
if firstIP != "" {
return cleanIP(firstIP)
}
}
}
// Try x-client-ip
if ip := headers.Get("x-client-ip"); ip != "" {
return cleanIP(ip)
}
// Try Cloudflare header
if ip := headers.Get("cf-connecting-ip"); ip != "" {
return cleanIP(ip)
}
// Try x-forwarded header
if forwarded := headers.Get("x-forwarded"); forwarded != "" {
// Parse "for=client,for=proxy" format
if strings.Contains(forwarded, "for=") {
parts := strings.Split(forwarded, ",")
for _, part := range parts {
if strings.HasPrefix(strings.TrimSpace(part), "for=") {
client := strings.TrimSpace(strings.TrimPrefix(part, "for="))
if client != "" && client != "unknown" {
return cleanIP(client)
}
}
}
}
}
// Fallback to RemoteAddr
if r.RemoteAddr != "" {
if ip, _, err := net.SplitHostPort(r.RemoteAddr); err != nil {
return cleanIP(r.RemoteAddr)
} else {
return cleanIP(ip)
}
}
return ""
}
func cleanIP(ip string) string {
ipa := strings.Split(ip, ",")
for i := len(ipa) - 1; i > -1; i-- {
ipa[i] = strings.TrimSpace(ipa[i])
if ipp := net.ParseIP(ipa[i]); ipp != nil {
return ipp.String()
}
}
return ""
}
func getHost(r *http.Request) string {
host := getPrimaryHost(r)
if host == "" {
// Fallback to r.Host if no headers found
return r.Host
}
return host
}
// getFullURL returns the full URL for redirect lookups, using enhanced host detection
func getFullURL(r *http.Request) string {
host := getPrimaryHost(r)
if host == "" {
// Fallback to original host if enhanced detection fails
host = r.Host
}
return fmt.Sprintf("%s%s", host, r.URL.Path)
}
// getJA4H generates a JA4H fingerprint from the HTTP request
func getJA4H(r *http.Request) string {
return goja4h.JA4H(r)
}
func Unzip(src, dest string) error {
r, err := zip.OpenReader(src)
if err != nil {
return err
}
defer func() {
if err := r.Close(); err != nil {
panic(err)
}
}()
os.MkdirAll(dest, 0755)
// Closure to address file descriptors issue with all the deferred .Close() methods
extractAndWriteFile := func(f *zip.File) error {
rc, err := f.Open()
if err != nil {
return err
}
defer func() {
if err := rc.Close(); err != nil {
panic(err)
}
}()
path := filepath.Join(dest, f.Name)
if f.FileInfo().IsDir() {
os.MkdirAll(path, f.Mode())
} else {
os.MkdirAll(filepath.Dir(path), f.Mode())
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
if err != nil {
return err
}
defer func() {
if err := f.Close(); err != nil {
panic(err)
}
}()
_, err = io.Copy(f, rc)
if err != nil {
return err
}
}
return nil
}
for _, f := range r.File {
err := extractAndWriteFile(f)
if err != nil {
return err
}
}
return nil
}
func checkRowExpired(row map[string]interface{}, meta *gocql.TableMetadata, p Prune, pruneSkipToTimestamp int64) (bool, *time.Time) {
var created *time.Time
expired := false
if ctemp1, ok := row["created"]; ok {
if ctemp2, okp := ctemp1.(time.Time); okp {
created = &ctemp2
}
}
if meta != nil && len(meta.PartitionKey) == 1 {
if tuuid, tok := row[meta.PartitionKey[0].Name]; tok {
if uuid, ok := tuuid.(gocql.UUID); ok {
if uuid.Version() == 1 {
c := uuid.Time()
if c.Before(*created) {
created = &c
}
}
}
}
}
if created != nil {
if created.Before(time.Unix(pruneSkipToTimestamp, 0)) {
return false, created
}
expired = (*created).Add(time.Second * time.Duration(p.TTL)).Before(time.Now().UTC())
} else {
expired = true
}
// if fmt.Sprintf("%T", row["cflags"]) != "int64" {
// err = fmt.Errorf("Table %s not supported for pruning (bad cflags type)", p.Table)
// goto tablefailed
// }
var ignore int64
for _, icflag := range p.CFlagsIgnore {
ignore += icflag
}
if cftemp1, ok := row["cflags"]; ok {
if cftemp2, okp := cftemp1.(int64); okp {
if cftemp2&ignore > 0 {
expired = false
}
}
}
return expired, created
}
func checkIdExpired(uuid *gocql.UUID, ttl int) bool {
//If the id is incorrectly formatted expire it
if uuid == nil || uuid.Version() != 1 {
return true
}
created := uuid.Time()
return created.Add(time.Second * time.Duration(ttl)).Before(time.Now().UTC())
}
func checkUUIDExpired(uuid *uuid.UUID, ttl int) bool {
//If the id is incorrectly formatted expire it
if uuid == nil || uuid.Version() != 1 {
return true
}
// Convert UUID timestamp to time.Time
// UUID v1 timestamp is 100-nanosecond intervals since UUID epoch (15 Oct 1582)
uuidTime := time.Unix(0, int64((uuid.Time()-0x01B21DD213814000)*100))
return uuidTime.Add(time.Second * time.Duration(ttl)).Before(time.Now().UTC())
}
func SetValueInJSON(iface interface{}, path string, value interface{}) interface{} {
m := iface.(map[string]interface{})
split := strings.Split(path, ".")
for k, v := range m {
if strings.EqualFold(k, split[0]) {
if len(split) == 1 {
m[k] = value
return m
}
switch v.(type) {
case map[string]interface{}:
return SetValueInJSON(v, strings.Join(split[1:], "."), value)
default:
return m
}
}
}
// path not found -> create
if len(split) == 1 {
m[split[0]] = value
} else {
newMap := make(map[string]interface{})
newMap[split[len(split)-1]] = value
for i := len(split) - 2; i > 0; i-- {
mTmp := make(map[string]interface{})
mTmp[split[i]] = newMap
newMap = mTmp
}
m[split[0]] = newMap
}
return m
}
// //////////////////////////////////////
// Mathematical utility functions
// //////////////////////////////////////
// maxInt returns the larger of two integers
func maxInt(a, b int) int {
if a > b {
return a
}
return b
}
// min returns the smaller of two integers
func min(a, b int) int {
if a < b {
return a
}
return b
}
// //////////////////////////////////////
// String/Type conversion utilities
// //////////////////////////////////////
// getStringValue converts interface{} to string with nil safety
func getStringValue(v interface{}) string {
if v == nil {
return ""
}
if str, ok := v.(string); ok {
return str
}
return fmt.Sprintf("%v", v)
}
// getDateTimeOrZero converts interface{} to time.Time for ClickHouse DateTime64
func getDateTimeOrZero(v interface{}) time.Time {
if v == nil {
return time.Unix(0, 0).UTC()
}
switch t := v.(type) {
case time.Time:
return t
case *time.Time:
if t != nil {
return *t
}
return time.Unix(0, 0).UTC()
case string:
if t == "" {
return time.Unix(0, 0).UTC()
}
// Try to parse the string as a time
if parsed, err := time.Parse(time.RFC3339, t); err == nil {
return parsed
}
if parsed, err := time.Parse("2006-01-02 15:04:05", t); err == nil {
return parsed
}
return time.Unix(0, 0).UTC()
case float64:
// Unix timestamp
return time.Unix(int64(t), 0).UTC()
case int64:
// Unix timestamp
return time.Unix(t, 0).UTC()
default:
return time.Unix(0, 0).UTC()
}
}
// getStringPtr converts interface{} to *string with nil safety
func getStringPtr(v interface{}) *string {
if v == nil {
return nil
}
str := getStringValue(v)
if str == "" {
return nil
}
return &str
}
// toInt64OrZero coerces a value of any JSON-numeric shape into an int64.
// JSON numbers arrive as float64 via encoding/json; query strings arrive as
// plain strings. Returns 0 on any failure.
func toInt64OrZero(v interface{}) int64 {
switch x := v.(type) {
case nil:
return 0
case int64:
return x
case int:
return int64(x)
case int32:
return int64(x)
case float64:
return int64(x)
case float32:
return int64(x)
case json.Number:
if i, err := x.Int64(); err == nil {
return i
}
if f, err := x.Float64(); err == nil {
return int64(f)
}
return 0
case string:
s := strings.TrimSpace(x)
if s == "" {
return 0
}
if i, err := strconv.ParseInt(s, 10, 64); err == nil {
return i
}
if f, err := strconv.ParseFloat(s, 64); err == nil {
return int64(f)
}
return 0
default:
return 0
}
}
// derefStringOrEmpty returns the dereferenced string or "" if the pointer is nil.
func derefStringOrEmpty(s *string) string {
if s == nil {
return ""
}
return *s
}
// parseUUID converts interface{} or *uuid.UUID to *uuid.UUID with error handling
// By default returns a pointer to the zero UUID instead of nil to prevent ClickHouse binding panics
// Pass allowNil=true as second argument to allow nil returns (for optional UUID fields)
// Accepts: interface{} (string conversion), *uuid.UUID (passthrough if not nil), uuid.UUID (returns pointer)
func parseUUID(v interface{}, allowNil ...bool) *uuid.UUID {
// Check if nil is allowed (default: false)
canReturnNil := false
if len(allowNil) > 0 {
canReturnNil = allowNil[0]
}
// Handle *uuid.UUID input directly
if uuidPtr, ok := v.(*uuid.UUID); ok {
if uuidPtr == nil {
if canReturnNil {
return nil
}
return &zeroUUID
}
return uuidPtr
}
// Handle uuid.UUID value (return pointer to it)
if uuidVal, ok := v.(uuid.UUID); ok {
return &uuidVal
}
// Handle string conversion via getStringValue
str := getStringValue(v)
if str == "" {
if canReturnNil {
return nil
}
return &zeroUUID
}
if parsed, err := parseUUIDString(str); err == nil {
return &parsed
}
if canReturnNil {
return nil
}
return &zeroUUID
}
// jsonAsMap converts interface to map[string]interface{} for scenarios where
// json.RawMessage is incompatible with the execution method.
// This is useful when:
// - Using immediate batch execution (session.Exec) with JSON columns
// - ClickHouse driver cannot handle json.RawMessage in certain contexts
// - You need a plain map instead of encoded JSON bytes
// Example: mtriage table with ImmediateBatch strategy requires this for JSON fields
func jsonAsMap(m interface{}) interface{} {
if m == nil {
return map[string]interface{}{}
}
switch v := m.(type) {
case *map[string]interface{}:
if v == nil {
return map[string]interface{}{}
}
return *v
case *map[string]float64:
if v == nil {
return map[string]interface{}{}
}
// Convert map[string]float64 to map[string]interface{}
result := make(map[string]interface{})
for k, val := range *v {
result[k] = val
}
return result
case map[string]interface{}:
return v
case map[string]float64:
// Convert map[string]float64 to map[string]interface{}
result := make(map[string]interface{})
for k, val := range v {
result[k] = val
}
return result
case string:
// If it's already a JSON string, try to unmarshal it
if v == "" {
return map[string]interface{}{}
}
var result map[string]interface{}
if err := json.Unmarshal([]byte(v), &result); err == nil {
return result
}
// If unmarshal fails, return as a wrapped string
return map[string]interface{}{"value": v}
case json.RawMessage:
// Unmarshal RawMessage to map
if len(v) == 0 {
return map[string]interface{}{}
}
var result map[string]interface{}
if err := json.Unmarshal(v, &result); err == nil {
return result
}
return map[string]interface{}{}
default:
if v == nil {
return map[string]interface{}{}
}
// Try to convert to map through JSON marshal/unmarshal
if jsonBytes, err := json.Marshal(v); err == nil {
var result map[string]interface{}
if err := json.Unmarshal(jsonBytes, &result); err == nil {
return result
}
}
return map[string]interface{}{}
}
}
// //////////////////////////////////////
// Metrics manipulation utilities
// //////////////////////////////////////
// incrementMetric increments a metric value in a map by 1, handling type conversion
func incrementMetric(metrics map[string]interface{}, key string) float64 {
if value, exists := metrics[key]; exists {
if floatValue, ok := value.(float64); ok {
return floatValue + 1
}
if intValue, ok := value.(int); ok {
return float64(intValue) + 1
}
if intValue, ok := value.(int64); ok {
return float64(intValue) + 1
}
}
return 1
}
// addToMetric adds a value to a metric in a map, handling type conversion
func addToMetric(metrics map[string]interface{}, key string, addition float64) float64 {
if value, exists := metrics[key]; exists {
if floatValue, ok := value.(float64); ok {
return floatValue + addition
}
if intValue, ok := value.(int); ok {
return float64(intValue) + addition
}
if intValue, ok := value.(int64); ok {
return float64(intValue) + addition
}
}
return addition
}
// getMetricValue gets a metric value from a map as float64, handling type conversion
func getMetricValue(metrics map[string]interface{}, key string) float64 {
if value, exists := metrics[key]; exists {
if floatValue, ok := value.(float64); ok {
return floatValue
}
if intValue, ok := value.(int); ok {
return float64(intValue)
}
if intValue, ok := value.(int64); ok {
return float64(intValue)
}
}
return 0
}
// formatClickHouseDateTime formats a time.Time for ClickHouse DateTime64(3)
// ClickHouse expects format: "2006-01-02 15:04:05.000" (no timezone suffix)
// This prevents errors like: "Cannot parse string '2026-01-27 10:07:24.814492 +0000 UTC' as DateTime64(3)"
func formatClickHouseDateTime(t *time.Time) string {
if t == nil {
return "1970-01-01 00:00:00.000"
}
// Format with millisecond precision (3 decimal places)
return t.UTC().Format("2006-01-02 15:04:05.000")
}
// //////////////////////////////////////
// JSON utility functions
// //////////////////////////////////////
// jsonOrNull converts a map to JSON string for ClickHouse JSON type
// TODO: could use []byte("null") or json.RawMessage("null") in the place of json.RawMessage("{}")
// getJSONString returns a JSON string for direct insertion (used with immediate execution)
func getJSONString(m interface{}) string {
if m == nil {
return "{}"
}
switch v := m.(type) {
case string:
// If it's already a JSON string, return it
if strings.HasPrefix(v, "{") || strings.HasPrefix(v, "[") {
return v
}
// Otherwise wrap it
return "{}"
case map[string]interface{}:
if jsonBytes, err := json.Marshal(v); err == nil {
return string(jsonBytes)
}
return "{}"
case *map[string]interface{}:
if v == nil {
return "{}"
}
if jsonBytes, err := json.Marshal(*v); err == nil {
return string(jsonBytes)
}
return "{}"
case []interface{}:
// Convert array to object for JSON columns
if jsonBytes, err := json.Marshal(map[string]interface{}{"data": v}); err == nil {
return string(jsonBytes)
}
return "{}"
default:
// Try to marshal whatever it is
if jsonBytes, err := json.Marshal(v); err == nil {
return string(jsonBytes)
}
return "{}"
}
}
func jsonOrNull(m interface{}) interface{} {
if m == nil {
return json.RawMessage("{}")
}
switch v := m.(type) {
case *map[string]interface{}:
if v == nil {
return json.RawMessage("{}")
}
if jsonBytes, err := json.Marshal(*v); err == nil {
return json.RawMessage(jsonBytes)
}
return json.RawMessage("{}")
case *map[string]float64:
if v == nil {
return json.RawMessage("{}")
}
if jsonBytes, err := json.Marshal(*v); err == nil {
return json.RawMessage(jsonBytes)
}
return json.RawMessage("{}")
case map[string]interface{}:
if jsonBytes, err := json.Marshal(v); err == nil {
return json.RawMessage(jsonBytes)
}
return json.RawMessage("{}")
case map[string]float64:
if jsonBytes, err := json.Marshal(v); err == nil {
return json.RawMessage(jsonBytes)
}
return json.RawMessage("{}")
case string:
// If it's already a JSON string
if v == "" {
return json.RawMessage("{}")
}
return json.RawMessage(v)
case json.RawMessage:
// Return RawMessage as-is
if len(v) == 0 {
return json.RawMessage("{}")
}
return v
default:
if v == nil {
return json.RawMessage("{}")
}
// Try to marshal other types
if jsonBytes, err := json.Marshal(v); err == nil {
return json.RawMessage(jsonBytes)
}
return json.RawMessage("{}")
}
}