Compare commits
No commits in common. "master" and "dev" have entirely different histories.
@ -1,6 +1,3 @@
|
||||
// Package config handles the configuration aspects of the KetoTrack application.
|
||||
// It includes definitions and routines necessary for setting up and retrieving
|
||||
// application configuration settings, such as data storage paths.
|
||||
package config
|
||||
|
||||
import (
|
||||
@ -8,23 +5,15 @@ import (
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// AppConfig holds configuration details for the application. It currently
|
||||
// only includes the path to the data storage location.
|
||||
type AppConfig struct {
|
||||
DataPath string
|
||||
}
|
||||
|
||||
// Load initializes the application configuration by ensuring the required
|
||||
// directories exist and setting the path for data storage. It creates a
|
||||
// directory named ".ketotrack" in the user's home directory if it does not already
|
||||
// exist, and sets the data path to "records.json" within this directory.
|
||||
// The function loads these settings into an AppConfig instance and returns it.
|
||||
func Load() AppConfig {
|
||||
// TODO: Handle os.UserHomeDir errors
|
||||
// TODO: Handle os.MkdirAll errors
|
||||
// TODO: Handle os.MkdirAll error gracefully
|
||||
homeDir, _ := os.UserHomeDir()
|
||||
ketotrackDir := filepath.Join(homeDir, ".ketotrack")
|
||||
os.MkdirAll(ketotrackDir, 0770)
|
||||
os.MkdirAll(ketotrackDir, 0770) // handle error appropriately
|
||||
return AppConfig{
|
||||
DataPath: filepath.Join(ketotrackDir, "records.json"),
|
||||
}
|
||||
|
25
data/data.go
25
data/data.go
@ -1,7 +1,3 @@
|
||||
// Package data manages the loading, saving, and manipulation of application data
|
||||
// pertaining to health tracking. It provides an abstraction layer over the storage
|
||||
// mechanism used for persisting records and offers convenient methods to interact
|
||||
// with the data.
|
||||
package data
|
||||
|
||||
import (
|
||||
@ -10,17 +6,12 @@ import (
|
||||
"os"
|
||||
)
|
||||
|
||||
// AppContext represents the application's data context, encapsulating the storage
|
||||
// and management of health-related records. It provides mechanisms to load and save
|
||||
// these records from a persistent storage.
|
||||
// AppContext is the application data store that holds Records
|
||||
type AppContext struct {
|
||||
Records []model.Record
|
||||
dataPath string
|
||||
}
|
||||
|
||||
// NewContext initializes a new application data context with the specified data
|
||||
// path. It attempts to load existing records from the provided data path, or
|
||||
// initializes an empty context if the records do not exist.
|
||||
func NewContext(dataPath string) (AppContext, error) {
|
||||
ctx := AppContext{dataPath: dataPath}
|
||||
err := ctx.LoadRecords()
|
||||
@ -30,9 +21,7 @@ func NewContext(dataPath string) (AppContext, error) {
|
||||
return ctx, nil
|
||||
}
|
||||
|
||||
// LoadRecords loads the records from the data file specified in the AppContext.
|
||||
// If the records file does not exist, it initializes an empty record list and
|
||||
// saves it to create the file.
|
||||
// LoadRecords will load records from file
|
||||
func (ctx *AppContext) LoadRecords() error {
|
||||
data, err := os.ReadFile(ctx.dataPath)
|
||||
|
||||
@ -48,9 +37,7 @@ func (ctx *AppContext) LoadRecords() error {
|
||||
return json.Unmarshal(data, &ctx.Records)
|
||||
}
|
||||
|
||||
// SaveRecords serializes the current records to JSON and writes them to the
|
||||
// data path specified in the AppContext. It ensures data persistence with
|
||||
// appropriate access permissions.
|
||||
// SaveRecords will save records to a file
|
||||
func (ctx *AppContext) SaveRecords() error {
|
||||
jsonData, err := json.Marshal(ctx.Records)
|
||||
if err != nil {
|
||||
@ -59,14 +46,12 @@ func (ctx *AppContext) SaveRecords() error {
|
||||
return os.WriteFile(ctx.dataPath, jsonData, 0660)
|
||||
}
|
||||
|
||||
// AddReading appends a new Reading record to the AppContext. It updates the
|
||||
// application context's records with the latest readings data.
|
||||
// AddReading will add a Reading to the AppContext
|
||||
func (ctx *AppContext) AddReading(reading model.Reading) {
|
||||
ctx.Records = append(ctx.Records, model.Record{Reading: &reading})
|
||||
}
|
||||
|
||||
// AddNote appends a new Note record to the AppContext. It updates the
|
||||
// application context's records with the newly added note.
|
||||
// AddNote will a add a Note to the AppContext
|
||||
func (ctx *AppContext) AddNote(note model.Note) {
|
||||
ctx.Records = append(ctx.Records, model.Record{Note: ¬e})
|
||||
}
|
||||
|
@ -1,22 +1,14 @@
|
||||
// Package model defines the data structures and associated functions used to represent
|
||||
// and manipulate the health data in the application. This includes defining formats for data entries,
|
||||
// calculations relevant to the metabolic tracking, and utilities for creating and handling these data
|
||||
// structures. This package serves as the central definition point for all data used in the application.
|
||||
package model
|
||||
|
||||
import "time"
|
||||
|
||||
// Note represents a text note marked with the time it was taken.
|
||||
// This structure can be used to store and retrieve anecdotal information that
|
||||
// may be relevant to the health data being tracked.
|
||||
// Note holds a text note along with the time the note was taken.
|
||||
type Note struct {
|
||||
Time time.Time `json:"time"`
|
||||
Text string `json:"text"`
|
||||
}
|
||||
|
||||
// NewNote creates a new Note instance with the current time and the provided text.
|
||||
// The function is a constructor for Note type, allowing quick instantiation with
|
||||
// automated timestamping.
|
||||
// NewNote returns a new Note
|
||||
func NewNote(text string) Note {
|
||||
return Note{
|
||||
Time: time.Now(),
|
||||
@ -24,20 +16,16 @@ func NewNote(text string) Note {
|
||||
}
|
||||
}
|
||||
|
||||
// Reading contains measurements for glucose and ketone levels taken at a specific time.
|
||||
// Each Reading instance also calculates the Glucose Ketone Index (GKI),
|
||||
// a key metric for managing metabolic health.
|
||||
// Reading holds the glucose and ketone level measurements along with the time the measurements were taken.
|
||||
type Reading struct {
|
||||
Time time.Time `json:"time"`
|
||||
Glucose float64 `json:"glucose"`
|
||||
Ketone float64 `json:"ketone"`
|
||||
GKI float64 `json:"gki"`
|
||||
GKI float64 `json:"GKI"`
|
||||
}
|
||||
|
||||
// NewReading creates a new Reading instance capturing the current time, glucose level,
|
||||
// and ketone level. It also calculates the Glucose Ketone Index (GKI) based on the provided
|
||||
// measurements. Glucose values should be in mg/dL and ketone levels in mmol/L.
|
||||
// If the ketone level is zero, the GKI is set to zero to prevent division by zero.
|
||||
// NewReading creates and returns a new Reading instance with the provided glucose and ketone levels, and records
|
||||
// the current time. The glucose value should be provided in mg/dL, while the ketone level should be in mmol/L.
|
||||
func NewReading(glucose, ketone float64) Reading {
|
||||
var gki float64
|
||||
if ketone == 0 {
|
||||
@ -54,10 +42,7 @@ func NewReading(glucose, ketone float64) Reading {
|
||||
}
|
||||
}
|
||||
|
||||
// Record represents a container that can hold either a Reading or a Note, but not both.
|
||||
// This type is used to conglomerate different types of entries in a unified format.
|
||||
// Each Record uses JSON tags to properly encode or decode either a Reading or a Note,
|
||||
// with omitted fields left out of the serialized form.
|
||||
// Record holds a pointer to a Reading or Note.
|
||||
type Record struct {
|
||||
Reading *Reading `json:"reading,omitempty"`
|
||||
Note *Note `json:"note,omitempty"`
|
||||
|
Loading…
Reference in New Issue
Block a user