mirror of
https://github.com/JustKato/drive-health.git
synced 2026-04-30 01:42:29 +03:00
Started moving to a sqlite3 implementation
This commit is contained in:
@@ -1,127 +1,67 @@
|
||||
package svc
|
||||
|
||||
import (
|
||||
"encoding/gob"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
"tea.chunkbyte.com/kato/drive-health/lib/config"
|
||||
"tea.chunkbyte.com/kato/drive-health/lib/hardware"
|
||||
)
|
||||
|
||||
// The path to where the snapshot database is located
|
||||
const SNAPSHOT_LIST_PATH = "./snapshots.dat"
|
||||
var db *gorm.DB
|
||||
|
||||
// A simple in-memory buffer for the history of snapshots
|
||||
var snapShotBuffer []*HardwareSnapshot
|
||||
|
||||
// A snapshot in time of the current state of the harddrives
|
||||
type HardwareSnapshot struct {
|
||||
TimeStamp time.Time
|
||||
HDD []*hardware.HardDrive
|
||||
}
|
||||
|
||||
type Snapshots struct {
|
||||
List []*HardwareSnapshot
|
||||
}
|
||||
|
||||
// The function itterates through all hard disks and takes a snapshot of their state,
|
||||
// returns a struct which contains metadata as well as the harddrives themselves.
|
||||
func TakeHardwareSnapshot() (*HardwareSnapshot, error) {
|
||||
drives, err := hardware.GetSystemHardDrives()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
func InitDB() {
|
||||
var err error
|
||||
dbPath := config.GetConfiguration().DatabaseFilePath
|
||||
if dbPath == "" {
|
||||
dbPath = "./data.db"
|
||||
}
|
||||
|
||||
snapShot := &HardwareSnapshot{
|
||||
TimeStamp: time.Now(),
|
||||
HDD: []*hardware.HardDrive{},
|
||||
db, err = gorm.Open(sqlite.Open(dbPath), &gorm.Config{})
|
||||
if err != nil {
|
||||
panic("failed to connect database")
|
||||
}
|
||||
|
||||
// Migrate the schema
|
||||
db.AutoMigrate(&hardware.HardDrive{}, &hardware.HardDriveTemperature{})
|
||||
}
|
||||
|
||||
func GetDatabaseRef() *gorm.DB {
|
||||
return db
|
||||
}
|
||||
|
||||
func LogDriveTemps() error {
|
||||
drives, err := hardware.GetSystemHardDrives(db, nil, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, hdd := range drives {
|
||||
hdd.GetTemperature(true)
|
||||
snapShot.HDD = append(snapShot.HDD, hdd)
|
||||
}
|
||||
|
||||
// Append to the in-memory listing
|
||||
snapShotBuffer = append(snapShotBuffer, snapShot)
|
||||
|
||||
// Return the snapshot just in case there is any need to modify it,
|
||||
// any modification to it will also affect the current buffer from memory.
|
||||
return snapShot, nil
|
||||
}
|
||||
|
||||
// The function wil check if the `.dat` file is present, if it is then it will load it into memory
|
||||
func UpdateHardwareSnapshotsFromFile() {
|
||||
file, err := os.Open(SNAPSHOT_LIST_PATH)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return // File does not exist, no snapshots to load
|
||||
}
|
||||
panic(err) // Handle error according to your error handling policy
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
decoder := gob.NewDecoder(file)
|
||||
var snapshots Snapshots
|
||||
if err := decoder.Decode(&snapshots); err != nil {
|
||||
if err == io.EOF {
|
||||
return // End of file reached
|
||||
}
|
||||
panic(err) // Handle error according to your error handling policy
|
||||
}
|
||||
|
||||
snapShotBuffer = snapshots.List
|
||||
|
||||
fmt.Printf("Loaded %v snapshots from .dat", len(snapShotBuffer))
|
||||
}
|
||||
|
||||
// Get the list of snapshots that have been buffered in memory
|
||||
func GetHardwareSnapshot() []*HardwareSnapshot {
|
||||
return snapShotBuffer
|
||||
}
|
||||
|
||||
// Dump the current snapshot history from memory to file
|
||||
func SaveSnapshotsToFile() error {
|
||||
file, err := os.Create(SNAPSHOT_LIST_PATH)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
encoder := gob.NewEncoder(file)
|
||||
snapshots := Snapshots{List: snapShotBuffer}
|
||||
if err := encoder.Encode(snapshots); err != nil {
|
||||
return err
|
||||
temp := hdd.GetTemperature()
|
||||
db.Create(&hardware.HardDriveTemperature{
|
||||
HardDriveID: hdd.ID,
|
||||
TimeStamp: time.Now(),
|
||||
Temperature: temp,
|
||||
})
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func RunService() {
|
||||
func RunLoggerService() {
|
||||
fmt.Println("Initializing Temperature Logging Service...")
|
||||
|
||||
tickTime := time.Duration(config.GetConfiguration().DiskFetchFrequency) * time.Second
|
||||
|
||||
// Snapshot taking routine
|
||||
go func() {
|
||||
waitTime := time.Duration(config.GetConfiguration().DiskFetchFrequency) * time.Second
|
||||
for {
|
||||
time.Sleep(waitTime)
|
||||
_, err := TakeHardwareSnapshot()
|
||||
time.Sleep(tickTime)
|
||||
err := LogDriveTemps()
|
||||
if err != nil {
|
||||
fmt.Printf("Hardware Fetch Error: %s", err)
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
// Periodic saving routine
|
||||
go func() {
|
||||
for {
|
||||
waitTime := time.Duration(config.GetConfiguration().MemoryDumpFrequency) * time.Second
|
||||
time.Sleep(waitTime)
|
||||
err := SaveSnapshotsToFile()
|
||||
if err != nil {
|
||||
fmt.Printf("Memory Dump Error: %s", err)
|
||||
fmt.Printf("🛑 Temperature logging failed: %s\n", err)
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
Reference in New Issue
Block a user