forked from go-graphite/graphite-clickhouse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdate.go
More file actions
71 lines (59 loc) · 1.72 KB
/
Copy pathdate.go
File metadata and controls
71 lines (59 loc) · 1.72 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
package finder
import (
"context"
"fmt"
"time"
"github.com/lomik/graphite-clickhouse/config"
"github.com/lomik/graphite-clickhouse/helper/clickhouse"
"github.com/lomik/graphite-clickhouse/metrics"
"github.com/lomik/graphite-clickhouse/pkg/scope"
"github.com/lomik/graphite-clickhouse/pkg/where"
)
type DateFinder struct {
*BaseFinder
tableVersion int
}
func NewDateFinder(url string, table string, tableVersion int, opts clickhouse.Options) Finder {
if tableVersion == 3 {
return NewDateFinderV3(url, table, opts)
}
b := &BaseFinder{
url: url,
table: table,
opts: opts,
}
return &DateFinder{b, tableVersion}
}
func (b *DateFinder) Execute(ctx context.Context, config *config.Config, query string, from int64, until int64) (err error) {
w := b.where(query)
dateWhere := where.New()
dateWhere.Andf(
"Date >='%s' AND Date <= '%s'",
time.Unix(from, 0).Format("2006-01-02"),
time.Unix(until, 0).Format("2006-01-02"),
)
b.stats = append(b.stats, metrics.FinderStat{})
stat := &b.stats[len(b.stats)-1]
if b.tableVersion == 2 {
b.body, stat.ChReadRows, stat.ChReadBytes, err = clickhouse.Query(
scope.WithTable(ctx, b.table),
b.url,
// TODO: consider consistent query generator
fmt.Sprintf(`SELECT Path FROM %s PREWHERE (%s) WHERE %s GROUP BY Path FORMAT TabSeparatedRaw`, b.table, dateWhere, w),
b.opts,
nil,
)
} else {
b.body, stat.ChReadRows, stat.ChReadBytes, err = clickhouse.Query(
scope.WithTable(ctx, b.table),
b.url,
// TODO: consider consistent query generator
fmt.Sprintf(`SELECT DISTINCT Path FROM %s PREWHERE (%s) WHERE (%s) FORMAT TabSeparatedRaw`, b.table, dateWhere, w),
b.opts,
nil,
)
}
stat.ReadBytes = int64(len(b.body))
stat.Table = b.table
return
}