Skip to content

Commit

Permalink
chore: Add memory history endpoint to API
Browse files Browse the repository at this point in the history
  • Loading branch information
andrasbacsai committed Jun 18, 2024
1 parent 27cd37f commit 6c4f859
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
14 changes: 14 additions & 0 deletions cmd/sentinel/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var version string = "0.0.5"
var logsDir string = "/app/logs"
var metricsDir string = "/app/metrics"
var cpuMetricsFile string = metricsDir + "/cpu.csv"
var memoryMetricsFile string = metricsDir + "/memory.csv"

// Arguments
var token string
Expand All @@ -41,6 +42,7 @@ func main() {
logsDir = "./logs"
metricsDir = "./metrics"
cpuMetricsFile = metricsDir + "/cpu.csv"
memoryMetricsFile = metricsDir + "/memory.csv"
}
if err := os.MkdirAll(logsDir, 0700); err != nil {
log.Fatalf("Error creating metrics directory: %v", err)
Expand Down Expand Up @@ -173,6 +175,18 @@ func main() {
usage = memoryCsvHeader + usage
c.String(200, usage)
})
authorized.GET("/memory/history", func(c *gin.Context) {
from := c.Query("from")
to := c.Query("to")
usage, err := getHistoryMemoryUsage(from, to)
if err != nil {
c.JSON(500, gin.H{
"error": err.Error(),
})
return
}
c.String(200, usage)
})
authorized.GET("/disk", func(c *gin.Context) {
usage, err := getDiskUsage()
if err != nil {
Expand Down
64 changes: 64 additions & 0 deletions cmd/sentinel/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ package main
import (
"encoding/json"
"fmt"
"os"
"strconv"
"strings"
"time"

"github.com/shirou/gopsutil/mem"
)
Expand Down Expand Up @@ -44,3 +48,63 @@ func getMemUsage(csv bool) (string, error) {
return string(jsonData), nil

}

func getHistoryMemoryUsage(from string, to string) (string, error) {
if from == "" && to == "" {
// return everything
file, err := os.ReadFile(memoryMetricsFile)
if err != nil {
fmt.Println("Failed to read file:", err)
return "", err
}
return string(file), nil
}
if from == "" {
from = "1970-01-01T00:00:00Z"
}
if to == "" {
to = time.Now().UTC().Format(time.RFC3339)
}
fromTime, err := time.Parse(time.RFC3339, from)
if err != nil {
fmt.Println("Failed to parse from time:", err)
return "", err
}
toTime, err := time.Parse(time.RFC3339, to)
if err != nil {
fmt.Println("Failed to parse to time:", err)
return "", err
}

fromTimeUnix := fromTime.UnixMilli()
toTimeUnix := toTime.UnixMilli()
file, err := os.ReadFile(memoryMetricsFile)
if err != nil {
fmt.Println("Failed to read file:", err)
return "", err
}
lines := string(file)
var result string
lines = lines[strings.Index(lines, "\n")+1:]
for _, line := range strings.Split(lines, "\n") {
if line == "" {
continue
}
parts := strings.Split(line, ",")
if len(parts) != 4 {
fmt.Println("Invalid line:", line)
continue
}
time, err := strconv.ParseInt(parts[0], 10, 64)
if err != nil {
fmt.Println("Failed to parse time:", err)
continue
}
if time >= fromTimeUnix && time <= toTimeUnix {
result += line + "\n"
}
}
result = memoryCsvHeader + result
return result, nil

}

0 comments on commit 6c4f859

Please sign in to comment.