generated from jfrog/jfrog-cli-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
graph.go
124 lines (109 loc) · 3.11 KB
/
graph.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package commands
import (
"context"
"fmt"
"github.com/eldada/metrics-viewer/models"
"github.com/eldada/metrics-viewer/provider"
"github.com/eldada/metrics-viewer/visualization"
"github.com/jfrog/jfrog-cli-core/v2/plugins/components"
"github.com/jfrog/jfrog-client-go/utils/log"
"strconv"
"time"
)
func GetGraphCommand() components.Command {
return components.Command{
Name: "graph",
Description: "Easily graph Open Metrics data in terminal",
Aliases: []string{"g"},
Flags: getGraphFlags(),
Action: func(c *components.Context) error {
return graphCmd(c)
},
}
}
func getGraphFlags() []components.Flag {
return append(
getCommonFlags(),
components.StringFlag{
Name: "time",
Description: "Time window to show in seconds",
DefaultValue: "300",
},
)
}
type graphConfiguration struct {
commonConfiguration
timeWindow time.Duration
}
func (c graphConfiguration) TimeWindow() time.Duration {
return c.timeWindow
}
func (c graphConfiguration) String() string {
return fmt.Sprintf("%s, time: %s", c.commonConfiguration, c.timeWindow)
}
func graphCmd(c *components.Context) error {
conf, err := parseGraphCmdConfig(c)
if err != nil {
return err
}
log.Debug("command config:", conf)
prov, err := newGraphMetricsProvider(conf)
if err != nil {
return err
}
visualization.NewIndex().Present(context.TODO(), conf.interval, prov)
return nil
}
func parseGraphCmdConfig(c cliContext) (*graphConfiguration, error) {
commonConfig, err := parseCommonConfig(c)
if err != nil {
return nil, err
}
conf := graphConfiguration{
commonConfiguration: *commonConfig,
}
flagValue := c.GetStringFlagValue("time")
intValue, err := strconv.ParseInt(flagValue, 10, 64)
if err != nil {
return nil, fmt.Errorf("failed to parse time window value: %s; cause: %w", flagValue, err)
}
if intValue <= 0 {
return nil, fmt.Errorf("time window value must be positive; got: %d", intValue)
}
conf.timeWindow = time.Duration(intValue) * time.Second
return &conf, nil
}
func newGraphMetricsProvider(conf provider.Config) (*graphMetricsProvider, error) {
prov, err := provider.New(conf)
if err != nil {
return nil, err
}
return &graphMetricsProvider{
provider: prov,
mapMetrics: provider.NewLabelsMetricsMapper(conf.AggregateIgnoreLabels(), ","),
shouldKeepMetrics: provider.NewRegexMetricsFilter(conf.Filter()),
cachedMetrics: provider.NewMetricsCache(conf.TimeWindow()),
}, nil
}
type graphMetricsProvider struct {
provider provider.Provider
mapMetrics provider.MetricsMapperFunc
shouldKeepMetrics provider.MetricsFilterFunc
cachedMetrics *provider.MetricsCache
}
func (p graphMetricsProvider) Get() ([]models.Metrics, error) {
metricsCollection, err := p.provider.Get()
if err != nil {
return nil, err
}
newCollection := p.mapMetrics(metricsCollection)
filteredCollection := make([]models.Metrics, 0)
for _, metrics := range newCollection {
if !p.shouldKeepMetrics(metrics) {
continue
}
filteredCollection = append(filteredCollection, metrics)
}
newCollection = p.cachedMetrics.Add(filteredCollection)
return newCollection, nil
}