From 548cb7cb3932fb6c238344e0a97764a4d59eefc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?O=CC=81scar=20M=2E=20Lage?= Date: Thu, 5 Dec 2024 19:26:18 +0100 Subject: [PATCH] Feat: Add total and unique commands counter --- internal/web/controller.go | 6 ++++-- internal/web/model.go | 8 +++++--- internal/web/templates/result.html | 4 ++++ internal/web/utils.go | 2 +- 4 files changed, 14 insertions(+), 6 deletions(-) diff --git a/internal/web/controller.go b/internal/web/controller.go index 064ba5e..bba8e95 100644 --- a/internal/web/controller.go +++ b/internal/web/controller.go @@ -42,7 +42,7 @@ func handleUpload(w http.ResponseWriter, r *http.Request) { return } - commandCounts, categories, pipeRedirectionCounts, commonPatterns := ProcessHistory(file) + totalCommands, uniqueCommands, commandCounts, categories, pipeRedirectionCounts, commonPatterns := ProcessHistory(file) limit := os.Getenv("TOP_N_COMMANDS") limitInt, err := strconv.Atoi(limit) @@ -78,7 +78,9 @@ func handleUpload(w http.ResponseWriter, r *http.Request) { commonPatternStats := ConvertAndSort(commonPatterns, limitInt) stats["commonPatternStats"] = commonPatternStats - // Pipe counts + // Counts + stats["total"] = totalCommands + stats["unique"] = uniqueCommands stats["pipeRedirectionCounts"] = pipeRedirectionCounts // Save results diff --git a/internal/web/model.go b/internal/web/model.go index a8111fe..ab4d512 100644 --- a/internal/web/model.go +++ b/internal/web/model.go @@ -12,15 +12,15 @@ type CommandStat struct { Count int } -func ProcessHistory(file multipart.File) (map[string]int, map[string]int, map[string]int, map[string]int) { +func ProcessHistory(file multipart.File) (int, int, map[string]int, map[string]int, map[string]int, map[string]int) { commandCounts := make(map[string]int) + totalCommands := 0 categories := make(map[string]int) pipeRedirectionCounts := make(map[string]int) commonPatterns := make(map[string]int) re := regexp.MustCompile(`\S+\.(log|txt|conf|json)`) - // Reset the file pointer to the beginning (if needed) if seeker, ok := file.(multipart.File); ok { seeker.Seek(0, 0) } @@ -32,6 +32,7 @@ func ProcessHistory(file multipart.File) (map[string]int, map[string]int, map[st continue } + totalCommands++ commandCounts[command]++ if strings.Contains(command, "git") { @@ -53,9 +54,10 @@ func ProcessHistory(file multipart.File) (map[string]int, map[string]int, map[st for _, match := range matches { commonPatterns[match]++ } + } - return commandCounts, categories, pipeRedirectionCounts, commonPatterns + return totalCommands, len(commandCounts), commandCounts, categories, pipeRedirectionCounts, commonPatterns } func GenerateStats(commandCounts map[string]int, categories map[string]int, commonPatterns map[string]int) map[string]interface{} { diff --git a/internal/web/templates/result.html b/internal/web/templates/result.html index 30efb10..1d55e0b 100644 --- a/internal/web/templates/result.html +++ b/internal/web/templates/result.html @@ -27,6 +27,10 @@

Command Statistics

+

Top Commands Chart

diff --git a/internal/web/utils.go b/internal/web/utils.go index 1ba8463..b9e9f45 100644 --- a/internal/web/utils.go +++ b/internal/web/utils.go @@ -117,7 +117,7 @@ func createBarChart(stats []CommandStat) *charts.Bar { } bar.SetXAxis(commands). - AddSeries("Freq", generateBarItems(counts)) + AddSeries("Frequency", generateBarItems(counts)) return bar }