aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBobby <[email protected]>2025-04-06 12:02:27 +0530
committerBobby <[email protected]>2025-04-06 12:02:27 +0530
commitc947eb5f6c9bdcd624347f02a1d0b0bc65d96622 (patch)
tree08247aeead7005034371c790769a29d2896aa3ca
parent7212757527ecccb6a936dcf5416981e48935f41d (diff)
downloadmetachan-c947eb5f6c9bdcd624347f02a1d0b0bc65d96622.tar.xz
metachan-c947eb5f6c9bdcd624347f02a1d0b0bc65d96622.zip
added stats specific utils
-rw-r--r--utils/stats/memory.go57
-rw-r--r--utils/stats/time.go8
2 files changed, 65 insertions, 0 deletions
diff --git a/utils/stats/memory.go b/utils/stats/memory.go
new file mode 100644
index 0000000..3c36419
--- /dev/null
+++ b/utils/stats/memory.go
@@ -0,0 +1,57 @@
+package stats
+
+import (
+ "fmt"
+ "math"
+ "metachan/types"
+ "runtime"
+ "time"
+)
+
+var startTime = time.Now()
+
+func GetMemoryStats() types.MemoryStats {
+ var m runtime.MemStats
+ runtime.ReadMemStats(&m)
+
+ used := m.Alloc / 1024 // Total allocated memory in MiB
+ total := m.Sys / 1024 // Total system memory obtained in MiB
+ free := (m.Sys - m.Alloc) / 1024
+ usage := math.Round(float64(m.Alloc)/float64(m.Sys)*100*100) / 100 // Memory usage percentage
+
+ return types.MemoryStats{
+ Used: fmt.Sprintf("%d MiB", used),
+ Total: fmt.Sprintf("%d MiB", total),
+ Free: fmt.Sprintf("%d MiB", free),
+ Usage: fmt.Sprintf("%.2f%%", usage),
+ }
+}
+
+func GetUptime() string {
+ var timeString string
+ uptime := time.Since(startTime)
+ // return in format "2d 3h 4m 5s"
+ days := int(uptime.Hours() / 24)
+ hours := int(uptime.Hours()) % 24
+ minutes := int(uptime.Minutes()) % 60
+ seconds := int(uptime.Seconds()) % 60
+
+ if days > 0 {
+ timeString += fmt.Sprintf("%dd ", days)
+ }
+ if hours > 0 {
+ timeString += fmt.Sprintf("%dh ", hours)
+ }
+ if minutes > 0 {
+ timeString += fmt.Sprintf("%dm ", minutes)
+ }
+ if seconds > 0 {
+ timeString += fmt.Sprintf("%ds ", seconds)
+ }
+ if timeString == "" {
+ timeString = "0s"
+ } else {
+ timeString = timeString[:len(timeString)-1] // Remove trailing space
+ }
+ return timeString
+}
diff --git a/utils/stats/time.go b/utils/stats/time.go
new file mode 100644
index 0000000..8ac50ac
--- /dev/null
+++ b/utils/stats/time.go
@@ -0,0 +1,8 @@
+package stats
+
+import "time"
+
+func GetCurrentTimestamp() string {
+ // Get the current UTC time and format it as RFC3339
+ return time.Now().UTC().Format(time.RFC3339)
+}