Skip to content

Commit

Permalink
Merge pull request #6 from rag594/new_features
Browse files Browse the repository at this point in the history
New features
  • Loading branch information
rag594 authored Sep 3, 2023
2 parents a1f77a7 + 6438e9f commit a629633
Show file tree
Hide file tree
Showing 14 changed files with 410 additions and 292 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.vscode
.idea
.idea
vendor/
nimbus
50 changes: 50 additions & 0 deletions alerts/alerts_table.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package alerts

import (
"github.com/fatih/color"
"github.com/rodaine/table"
)

type alertsTable struct {
table table.Table
alerts *VMAlertsResponse
}

func NewAlertsTable(alerts *VMAlertsResponse, columns ...interface{}) alertsTable {
headerFmt := color.New(color.FgGreen, color.Underline).SprintfFunc()
columnFmt := color.New(color.FgYellow).SprintfFunc()

tbl := table.New(columns...)
tbl.WithHeaderFormatter(headerFmt).WithFirstColumnFormatter(columnFmt)

return alertsTable{table: tbl, alerts: alerts}
}

func (t *alertsTable) ListAlertsByName(name string) table.Table {
if name == "" {
return nil
}

for _, vmAlert := range t.alerts.Data.Alerts {
if vmAlert.Name == name {
t.table.AddRow(vmAlert.Name, vmAlert.FormatLabels(), vmAlert.FormatAnnotations(), vmAlert.State, vmAlert.GetActivateAt())
}
}

return t.table
}

func (t *alertsTable) ListAlertsByLabels(label, value string) table.Table {
if label == "" && value == "" {
return nil
}

for _, vmAlert := range t.alerts.Data.Alerts {
val, ok := vmAlert.Labels[label]
if ok && val == value {
t.table.AddRow(vmAlert.Name, vmAlert.FormatLabels(), vmAlert.FormatAnnotations(), vmAlert.State, vmAlert.GetActivateAt())
}
}

return t.table
}
61 changes: 61 additions & 0 deletions alerts/cmd/alerts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package cmd

import (
"github.com/rag594/nimbus/alerts"
"github.com/rag594/nimbus/httpClient"
"github.com/urfave/cli/v2"
)

type AlertCommand struct {
Command *cli.Command
}

func NewAlertCommand(vmClient *httpClient.VMClient) *AlertCommand {
var (
label string
name string
value string
)
command := &cli.Command{
Name: "alerts",
Usage: "filter your alerts by name or labels",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "label",
Usage: "filter alerts by labels, provide key and then value. Example --label <key> --value <value>",
Destination: &label,
Category: "Labels",
},
&cli.StringFlag{
Name: "value",
Usage: "filter alerts by value, value should be provided with label. Example --label <key> --value <value>",
Destination: &value,
Category: "Labels",
},
&cli.StringFlag{
Name: "name",
Usage: "filter alerts by name",
Destination: &name,
},
},
Action: func(cCtx *cli.Context) error {
vmAlerts, err := vmClient.GetVMAlerts()
if err != nil {
return err
}

alertsTable := alerts.NewAlertsTable(vmAlerts, "AlertName", "Labels", "Annotaions", "State", "ActiveAt")
if table := alertsTable.ListAlertsByLabels(label, value); table != nil {
table.Print()
}

if table := alertsTable.ListAlertsByName(name); table != nil {
table.Print()
}

return nil
},
}

return &AlertCommand{Command: command}
}
51 changes: 51 additions & 0 deletions alerts/vmalerts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package alerts

import (
"fmt"
"strings"
"time"
)

type VMAlertsResponse struct {
Status string `json:"status"`
Data AlertsData `json:"data"`
}

type AlertsData struct {
Alerts []Alert `json:"alerts"`
}

type Alert struct {
State string `json:"state"`
Name string `json:"name"`
Value string `json:"value"`
Labels map[string]string `json:"labels"`
Annotations map[string]string `json:"annotations"`
ActiveAt time.Time `json:"activeAt"`
Id string `json:"id"`
RuleId string `json:"rule_id"`
GroupId string `json:"group_id"`
Expression string `json:"expression"`
Source string `json:"source"`
Restored bool `json:"restored"`
}

func (a Alert) FormatLabels() string {
var labelsBuilder strings.Builder
for key, value := range a.Labels {
labelsBuilder.WriteString(fmt.Sprintf("%s_%s\n", key, value))
}
return labelsBuilder.String()
}

func (a Alert) FormatAnnotations() string {
var annotationsBuilder strings.Builder
for key, value := range a.Annotations {
annotationsBuilder.WriteString(fmt.Sprintf("%s_%s\n", key, value))
}
return annotationsBuilder.String()
}

func (a Alert) GetActivateAt() string {
return a.ActiveAt.Local().Format("2006-01-02 15:04:05")
}
94 changes: 0 additions & 94 deletions commands/alerts.go

This file was deleted.

100 changes: 0 additions & 100 deletions commands/groups.go

This file was deleted.

2 changes: 1 addition & 1 deletion httpClient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"time"
)

//Client defines Http Client
// Client defines Http Client
type Client struct {
httpClient *http.Client
}
Expand Down
Loading

0 comments on commit a629633

Please sign in to comment.