@@ -3,6 +3,10 @@ package main
3
3
import (
4
4
"encoding/json"
5
5
"fmt"
6
+ "os"
7
+ "strconv"
8
+ "strings"
9
+ "time"
6
10
7
11
"github.com/shirou/gopsutil/mem"
8
12
)
@@ -44,3 +48,63 @@ func getMemUsage(csv bool) (string, error) {
44
48
return string (jsonData ), nil
45
49
46
50
}
51
+
52
+ func getHistoryMemoryUsage (from string , to string ) (string , error ) {
53
+ if from == "" && to == "" {
54
+ // return everything
55
+ file , err := os .ReadFile (memoryMetricsFile )
56
+ if err != nil {
57
+ fmt .Println ("Failed to read file:" , err )
58
+ return "" , err
59
+ }
60
+ return string (file ), nil
61
+ }
62
+ if from == "" {
63
+ from = "1970-01-01T00:00:00Z"
64
+ }
65
+ if to == "" {
66
+ to = time .Now ().UTC ().Format (time .RFC3339 )
67
+ }
68
+ fromTime , err := time .Parse (time .RFC3339 , from )
69
+ if err != nil {
70
+ fmt .Println ("Failed to parse from time:" , err )
71
+ return "" , err
72
+ }
73
+ toTime , err := time .Parse (time .RFC3339 , to )
74
+ if err != nil {
75
+ fmt .Println ("Failed to parse to time:" , err )
76
+ return "" , err
77
+ }
78
+
79
+ fromTimeUnix := fromTime .UnixMilli ()
80
+ toTimeUnix := toTime .UnixMilli ()
81
+ file , err := os .ReadFile (memoryMetricsFile )
82
+ if err != nil {
83
+ fmt .Println ("Failed to read file:" , err )
84
+ return "" , err
85
+ }
86
+ lines := string (file )
87
+ var result string
88
+ lines = lines [strings .Index (lines , "\n " )+ 1 :]
89
+ for _ , line := range strings .Split (lines , "\n " ) {
90
+ if line == "" {
91
+ continue
92
+ }
93
+ parts := strings .Split (line , "," )
94
+ if len (parts ) != 4 {
95
+ fmt .Println ("Invalid line:" , line )
96
+ continue
97
+ }
98
+ time , err := strconv .ParseInt (parts [0 ], 10 , 64 )
99
+ if err != nil {
100
+ fmt .Println ("Failed to parse time:" , err )
101
+ continue
102
+ }
103
+ if time >= fromTimeUnix && time <= toTimeUnix {
104
+ result += line + "\n "
105
+ }
106
+ }
107
+ result = memoryCsvHeader + result
108
+ return result , nil
109
+
110
+ }
0 commit comments