Skip to content

Commit 29c77cf

Browse files
committed
use index table in /metrics/index.json
1 parent 9c9c96f commit 29c77cf

File tree

1 file changed

+28
-10
lines changed

1 file changed

+28
-10
lines changed

index/index.go

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"net/http"
1010

1111
"github.com/lomik/graphite-clickhouse/config"
12+
"github.com/lomik/graphite-clickhouse/finder"
1213
"github.com/lomik/graphite-clickhouse/helper/clickhouse"
1314
"github.com/lomik/graphite-clickhouse/pkg/scope"
1415
)
@@ -19,18 +20,35 @@ type Index struct {
1920
}
2021

2122
func New(config *config.Config, ctx context.Context) (*Index, error) {
22-
opts := clickhouse.Options{
23-
Timeout: config.ClickHouse.TreeTimeout.Value(),
24-
ConnectTimeout: config.ClickHouse.ConnectTimeout.Value(),
23+
var reader io.ReadCloser
24+
var err error
25+
26+
if config.ClickHouse.IndexTable != "" {
27+
opts := clickhouse.Options{
28+
Timeout: config.ClickHouse.IndexTimeout.Value(),
29+
ConnectTimeout: config.ClickHouse.ConnectTimeout.Value(),
30+
}
31+
reader, err = clickhouse.Reader(
32+
scope.WithTable(ctx, config.ClickHouse.IndexTable),
33+
config.ClickHouse.Url,
34+
fmt.Sprintf("SELECT Path FROM %s WHERE Date = '%s' AND Level >= %d AND Level < %d GROUP BY Path",
35+
config.ClickHouse.IndexTable, finder.DefaultTreeDate, finder.TreeLevelOffset, finder.ReverseTreeLevelOffset),
36+
opts,
37+
)
38+
} else {
39+
opts := clickhouse.Options{
40+
Timeout: config.ClickHouse.TreeTimeout.Value(),
41+
ConnectTimeout: config.ClickHouse.ConnectTimeout.Value(),
42+
}
43+
reader, err = clickhouse.Reader(
44+
scope.WithTable(ctx, config.ClickHouse.TreeTable),
45+
config.ClickHouse.Url,
46+
fmt.Sprintf("SELECT Path FROM %s GROUP BY Path", config.ClickHouse.TreeTable),
47+
opts,
48+
)
2549
}
26-
reader, err := clickhouse.Reader(
27-
scope.WithTable(ctx, config.ClickHouse.TreeTable),
28-
config.ClickHouse.Url,
29-
fmt.Sprintf("SELECT Path FROM %s GROUP BY Path", config.ClickHouse.TreeTable),
30-
opts,
31-
)
50+
3251
if err != nil {
33-
reader.Close()
3452
return nil, err
3553
}
3654

0 commit comments

Comments
 (0)