Compare commits

...

4 Commits

Author SHA1 Message Date
18436e050f implement AppContext, add Note type, add menu 2024-05-12 14:38:55 -04:00
7128dbd38e add AppContext 2024-05-12 14:22:04 -04:00
e5ab7ec5ef add Record and Note types 2024-05-12 14:13:24 -04:00
174173f99b refactor: add GKI field to Reading 2024-05-12 14:09:50 -04:00
2 changed files with 97 additions and 57 deletions

View File

@ -21,4 +21,12 @@ Reading data will be saved to `$HOME/.ketotrack/readings.json`.
```shell ```shell
go build go build
mv ketotrack $HOME/.local/bin mv ketotrack $HOME/.local/bin
``` ```
## Refactoring Notes
- [X] Create `Record` and `Note` struct types
- [X] Add `GKI` field to `Reading` struct type
- [X] Encapsulate application data with an `AppContext` struct that holds a `Records` slice
- [X] `LoadRecords`, `SaveRecords`, `AddReading`, `AddNote` receiver methods
- [X] Add interactive menu for adding either a `Note` or a `Record`
- [ ] Standardize method names for getting user input to `handleNewNote` and `handleNewReading`

144
main.go
View File

@ -12,7 +12,7 @@ import (
"time" "time"
) )
var readingsFilename string var recordsFilename string
func init() { func init() {
// Get user's homeDir directory // Get user's homeDir directory
@ -33,7 +33,19 @@ func init() {
} }
// Set data file path for readings // Set data file path for readings
readingsFilename = filepath.Join(ketotrackDir, "readings.json") recordsFilename = filepath.Join(ketotrackDir, "records.json")
}
// Record holds a pointer to a Reading or Note.
type Record struct {
Reading *Reading `json:"reading,omitempty"`
Note *Note `json:"note,omitempty"`
}
// 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"`
} }
// Reading holds the glucose and ketone level measurements along with the time the measurements were taken. // Reading holds the glucose and ketone level measurements along with the time the measurements were taken.
@ -41,89 +53,109 @@ type Reading struct {
Time time.Time `json:"time"` Time time.Time `json:"time"`
Glucose float64 `json:"glucose"` Glucose float64 `json:"glucose"`
Ketone float64 `json:"ketone"` Ketone float64 `json:"ketone"`
GKI float64 `json:"GKI"`
} }
// NewReading creates and returns a new Reading instance with the provided glucose and ketone levels, and records // 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. // 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 { func NewReading(glucose, ketone float64) Reading {
var gki float64
if ketone == 0 {
gki = 0
} else {
gki = (glucose / 18) / ketone
}
return Reading{ return Reading{
Time: time.Now(), Time: time.Now(),
Glucose: glucose, Glucose: glucose,
Ketone: ketone, Ketone: ketone,
GKI: gki,
} }
} }
// GKI calculates and returns the Glucose Ketone Index of the reading. // AppContext is the application data store that holds Records
// The Glucose Ketone Index helps determine the efficiency of glucose and ketone levels within the body. It is type AppContext struct {
// calculated as (glucose level in mg/dL / 18) divided by the ketone level. Records []Record
func (r Reading) GKI() (float64, error) {
if r.Ketone == 0 {
return 0, errors.New("ketone level must not be zero to calculate GKI")
}
return (r.Glucose / 18) / r.Ketone, nil
} }
func main() { // LoadRecords will load records from file
// Take a new reading from the user func (ctx *AppContext) LoadRecords(filename string) error {
reading, err := getReading() data, err := os.ReadFile(filename)
if err != nil { if err != nil {
fmt.Println(err.Error()) return err
return
}
// Calculate GKI
gki, err := reading.GKI()
if err != nil {
fmt.Println(err.Error())
fmt.Println("You are not in ketosis.")
return
}
fmt.Printf("\nYour GKI is: %0.2f\n", gki)
// Display level of ketosis
switch {
case gki <= 1:
fmt.Println("You're in the highest level of ketosis.")
case gki < 3:
fmt.Println("You're in a high therapeutic level of ketosis.")
case gki < 6:
fmt.Println("You're in a moderate level of ketosis.")
case gki <= 9:
fmt.Println("You're in a low level of ketosis.")
default:
fmt.Println("You are not in ketosis.")
}
// Save latest reading to readings file
readings, _ := loadReadingsFromFile(readingsFilename)
readings = append(readings, reading)
err = saveReadingsToFile(readingsFilename, readings)
if err != nil {
fmt.Printf("error saving data: %s\n", err.Error())
} }
return json.Unmarshal(data, &ctx.Records)
} }
func saveReadingsToFile(filename string, data []Reading) error { // SaveRecords will save records to a file
jsonData, err := json.Marshal(data) func (ctx *AppContext) SaveRecords(filename string) error {
jsonData, err := json.Marshal(ctx.Records)
if err != nil { if err != nil {
return err return err
} }
return os.WriteFile(filename, jsonData, 0660) return os.WriteFile(filename, jsonData, 0660)
} }
func loadReadingsFromFile(filename string) ([]Reading, error) { // AddReading will add a Reading to the AppContext
data, err := os.ReadFile(filename) func (ctx *AppContext) AddReading(reading Reading) {
ctx.Records = append(ctx.Records, Record{Reading: &reading})
}
// AddNote will a add a Note to the AppContext
func (ctx *AppContext) AddNote(note Note) {
ctx.Records = append(ctx.Records, Record{Note: &note})
}
func main() {
var appCtx AppContext
err := appCtx.LoadRecords(recordsFilename)
if err != nil { if err != nil {
return nil, err fmt.Printf("Error loading records: %s\n", err)
} }
var readings []Reading // Display menu
err = json.Unmarshal(data, &readings) choice, err := getUserInput("1. Enter new reading\n2. Enter new note\n3. Exit\nYour choice")
if err != nil { switch choice {
return nil, err case "1":
// Take a new reading from the user
reading, err := getReading()
if err != nil {
fmt.Printf("Error getting reading: %s\n", err)
} else {
// Display GKI and level of ketosis
fmt.Printf("\nYour GKI is: %0.2f\n", reading.GKI)
switch {
case reading.GKI <= 1:
fmt.Println("You're in the highest level of ketosis.")
case reading.GKI < 3:
fmt.Println("You're in a high therapeutic level of ketosis.")
case reading.GKI < 6:
fmt.Println("You're in a moderate level of ketosis.")
case reading.GKI <= 9:
fmt.Println("You're in a low level of ketosis.")
default:
fmt.Println("You are not in ketosis.")
}
appCtx.AddReading(reading)
}
case "2":
noteText, err := getUserInput("Enter your note text: ")
if err != nil {
fmt.Println("Error getting note: %s\n", err)
} else {
appCtx.AddNote(Note{
Time: time.Now(),
Text: noteText,
})
}
default:
fmt.Println("Quitting...")
} }
return readings, nil // Save Records before exiting
err = appCtx.SaveRecords(recordsFilename)
} }
func getReading() (Reading, error) { func getReading() (Reading, error) {