-
Notifications
You must be signed in to change notification settings - Fork 153
/
input.go
487 lines (428 loc) · 10.9 KB
/
input.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
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
package gen
import (
"container/heap"
"errors"
"math"
"math/rand"
"strings"
"time"
"github.com/influxdata/flux"
"github.com/influxdata/flux/csv"
"github.com/influxdata/flux/execute"
"github.com/influxdata/flux/memory"
"github.com/influxdata/flux/plan"
"github.com/influxdata/flux/semantic"
"github.com/influxdata/flux/values"
)
const (
// DefaultNumPoints is the default number of points that should
// be generated for each series.
DefaultNumPoints = 6
// DefaultPeriod is the default period between points in a series.
DefaultPeriod = 10 * time.Second
)
// Tag includes the tag name and the cardinality for that tag in
// the schema.
type Tag struct {
Name string
Cardinality int
}
// Schema describes the schema to be generated.
type Schema struct {
// Start is the start time for generating data. This will default
// so the current time would be the last point generated, but
// truncated to the period.
Start time.Time
// Tags is a listing of tags and the generated cardinality for
// that tag.
Tags []Tag
// NumPoints is the number of points that should be generated
// for each series. This defaults to 6.
NumPoints int
// Nulls sets the percentage changes that a null value will
// be used in the input. This should be a number between 0 and 1.
Nulls float64
// Period contains the distance between each point in a series.
// This defaults to 10 seconds.
Period time.Duration
// GroupBy is a list of tags that, if they have the same value,
// will have the same type even if the types ratio becomes
// impossible to fulfill. This only does something if Types
// has been set.
GroupBy []string
// Types includes a mapping of the column value type
// to the ratio for how frequently it should show up
// in the output. If this is left blank, all series
// will be generated with a float value.
Types map[flux.ColType]int
// Seed is the (optional) seed to be used by the random
// number generator. If this is null, the current time
// will be used.
Seed *int64
// Alloc assigns an allocator to use when generating the
// tables. If this is not set, an unlimited allocator is
// used.
Alloc *memory.Allocator
}
// Input constructs a ResultIterator with randomly generated
// data according to the Schema.
func Input(schema Schema) (flux.ResultIterator, error) {
tags := schema.Tags
var seed int64
if schema.Seed != nil {
seed = *schema.Seed
} else {
seed = time.Now().UnixNano()
}
r := rand.New(rand.NewSource(seed))
series := genSeriesKeys(tags, r)
if len(series) == 0 {
return nil, errors.New("at least one tag with a positive cardinality is required")
}
var ti typeInfo
if len(schema.Types) > 0 {
var total int
for _, count := range schema.Types {
total += count
}
for typ, count := range schema.Types {
ti = append(ti, valueType{
Type: typ,
Number: int(math.Round(float64(len(series)) * float64(count) / float64(total))),
})
}
} else {
ti = typeInfo{
valueType{
Type: flux.TFloat,
Number: len(series),
},
}
}
heap.Init(&ti)
var groupTags []string
if len(ti) > 1 {
groupTags = schema.GroupBy
}
groups := seriesGroups(groupBy(series, groupTags))
heap.Init(&groups)
period := schema.Period
if period == 0 {
period = DefaultPeriod
}
numPoints := schema.NumPoints
if numPoints == 0 {
numPoints = DefaultNumPoints
}
g := &dataGenerator{
Period: values.Duration(period),
NumPoints: numPoints,
Nulls: schema.Nulls,
}
if !schema.Start.IsZero() {
g.Start = values.ConvertTime(schema.Start)
} else {
ts := time.Now().Truncate(period).Add(-period * time.Duration(numPoints))
g.Start = values.ConvertTime(ts)
}
alloc := schema.Alloc
if alloc == nil {
alloc = &memory.Allocator{}
}
cache := execute.NewTableBuilderCache(alloc)
cache.SetTriggerSpec(plan.DefaultTriggerSpec)
for {
if len(groups) == 0 {
break
}
sg := heap.Pop(&groups).(seriesGroup)
vt := heap.Pop(&ti).(valueType)
vt.Number -= len(sg.Series)
sg.Type = vt.Type
for _, s := range sg.Series {
builder, _ := cache.TableBuilder(s)
startIdx, _ := builder.AddCol(flux.ColMeta{
Label: execute.DefaultStartColLabel,
Type: flux.TTime,
})
stopIdx, _ := builder.AddCol(flux.ColMeta{
Label: execute.DefaultStopColLabel,
Type: flux.TTime,
})
_ = execute.AddTableKeyCols(s, builder)
start, stop := g.Generate(builder, r, sg.Type)
for i := 0; i < g.NumPoints; i++ {
_ = builder.AppendTime(startIdx, start)
_ = builder.AppendTime(stopIdx, stop)
_ = execute.AppendKeyValues(s, builder)
}
}
heap.Push(&ti, vt)
}
var (
tables []flux.Table
err error
)
cache.ForEachBuilder(func(key flux.GroupKey, builder execute.TableBuilder) {
tbl, terr := builder.Table()
if terr != nil && err == nil {
err = terr
return
}
tables = append(tables, tbl)
})
if err != nil {
return nil, err
}
return flux.NewSliceResultIterator([]flux.Result{
&result{tables: tables},
}), nil
}
// CsvInput generates a csv input based on the Schema.
func CsvInput(schema Schema) (string, error) {
results, err := Input(schema)
if err != nil {
return "", err
}
var buf strings.Builder
enc := csv.NewMultiResultEncoder(csv.DefaultEncoderConfig())
if _, err := enc.Encode(&buf, results); err != nil {
return "", err
}
return buf.String(), nil
}
// seriesGroup is a group of series that should have the same type.
type seriesGroup struct {
Series []flux.GroupKey
Type flux.ColType
}
type seriesGroups []seriesGroup
func (a *seriesGroups) Len() int {
return len(*a)
}
func (a *seriesGroups) Less(i, j int) bool {
return len((*a)[i].Series) > len((*a)[j].Series)
}
func (a *seriesGroups) Swap(i, j int) {
(*a)[i], (*a)[j] = (*a)[j], (*a)[i]
}
func (a *seriesGroups) Push(x interface{}) {
*a = append(*a, x.(seriesGroup))
}
func (a *seriesGroups) Pop() interface{} {
sg := (*a)[len(*a)-1]
*a = (*a)[:len(*a)-1]
return sg
}
// valueType keeps a mapping of the number of series we wish to generate for each type.
type valueType struct {
Type flux.ColType
Number int
}
type typeInfo []valueType
func (a *typeInfo) Len() int {
return len(*a)
}
func (a *typeInfo) Less(i, j int) bool {
return (*a)[i].Number > (*a)[j].Number
}
func (a *typeInfo) Swap(i, j int) {
(*a)[i], (*a)[j] = (*a)[j], (*a)[i]
}
func (a *typeInfo) Push(x interface{}) {
*a = append(*a, x.(valueType))
}
func (a *typeInfo) Pop() interface{} {
vt := (*a)[len(*a)-1]
*a = (*a)[:len(*a)-1]
return vt
}
func genTagValue(r *rand.Rand, min, max int) string {
var buf strings.Builder
sz := r.Intn(max-min) + min
for i := 0; i < sz; i++ {
chars := 62
if i == 0 {
chars = 52
}
switch n := r.Intn(chars); {
case n >= 0 && n < 26:
buf.WriteByte('A' + byte(n))
case n >= 26 && n < 52:
buf.WriteByte('a' + byte(n-26))
case n >= 52:
buf.WriteByte('0' + byte(n-52))
}
}
return buf.String()
}
func genTagValues(r *rand.Rand, cardinality, min, max int) []string {
values := make([]string, 0, cardinality)
for i := 0; i < cardinality; i++ {
v := genTagValue(r, min, max)
values = append(values, v)
}
return values
}
func appendTagKey(series []flux.GroupKey, k string, vs []string) []flux.GroupKey {
if len(vs) == 0 {
return series
}
if len(series) == 0 {
series = []flux.GroupKey{nil}
}
newSeries := make([]flux.GroupKey, 0, len(series)*len(vs))
for _, s := range series {
for _, v := range vs {
gkb := execute.NewGroupKeyBuilder(s)
gkb.AddKeyValue(k, values.NewString(v))
key, _ := gkb.Build()
newSeries = append(newSeries, key)
}
}
return newSeries
}
func genSeriesKeys(tags []Tag, r *rand.Rand) []flux.GroupKey {
var keys []flux.GroupKey
for _, tag := range tags {
if tag.Cardinality == 0 {
continue
}
keys = appendTagKey(keys, tag.Name, genTagValues(r, tag.Cardinality, 3, 8))
}
return keys
}
func groupBy(keys []flux.GroupKey, by []string) []seriesGroup {
if len(by) == 0 {
groups := make([]seriesGroup, 0, len(keys))
for _, k := range keys {
groups = append(groups, seriesGroup{
Series: []flux.GroupKey{k},
})
}
return groups
}
var groups []seriesGroup
mapping := make(map[string]*seriesGroup)
for _, k := range keys {
parts := make([]string, 0, len(by))
for _, s := range by {
idx := execute.ColIdx(s, k.Cols())
if idx == -1 {
continue
}
parts = append(parts, k.ValueString(idx))
}
if len(parts) == 0 {
groups = append(groups, seriesGroup{
Series: []flux.GroupKey{k},
})
continue
}
groupKey := strings.Join(parts, ",")
gr, ok := mapping[groupKey]
if !ok {
groups = append(groups, seriesGroup{
Series: []flux.GroupKey{k},
})
mapping[groupKey] = &groups[len(groups)-1]
continue
}
gr.Series = append(gr.Series, k)
}
return groups
}
type dataGenerator struct {
Start values.Time
Period values.Duration
Jitter values.Duration
Nulls float64
NumPoints int
}
func (dg *dataGenerator) Generate(tb execute.TableBuilder, r *rand.Rand, typ flux.ColType) (start, stop values.Time) {
var next func() values.Value
switch typ {
case flux.TFloat:
next = func() values.Value {
if dg.Nulls > 0.0 && dg.Nulls > r.Float64() {
return values.NewNull(semantic.Float)
}
v := rand.NormFloat64() * 50
return values.NewFloat(v)
}
case flux.TInt:
next = func() values.Value {
if dg.Nulls > 0.0 && dg.Nulls > r.Float64() {
return values.NewNull(semantic.Int)
}
v := rand.Intn(201) - 100
return values.NewInt(int64(v))
}
case flux.TUInt:
next = func() values.Value {
if dg.Nulls > 0.0 && dg.Nulls > r.Float64() {
return values.NewNull(semantic.UInt)
}
v := rand.Intn(101)
return values.NewUInt(uint64(v))
}
case flux.TString:
next = func() values.Value {
if dg.Nulls > 0.0 && dg.Nulls > r.Float64() {
return values.NewNull(semantic.String)
}
v := genTagValue(r, 3, 8)
return values.NewString(v)
}
case flux.TBool:
next = func() values.Value {
if dg.Nulls > 0.0 && dg.Nulls > r.Float64() {
return values.NewNull(semantic.Bool)
}
v := r.Intn(2) == 1
return values.NewBool(v)
}
default:
panic("implement me")
}
timeIdx, _ := tb.AddCol(flux.ColMeta{
Label: execute.DefaultTimeColLabel,
Type: flux.TTime,
})
valueIdx, _ := tb.AddCol(flux.ColMeta{
Label: execute.DefaultValueColLabel,
Type: typ,
})
start, stop = dg.Start, dg.Start
for i := 0; i < dg.NumPoints; i++ {
ts := dg.Start.Add(values.Duration(i) * dg.Period)
if dg.Jitter != 0 {
jitter := r.Intn(int(dg.Jitter)*2 + 1)
ts = ts.Add(values.Duration(jitter))
}
_ = tb.AppendTime(timeIdx, ts)
_ = tb.AppendValue(valueIdx, next())
_ = tb.AppendValue(valueIdx, next())
if ts > stop {
stop = ts
}
}
return start, stop
}
type result struct {
tables []flux.Table
}
func (r *result) Do(f func(flux.Table) error) error {
for _, tbl := range r.tables {
if err := f(tbl); err != nil {
return err
}
}
return nil
}
func (r *result) Name() string {
return ""
}
func (r *result) Tables() flux.TableIterator {
return r
}