master
go 60 lines 1.43 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package clickhouse
4
5 import (
6 "context"
7 "fmt"
8
9 "github.com/netdata/netdata/go/plugins/pkg/funcapi"
10 "github.com/netdata/netdata/go/plugins/plugin/framework/collectorapi"
11 )
12
13 func clickhouseMethods() []funcapi.MethodConfig {
14 return []funcapi.MethodConfig{
15 topQueriesMethodConfig(),
16 }
17 }
18
19 func clickhouseFunctionHandler(job collectorapi.RuntimeJob) funcapi.MethodHandler {
20 c, ok := job.Collector().(*Collector)
21 if !ok {
22 return nil
23 }
24 return c.funcRouter
25 }
26
27 // funcRouter routes method calls to appropriate function handlers.
28 type funcRouter struct {
29 collector *Collector
30 handlers map[string]funcapi.MethodHandler
31 }
32
33 func newFuncRouter(c *Collector) *funcRouter {
34 r := &funcRouter{
35 collector: c,
36 handlers: make(map[string]funcapi.MethodHandler),
37 }
38 r.handlers[topQueriesMethodID] = newFuncTopQueries(r)
39 return r
40 }
41
42 func (r *funcRouter) MethodParams(ctx context.Context, method string) ([]funcapi.ParamConfig, error) {
43 if h, ok := r.handlers[method]; ok {
44 return h.MethodParams(ctx, method)
45 }
46 return nil, fmt.Errorf("unknown method: %s", method)
47 }
48
49 func (r *funcRouter) Handle(ctx context.Context, method string, params funcapi.ResolvedParams) *funcapi.FunctionResponse {
50 if h, ok := r.handlers[method]; ok {
51 return h.Handle(ctx, method, params)
52 }
53 return funcapi.NotFoundResponse(method)
54 }
55
56 func (r *funcRouter) Cleanup(ctx context.Context) {
57 for _, h := range r.handlers {
58 h.Cleanup(ctx)
59 }
60 }