|
| 1 | +package modzy |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net/url" |
| 7 | + "strconv" |
| 8 | + "strings" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/modzy/sdk-go/internal/impossible" |
| 12 | + "github.com/modzy/sdk-go/model" |
| 13 | +) |
| 14 | + |
| 15 | +type DashboardClient interface { |
| 16 | + GetAlerts(ctx context.Context, input *GetAlertsInput) (*GetAlertsOutput, error) |
| 17 | + GetAlertDetails(ctx context.Context, input *GetAlertDetailsInput) (*GetAlertDetailsOutput, error) |
| 18 | + GetDataProcessed(ctx context.Context, input *GetDataProcessedInput) (*GetDataProcessedOutput, error) |
| 19 | + GetPredictionsMade(ctx context.Context, input *GetPredictionsMadeInput) (*GetPredictionsMadeOutput, error) |
| 20 | + GetActiveUsers(ctx context.Context, input *GetActiveUsersInput) (*GetActiveUsersOutput, error) |
| 21 | + GetActiveModels(ctx context.Context, input *GetActiveModelsInput) (*GetActiveModelsOutput, error) |
| 22 | + GetPrometheusMetric(ctx context.Context, input *GetPrometheusMetricInput) (*GetPrometheusMetricOutput, error) |
| 23 | +} |
| 24 | + |
| 25 | +type standardDashboardClient struct { |
| 26 | + baseClient *standardClient |
| 27 | +} |
| 28 | + |
| 29 | +var _ DashboardClient = &standardDashboardClient{} |
| 30 | + |
| 31 | +func (c *standardDashboardClient) GetAlerts(ctx context.Context, input *GetAlertsInput) (*GetAlertsOutput, error) { |
| 32 | + var out model.AlertsList |
| 33 | + path := "/api/notifications/alerts" |
| 34 | + _, err := c.baseClient.requestor.Get(ctx, path, &out) |
| 35 | + if err != nil { |
| 36 | + return nil, err |
| 37 | + } |
| 38 | + |
| 39 | + summaries := []AlertSummary{} |
| 40 | + for _, s := range out { |
| 41 | + summaries = append(summaries, AlertSummary{ |
| 42 | + Type: AlertType(s.Type), |
| 43 | + Count: s.Count, |
| 44 | + }) |
| 45 | + } |
| 46 | + return &GetAlertsOutput{ |
| 47 | + Alerts: summaries, |
| 48 | + }, nil |
| 49 | +} |
| 50 | + |
| 51 | +func (c *standardDashboardClient) GetAlertDetails(ctx context.Context, input *GetAlertDetailsInput) (*GetAlertDetailsOutput, error) { |
| 52 | + var out []string |
| 53 | + path := fmt.Sprintf("/api/notifications/alerts/%s", input.Type) |
| 54 | + _, err := c.baseClient.requestor.Get(ctx, path, &out) |
| 55 | + if err != nil { |
| 56 | + return nil, err |
| 57 | + } |
| 58 | + return &GetAlertDetailsOutput{ |
| 59 | + Type: input.Type, |
| 60 | + Entities: out, |
| 61 | + }, nil |
| 62 | +} |
| 63 | + |
| 64 | +func (c *standardDashboardClient) GetDataProcessed(ctx context.Context, input *GetDataProcessedInput) (*GetDataProcessedOutput, error) { |
| 65 | + url := c.parseDashboardFilters("/api/metrics/data-processed", dashboardFilters{ |
| 66 | + BeginDate: input.BeginDate, |
| 67 | + EndDate: input.EndDate, |
| 68 | + UserIdentifier: input.UserIdentifier, |
| 69 | + AccessKeyPrefix: input.AccessKeyPrefix, |
| 70 | + ModelIdentifier: input.ModelIdentifier, |
| 71 | + TeamIdentifier: input.TeamIdentifier, |
| 72 | + }) |
| 73 | + |
| 74 | + var out GetDataProcessedOutput |
| 75 | + _, err := c.baseClient.requestor.Get(ctx, url, &out) |
| 76 | + if err != nil { |
| 77 | + return nil, err |
| 78 | + } |
| 79 | + |
| 80 | + return &out, nil |
| 81 | +} |
| 82 | + |
| 83 | +func (c *standardDashboardClient) GetPredictionsMade(ctx context.Context, input *GetPredictionsMadeInput) (*GetPredictionsMadeOutput, error) { |
| 84 | + url := c.parseDashboardFilters("/api/metrics/predictions-made", dashboardFilters{ |
| 85 | + BeginDate: input.BeginDate, |
| 86 | + EndDate: input.EndDate, |
| 87 | + UserIdentifier: input.UserIdentifier, |
| 88 | + AccessKeyPrefix: input.AccessKeyPrefix, |
| 89 | + ModelIdentifier: input.ModelIdentifier, |
| 90 | + TeamIdentifier: input.TeamIdentifier, |
| 91 | + }) |
| 92 | + |
| 93 | + var out GetPredictionsMadeOutput |
| 94 | + _, err := c.baseClient.requestor.Get(ctx, url, &out) |
| 95 | + if err != nil { |
| 96 | + return nil, err |
| 97 | + } |
| 98 | + |
| 99 | + return &out, nil |
| 100 | +} |
| 101 | + |
| 102 | +func (c *standardDashboardClient) GetActiveUsers(ctx context.Context, input *GetActiveUsersInput) (*GetActiveUsersOutput, error) { |
| 103 | + url := c.parseDashboardFilters("/api/metrics/active-users", dashboardFilters{ |
| 104 | + BeginDate: input.BeginDate, |
| 105 | + EndDate: input.EndDate, |
| 106 | + UserIdentifier: input.UserIdentifier, |
| 107 | + AccessKeyPrefix: input.AccessKeyPrefix, |
| 108 | + ModelIdentifier: input.ModelIdentifier, |
| 109 | + TeamIdentifier: input.TeamIdentifier, |
| 110 | + }) |
| 111 | + |
| 112 | + var out []model.ActiveUserSummary |
| 113 | + _, err := c.baseClient.requestor.Get(ctx, url, &out) |
| 114 | + if err != nil { |
| 115 | + return nil, err |
| 116 | + } |
| 117 | + |
| 118 | + return &GetActiveUsersOutput{ |
| 119 | + Users: out, |
| 120 | + }, nil |
| 121 | +} |
| 122 | + |
| 123 | +func (c *standardDashboardClient) GetActiveModels(ctx context.Context, input *GetActiveModelsInput) (*GetActiveModelsOutput, error) { |
| 124 | + url := c.parseDashboardFilters("/api/metrics/active-models", dashboardFilters{ |
| 125 | + BeginDate: input.BeginDate, |
| 126 | + EndDate: input.EndDate, |
| 127 | + UserIdentifier: input.UserIdentifier, |
| 128 | + AccessKeyPrefix: input.AccessKeyPrefix, |
| 129 | + ModelIdentifier: input.ModelIdentifier, |
| 130 | + TeamIdentifier: input.TeamIdentifier, |
| 131 | + }) |
| 132 | + |
| 133 | + var out []model.ActiveModelSummary |
| 134 | + _, err := c.baseClient.requestor.Get(ctx, url, &out) |
| 135 | + if err != nil { |
| 136 | + return nil, err |
| 137 | + } |
| 138 | + |
| 139 | + return &GetActiveModelsOutput{ |
| 140 | + Models: out, |
| 141 | + }, nil |
| 142 | +} |
| 143 | + |
| 144 | +func (c *standardDashboardClient) GetPrometheusMetric(ctx context.Context, input *GetPrometheusMetricInput) (*GetPrometheusMetricOutput, error) { |
| 145 | + url := c.parseDashboardFilters( |
| 146 | + fmt.Sprintf("/api/metrics/prometheus/%s", input.Metric), |
| 147 | + dashboardFilters{ |
| 148 | + BeginDate: input.BeginDate, |
| 149 | + EndDate: input.EndDate, |
| 150 | + }, |
| 151 | + ) |
| 152 | + |
| 153 | + var out model.PrometheusResponse |
| 154 | + _, err := c.baseClient.requestor.Get(ctx, url, &out) |
| 155 | + if err != nil { |
| 156 | + return nil, err |
| 157 | + } |
| 158 | + |
| 159 | + var parsedValues []PrometheusValue |
| 160 | + |
| 161 | + for _, result := range out.Data.Results { |
| 162 | + for _, value := range result.Values { |
| 163 | + msg := fmt.Sprintf("%s", value) |
| 164 | + msg = strings.TrimPrefix(msg, "[") |
| 165 | + msg = strings.TrimSuffix(msg, "]") |
| 166 | + msgParts := strings.Split(msg, ",") |
| 167 | + |
| 168 | + if len(msgParts) < 2 { |
| 169 | + continue |
| 170 | + } |
| 171 | + |
| 172 | + parsedIntTime, err := strconv.ParseInt(strings.TrimSpace(msgParts[0]), 10, 64) |
| 173 | + if err != nil { |
| 174 | + continue |
| 175 | + } |
| 176 | + |
| 177 | + parsedValue := strings.TrimSuffix(strings.TrimPrefix(strings.TrimSpace(msgParts[1]), `"`), `"`) |
| 178 | + parsedValues = append(parsedValues, PrometheusValue{ |
| 179 | + Time: time.Unix(parsedIntTime, 0), |
| 180 | + Value: fmt.Sprintf("%v", parsedValue), |
| 181 | + }) |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + return &GetPrometheusMetricOutput{ |
| 186 | + Values: parsedValues, |
| 187 | + }, nil |
| 188 | +} |
| 189 | + |
| 190 | +func (c *standardDashboardClient) parseDashboardFilters(path string, filters dashboardFilters) string { |
| 191 | + partialUrl, err := url.Parse(path) |
| 192 | + impossible.HandleError(err) |
| 193 | + |
| 194 | + q := partialUrl.Query() |
| 195 | + if !filters.BeginDate.IsZero() { |
| 196 | + q.Add("begin-date", filters.BeginDate.String()) |
| 197 | + } |
| 198 | + if !filters.EndDate.IsZero() { |
| 199 | + q.Add("end-date", filters.EndDate.String()) |
| 200 | + } |
| 201 | + if filters.UserIdentifier != "" { |
| 202 | + q.Add("user-identifier", filters.UserIdentifier) |
| 203 | + } |
| 204 | + if filters.AccessKeyPrefix != "" { |
| 205 | + q.Add("access-key", filters.AccessKeyPrefix) |
| 206 | + } |
| 207 | + if filters.ModelIdentifier != "" { |
| 208 | + q.Add("model-identifier", filters.ModelIdentifier) |
| 209 | + } |
| 210 | + if filters.TeamIdentifier != "" { |
| 211 | + q.Add("team-identifier", filters.TeamIdentifier) |
| 212 | + } |
| 213 | + partialUrl.RawQuery = q.Encode() |
| 214 | + return partialUrl.String() |
| 215 | +} |
| 216 | + |
| 217 | +type dashboardFilters struct { |
| 218 | + BeginDate model.ModzyDate |
| 219 | + EndDate model.ModzyDate |
| 220 | + UserIdentifier string |
| 221 | + AccessKeyPrefix string |
| 222 | + ModelIdentifier string |
| 223 | + TeamIdentifier string |
| 224 | +} |
0 commit comments