-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from rag594/new_features
New features
- Loading branch information
Showing
14 changed files
with
410 additions
and
292 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
.vscode | ||
.idea | ||
.idea | ||
vendor/ | ||
nimbus |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.