github.com/choria-io/go-choria@v0.28.1-0.20240416190746-b3bf9c7d5a45/providers/agent/mcorpc/aggregate/aggregate.go (about) 1 // Copyright (c) 2020-2022, R.I. Pienaar and the Choria Project contributors 2 // 3 // SPDX-License-Identifier: Apache-2.0 4 5 package aggregate 6 7 import ( 8 "fmt" 9 ) 10 11 // Aggregator can summarize rpc reply data 12 type Aggregator interface { 13 ProcessValue(any) error 14 ResultStrings() (map[string]string, error) 15 ResultFormattedStrings(format string) ([]string, error) 16 ResultJSON() ([]byte, error) 17 Type() string 18 } 19 20 // AggregatorByType retrieves an instance of an aggregator given its type like "summarize" 21 func AggregatorByType(t string, args []any) (Aggregator, error) { 22 switch t { 23 case "summary", "boolean_summary": 24 return NewSummaryAggregator(args) 25 26 case "average": 27 return NewAverageAggregator(args) 28 29 case "chart": 30 return NewChartAggregator(args) 31 32 default: 33 return nil, fmt.Errorf("unknown aggregator '%s'", t) 34 } 35 } 36 37 func parseFormatFromArgs(args []any) string { 38 if len(args) == 2 { 39 cfg, ok := args[1].(map[string]any) 40 if ok { 41 fmt, ok := cfg["format"] 42 if ok { 43 format, ok := fmt.(string) 44 if ok { 45 return format 46 } 47 } 48 } 49 } 50 51 return "" 52 }