github.com/thanos-io/thanos@v0.32.5/pkg/ui/query.go (about)

     1  // Copyright (c) The Thanos Authors.
     2  // Licensed under the Apache License 2.0.
     3  
     4  package ui
     5  
     6  import (
     7  	"html/template"
     8  	"strings"
     9  	"time"
    10  
    11  	"github.com/go-kit/log"
    12  	"github.com/prometheus/common/model"
    13  	"github.com/prometheus/common/route"
    14  
    15  	"github.com/thanos-io/thanos/pkg/api"
    16  	"github.com/thanos-io/thanos/pkg/component"
    17  	extpromhttp "github.com/thanos-io/thanos/pkg/extprom/http"
    18  	"github.com/thanos-io/thanos/pkg/query"
    19  )
    20  
    21  type Query struct {
    22  	*BaseUI
    23  	endpointSet *query.EndpointSet
    24  
    25  	externalPrefix, prefixHeader string
    26  
    27  	cwd     string
    28  	birth   time.Time
    29  	version api.ThanosVersion
    30  	now     func() model.Time
    31  }
    32  
    33  func NewQueryUI(logger log.Logger, endpointSet *query.EndpointSet, externalPrefix, prefixHeader, alertQueryURL string) *Query {
    34  	tmplVariables := map[string]string{
    35  		"Component": component.Query.String(),
    36  		"queryURL":  alertQueryURL,
    37  	}
    38  	runtimeInfo := api.GetRuntimeInfoFunc(logger)
    39  
    40  	tmplFuncs := queryTmplFuncs()
    41  
    42  	return &Query{
    43  		BaseUI:         NewBaseUI(logger, tmplFuncs, tmplVariables, externalPrefix, prefixHeader, component.Query),
    44  		endpointSet:    endpointSet,
    45  		externalPrefix: externalPrefix,
    46  		prefixHeader:   prefixHeader,
    47  		cwd:            runtimeInfo().CWD,
    48  		birth:          runtimeInfo().StartTime,
    49  		version:        *api.BuildInfo,
    50  		now:            model.Now,
    51  	}
    52  }
    53  
    54  func queryTmplFuncs() template.FuncMap {
    55  	return template.FuncMap{
    56  		"since": func(t time.Time) time.Duration {
    57  			return time.Since(t) / time.Millisecond * time.Millisecond
    58  		},
    59  		"formatTimestamp": func(timestamp int64) string {
    60  			return time.Unix(timestamp/1000, 0).Format(time.RFC3339)
    61  		},
    62  		"title": strings.Title, // nolint TODO(bwplotka): Find replacement.
    63  	}
    64  }
    65  
    66  // Register registers new GET routes for subpages and redirects from / to /graph.
    67  func (q *Query) Register(r *route.Router, ins extpromhttp.InstrumentationMiddleware) {
    68  	registerReactApp(r, ins, q.BaseUI)
    69  
    70  	// TODO(bplotka): Consider adding more Thanos related data e.g:
    71  	// - What store nodes we see currently.
    72  	// - What sidecars we see currently.
    73  }