This commit is contained in:
parent
2e360f4b20
commit
a75ce3287d
1 changed files with 61 additions and 0 deletions
|
@ -8,6 +8,7 @@ import (
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"git.lastassault.de/speatzle/morffix/constants"
|
"git.lastassault.de/speatzle/morffix/constants"
|
||||||
"github.com/go-echarts/go-echarts/v2/charts"
|
"github.com/go-echarts/go-echarts/v2/charts"
|
||||||
|
@ -194,6 +195,66 @@ func generateStats(ctx context.Context) ([]ChartData, error) {
|
||||||
}
|
}
|
||||||
data = append(data, generatePie("Task Status", res))
|
data = append(data, generatePie("Task Status", res))
|
||||||
|
|
||||||
|
type BarTaskRowValue struct {
|
||||||
|
Date time.Time
|
||||||
|
Status constants.TaskStatus
|
||||||
|
Count int
|
||||||
|
}
|
||||||
|
|
||||||
|
rows, err = db.Query(ctx,
|
||||||
|
`SELECT date_trunc('day', updated_at) date, status, COUNT(*) AS count
|
||||||
|
FROM tasks
|
||||||
|
WHERE status = $1 OR status = $2
|
||||||
|
GROUP BY 1,2;`, constants.TASK_STATUS_SUCCESS, constants.TASK_STATUS_FAILED)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("Query Task Status Day: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
taskStatusDayCounts, err := pgx.CollectRows(rows, pgx.RowToStructByName[BarTaskRowValue])
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("Collect Task Status Day Data: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
days := []string{}
|
||||||
|
successBarData := []opts.BarData{}
|
||||||
|
failedBarData := []opts.BarData{}
|
||||||
|
for _, v := range taskStatusDayCounts {
|
||||||
|
days = append(days, v.Date.Format(time.DateOnly))
|
||||||
|
if v.Status == constants.TASK_STATUS_SUCCESS {
|
||||||
|
successBarData = append(successBarData, opts.BarData{
|
||||||
|
Value: v.Count,
|
||||||
|
})
|
||||||
|
} else if v.Status == constants.TASK_STATUS_FAILED {
|
||||||
|
failedBarData = append(failedBarData, opts.BarData{
|
||||||
|
Value: v.Count,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
bar := charts.NewBar()
|
||||||
|
bar.SetGlobalOptions(
|
||||||
|
charts.WithInitializationOpts(opts.Initialization{
|
||||||
|
Theme: "dark",
|
||||||
|
BackgroundColor: "#111",
|
||||||
|
}),
|
||||||
|
charts.WithTitleOpts(opts.Title{
|
||||||
|
Title: "Task Success/Failed Per Day",
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
bar.SetXAxis(days).
|
||||||
|
AddSeries("Success", successBarData).
|
||||||
|
AddSeries("Failed", failedBarData).
|
||||||
|
SetSeriesOptions(charts.WithBarChartOpts(opts.BarChart{
|
||||||
|
Stack: "stackA",
|
||||||
|
}))
|
||||||
|
|
||||||
|
snippet := bar.RenderSnippet()
|
||||||
|
|
||||||
|
data = append(data, ChartData{
|
||||||
|
Element: template.HTML(snippet.Element),
|
||||||
|
Script: template.HTML(snippet.Script),
|
||||||
|
})
|
||||||
|
|
||||||
return data, nil
|
return data, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue