Skip to content

Commit

Permalink
feat: add action workflows stats
Browse files Browse the repository at this point in the history
Signed-off-by: rare-magma <[email protected]>
  • Loading branch information
rare-magma committed Feb 15, 2025
1 parent 30b9937 commit 42e4adb
Show file tree
Hide file tree
Showing 3 changed files with 192 additions and 8 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ systemctl --user list-timers
- github_stats_views: Number of unique views and total count per repo
- github_stats_stars: Number of stars per repo
- github_stats_forks: Number of forks per repo
- github_stats_actions: Total duration in seconds of a workflow run per repo
## Exported metrics example
Expand All @@ -134,6 +135,7 @@ github_stats_referrals,repo=rare-magma/guitos,referrer=github.com count=74,uniqu
github_stats_views,repo=rare-magma/guitos count=1,uniques=1 1723161600
github_stats_stars,repo=rare-magma/guitos count=30 1723285063
github_stats_forks,repo=rare-magma/guitos count=3 1723285063
github_stats_actions,repo=rare-magma/guitos,workflow=Dependabot\ Updates duration=46 1739184696000
```
## Example grafana dashboard
Expand Down
176 changes: 168 additions & 8 deletions github-dashboard.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@
},
"editable": true,
"fiscalYearStartMonth": 0,
"graphTooltip": 0,
"graphTooltip": 1,
"id": 83,
"links": [],
"liveNow": false,
"panels": [
{
"datasource": {
Expand All @@ -39,6 +38,7 @@
"axisLabel": "",
"axisPlacement": "auto",
"barAlignment": 0,
"barWidthFactor": 0.6,
"drawStyle": "line",
"fillOpacity": 0,
"gradientMode": "none",
Expand Down Expand Up @@ -93,18 +93,19 @@
"calcs": [
"min",
"mean",
"max",
"sum"
"max"
],
"displayMode": "table",
"placement": "bottom",
"showLegend": true
},
"tooltip": {
"hideZeros": false,
"mode": "single",
"sort": "none"
}
},
"pluginVersion": "11.5.1",
"targets": [
{
"alias": "$tag_repo",
Expand Down Expand Up @@ -174,6 +175,7 @@
"axisLabel": "",
"axisPlacement": "auto",
"barAlignment": 0,
"barWidthFactor": 0.6,
"drawStyle": "line",
"fillOpacity": 0,
"gradientMode": "none",
Expand Down Expand Up @@ -236,10 +238,12 @@
"showLegend": true
},
"tooltip": {
"hideZeros": false,
"mode": "single",
"sort": "none"
}
},
"pluginVersion": "11.5.1",
"targets": [
{
"alias": "$tag_repo count",
Expand Down Expand Up @@ -357,6 +361,7 @@
"axisLabel": "",
"axisPlacement": "auto",
"barAlignment": 0,
"barWidthFactor": 0.6,
"drawStyle": "line",
"fillOpacity": 0,
"gradientMode": "none",
Expand Down Expand Up @@ -419,10 +424,12 @@
"showLegend": true
},
"tooltip": {
"hideZeros": false,
"mode": "single",
"sort": "none"
}
},
"pluginVersion": "11.5.1",
"targets": [
{
"alias": "$tag_path count",
Expand Down Expand Up @@ -546,6 +553,7 @@
"axisLabel": "",
"axisPlacement": "auto",
"barAlignment": 0,
"barWidthFactor": 0.6,
"drawStyle": "line",
"fillOpacity": 0,
"gradientMode": "none",
Expand Down Expand Up @@ -608,10 +616,12 @@
"showLegend": true
},
"tooltip": {
"hideZeros": false,
"mode": "single",
"sort": "none"
}
},
"pluginVersion": "11.5.1",
"targets": [
{
"alias": "$tag_repo count",
Expand Down Expand Up @@ -729,6 +739,7 @@
"axisLabel": "",
"axisPlacement": "auto",
"barAlignment": 0,
"barWidthFactor": 0.6,
"drawStyle": "line",
"fillOpacity": 0,
"gradientMode": "none",
Expand Down Expand Up @@ -790,10 +801,12 @@
"showLegend": true
},
"tooltip": {
"hideZeros": false,
"mode": "single",
"sort": "none"
}
},
"pluginVersion": "11.5.1",
"targets": [
{
"alias": "$tag_repo $tag_referrer count",
Expand Down Expand Up @@ -923,6 +936,7 @@
"axisLabel": "",
"axisPlacement": "auto",
"barAlignment": 0,
"barWidthFactor": 0.6,
"drawStyle": "line",
"fillOpacity": 0,
"gradientMode": "none",
Expand Down Expand Up @@ -977,18 +991,19 @@
"calcs": [
"min",
"mean",
"max",
"sum"
"max"
],
"displayMode": "table",
"placement": "bottom",
"showLegend": true
},
"tooltip": {
"hideZeros": false,
"mode": "single",
"sort": "none"
}
},
"pluginVersion": "11.5.1",
"targets": [
{
"alias": "$tag_repo",
Expand Down Expand Up @@ -1040,10 +1055,155 @@
],
"title": "Forks",
"type": "timeseries"
},
{
"datasource": {
"type": "influxdb",
"uid": "cducltn2d52bkd"
},
"fieldConfig": {
"defaults": {
"color": {
"mode": "palette-classic"
},
"custom": {
"axisBorderShow": false,
"axisCenteredZero": false,
"axisColorMode": "text",
"axisLabel": "",
"axisPlacement": "auto",
"barAlignment": 0,
"barWidthFactor": 0.6,
"drawStyle": "line",
"fillOpacity": 0,
"gradientMode": "none",
"hideFrom": {
"legend": false,
"tooltip": false,
"viz": false
},
"insertNulls": false,
"lineInterpolation": "linear",
"lineWidth": 1,
"pointSize": 5,
"scaleDistribution": {
"type": "linear"
},
"showPoints": "auto",
"spanNulls": false,
"stacking": {
"group": "A",
"mode": "none"
},
"thresholdsStyle": {
"mode": "off"
}
},
"mappings": [],
"thresholds": {
"mode": "absolute",
"steps": [
{
"color": "green",
"value": null
},
{
"color": "red",
"value": 80
}
]
},
"unit": "s"
},
"overrides": []
},
"gridPos": {
"h": 11,
"w": 12,
"x": 0,
"y": 33
},
"id": 8,
"options": {
"legend": {
"calcs": [
"min",
"mean",
"max"
],
"displayMode": "table",
"placement": "bottom",
"showLegend": true
},
"tooltip": {
"hideZeros": false,
"mode": "single",
"sort": "none"
}
},
"pluginVersion": "11.5.1",
"targets": [
{
"alias": "$tag_repo $tag_workflow",
"datasource": {
"type": "influxdb",
"uid": "cducltn2d52bkd"
},
"groupBy": [
{
"params": [
"$__interval"
],
"type": "time"
},
{
"params": [
"repo::tag"
],
"type": "tag"
},
{
"params": [
"workflow::tag"
],
"type": "tag"
},
{
"params": [
"none"
],
"type": "fill"
}
],
"measurement": "github_stats_actions",
"orderByTime": "ASC",
"policy": "default",
"refId": "A",
"resultFormat": "time_series",
"select": [
[
{
"params": [
"duration"
],
"type": "field"
},
{
"params": [],
"type": "last"
}
]
],
"tags": []
}
],
"title": "Actions",
"type": "timeseries"
}
],
"preload": false,
"refresh": "",
"schemaVersion": 39,
"schemaVersion": 40,
"tags": [],
"templating": {
"list": []
Expand All @@ -1056,6 +1216,6 @@
"timezone": "",
"title": "Github",
"uid": "4f7a9a9e-568a-45a5-88f9-3ca877de5996",
"version": 2,
"version": 6,
"weekStart": ""
}
22 changes: 22 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"math"
"net/http"
"os"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -195,6 +196,27 @@ func main() {
forksLine := fmt.Sprintf("github_stats_forks,repo=%s count=%d %v\n", repo.GetFullName(), *repo.ForksCount, time.Now().Unix())
payload.WriteString(forksLine)
}(&payload)

wg.Add(1)
go func(payload *bytes.Buffer) {
defer wg.Done()
workflows, _, err := ghc.Actions.ListWorkflows(context.Background(), repo.GetOwner().GetLogin(), repo.GetName(), nil)
if err != nil {
log.Fatalln("Error getting workflows: ", err)
}
for _, workflow := range workflows.Workflows {
runs, _, err := ghc.Actions.ListWorkflowRunsByID(context.Background(), repo.GetOwner().GetLogin(), repo.GetName(), *workflow.ID, nil)
if err != nil {
log.Fatalln("Error getting workflow runs: ", err)
}
for _, run := range runs.WorkflowRuns {
duration := run.UpdatedAt.GetTime().Sub(*run.RunStartedAt.GetTime()).Seconds()
influxLine := fmt.Sprintf("github_stats_actions,repo=%s,workflow=%s duration=%.0f %v\n", repo.GetFullName(), strings.ReplaceAll(*workflow.Name, " ", "\\ "), duration, run.CreatedAt.GetTime().UnixMilli())
payload.WriteString(influxLine)
}
}
}(&payload)

}
wg.Wait()

Expand Down

0 comments on commit 42e4adb

Please sign in to comment.