Feat: Add total and unique commands counter

This commit is contained in:
Óscar M. Lage 2024-12-05 19:26:18 +01:00
parent ac607faa7f
commit 548cb7cb39
4 changed files with 14 additions and 6 deletions

View File

@ -42,7 +42,7 @@ func handleUpload(w http.ResponseWriter, r *http.Request) {
return return
} }
commandCounts, categories, pipeRedirectionCounts, commonPatterns := ProcessHistory(file) totalCommands, uniqueCommands, commandCounts, categories, pipeRedirectionCounts, commonPatterns := ProcessHistory(file)
limit := os.Getenv("TOP_N_COMMANDS") limit := os.Getenv("TOP_N_COMMANDS")
limitInt, err := strconv.Atoi(limit) limitInt, err := strconv.Atoi(limit)
@ -78,7 +78,9 @@ func handleUpload(w http.ResponseWriter, r *http.Request) {
commonPatternStats := ConvertAndSort(commonPatterns, limitInt) commonPatternStats := ConvertAndSort(commonPatterns, limitInt)
stats["commonPatternStats"] = commonPatternStats stats["commonPatternStats"] = commonPatternStats
// Pipe counts // Counts
stats["total"] = totalCommands
stats["unique"] = uniqueCommands
stats["pipeRedirectionCounts"] = pipeRedirectionCounts stats["pipeRedirectionCounts"] = pipeRedirectionCounts
// Save results // Save results

View File

@ -12,15 +12,15 @@ type CommandStat struct {
Count int 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) commandCounts := make(map[string]int)
totalCommands := 0
categories := make(map[string]int) categories := make(map[string]int)
pipeRedirectionCounts := make(map[string]int) pipeRedirectionCounts := make(map[string]int)
commonPatterns := make(map[string]int) commonPatterns := make(map[string]int)
re := regexp.MustCompile(`\S+\.(log|txt|conf|json)`) re := regexp.MustCompile(`\S+\.(log|txt|conf|json)`)
// Reset the file pointer to the beginning (if needed)
if seeker, ok := file.(multipart.File); ok { if seeker, ok := file.(multipart.File); ok {
seeker.Seek(0, 0) seeker.Seek(0, 0)
} }
@ -32,6 +32,7 @@ func ProcessHistory(file multipart.File) (map[string]int, map[string]int, map[st
continue continue
} }
totalCommands++
commandCounts[command]++ commandCounts[command]++
if strings.Contains(command, "git") { 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 { for _, match := range matches {
commonPatterns[match]++ 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{} { func GenerateStats(commandCounts map[string]int, categories map[string]int, commonPatterns map[string]int) map[string]interface{} {

View File

@ -27,6 +27,10 @@
<h2 class="text-4xl sm:text-3xl font-semibold text-center text-gray-100 mb-6"> <h2 class="text-4xl sm:text-3xl font-semibold text-center text-gray-100 mb-6">
Command Statistics Command Statistics
</h2> </h2>
<ul>
<li class="text-center">Total commands: <span class="font-semibold text-blue-400">{{.total}}</span></li>
<li class="text-center">Unique commands: <span class="font-semibold text-blue-400">{{.unique}}</span></li>
</ul>
<div class="chart-container"> <div class="chart-container">
<h2 class="text-2xl font-bold text-gray-200 mb-4">Top Commands Chart</h2> <h2 class="text-2xl font-bold text-gray-200 mb-4">Top Commands Chart</h2>

View File

@ -117,7 +117,7 @@ func createBarChart(stats []CommandStat) *charts.Bar {
} }
bar.SetXAxis(commands). bar.SetXAxis(commands).
AddSeries("Freq", generateBarItems(counts)) AddSeries("Frequency", generateBarItems(counts))
return bar return bar
} }