Skip to content

Commit

Permalink
Document PrettifiedMetrics (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
remi00 authored Feb 24, 2025
1 parent 1dd1bbd commit 1b13fb2
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 2 deletions.
43 changes: 43 additions & 0 deletions docs/TEMPLATING_REFERENCE.md
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,49 @@ Example:
}]
```

For metrics there is a dedicated method `PrettifiedMetrics` which provided on the Details collection that returns only the details with `metric` tag with pre-formatted values:

- returns them as string type rather than floating numbers;
- rounds the floating values to 2 decimal digits or none if the fraction is below 0.05 delta
- converts bit/s unit into the one the most applicable (Kbit/s, Mbit/s, Gbit/s etc.)

**Example - Using PrettifiedMetrics**

```go-template
{{- if .IsSingleEvent -}}
{{- with .Event -}}
{{- .Details.PrettifiedMetrics | toJSON -}}
{{- end -}}
{{- end -}}
}
```

For the previous example it would render as:

```json
[{
"Label": "Kbit/s",
"Name": "bits",
"Tag": "metric",
"Value": "46.25"
},
{
"Label": "Packets/second",
"Name": "packets",
"Tag": "metric",
"Value": "240.26"
},
{
"Label": "Unique Source IPs",
"Name": "unique_src_ip",
"Tag": "metric",
"Value": "1"
}]
```




#### Labels

Tags: `label`, `device_label`
Expand Down
68 changes: 68 additions & 0 deletions pkg/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"math"
"strings"
"time"
)
Expand Down Expand Up @@ -373,3 +374,70 @@ func (vm *NotificationViewModel) Summary() string {
}
return strings.Join(segments, ", ")
}

// filter details to only ones with a metric tag and prettify their
// values rounding the float values and converting bits to x-bits/s
func (details EventViewModelDetails) PrettifiedMetrics() EventViewModelDetails {
result := make(EventViewModelDetails, 0)
for _, detail := range details {
if detail.Tag != "metric" {
continue
}

floatValue, err := toFloat(detail.Value)
if err != nil {
result = append(result, detail)
continue
}

label := detail.Label

// format bits with proper unit
if strings.HasPrefix(detail.Name, "bits") {
var prefix string
floatValue, prefix = formatBits(floatValue)
label = fmt.Sprintf("%sbits/s", prefix)
}

// prevent showing fractions when unnecessary
stringValue := fmt.Sprintf("%.2f", floatValue)
if _, fraction := math.Modf(floatValue); fraction < 0.05 {
stringValue = fmt.Sprintf("%.0f", floatValue)
}

formatted := &EventViewModelDetail{
Name: detail.Name,
Label: label,
Tag: detail.Tag,
Value: stringValue,
}

result = append(result, formatted)
}
return result
}

func toFloat(value interface{}) (float64, error) {
switch v := value.(type) {
case float64:
return v, nil
case int:
return float64(v), nil
default:
return 0, fmt.Errorf("don't know how to convert %T to float64", v)
}
}

func formatBits(bits float64) (float64, string) {
const unit = 1024
const suffixes = "KMGTPE"

exp := math.Floor(math.Log(bits) / math.Log(unit))
suffix := ""
if exp > 0 {
suffix = string(suffixes[int(exp)-1])
}
value := bits / math.Pow(unit, exp)

return value, suffix
}
4 changes: 2 additions & 2 deletions templates/discord.json.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ Webhook execute parameters: https://discord.com/developers/docs/resources/webhoo
{{- end -}}


{{- with $metrics := .Details.WithTag "metric" -}}
{{- with $metrics := .Details.PrettifiedMetrics -}}
{{- if gt (len $metrics) 0 -}}
**Metrics**:\n
{{- range $index, $detail := $metrics -}}
- **{{ $detail.LabelOrName }}**: {{ $detail.Value }}\n
- **{{ $detail.Value }} {{ $detail.LabelOrName }}**\n
{{- end -}}
{{- end -}}
{{- end -}}
Expand Down

0 comments on commit 1b13fb2

Please # to comment.