yoake/internal/health/db.go

209 lines
5.2 KiB
Go
Raw Permalink Normal View History

2022-11-16 14:54:46 -06:00
package health
import (
"errors"
"sort"
"time"
"github.com/eternal-flame-AD/yoake/internal/db"
"github.com/eternal-flame-AD/yoake/internal/echoerror"
"github.com/eternal-flame-AD/yoake/internal/util"
"github.com/google/uuid"
)
func DBMedListGet(database db.DB) ([]Direction, error) {
txn := database.NewTransaction(false)
defer txn.Discard()
var meds []Direction
if err := db.GetJSON(txn, []byte("health_meds_list"), &meds); db.IsNotFound(err) {
err = DBMedListSet(database, []Direction{})
return meds, err
} else if err != nil {
return nil, err
}
return meds, nil
}
func DBMedListSet(database db.DB, meds []Direction) error {
txn := database.NewTransaction(true)
defer txn.Discard()
for i, med := range meds {
_, meds[i].DirectionShorthand = med.ShortHand()
}
if err := db.SetJSON(txn, []byte("health_meds_list"), meds); err != nil {
return err
}
return txn.Commit()
}
const dbMedComplianceLogPrefix = "health_meds_compliance_log_"
func DBMedComplianceLogGet(database db.DB, dates util.DateRange) (ComplianceLogList, error) {
txn := database.NewTransaction(false)
defer txn.Discard()
endIndex := dates.To.UTC().Format("2006-01")
indexesToFetch := []string{endIndex}
for indexNow := dates.From.UTC().AddDate(0, -1, 0); indexNow.Before(dates.To.AddDate(0, 1, 0)); indexNow = indexNow.AddDate(0, 1, 0) {
indexesToFetch = append(indexesToFetch, indexNow.Format("2006-01"))
}
indexesToFetch = util.Unique(indexesToFetch)
sort.Strings(indexesToFetch)
var res ComplianceLogList
for _, index := range indexesToFetch {
var log []ComplianceLog
if err := db.GetJSON(txn, []byte(dbMedComplianceLogPrefix+index), &log); db.IsNotFound(err) {
continue
} else if err != nil {
return nil, err
}
res = append(res, log...)
}
sort.Sort(res)
return res, nil
}
2022-11-18 23:37:47 -06:00
func DBMedComplianceLogAppend(database db.DB, pending ComplianceLogList) error {
txn := database.NewTransaction(true)
defer txn.Discard()
for len(pending) > 0 {
index := pending[0].Actual.Time.UTC().Format("2006-01")
var origLogs ComplianceLogList
if err := db.GetJSON(txn, []byte(dbMedComplianceLogPrefix+index), &origLogs); db.IsNotFound(err) {
origLogs = []ComplianceLog{}
} else if err != nil {
return err
}
for i := len(pending) - 1; i >= 0; i-- {
if pending[i].Actual.Time.UTC().Format("2006-01") != index {
continue
}
origLogs = append(origLogs, pending[i])
pending = append(pending[:i], pending[i+1:]...)
uuidMap := make(map[string]int)
for j := len(origLogs) - 1; j >= 0; j-- {
if _, ok := uuidMap[origLogs[j].UUID]; ok {
origLogs = append(origLogs[:j], origLogs[j+1:]...)
} else {
uuidMap[origLogs[j].UUID] = j
}
}
}
sort.Sort(origLogs)
if err := db.SetJSON(txn, []byte(dbMedComplianceLogPrefix+index), origLogs); err != nil {
return err
}
}
return txn.Commit()
}
2022-11-16 14:54:46 -06:00
func DBMedComplianceLogSetOne(database db.DB, dir Direction, log *ComplianceLog) error {
index := log.Actual.Time.UTC().Format("2006-01")
existingLogs, err := DBMedComplianceLogGet(database, util.DateRangeAround(log.Actual.Time, 1))
if err != nil {
return err
}
txn := database.NewTransaction(true)
defer txn.Discard()
del := false
if log.Actual.Dose == 0 {
return echoerror.NewHttp(400, errors.New("dose cannot be zero"))
} else if log.Actual.Dose < 0 {
del = true
}
if log.UUID != "" {
foundIdx := -1
for i, existingLog := range existingLogs {
if existingLog.UUID == log.UUID {
log.UpdatedAt = time.Now()
foundIdx = i
break
}
}
if foundIdx < 0 {
return echoerror.NewHttp(404, errors.New("log with specified UUID not found"))
}
origLog := existingLogs[foundIdx]
log.CreatedAt = origLog.CreatedAt
origLogIdx := origLog.Actual.Time.UTC().Format("2006-01")
if origLogIdx == index {
// update and return
if del {
existingLogs = append(existingLogs[:foundIdx], existingLogs[foundIdx+1:]...)
} else {
log.UpdatedAt = time.Now()
existingLogs[foundIdx] = *log
}
2022-11-18 23:37:47 -06:00
sort.Sort(existingLogs)
2022-11-16 14:54:46 -06:00
if err := db.SetJSON(txn, []byte(dbMedComplianceLogPrefix+index), existingLogs); err != nil {
return err
}
return txn.Commit()
} else {
// delete from old index
existingLogs = append(existingLogs[:foundIdx], existingLogs[foundIdx+1:]...)
if err := db.SetJSON(txn, []byte(dbMedComplianceLogPrefix+origLogIdx), existingLogs); err != nil {
return err
}
}
}
if del {
return txn.Commit()
}
if log.UUID == "" {
log.UUID = uuid.New().String()
log.CreatedAt = time.Now()
2022-11-18 23:37:47 -06:00
if log.Expected.Dose == 0 {
nextDose := existingLogs.ProjectNextDose(dir)
log.Expected.Time = nextDose.Expected.Time
log.Expected.Dose = nextDose.Expected.Dose
}
2022-11-16 14:54:46 -06:00
}
log.UpdatedAt = time.Now()
var logs ComplianceLogList
if err := db.GetJSON(txn, []byte(dbMedComplianceLogPrefix+index), &logs); db.IsNotFound(err) {
logs = []ComplianceLog{*log}
} else if err != nil {
return err
} else {
logs = append(logs, *log)
}
2022-11-18 23:37:47 -06:00
logs.UpdateDoseOffset(dir)
2022-11-20 12:05:14 -06:00
uuid := log.UUID
2022-11-16 14:54:46 -06:00
sort.Sort(ComplianceLogList(logs))
2022-11-18 23:37:47 -06:00
2022-11-16 14:54:46 -06:00
if err := db.SetJSON(txn, []byte(dbMedComplianceLogPrefix+index), logs); err != nil {
return err
}
2022-11-20 12:05:14 -06:00
for i, l := range logs {
if l.UUID == uuid {
*log = logs[i]
}
}
2022-11-16 14:54:46 -06:00
return txn.Commit()
}