wrappd.sh/internal/web/model.go
Óscar M. Lage f7cf197b4e Imp: The file does not need to be copied in disk anymore
File is being proccessed from memory directly. We don't need to
tempcopy nor delete it because it's not saved to disk.
2024-12-05 16:43:51 +01:00

69 lines
1.6 KiB
Go

package web
import (
"bufio"
"mime/multipart"
"regexp"
"strings"
)
type CommandStat struct {
Command string
Count int
}
func ProcessHistory(file multipart.File) (map[string]int, map[string]int, map[string]int, map[string]int) {
commandCounts := make(map[string]int)
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)
}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
command := scanner.Text()
if command == "" {
continue
}
commandCounts[command]++
if strings.Contains(command, "git") {
categories["Git"]++
} else if strings.Contains(command, "docker") {
categories["Docker"]++
} else if strings.Contains(command, "ssh") {
categories["SSH"]++
}
if strings.Contains(command, "|") {
pipeRedirectionCounts["pipe"]++
}
if strings.Contains(command, ">") || strings.Contains(command, "<") || strings.Contains(command, ">>") {
pipeRedirectionCounts["redirection"]++
}
matches := re.FindAllString(command, -1)
for _, match := range matches {
commonPatterns[match]++
}
}
return commandCounts, categories, pipeRedirectionCounts, commonPatterns
}
func GenerateStats(commandCounts map[string]int, categories map[string]int, commonPatterns map[string]int) map[string]interface{} {
stats := map[string]interface{}{
"topCommands": commandCounts,
"categories": categories,
"commonPatterns": commonPatterns,
}
return stats
}