Messy JS + config from .env

This commit is contained in:
2024-01-20 02:06:27 +02:00
parent 71d4eb6cc3
commit 498aba835f
8 changed files with 202 additions and 41 deletions

View File

@@ -1,19 +1,48 @@
package config
import (
"log"
"os"
"strconv"
"github.com/joho/godotenv"
)
type DHConfig struct {
DiskFetchFrequency int `json:"diskFetchFrequency" comment:"How often should a snapshot be taken of the current state of the disks"`
MemoryDumpFrequency int `json:"memoryDumpFrequency" comment:"How often should we save the snapshots from memory to disk"`
MaxHistoryAge int
DiskFetchFrequency int `json:"diskFetchFrequency"`
MemoryDumpFrequency int `json:"memoryDumpFrequency"`
MaxHistoryAge int `json:"maxHistoryAge"`
}
func GetConfiguration() DHConfig {
// TODO: Read from os.environment or simply load the defaults
return DHConfig{
DiskFetchFrequency: 5,
MemoryDumpFrequency: 16,
MaxHistoryAge: 2592000,
// Load .env file if it exists
if err := godotenv.Load(); err != nil {
log.Println("No .env file found")
}
config := DHConfig{
DiskFetchFrequency: 5, // default value
MemoryDumpFrequency: 60, // default value
MaxHistoryAge: 2592000, // default value
}
if val, exists := os.LookupEnv("DISK_FETCH_FREQUENCY"); exists {
if intValue, err := strconv.Atoi(val); err == nil {
config.DiskFetchFrequency = intValue
}
}
if val, exists := os.LookupEnv("MEMORY_DUMP_FREQUENCY"); exists {
if intValue, err := strconv.Atoi(val); err == nil {
config.MemoryDumpFrequency = intValue
}
}
if val, exists := os.LookupEnv("MAX_HISTORY_AGE"); exists {
if intValue, err := strconv.Atoi(val); err == nil {
config.MaxHistoryAge = intValue
}
}
return config
}

View File

@@ -104,16 +104,12 @@ func RunService() {
// Snapshot taking routine
go func() {
waitTime := time.Duration(config.GetConfiguration().DiskFetchFrequency) * time.Second
for {
time.Sleep(time.Duration(config.GetConfiguration().DiskFetchFrequency) * time.Second)
data, err := TakeHardwareSnapshot()
time.Sleep(waitTime)
_, err := TakeHardwareSnapshot()
if err != nil {
fmt.Printf("Hardware Fetch Error: %s", err)
} else {
fmt.Println("Got Snapshot for " + data.TimeStamp.Format("02/01/2006"))
for _, hdd := range data.HDD {
fmt.Printf("%s[%s]: %v\n", hdd.Model, hdd.Size, hdd.Temperature)
}
}
}
}()
@@ -121,13 +117,12 @@ func RunService() {
// Periodic saving routine
go func() {
for {
time.Sleep(time.Duration(config.GetConfiguration().MemoryDumpFrequency) * time.Second)
waitTime := time.Duration(config.GetConfiguration().MemoryDumpFrequency) * time.Second
time.Sleep(waitTime)
err := SaveSnapshotsToFile()
if err != nil {
fmt.Printf("Memory Dump Error: %s", err)
}
fmt.Println("Saved Snapshots to file")
}
}()
}