| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package elasticsearch |
| 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 | // funcRouter routes method calls to appropriate function handlers. |
| 14 | type funcRouter struct { |
| 15 | collector *Collector |
| 16 | handlers map[string]funcapi.MethodHandler |
| 17 | } |
| 18 | |
| 19 | func newFuncRouter(c *Collector) *funcRouter { |
| 20 | r := &funcRouter{ |
| 21 | collector: c, |
| 22 | handlers: make(map[string]funcapi.MethodHandler), |
| 23 | } |
| 24 | r.handlers[topQueriesMethodID] = newFuncTopQueries(r) |
| 25 | return r |
| 26 | } |
| 27 | |
| 28 | // Compile-time interface check. |
| 29 | var _ funcapi.MethodHandler = (*funcRouter)(nil) |
| 30 | |
| 31 | func (r *funcRouter) MethodParams(ctx context.Context, method string) ([]funcapi.ParamConfig, error) { |
| 32 | if h, ok := r.handlers[method]; ok { |
| 33 | return h.MethodParams(ctx, method) |
| 34 | } |
| 35 | return nil, fmt.Errorf("unknown method: %s", method) |
| 36 | } |
| 37 | |
| 38 | func (r *funcRouter) Handle(ctx context.Context, method string, params funcapi.ResolvedParams) *funcapi.FunctionResponse { |
| 39 | if h, ok := r.handlers[method]; ok { |
| 40 | return h.Handle(ctx, method, params) |
| 41 | } |
| 42 | return funcapi.NotFoundResponse(method) |
| 43 | } |
| 44 | |
| 45 | func (r *funcRouter) Cleanup(ctx context.Context) { |
| 46 | for _, h := range r.handlers { |
| 47 | h.Cleanup(ctx) |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | func elasticsearchMethods() []funcapi.MethodConfig { |
| 52 | return []funcapi.MethodConfig{ |
| 53 | topQueriesMethodConfig(), |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | func elasticsearchFunctionHandler(job collectorapi.RuntimeJob) funcapi.MethodHandler { |
| 58 | c, ok := job.Collector().(*Collector) |
| 59 | if !ok { |
| 60 | return nil |
| 61 | } |
| 62 | return c.funcRouter |
| 63 | } |