From 29c77cf6ea1a6e28110ae37c7c87a3039b141473 Mon Sep 17 00:00:00 2001
From: Roman Lomonosov <r.lomonosov@gmail.com>
Date: Sun, 14 Jul 2019 12:31:24 +0300
Subject: [PATCH] use index table in /metrics/index.json

---
 index/index.go | 38 ++++++++++++++++++++++++++++----------
 1 file changed, 28 insertions(+), 10 deletions(-)

diff --git a/index/index.go b/index/index.go
index da657dd83..7612ff1b4 100644
--- a/index/index.go
+++ b/index/index.go
@@ -9,6 +9,7 @@ import (
 	"net/http"
 
 	"github.com/lomik/graphite-clickhouse/config"
+	"github.com/lomik/graphite-clickhouse/finder"
 	"github.com/lomik/graphite-clickhouse/helper/clickhouse"
 	"github.com/lomik/graphite-clickhouse/pkg/scope"
 )
@@ -19,18 +20,35 @@ type Index struct {
 }
 
 func New(config *config.Config, ctx context.Context) (*Index, error) {
-	opts := clickhouse.Options{
-		Timeout:        config.ClickHouse.TreeTimeout.Value(),
-		ConnectTimeout: config.ClickHouse.ConnectTimeout.Value(),
+	var reader io.ReadCloser
+	var err error
+
+	if config.ClickHouse.IndexTable != "" {
+		opts := clickhouse.Options{
+			Timeout:        config.ClickHouse.IndexTimeout.Value(),
+			ConnectTimeout: config.ClickHouse.ConnectTimeout.Value(),
+		}
+		reader, err = clickhouse.Reader(
+			scope.WithTable(ctx, config.ClickHouse.IndexTable),
+			config.ClickHouse.Url,
+			fmt.Sprintf("SELECT Path FROM %s WHERE Date = '%s' AND Level >= %d AND Level < %d GROUP BY Path",
+				config.ClickHouse.IndexTable, finder.DefaultTreeDate, finder.TreeLevelOffset, finder.ReverseTreeLevelOffset),
+			opts,
+		)
+	} else {
+		opts := clickhouse.Options{
+			Timeout:        config.ClickHouse.TreeTimeout.Value(),
+			ConnectTimeout: config.ClickHouse.ConnectTimeout.Value(),
+		}
+		reader, err = clickhouse.Reader(
+			scope.WithTable(ctx, config.ClickHouse.TreeTable),
+			config.ClickHouse.Url,
+			fmt.Sprintf("SELECT Path FROM %s GROUP BY Path", config.ClickHouse.TreeTable),
+			opts,
+		)
 	}
-	reader, err := clickhouse.Reader(
-		scope.WithTable(ctx, config.ClickHouse.TreeTable),
-		config.ClickHouse.Url,
-		fmt.Sprintf("SELECT Path FROM %s GROUP BY Path", config.ClickHouse.TreeTable),
-		opts,
-	)
+
 	if err != nil {
-		reader.Close()
 		return nil, err
 	}