|
| 1 | +package otelmapping |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net/http" |
| 6 | + |
| 7 | + "github.com/spf13/cobra" |
| 8 | + "github.com/stackvista/stackstate-cli/generated/stackstate_api" |
| 9 | + "github.com/stackvista/stackstate-cli/internal/common" |
| 10 | + "github.com/stackvista/stackstate-cli/internal/di" |
| 11 | + "github.com/stackvista/stackstate-cli/internal/printer" |
| 12 | + "golang.org/x/text/cases" |
| 13 | + "golang.org/x/text/language" |
| 14 | +) |
| 15 | + |
| 16 | +type StatusArgs struct { |
| 17 | + Identifier string |
| 18 | +} |
| 19 | + |
| 20 | +func FormatOtelMappingStatusTable(otelmappings []stackstate_api.OtelMappingStatusItem) printer.TableData { |
| 21 | + data := make([][]interface{}, len(otelmappings)) |
| 22 | + |
| 23 | + for i, otelmapping := range otelmappings { |
| 24 | + identifier := "-" |
| 25 | + if otelmapping.HasIdentifier() { |
| 26 | + identifier = otelmapping.GetIdentifier() |
| 27 | + } |
| 28 | + data[i] = []interface{}{ |
| 29 | + otelmapping.Name, |
| 30 | + identifier, |
| 31 | + otelmapping.ComponentCount, |
| 32 | + otelmapping.RelationCount, |
| 33 | + } |
| 34 | + } |
| 35 | + return printer.TableData{ |
| 36 | + Header: []string{"Name", "Identifier", "Components", "Relations"}, |
| 37 | + Data: data, |
| 38 | + MissingTableDataMsg: printer.NotFoundMsg{Types: "otel mappings"}, |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func FormatOtelMappingTable(otelmappings []stackstate_api.OtelMappingItem) printer.TableData { |
| 43 | + data := make([][]interface{}, len(otelmappings)) |
| 44 | + |
| 45 | + for i, otelmapping := range otelmappings { |
| 46 | + identifier := "-" |
| 47 | + if otelmapping.HasIdentifier() { |
| 48 | + identifier = otelmapping.GetIdentifier() |
| 49 | + } |
| 50 | + data[i] = []interface{}{ |
| 51 | + otelmapping.Name, |
| 52 | + identifier, |
| 53 | + } |
| 54 | + } |
| 55 | + return printer.TableData{ |
| 56 | + Header: []string{"Name", "Identifier"}, |
| 57 | + Data: data, |
| 58 | + MissingTableDataMsg: printer.NotFoundMsg{Types: "otel mappings"}, |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +type StatusFetcher func(cli *di.Deps, api *stackstate_api.APIClient, identifier string) (*stackstate_api.OtelMappingStatus, *http.Response, error) |
| 63 | + |
| 64 | +func FetchComponentStatus(cli *di.Deps, api *stackstate_api.APIClient, identifier string) (*stackstate_api.OtelMappingStatus, *http.Response, error) { |
| 65 | + return api.OtelMappingApi.GetOtelComponentMappingStatus(cli.Context, identifier).Execute() |
| 66 | +} |
| 67 | + |
| 68 | +func FetchRelationStatus(cli *di.Deps, api *stackstate_api.APIClient, identifier string) (*stackstate_api.OtelMappingStatus, *http.Response, error) { |
| 69 | + return api.OtelMappingApi.GetOtelRelationMappingStatus(cli.Context, identifier).Execute() |
| 70 | +} |
| 71 | + |
| 72 | +func RunStatus(args *StatusArgs, mappingType string, fetch StatusFetcher) di.CmdWithApiFn { |
| 73 | + return func(cmd *cobra.Command, cli *di.Deps, api *stackstate_api.APIClient, serverInfo *stackstate_api.ServerInfo) common.CLIError { |
| 74 | + mappingStatus, resp, err := fetch(cli, api, args.Identifier) |
| 75 | + if err != nil { |
| 76 | + return common.NewResponseError(err, resp) |
| 77 | + } |
| 78 | + |
| 79 | + jsonKey := fmt.Sprintf("otel-%s-mapping", mappingType) |
| 80 | + title := cases.Title(language.English).String(mappingType) |
| 81 | + mappingTitle := fmt.Sprintf("Otel %s Mapping:", title) |
| 82 | + metricsTitle := fmt.Sprintf("Otel %s Mapping Metrics:", title) |
| 83 | + errorsTitle := fmt.Sprintf("Otel %s Mapping Errors:", title) |
| 84 | + errorsNotFoundMsg := fmt.Sprintf("otel %s mapping errors", mappingType) |
| 85 | + |
| 86 | + if cli.IsJson() { |
| 87 | + cli.Printer.PrintJson(map[string]interface{}{ |
| 88 | + jsonKey: mappingStatus, |
| 89 | + }) |
| 90 | + } else { |
| 91 | + cli.Printer.PrintLn("\n") |
| 92 | + cli.Printer.PrintLn(mappingTitle) |
| 93 | + cli.Printer.Table(FormatOtelMappingStatusTable([]stackstate_api.OtelMappingStatusItem{ |
| 94 | + mappingStatus.Item, |
| 95 | + })) |
| 96 | + |
| 97 | + if mappingStatus.HasMetrics() { |
| 98 | + cli.Printer.PrintLn("\n") |
| 99 | + cli.Printer.PrintLn(metricsTitle) |
| 100 | + size := mappingStatus.Metrics.BucketSizeSeconds |
| 101 | + cli.Printer.Table(printer.TableData{ |
| 102 | + Header: []string{"Metric", fmt.Sprintf("%ds ago", size), fmt.Sprintf("%d-%ds ago", size, 2*size), fmt.Sprintf("%d-%ds ago", 2*size, 3*size)}, //nolint:mnd |
| 103 | + Data: [][]interface{}{ |
| 104 | + printer.MetricBucketToRow("latency seconds", mappingStatus.Metrics.LatencySeconds), |
| 105 | + }, |
| 106 | + MissingTableDataMsg: printer.NotFoundMsg{Types: "metrics"}, |
| 107 | + }) |
| 108 | + } |
| 109 | + |
| 110 | + data := make([][]interface{}, len(mappingStatus.ErrorDetails)) |
| 111 | + for i, error := range mappingStatus.ErrorDetails { |
| 112 | + id := "-" |
| 113 | + if error.HasIssueId() { |
| 114 | + id = *error.IssueId |
| 115 | + } |
| 116 | + data[i] = []interface{}{id, error.Level, error.Message} |
| 117 | + } |
| 118 | + |
| 119 | + cli.Printer.PrintLn("\n") |
| 120 | + cli.Printer.PrintLn(errorsTitle) |
| 121 | + cli.Printer.Table(printer.TableData{ |
| 122 | + Header: []string{"Issue Id", "Level", "Message"}, |
| 123 | + Data: data, |
| 124 | + MissingTableDataMsg: printer.NotFoundMsg{Types: errorsNotFoundMsg}, |
| 125 | + }) |
| 126 | + } |
| 127 | + |
| 128 | + return nil |
| 129 | + } |
| 130 | +} |
0 commit comments