-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_helper.go
115 lines (89 loc) · 2.55 KB
/
gen_helper.go
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
package main
import (
"bytes"
"math/rand"
"os"
"path"
"text/template"
"time"
"github.com/gmodx/prometheus-data-generator/log"
)
type GenHelper_WithoutUnix struct {
GenBaseHelper
From int64
To int64
ResolutionSeconds int
Timestamps []int64
Items []any
}
type GenBaseHelper struct {
}
func (h GenBaseHelper) RandomInt(s, e int) int {
return rand.Intn(e-s+1) + s
}
func (h GenBaseHelper) RandomFloat64(s, e float64) float64 {
return rand.Float64()*(e-s) + s
}
func BuildGenHelper_WithoutUnix(from, to int64, resolutionSeconds int) GenHelper_WithoutUnix {
helper := GenHelper_WithoutUnix{
From: from,
To: to,
ResolutionSeconds: resolutionSeconds,
GenBaseHelper: GenBaseHelper{},
}
if resolutionSeconds == 0 {
return helper
}
stepN := int64(resolutionSeconds)
num := int(((to - from) / stepN) + 1)
helper.Timestamps = make([]int64, 0, num)
for t := from; t < to; t += stepN {
helper.Timestamps = append(helper.Timestamps, t)
}
return helper
}
func (helper GenHelper_WithoutUnix) Exec(templateFilePath, outputDir string, items []any, blockHours int) error {
helper.Items = items
log.Green("process template...")
buf, err := ProcessTemplate(templateFilePath, outputDir, helper)
if err != nil {
return err
}
resultBytes := buf.Bytes()
resultBytes = append(resultBytes, []byte("# EOF")...)
log.Green("create blocks...")
_ = os.MkdirAll(outputDir, os.ModePerm)
err = Backfill(5000, resultBytes, outputDir, true, false, time.Duration(blockHours)*time.Hour)
return err
}
func (helper GenHelper_WithUnix) Exec(templateFilePath, outputDir string, items []any, blockHours int) error {
helper.Items = items
log.Green("process template...")
buf, err := ProcessTemplate(templateFilePath, outputDir, helper)
if err != nil {
return err
}
resultBytes := buf.Bytes()
resultBytes = append(resultBytes, []byte("# EOF")...)
log.Green("create blocks...")
_ = os.MkdirAll(outputDir, os.ModePerm)
err = Backfill(5000, resultBytes, outputDir, true, false, time.Duration(blockHours)*time.Hour)
return err
}
func ProcessTemplate(templateFilePath, outputDir string, tplValue any) (bytes.Buffer, error) {
name := path.Base(templateFilePath)
temp := template.Must(template.New(name).ParseFiles(templateFilePath))
var buf bytes.Buffer
err := temp.Execute(&buf, tplValue)
return buf, err
}
type GenHelper_WithUnix struct {
GenBaseHelper
Items []any
}
func BuildGenHelper_WithUnix() GenHelper_WithUnix {
helper := GenHelper_WithUnix{
GenBaseHelper: GenBaseHelper{},
}
return helper
}