38 Commits

Author SHA1 Message Date
531afabf61 Refactor the other services 2025-01-05 18:00:04 +01:00
a05a4eb9e2 Refactor weight service 2025-01-05 17:55:13 +01:00
1831ed12c5 Bump dependencies 2024-12-16 10:16:16 +01:00
bee45ebc59 Implement date formatting on frontend this time 2024-11-23 20:29:48 +01:00
2025f450e6 "Fix" datetime generation in db 2024-11-23 20:04:27 +01:00
fc688bf4bc Code format 2024-11-08 10:36:24 +01:00
31c7b31e65 Make it not look like such dogshit 2024-11-08 10:34:32 +01:00
cb0d8860ed Make better autocomplete selections 2024-11-08 09:05:55 +01:00
24546a4ef5 Migrate the rest of everything from rowid to id 2024-10-25 16:37:37 +02:00
b89e27d5b4 Add primary keys to tables 2024-10-25 15:16:10 +02:00
cbee5bd204 Ensure only last rows are queried for search 2024-09-11 09:04:39 +02:00
ec5310a1f3 Embed spellfix.dll 2024-09-04 23:57:13 +02:00
ec1c196752 Fix issue with focusout preventing onclick from autocomplete 2024-08-15 22:09:13 +02:00
6064d9847c Improve the autocomplete UX a little 2024-08-15 18:17:08 +02:00
235a90b0a7 Replace os.exit with wails quit
Exit would prevent defers from running which meant db would be left
dangling
2024-08-15 02:47:32 +02:00
4abddac94f Make search a little more responseive, hopefully 2024-08-14 12:52:11 +02:00
f12c353905 Add spellfix.dll 2024-08-14 12:49:31 +02:00
2586f0749f Fix broken init script 2024-08-13 21:44:47 +02:00
1387eecc96 Update per100 on select 2024-08-13 19:30:30 +02:00
32563d7d33 Enable clicking on autocomplete 2024-08-13 18:51:50 +02:00
12b00e5147 Implement a basic autocomplete style search 2024-08-13 18:51:50 +02:00
0051ae71d9 Fix go services returning nulls
I did not know that var foo []bar does not initialize foo to empty []!
Fuck
2024-08-13 18:51:50 +02:00
0e64ff9eb2 Implement search on the frontend 2024-08-13 18:51:50 +02:00
8fd8d53cc3 Rework search to return a list
Like a proper search would!"
2024-08-13 18:51:50 +02:00
a6626761e7 Rework food service to use new search query 2024-08-13 17:21:12 +02:00
1983c6c932 Implement spellfix 2024-08-13 17:21:12 +02:00
6bfd5cc26a Rework lastper100 to return a food instead of float32 2024-08-13 15:31:50 +02:00
abb25c1357 Fix issue with /Weight redirecting to /Energy which does not exist 2024-08-10 20:56:22 +02:00
e0eb7f9748 Make ctrl+tab work a little better
Hopefully I'll stop getting ideas soon..
2024-08-10 20:14:16 +02:00
b291fec8a0 Add linear regression to temporal graphs 2024-08-10 20:08:27 +02:00
d290cae3f7 Auto focus input element after inserting food 2024-08-10 19:39:36 +02:00
376201373e Improve and fix issues with color generation 2024-08-10 19:36:26 +02:00
84002d1856 Implement ctrl-tab navigation between weight and energy 2024-08-10 19:36:17 +02:00
3118069297 Implement refresh on ctrl-r and close on ctrl-w 2024-08-10 19:22:19 +02:00
80fb660677 Add refresh button for aggregate stores 2024-08-10 14:03:20 +02:00
de461cb031 Fix issue with crashing when having no entries today 2024-08-10 13:51:04 +02:00
6d684b34ef Implement DB auto init 2024-08-10 01:03:11 +02:00
35ca7620f4 Fix DB not closing on exit 2024-08-10 00:43:53 +02:00
46 changed files with 1315 additions and 497 deletions

13
app.go
View File

@@ -2,6 +2,8 @@ package main
import (
"context"
"github.com/wailsapp/wails/v2/pkg/runtime"
)
// App struct
@@ -43,12 +45,12 @@ func (a *App) UpdateFood(food Food) WailsFood1 {
return WailsFood1{Data: data, Success: true}
}
func (a *App) GetLastPer100(name string) WailsPer100 {
func (a *App) GetLastPer100(name string) WailsFoodSearch {
data, err := foodService.GetLastPer100(name)
if err != nil {
return WailsPer100{Success: false, Error: err.Error()}
return WailsFoodSearch{Success: false, Error: err.Error()}
}
return WailsPer100{Data: data, Success: true}
return WailsFoodSearch{Data: data, Success: true}
}
func (a *App) GetDailyFood() WailsAggregateFood {
@@ -135,3 +137,8 @@ func (a *App) SetSetting(key string, value int64) WailsGenericAck {
}
return WailsGenericAck{Success: true}
}
//region other
func (a *App) Close() {
runtime.Quit(a.ctx)
}

85
db.go
View File

@@ -3,9 +3,11 @@ package main
import (
"database/sql"
"fmt"
"log"
"os"
"time"
_ "github.com/mattn/go-sqlite3"
"github.com/mattn/go-sqlite3"
)
type DB struct {
@@ -20,7 +22,27 @@ func (db *DB) Open() error {
return fmt.Errorf("database path not set")
}
writeConn, err := sql.Open("sqlite3", db.path+"?_journal=WAL&_synchronous=NORMAL")
file, err := os.Open(db.path)
if err != nil {
if os.IsNotExist(err) {
log.Printf("Database file does not exist at %s, creating", db.path)
file, err := os.Create(db.path)
if err != nil {
return fmt.Errorf("failed to create database file: %v", err)
}
log.Printf("Database created at %s", db.path)
file.Close()
} else {
return fmt.Errorf("failed to open database file: %v", err)
}
}
file.Close()
sql.Register("spellfixlite", &sqlite3.SQLiteDriver{
Extensions: []string{"spellfix"},
})
writeConn, err := sql.Open("spellfixlite", db.path+"?_journal=WAL&_synchronous=NORMAL")
if err != nil {
Error.Printf("%++v", err)
return err
@@ -30,7 +52,7 @@ func (db *DB) Open() error {
writeConn.SetConnMaxLifetime(30 * time.Second)
db.writeConn = writeConn
readConn, err := sql.Open("sqlite3", db.path+"?mode=ro&_journal=WAL&_synchronous=NORMAL&_mode=ro")
readConn, err := sql.Open("spellfixlite", db.path+"?mode=ro&_journal=WAL&_synchronous=NORMAL&_mode=ro")
if err != nil {
Error.Printf("%++v", err)
return err
@@ -44,6 +66,63 @@ func (db *DB) Open() error {
return nil
}
func (db *DB) Init(ddl string) error {
if !db.Ready {
return fmt.Errorf("database not ready")
}
var rows map[string]struct{} = make(map[string]struct{})
// TODO: Maybe make this better one day...
var expected = map[string]struct{}{
"food": {},
"weight": {},
"settings": {},
}
res, err := db.readConn.Query("SELECT name FROM sqlite_master WHERE type='table';")
if err != nil {
Error.Printf("%++v", err)
return err
}
defer res.Close()
for res.Next() {
var name string
err := res.Scan(&name)
if err != nil {
Error.Printf("%++v", err)
return err
}
rows[name] = struct{}{}
}
var needsInit bool
for table := range expected {
if _, ok := rows[table]; !ok {
log.Printf("Table %s not found, initializing", table)
needsInit = true
break
}
}
if !needsInit {
log.Printf("Database already initialized")
return nil
}
_, err = db.writeConn.Exec(ddl)
if err != nil {
Error.Printf("%++v", err)
log.Printf("%#v", "Rolling back")
_, err2 := db.writeConn.Exec("ROLLBACK;")
if err2 != nil {
Error.Printf("Error rollingback! %++v", err)
}
return err
}
return nil
}
func (db *DB) Close() error {
err := db.writeConn.Close()
if err != nil {

103
food.ddl
View File

@@ -1,11 +1,11 @@
begin transaction;
begin;
create table weight (
date datetime default (datetime('now', '+2 hours')),
id integer primary key,
date datetime default (datetime('now')),
weight real not null
);
create index weightDateIdx on weight(date);
create index weightDailyIdx on weight(strftime('%Y-%m-%d', date));
create index weightWeeklyIdx on weight(strftime('%Y-%W', date));
@@ -19,7 +19,7 @@ drop view if exists weightMonthly;
drop view if exists weightYearly;
create view weightView as
select rowid,
select id,
date,
round(weight, 2) as weight
from weight
@@ -28,33 +28,34 @@ order by date desc;
create view weightDaily as
select strftime('%Y-%m-%d', date) as period,
round(avg(weight), 2) as amount
from weight
from weight
group by strftime('%Y-%m-%d', date)
order by date desc;
create view weightWeekly as
select strftime('%Y-%W', date) as period,
round(avg(weight), 2) as amount
from weight
from weight
group by strftime('%Y-%W', date)
order by date desc;
create view weightMonthly as
select strftime('%Y-%m', date) as period,
round(avg(weight), 2) as amount
from weight
from weight
group by strftime('%Y-%m', date)
order by date desc;
create view weightYearly as
select strftime('%Y', date) as period,
round(avg(weight), 2) as amount
from weight
from weight
group by strftime('%Y', date)
order by date desc;
create table food(
date datetime default (datetime('now', '+2 hours')),
id integer primary key,
date datetime default (datetime('now')),
food varchar not null,
description varchar,
amount real not null,
@@ -62,6 +63,79 @@ create table food(
energy generated always as (coalesce(amount, 0) * coalesce(per100, 0) / 100) stored
);
create virtual table foodfix using spellfix1;
insert into foodfix (word, rank)
select food as word,
count(*) as rank
from food
group by food;
drop trigger if exists food_foodfix_insert;
create trigger food_foodfix_insert AFTER
insert on food for EACH row
begin
update foodfix
set rank = rank + 1
where word = new.food;
insert into foodfix (word, rank)
select new.food,
1
where not exists (
select 1
from foodfix
where word = new.food
);
end;
drop trigger if exists food_foodfix_delete;
create trigger food_foodfix_delete AFTER
delete on food for EACH row
begin
update foodfix
set rank = rank - 1
where word = old.food;
delete from foodfix
where word = old.food
and rank <= 0;
end;
drop trigger if exists food_foodfix_update;
create trigger food_foodfix_update AFTER
update on food for EACH row
begin
update foodfix
set rank = rank - 1
where word = old.food;
delete from foodfix
where word = old.food
and rank <= 0;
update foodfix
set rank = rank + 1
where word = new.food;
insert into foodfix (word, rank)
select new.food,
1
where not exists (
select 1
from foodfix
where word = new.food
);
end;
with search_results as (
select word, score, f.rowid, f.*
from foodfix
inner join food f on f.food == word
where word match 'B'
)
select rowid, food, score, date, description, amount, per100, energy
from search_results
group by food
order by score asc, date desc;
create index dailyIdx on food(strftime('%Y-%m-%d', date));
create index weeklyIdx on food(strftime('%Y-%W', date));
create index monthlyIdx on food(strftime('%Y-%m', date));
@@ -77,7 +151,7 @@ drop view if exists foodYearly;
drop view if exists foodRecent;
create view foodView as
select rowid,
select id,
date,
food,
description,
@@ -123,8 +197,7 @@ group by strftime('%Y', date)
order by date desc;
create view foodRecent as
select rowid,
*
select *
from food
order by date desc
limit 10;
@@ -135,17 +208,17 @@ insert on food
begin
update food
set per100 = coalesce(
new .per100,
new.per100,
(
select per100
from food
where food = new .food
where food = new.food
and per100 is not null
order by date desc
limit 1
)
)
where rowid = new .rowid;
where id = new.id;
end;
create table settings(

View File

@@ -11,7 +11,7 @@ type (
db *DB
}
Food struct {
Rowid int64 `json:"rowid"`
Id int64 `json:"id"`
Date string `json:"date"`
Food string `json:"food"`
Descripton string `json:"description"`
@@ -19,6 +19,10 @@ type (
Per100 float32 `json:"per100"`
Energy float32 `json:"energy"`
}
FoodSearch struct {
Food
Score int64 `json:"score"`
}
AggregatedFood struct {
Period string `json:"period"`
Amount float32 `json:"amount"`
@@ -27,24 +31,23 @@ type (
}
)
const foodColumns = "rowid, date, food, description, amount, per100, energy"
const foodColumns = "id, date, food, description, amount, per100, energy"
const foodAggregatedColumns = "period, amount, avgPer100, energy"
func (s *FoodService) GetRecent() ([]Food, error) {
var res []Food
func (s *FoodService) GetRecent() (res []Food, err error) {
log.Printf("Getting recent food")
if s.db == nil || !s.db.Ready {
return res, fmt.Errorf("cannot get recent food, db is nil or is not ready")
}
row, err := s.db.readConn.Query(fmt.Sprintf("SELECT %s from foodView WHERE date > datetime('now', '-%d days') order by date desc;", foodColumns, Settings.FoodDaysLookback))
if err != nil {
log.Printf("error getting daily food: %v", err)
return res, err
return res, fmt.Errorf("error getting recent food: %w", err)
}
for row.Next() {
var food Food
err := row.Scan(&food.Rowid, &food.Date, &food.Food, &food.Descripton, &food.Amount, &food.Per100, &food.Energy)
err = row.Scan(&food.Id, &food.Date, &food.Food, &food.Descripton, &food.Amount, &food.Per100, &food.Energy)
if err != nil {
log.Printf("error scanning row: %v", err)
continue
@@ -53,25 +56,56 @@ func (s *FoodService) GetRecent() ([]Food, error) {
res = append(res, food)
}
log.Printf("Got %d recent foods", len(res))
return
}
func (s *FoodService) GetLastPer100(name string) (res []FoodSearch, err error) {
log.Printf("Getting last per100 for %s", name)
if s.db == nil || !s.db.Ready {
return res, fmt.Errorf("cannot get last per100, db is nil or is not ready")
}
query := fmt.Sprintf(`
with search_results as (
select word, score, f.rowid, f.*,
row_number() over (partition by f.food order by score asc, date desc) as rn
from foodfix
inner join food f on f.food == word
where word MATCH ?
)
select %s, score
from search_results
where rn = 1
order by score asc, date desc
limit %d
`, foodColumns, Settings.SearchLimit)
// log.Printf("%#v", query)
rows, err := s.db.readConn.Query(query, name)
if err != nil {
return res, fmt.Errorf("error getting last per100: %w", err)
}
for rows.Next() {
var f FoodSearch
err = rows.Scan(&f.Id, &f.Date, &f.Food.Food, &f.Descripton, &f.Amount, &f.Per100, &f.Energy, &f.Score)
if err != nil {
log.Printf("error scanning row: %v", err)
continue
}
res = append(res, f)
}
if len(res) == 0 {
return nil, fmt.Errorf("no results found for %s", name)
}
log.Printf("Got %d last per100 foods for %s", len(res), name)
return res, nil
}
func (s *FoodService) GetLastPer100(name string) (float32, error) {
if s.db == nil || !s.db.Ready {
return 0, fmt.Errorf("cannot get last per100, db is nil or is not ready")
}
row := s.db.readConn.QueryRow("SELECT per100 FROM food WHERE food like ? ORDER BY rowid DESC LIMIT 1", name+"%")
var per100 float32
err := row.Scan(&per100)
if err != nil {
return 0, fmt.Errorf("error scanning row: %v", err)
}
return per100, nil
}
func (s *FoodService) Create(food Food) (Food, error) {
func (s *FoodService) Create(food Food) (res Food, err error) {
log.Printf("Creating food %v", food)
if s.db == nil || !s.db.Ready {
return food, fmt.Errorf("cannot create food, db is nil or is not ready")
}
@@ -82,34 +116,35 @@ func (s *FoodService) Create(food Food) (Food, error) {
return food, fmt.Errorf("cannot create food, amount is less than or equal to 0")
}
var res sql.Result
var err error
var dbres sql.Result
if food.Per100 > 0 {
res, err = s.db.writeConn.Exec("INSERT INTO food (food, description, amount, per100) VALUES (?, ?, ?, ?)", food.Food, food.Descripton, food.Amount, food.Per100)
dbres, err = s.db.writeConn.Exec("INSERT INTO food (food, description, amount, per100) VALUES (?, ?, ?, ?)", food.Food, food.Descripton, food.Amount, food.Per100)
if err != nil {
return food, fmt.Errorf("error inserting food: %v", err)
return food, fmt.Errorf("error inserting food: %w", err)
}
} else {
res, err = s.db.writeConn.Exec("INSERT INTO food (food, description, amount) VALUES (?, ?, ?)", food.Food, food.Descripton, food.Amount)
dbres, err = s.db.writeConn.Exec("INSERT INTO food (food, description, amount) VALUES (?, ?, ?)", food.Food, food.Descripton, food.Amount)
if err != nil {
return food, fmt.Errorf("error inserting food: %v", err)
return food, fmt.Errorf("error inserting food: %w", err)
}
}
rowid, err := res.LastInsertId()
id, err := dbres.LastInsertId()
if err != nil {
return food, fmt.Errorf("error getting last insert id: %v", err)
return food, fmt.Errorf("error getting last insert id: %w", err)
}
return s.GetByRowid(rowid)
log.Printf("Created food %s with id %d", food.Food, id)
return s.GetById(id)
}
func (s *FoodService) Update(food Food) (Food, error) {
func (s *FoodService) Update(food Food) (res Food, err error) {
log.Printf("Updating food %s with id %d", food.Food, food.Id)
if s.db == nil || !s.db.Ready {
return food, fmt.Errorf("cannot update food, db is nil or is not ready")
}
if food.Rowid <= 0 {
return food, fmt.Errorf("cannot update food, rowid is less than or equal to 0")
if food.Id <= 0 {
return food, fmt.Errorf("cannot update food, id is less than or equal to 0")
}
if food.Food == "" {
return food, fmt.Errorf("cannot update food, food is empty")
@@ -119,47 +154,48 @@ func (s *FoodService) Update(food Food) (Food, error) {
}
if food.Per100 > 0 {
_, err := s.db.writeConn.Exec("UPDATE food SET food = ?, description = ?, amount = ?, per100 = ? WHERE rowid = ?", food.Food, food.Descripton, food.Amount, food.Per100, food.Rowid)
_, err := s.db.writeConn.Exec("UPDATE food SET food = ?, description = ?, amount = ?, per100 = ? WHERE Id = ?", food.Food, food.Descripton, food.Amount, food.Per100, food.Id)
if err != nil {
return food, fmt.Errorf("error updating food: %v", err)
return food, fmt.Errorf("error updating food: %w", err)
}
} else {
_, err := s.db.writeConn.Exec("UPDATE food SET food = ?, description = ?, amount = ? WHERE rowid = ?", food.Food, food.Descripton, food.Amount, food.Rowid)
_, err := s.db.writeConn.Exec("UPDATE food SET food = ?, description = ?, amount = ? WHERE Id = ?", food.Food, food.Descripton, food.Amount, food.Id)
if err != nil {
return food, fmt.Errorf("error updating food: %v", err)
return food, fmt.Errorf("error updating food: %w", err)
}
}
return s.GetByRowid(food.Rowid)
log.Printf("Updated food %s with id %d", food.Food, food.Id)
return s.GetById(food.Id)
}
func (s *FoodService) GetByRowid(rowid int64) (Food, error) {
var res Food
func (s *FoodService) GetById(id int64) (res Food, err error) {
log.Printf("Getting food by id %d", id)
if s.db == nil || !s.db.Ready {
return res, fmt.Errorf("cannot get food by rowid, db is nil or is not ready")
return res, fmt.Errorf("cannot get food by id, db is nil or is not ready")
}
row := s.db.readConn.QueryRow(fmt.Sprintf("SELECT %s from foodView WHERE rowid = ?", foodColumns), rowid)
err := row.Scan(&res.Rowid, &res.Date, &res.Food, &res.Descripton, &res.Amount, &res.Per100, &res.Energy)
row := s.db.readConn.QueryRow(fmt.Sprintf("SELECT %s from foodView WHERE id = ?", foodColumns), id)
err = row.Scan(&res.Id, &res.Date, &res.Food, &res.Descripton, &res.Amount, &res.Per100, &res.Energy)
if err != nil {
return res, fmt.Errorf("error scanning row: %v", err)
return res, fmt.Errorf("error scanning row: %w", err)
}
log.Printf("Got food %s with id %d", res.Food, res.Id)
return res, nil
}
// I could probably refactor this to be less of a disaster...
// But I think it'll work for now
func (s *FoodService) GetDaily() ([]AggregatedFood, error) {
var res []AggregatedFood
func (s *FoodService) GetDaily() (res []AggregatedFood, err error) {
log.Printf("Getting daily food")
if s.db == nil || !s.db.Ready {
return res, fmt.Errorf("cannot get daily food, db is nil or is not ready")
}
row, err := s.db.readConn.Query(fmt.Sprintf("SELECT %s from foodDaily LIMIT %d", foodAggregatedColumns, Settings.FoodDailyLookback))
if err != nil {
log.Printf("error getting daily food: %v", err)
return res, err
return res, fmt.Errorf("error getting daily food: %w", err)
}
for row.Next() {
@@ -173,24 +209,24 @@ func (s *FoodService) GetDaily() ([]AggregatedFood, error) {
res = append(res, food)
}
log.Printf("Got %d daily foods", len(res))
return res, nil
}
func (s *FoodService) GetWeekly() ([]AggregatedFood, error) {
var res []AggregatedFood
func (s *FoodService) GetWeekly() (res []AggregatedFood, err error) {
log.Printf("Getting weekly food")
if s.db == nil || !s.db.Ready {
return res, fmt.Errorf("cannot get weekly food, db is nil or is not ready")
}
row, err := s.db.readConn.Query(fmt.Sprintf("SELECT %s from foodWeekly LIMIT %d", foodAggregatedColumns, Settings.FoodWeeklyLookback))
if err != nil {
log.Printf("error getting weekly food: %v", err)
return res, err
return res, fmt.Errorf("error getting weekly food: %w", err)
}
for row.Next() {
var food AggregatedFood
err := row.Scan(&food.Period, &food.Amount, &food.AvgPer100, &food.Energy)
err = row.Scan(&food.Period, &food.Amount, &food.AvgPer100, &food.Energy)
if err != nil {
log.Printf("error scanning row: %v", err)
continue
@@ -199,24 +235,24 @@ func (s *FoodService) GetWeekly() ([]AggregatedFood, error) {
res = append(res, food)
}
log.Printf("Got %d weekly foods", len(res))
return res, nil
}
func (s *FoodService) GetMonthly() ([]AggregatedFood, error) {
var res []AggregatedFood
func (s *FoodService) GetMonthly() (res []AggregatedFood, err error) {
log.Printf("Getting monthly food")
if s.db == nil || !s.db.Ready {
return res, fmt.Errorf("cannot get monthly food, db is nil or is not ready")
}
row, err := s.db.readConn.Query(fmt.Sprintf("SELECT %s from foodMonthly LIMIT %d", foodAggregatedColumns, Settings.FoodMonthlyLookback))
if err != nil {
log.Printf("error getting monthly food: %v", err)
return res, err
return res, fmt.Errorf("error getting monthly food: %w", err)
}
for row.Next() {
var food AggregatedFood
err := row.Scan(&food.Period, &food.Amount, &food.AvgPer100, &food.Energy)
err = row.Scan(&food.Period, &food.Amount, &food.AvgPer100, &food.Energy)
if err != nil {
log.Printf("error scanning row: %v", err)
continue
@@ -225,24 +261,24 @@ func (s *FoodService) GetMonthly() ([]AggregatedFood, error) {
res = append(res, food)
}
log.Printf("Got %d monthly foods", len(res))
return res, nil
}
func (s *FoodService) GetYearly() ([]AggregatedFood, error) {
var res []AggregatedFood
func (s *FoodService) GetYearly() (res []AggregatedFood, err error) {
log.Printf("Getting yearly food")
if s.db == nil || !s.db.Ready {
return res, fmt.Errorf("cannot get yearly food, db is nil or is not ready")
}
row, err := s.db.readConn.Query(fmt.Sprintf("SELECT %s from foodYearly LIMIT %d", foodAggregatedColumns, Settings.FoodYearlyLookback))
if err != nil {
log.Printf("error getting yearly food: %v", err)
return res, err
return res, fmt.Errorf("error getting yearly food: %w", err)
}
for row.Next() {
var food AggregatedFood
err := row.Scan(&food.Period, &food.Amount, &food.AvgPer100, &food.Energy)
err = row.Scan(&food.Period, &food.Amount, &food.AvgPer100, &food.Energy)
if err != nil {
log.Printf("error scanning row: %v", err)
continue
@@ -251,5 +287,6 @@ func (s *FoodService) GetYearly() ([]AggregatedFood, error) {
res = append(res, food)
}
log.Printf("Got %d yearly foods", len(res))
return res, nil
}

View File

@@ -1,12 +1,15 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<meta content="width=device-width, initial-scale=1.0" name="viewport"/>
<meta charset="UTF-8" />
<meta content="width=device-width, initial-scale=1.0" name="viewport" />
<title>calorie-counter</title>
</head>
<body>
<div id="app"></div>
<script src="./src/main.ts" type="module"></script>
<div id="app"></div>
<script src="./src/main.ts" type="module"></script>
</body>
</html>

View File

@@ -10,6 +10,10 @@
"check": "svelte-check --tsconfig ./tsconfig.json"
},
"devDependencies": {
"@fortawesome/fontawesome-svg-core": "^6.5.2",
"@fortawesome/free-brands-svg-icons": "^6.5.2",
"@fortawesome/free-regular-svg-icons": "^6.5.2",
"@fortawesome/free-solid-svg-icons": "^6.5.2",
"@sveltejs/vite-plugin-svelte": "^1.0.1",
"@tsconfig/svelte": "^3.0.0",
"svelte": "^3.49.0",
@@ -21,15 +25,12 @@
"tailwindcss": "^3.4.3",
"tslib": "^2.4.0",
"typescript": "^4.6.4",
"vite": "^3.0.7",
"@fortawesome/fontawesome-svg-core": "^6.5.2",
"@fortawesome/free-brands-svg-icons": "^6.5.2",
"@fortawesome/free-regular-svg-icons": "^6.5.2",
"@fortawesome/free-solid-svg-icons": "^6.5.2"
"vite": "^3.0.7"
},
"dependencies": {
"autoprefixer": "^10.4.20",
"chart.js": "^4.4.3",
"regression": "^2.0.1",
"svelte-chartjs": "^3.1.5"
}
}

View File

@@ -1 +1 @@
bd9b801d4541f25052f0d4cb72b7d95a
23e2cc3e28f96f9d63ed35c0a34c1dcd

View File

@@ -14,6 +14,9 @@ importers:
chart.js:
specifier: ^4.4.3
version: 4.4.3
regression:
specifier: ^2.0.1
version: 2.0.1
svelte-chartjs:
specifier: ^3.1.5
version: 3.1.5(chart.js@4.4.3)(svelte@3.59.2)
@@ -714,6 +717,9 @@ packages:
resolution: {integrity: sha512-A1PeDEYMrkLrfyOwv2jwihXbo9qxdGD3atBYQA9JJgreAx8/7rC6IUkWOw2NQlOxLp2wL0ifQbh1HuidDfYA6w==}
engines: {node: '>=8'}
regression@2.0.1:
resolution: {integrity: sha512-A4XYsc37dsBaNOgEjkJKzfJlE394IMmUPlI/p3TTI9u3T+2a+eox5Pr/CPUqF0eszeWZJPAc6QkroAhuUpWDJQ==}
resolve-from@4.0.0:
resolution: {integrity: sha512-pb/MYmXstAkysRFx8piNI1tGFNQIFA3vkE3Gq4EuA1dF6gHp/+vgZqsCGJapvy8N3Q+4o7FwvquPJcnZ7RYy4g==}
engines: {node: '>=4'}
@@ -1512,6 +1518,8 @@ snapshots:
regexparam@2.0.2: {}
regression@2.0.1: {}
resolve-from@4.0.0: {}
resolve@1.22.8:

View File

@@ -1,9 +1,32 @@
<script lang="ts">
import Header from "$lib/components/Header.svelte";
import Router from "$lib/router/Router.svelte";
import { Toaster } from 'svelte-sonner'
import { Close } from "$wails/main/App";
import { Toaster } from "svelte-sonner";
import * as srouter from "svelte-spa-router";
import { location } from "svelte-spa-router";
const energyLocRegex = /^\/(?:Energy)?(?!Weight)/;
const weightLocRegex = /^\/(?:Weight)(?!Energy)/;
function keyDown(event: KeyboardEvent) {
if (event.ctrlKey && event.key == "r") {
window.location.reload();
}
if (event.ctrlKey && event.key == "w") {
Close();
}
if (event.ctrlKey && event.key == "Tab") {
if (energyLocRegex.test($location)) {
srouter.replace($location.replace(energyLocRegex, "/Weight"));
} else if (weightLocRegex.test($location)) {
srouter.replace($location.replace(weightLocRegex, "/Energy"));
}
}
}
</script>
<svelte:window on:keydown={keyDown} />
<Toaster />
<template>
<Header />

View File

@@ -1,13 +1,15 @@
<script lang="ts">
import { main } from "$wails/models";
export let item: main.AggregatedFood
export let item: main.AggregatedFood;
</script>
<template>
<tr class="border-b border-gray-200 dark:border-gray-700">
<th class="px-6 py-4 font-medium text-gray-900 whitespace-nowrap bg-gray-50 dark:text-white dark:bg-gray-800"
scope="row">
<th
class="px-6 py-4 font-medium text-gray-900 whitespace-nowrap bg-gray-50 dark:text-white dark:bg-gray-800"
scope="row"
>
{item.period}
</th>
<td class="px-6 py-4">

View File

@@ -3,6 +3,7 @@
import AggregatedFoodComp from "$lib/components/Energy/Aggregated/AggregatedFoodComp.svelte";
import { main } from "$wails/models";
import type { ChartData, Point } from "chart.js";
import regression from "regression";
import {
CategoryScale,
Chart as ChartJS,
@@ -13,6 +14,7 @@
Title,
Tooltip,
} from "chart.js";
import { CalculateR2 } from "$lib/utils";
ChartJS.register(Title, Tooltip, Legend, LineElement, LinearScale, PointElement, CategoryScale);
@@ -22,17 +24,18 @@
let reversedItems = items.slice().reverse();
const defaultOptions = {
backgroundColor: "rgba(225, 204,230, .3)",
borderColor: "rgb(205, 130, 158)",
pointBorderColor: "rgb(205, 130, 158)",
backgroundColor: "rgba(59, 130, 246, 0.1)",
borderColor: "rgb(59, 130, 246)",
pointBorderColor: "rgb(59, 130, 246)",
pointBackgroundColor: "rgb(255, 255, 255)",
pointBorderWidth: 10,
pointHoverRadius: 5,
pointHoverBackgroundColor: "rgb(0, 157, 123)",
pointHoverBorderColor: "rgba(220, 220, 220, 1)",
pointBorderWidth: 2,
pointHoverRadius: 6,
pointHoverBackgroundColor: "rgb(59, 130, 246)",
pointHoverBorderColor: "rgb(255, 255, 255)",
pointHoverBorderWidth: 2,
pointRadius: 1,
pointHitRadius: 10,
pointRadius: 3,
pointHitRadius: 20,
tension: 0.4,
};
const data: ChartData<"line", (number | Point)[]> = {
labels: reversedItems.map((f) => f.period),
@@ -74,28 +77,83 @@
},
],
};
data.datasets.push({
...defaultOptions,
label: "R2",
data: CalculateR2(reversedItems.map((f, i) => [i, f.energy])),
borderColor: "#04d1d1",
pointBorderColor: "#04d1d1",
pointRadius: 0,
});
</script>
<template>
<Line {data} class="max-h-[50vh] h-[50vh]" />
<div class="relative flex flex-col h-[43vh]" data-vaul-drawer-wrapper id="page">
<div class="relative overflow-x-auto shadow-md sm:rounded-lg">
<table class="w-full text-sm text-left rtl:text-right text-gray-500 dark:text-gray-400">
<thead class="text-xs text-gray-700 uppercase dark:text-gray-400">
<tr>
<th class="px-6 py-3 bg-gray-50 dark:bg-gray-800 w-2/12" scope="col"> Period </th>
<th class="px-6 py-3" scope="col"> Amount </th>
<th class="px-6 py-3 bg-gray-50 dark:bg-gray-800" scope="col"> AvgPer100 </th>
<th class="px-6 py-3" scope="col"> Energy </th>
</tr>
</thead>
<tbody>
{#each items as f}
<AggregatedFoodComp item={f} />
{/each}
</tbody>
</table>
<div class="flex flex-col gap-6 p-6 bg-gray-900 min-h-screen">
<div class="bg-gray-800 rounded-lg p-6 shadow-lg border border-gray-700">
<Line
{data}
options={{
responsive: true,
maintainAspectRatio: false,
plugins: {
legend: {
position: "top",
labels: {
padding: 20,
color: "rgb(229, 231, 235)",
font: {
size: 12,
family: "'Nunito', sans-serif",
},
},
},
},
scales: {
y: {
grid: {
color: "rgba(75, 85, 99, 0.2)",
},
ticks: {
color: "rgb(229, 231, 235)",
font: {
family: "'Nunito', sans-serif",
},
},
},
x: {
grid: {
color: "rgba(75, 85, 99, 0.2)",
},
ticks: {
color: "rgb(229, 231, 235)",
font: {
family: "'Nunito', sans-serif",
},
},
},
},
}}
class="max-h-[50vh] h-[50vh]"
/>
</div>
<div class="relative flex flex-col h-[43vh]" data-vaul-drawer-wrapper id="page">
<div class="relative overflow-x-auto shadow-md rounded-lg">
<table class="w-full text-sm text-left text-gray-400">
<thead class="text-xs uppercase bg-gray-800 text-gray-200 sticky top-0">
<tr>
<th class="px-6 py-4 font-semibold" scope="col">Period</th>
<th class="px-6 py-4 font-semibold" scope="col">Amount</th>
<th class="px-6 py-4 font-semibold" scope="col">AvgPer100</th>
<th class="px-6 py-4 font-semibold" scope="col">Energy</th>
</tr>
</thead>
<tbody class="divide-y divide-gray-700">
{#each items as f}
<AggregatedFoodComp item={f} />
{/each}
</tbody>
</table>
</div>
</div>
<div></div>
</div>
</template>

View File

@@ -3,12 +3,13 @@
import { main } from "$wails/models";
import { CreateFood, GetLastPer100 } from "$wails/main/App";
import { foodStore } from "$lib/store/Energy/foodStore";
import FoodSearchEntry from "./FoodSearchEntry.svelte";
let item: main.Food = {
food: "",
amount: 0,
description: "",
rowid: 0,
id: 0,
date: "",
per100: 0,
energy: 0,
@@ -19,6 +20,53 @@
let per100: string = "";
let per100Edited: boolean = false;
let per100Element: HTMLTableCellElement;
let nameElement: HTMLTableCellElement;
let autocompleteList: HTMLUListElement;
let foodSearch: main.Food[] = [];
let hiLiteIndex: number = -1;
$: {
name = name.trim();
if (!name) {
foodSearch = [];
} else {
updateAutocomplete();
}
}
function updateAutocomplete() {
if (!per100Edited)
GetLastPer100(name.trim()).then((res) => {
if (res.success && res.data && name) {
foodSearch = res.data;
hiLiteIndex = -1;
} else {
foodSearch = [];
}
});
}
async function handleSubmit() {
item.food = name;
item.description = description;
item.amount = parseInt(amount);
item.per100 = parseInt(per100);
const res = await CreateFood(item);
name = "";
amount = "";
per100 = "";
per100Edited = false;
if (!res.success) {
toast.error(`failed to create item with error ${res.error}`);
return;
}
foodStore.update((value) => [res.data, ...value]);
nameElement.focus();
foodSearch = [];
}
async function update(event: KeyboardEvent & { currentTarget: EventTarget & HTMLTableCellElement }) {
name = name.trim();
@@ -26,64 +74,103 @@
description = description.trim();
per100 = per100.trim();
if (!per100Edited && event.currentTarget === per100Element) per100Edited = true;
if (!name) {
foodSearch = [];
return;
}
if (event.key == "Enter") {
if (!per100Edited && event.currentTarget == per100Element) per100Edited = true;
if (event.key === "Enter") {
event.preventDefault();
item.food = name;
item.description = description;
item.amount = parseInt(amount);
item.per100 = parseInt(per100);
const res = await CreateFood(item);
name = "";
amount = "";
// description = ''
per100 = "";
per100Edited = false;
if (!res.success) {
toast.error(`failed to create item with error ${res.error}`);
// If suggestions are visible and we have a highlighted item or at least one suggestion
if (foodSearch.length > 0) {
const selectedFood = hiLiteIndex >= 0 ? foodSearch[hiLiteIndex] : foodSearch[0];
setInputVal(selectedFood);
return;
}
foodStore.update((value) => [res.data, ...value]);
// Only submit if we have no suggestions visible
if (name && amount && per100) {
await handleSubmit();
}
}
}
if (!per100Edited)
GetLastPer100(name.trim()).then((res) => {
if (res.success) {
per100 = res.data.toString();
}
});
function navigateList(e: KeyboardEvent) {
if (!foodSearch.length) return;
switch (e.key) {
case "ArrowDown":
e.preventDefault();
hiLiteIndex = Math.min(hiLiteIndex + 1, foodSearch.length - 1);
break;
case "ArrowUp":
e.preventDefault();
hiLiteIndex = Math.max(hiLiteIndex - 1, -1);
break;
case "Escape":
e.preventDefault();
foodSearch = [];
hiLiteIndex = -1;
break;
}
}
function setInputVal(food: main.Food) {
name = food.food;
per100 = String(food.per100);
amount = String(food.amount);
hiLiteIndex = -1;
foodSearch = [];
}
$: {
if (nameElement && autocompleteList) {
const { top, left, height } = nameElement.getBoundingClientRect();
autocompleteList.style.top = `${top + height}px`;
autocompleteList.style.left = `${left}px`;
}
}
let timeout: number;
function handleFocusOut() {
timeout = setTimeout(() => {
foodSearch = [];
hiLiteIndex = -1;
}, 100);
}
function handleFocusIn() {
clearTimeout(timeout);
updateAutocomplete();
}
</script>
<svelte:window on:keydown={navigateList} />
<template>
<tr class="border-b border-gray-200 dark:border-gray-700 text-lg font-bold">
<th
class="px-6 py-4 font-medium text-gray-900 whitespace-nowrap bg-gray-50 dark:text-white dark:bg-gray-800"
scope="row"
>
</th>
<!-- svelte-ignore a11y-autofocus -->
<tr class="border-b border-gray-700 text-lg font-medium hover:bg-gray-800/50 transition-colors">
<th class="px-6 py-4 font-medium text-gray-200 whitespace-nowrap" scope="row" />
<td
bind:innerText={name}
class:border-[3px]={!name}
class:border-red-600={!name}
class="px-6 py-4 overflow-hidden"
class="px-6 py-4 overflow-hidden focus:outline-none focus:ring-2 focus:ring-blue-500 rounded transition-all"
class:ring-2={!name}
class:ring-red-500={!name}
contenteditable="true"
autofocus
on:keydown={update}
>
</td>
on:focusin={handleFocusIn}
on:focusout={handleFocusOut}
bind:this={nameElement}
/>
<td
bind:innerText={description}
class="px-6 py-4 bg-gray-50 dark:bg-gray-800 overflow-hidden"
contenteditable="true"
on:keydown={update}
>
</td>
/>
<td
bind:innerText={amount}
class:border-[3px]={!amount}
@@ -91,8 +178,7 @@
class="px-6 py-4 overflow-hidden"
contenteditable="true"
on:keydown={update}
>
</td>
/>
<td
bind:this={per100Element}
bind:innerText={per100}
@@ -101,7 +187,13 @@
class:border-orange-600={!per100}
contenteditable="true"
on:keydown={update}
>
</td>
/>
</tr>
{#if foodSearch.length > 0}
<ul bind:this={autocompleteList} class="z-50 fixed top-0 left-0 w-3/12 border border-x-gray-800">
{#each foodSearch as f, i}
<FoodSearchEntry itemLabel={f.food} highlighted={i === hiLiteIndex} on:click={() => setInputVal(f)} />
{/each}
</ul>
{/if}
</template>

View File

@@ -34,16 +34,18 @@
remainingToday = $settingsStore.target;
let now = new Date();
let todayDate = formatter.format(now);
const [day, month, year] = todayDate.split('/');
const [day, month, year] = todayDate.split("/");
todayDate = `${year}-${month}-${day}`;
$foodStore.forEach((food) => {
if (food.date.split("T")[0] == todayDate) {
remainingToday -= food.energy;
} else {
return;
}
});
if ($foodStore) {
$foodStore.forEach((food) => {
if (food.date.split("T")[0] == todayDate) {
remainingToday -= food.energy;
} else {
return;
}
});
}
remainingToday = Math.round(remainingToday);
computeColor();
}
@@ -55,5 +57,11 @@
</script>
<template>
<div class="px-4 text-bold text-3xl" style="color: {color}">{remainingToday}</div>
<div
class="px-6 py-3 text-3xl font-bold rounded-lg bg-gray-800/50 shadow-lg border border-gray-700"
style="color: {color}"
>
<span class="mr-2">{remainingToday}</span>
<span class="text-lg text-gray-400">kcal remaining</span>
</div>
</template>

View File

@@ -1,84 +1,105 @@
<script lang="ts">
import { UpdateFood } from '$wails/main/App';
import {main} from '$wails/models'
import { toast } from 'svelte-sonner'
import { UpdateFood } from "$wails/main/App";
import { main } from "$wails/models";
import { toast } from "svelte-sonner";
export let item: main.Food
export let energyColor: string
export let nameColor: string
export let dateColor: string
export let item: main.Food;
export let energyColor: string;
export let nameColor: string;
export let dateColor: string;
let amount: string = item.amount.toString()
let per100: string = item.per100?.toString() ?? ''
let description: string = item.description ?? ''
let name: string = item.food
let date = new Date(item.date);
let dateString = new Intl.DateTimeFormat("en-CA", {
year: "numeric",
month: "2-digit",
day: "2-digit",
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
hour12: false,
}).format(date);
async function update(event: KeyboardEvent & { currentTarget: (EventTarget & HTMLTableCellElement) }) {
if (event.key == 'Enter') {
event.preventDefault()
await updateItem()
let amount: string = item.amount.toString();
let per100: string = item.per100?.toString() ?? "";
let description: string = item.description ?? "";
let name: string = item.food;
async function update(event: KeyboardEvent & { currentTarget: EventTarget & HTMLTableCellElement }) {
if (event.key == "Enter") {
event.preventDefault();
await updateItem();
}
}
async function focusOutUpdate() {
await updateItem()
await updateItem();
}
async function updateItem() {
amount = amount.trim()
per100 = per100.trim()
description = description.trim()
name = name.trim()
amount = amount.trim();
per100 = per100.trim();
description = description.trim();
name = name.trim();
item.food = name
item.description = description
item.amount = parseInt(amount)
item.per100 = parseInt(per100)
item.food = name;
item.description = description;
item.amount = parseInt(amount);
item.per100 = parseInt(per100);
const res = await UpdateFood(item)
const res = await UpdateFood(item);
if (!res.success) {
toast.error(`failed to update food item with error ${res.error}`)
toast.error(`failed to update food item with error ${res.error}`);
} else {
item = res.data
item = res.data;
}
name = item.food
description = item.description ?? ''
amount = item.amount.toString()
per100 = item.per100?.toString() ?? ''
name = item.food;
description = item.description ?? "";
amount = item.amount.toString();
per100 = item.per100?.toString() ?? "";
}
</script>
<template>
<tr class="border-b border-gray-200 dark:border-gray-700 font-bold text-lg">
<th class="px-6 py-4 font-medium text-gray-900 whitespace-nowrap bg-gray-50 dark:text-white dark:bg-gray-800"
style="color: {dateColor}"
scope="row">
{item.date}
<th
class="px-6 py-4 font-medium text-gray-900 whitespace-nowrap bg-gray-50 dark:text-white dark:bg-gray-800"
style="color: {dateColor}"
scope="row"
>
{dateString}
</th>
<td class="px-6 py-4"
style="color: {nameColor}"
contenteditable="true"
bind:innerText={name}
on:focusout={focusOutUpdate}
on:keydown={update}>
<td
class="px-6 py-4"
style="color: {nameColor}"
contenteditable="true"
bind:innerText={name}
on:focusout={focusOutUpdate}
on:keydown={update}
>
</td>
<td class="px-6 py-4 bg-gray-50 dark:bg-gray-800"
contenteditable="true"
bind:innerText={description}
on:focusout={focusOutUpdate}
on:keydown={update}>
<td
class="px-6 py-4 bg-gray-50 dark:bg-gray-800"
contenteditable="true"
bind:innerText={description}
on:focusout={focusOutUpdate}
on:keydown={update}
>
</td>
<td class="px-6 py-4"
contenteditable="true"
bind:innerText={amount}
on:focusout={focusOutUpdate}
on:keydown={update}>
<td
class="px-6 py-4"
contenteditable="true"
bind:innerText={amount}
on:focusout={focusOutUpdate}
on:keydown={update}
>
</td>
<td class="px-6 py-4 bg-gray-50 dark:bg-gray-800"
contenteditable="true"
bind:innerText={per100}
on:focusout={focusOutUpdate}
on:keydown={update}>
<td
class="px-6 py-4 bg-gray-50 dark:bg-gray-800"
contenteditable="true"
bind:innerText={per100}
on:focusout={focusOutUpdate}
on:keydown={update}
>
</td>
<td class="px-6 py-4" style="color: {energyColor}">
{item.energy}

View File

@@ -0,0 +1,13 @@
<script lang="ts">
export let itemLabel: string;
export let highlighted: boolean;
</script>
<li
class="list-none px-4 py-3 cursor-pointer bg-gray-800 text-gray-200 text-base border-b border-gray-700 transition-colors"
class:bg-blue-600={highlighted}
class:text-white={highlighted}
on:click
>
{itemLabel}
</li>

View File

@@ -1,10 +1,11 @@
<script lang="ts">
import { GenerateColor } from "$lib/utils";
import { GenerateColor, RemoveExistingColors } from "$lib/utils";
import { main } from "$wails/models";
import EmptyFoodComp from "./EmptyFoodComp.svelte";
import FoodComp from "./FoodComp.svelte";
export let items: main.Food[] = [];
RemoveExistingColors();
let minCal = 1e5;
let maxCal = 0;
@@ -58,20 +59,20 @@
</script>
<template>
<div class="relative flex flex-col flex-grow h-[93vh] select-none" data-vaul-drawer-wrapper id="page">
<div class="relative overflow-auto h-full shadow-md sm:rounded-lg">
<table class="w-full text-sm text-left rtl:text-right text-gray-500 dark:text-gray-400">
<thead class="text-xs text-gray-700 uppercase dark:text-gray-400">
<div class="relative flex flex-col flex-grow h-[93vh] select-none bg-gray-900" data-vaul-drawer-wrapper id="page">
<div class="relative overflow-auto h-full shadow-md rounded-lg">
<table class="w-full text-sm text-left text-gray-400">
<thead class="text-xs uppercase bg-gray-800 text-gray-200 sticky top-0">
<tr>
<th class="px-6 py-3 bg-gray-50 dark:bg-gray-800 w-2/12" scope="col"> Date </th>
<th class="px-6 py-3 w-3/12" scope="col"> Food </th>
<th class="px-6 py-3 bg-gray-50 dark:bg-gray-800 w-4/12" scope="col"> Description </th>
<th class="px-6 py-3 w-1/12" scope="col"> Amount </th>
<th class="px-6 py-3 bg-gray-50 dark:bg-gray-800 w-1/12" scope="col"> Cal Per 100 </th>
<th class="px-6 py-3 w-1/12" scope="col"> Energy </th>
<th class="px-6 py-4 font-semibold w-2/12" scope="col">Date</th>
<th class="px-6 py-4 font-semibold" scope="col">Food</th>
<th class="px-6 py-4 font-semibold" scope="col">Description</th>
<th class="px-6 py-4 font-semibold" scope="col">Amount</th>
<th class="px-6 py-4 font-semibold" scope="col">Cal Per 100</th>
<th class="px-6 py-4 font-semibold" scope="col">Energy</th>
</tr>
</thead>
<tbody>
<tbody class="divide-y divide-gray-700">
<EmptyFoodComp />
{#each items as f}
<FoodComp
@@ -84,6 +85,5 @@
</tbody>
</table>
</div>
<div></div>
</div>
</template>

View File

@@ -4,6 +4,7 @@
import Fa from "svelte-fa";
import Settings from "./Settings/Settings.svelte";
import EnergyToday from "./Energy/EnergyToday.svelte";
import RefreshComponent from "./RefreshComponent.svelte";
Fa;
type Link = {
@@ -48,47 +49,40 @@
</script>
<header
class="flex h-22 items-center justify-center bg-base-100 shadow-lg sticky top-0 z-50 border-b
border-border/40 bg-background/95 backdrop-blur supports-[backdrop-filter]:bg-background/60"
class="flex h-24 items-center justify-between bg-gray-900 shadow-lg sticky top-0 z-50 border-b border-gray-700/40 backdrop-blur supports-[backdrop-filter]:bg-gray-900/60 px-6"
>
<div>
<nav class="flex space-x-4 text-2xl font-bold select-none justify-center">
<div class="flex flex-col gap-2">
<nav class="flex space-x-6 text-2xl font-bold select-none">
<a
use:link
class={"/" == $location
? "transition-colors hover:text-foreground/80"
: "transition-colors hover:text-foreground/80 text-foreground/60"}
class="transition-colors hover:text-gray-200 {$location === '/' ? 'text-white' : 'text-gray-400'}"
href="/"
on:click={(e) => (selected = "Energy")}>Energy</a
on:click={() => (selected = "Energy")}>Energy</a
>
<a
use:link
class={"/Weight" == $location
? "transition-colors hover:text-foreground/80"
: "transition-colors hover:text-foreground/80 text-foreground/60"}
class="transition-colors hover:text-gray-200 {$location === '/Weight' ? 'text-white' : 'text-gray-400'}"
href="/Weight"
on:click={(e) => (selected = "Weight")}>Weight</a
on:click={() => (selected = "Weight")}>Weight</a
>
</nav>
<nav class="flex space-x-4 text-2xl font-bold select-none justify-center">
<nav class="flex space-x-6 text-xl font-medium select-none">
{#each sublinks as { label, href }}
<a
use:link
{href}
class={href == $location
? "transition-colors hover:text-foreground/80"
: "transition-colors hover:text-foreground/80 text-foreground/60"}>{label}</a
class="transition-colors hover:text-gray-200 {href === $location ? 'text-white' : 'text-gray-400'}"
>{label}</a
>
{/each}
</nav>
</div>
<!-- svelte-ignore a11y-click-events-have-key-events -->
<div class="absolute right-0 pt-4 pb-4 pr-8 pl-8 cursor-pointer" on:click={() => (showModal = true)}>
<button>
<div class="flex items-center gap-6">
<EnergyToday />
<button class="p-3 hover:bg-gray-800 rounded-lg transition-colors" on:click={() => (showModal = true)}>
<Fa icon={faGear} scale={2} />
</button>
</div>
<EnergyToday />
</header>
<Settings bind:showModal />

View File

@@ -0,0 +1,42 @@
<script lang="ts">
import { dailyFoodStore } from "$lib/store/Energy/dailyFoodStore";
import { monthlyFoodStore } from "$lib/store/Energy/monthlyFoodStore";
import { weeklyFoodStore } from "$lib/store/Energy/weeklyFoodStore";
import { yearlyFoodStore } from "$lib/store/Energy/yearlyFoodStore";
import { dailyWeightStore } from "$lib/store/Weight/dailyWeightStore";
import { monthlyWeightStore } from "$lib/store/Weight/monthlyWeightStore";
import { weeklyWeightStore } from "$lib/store/Weight/weeklyWeightStore";
import { yearlyWeightStore } from "$lib/store/Weight/yearlyWeightStore";
import { faRefresh } from "@fortawesome/free-solid-svg-icons";
import Fa from "svelte-fa";
Fa;
// YES refresh DOES exist FFS
function refreshStores() {
// @ts-ignore
dailyFoodStore.refresh();
// @ts-ignore
weeklyFoodStore.refresh();
// @ts-ignore
monthlyFoodStore.refresh();
// @ts-ignore
yearlyFoodStore.refresh();
// @ts-ignore
dailyWeightStore.refresh();
// @ts-ignore
weeklyWeightStore.refresh();
// @ts-ignore
monthlyWeightStore.refresh();
// @ts-ignore
yearlyWeightStore.refresh();
}
</script>
<template>
<!-- svelte-ignore a11y-click-events-have-key-events -->
<div class="absolute right-20 pt-4 pb-4 pr-8 pl-8 cursor-pointer" on:click={refreshStores}>
<button class="p-3 hover:bg-gray-800 rounded-lg transition-colors">
<Fa icon={faRefresh} scale={2} />
</button>
</div>
</template>

View File

@@ -18,7 +18,7 @@
if (!res.success) {
toast.error(`Failed to set setting with error ${res.error}`);
editSetting = String(setting);
return
return;
}
setting = numSetting;
settingsStore.update((store) => {

View File

@@ -11,7 +11,7 @@
<div class="flex flex-2 flex-col gap-4 text-left text-xl">
{#each Object.keys($settingsStore) as key}
<Setting key={key} setting={$settingsStore[key]} />
<Setting {key} setting={$settingsStore[key]} />
{/each}
</div>
</Modal>

View File

@@ -1,13 +1,15 @@
<script lang="ts">
import {main} from '$wails/models'
import { main } from "$wails/models";
export let item: main.AggregatedWeight
export let item: main.AggregatedWeight;
</script>
<template>
<tr class="border-b border-gray-200 dark:border-gray-700">
<th class="px-6 py-4 font-medium text-gray-900 whitespace-nowrap bg-gray-50 dark:text-white dark:bg-gray-800"
scope="row">
<th
class="px-6 py-4 font-medium text-gray-900 whitespace-nowrap bg-gray-50 dark:text-white dark:bg-gray-800"
scope="row"
>
{item.period}
</th>
<td class="px-6 py-4">

View File

@@ -13,6 +13,7 @@
Title,
Tooltip,
} from "chart.js";
import { CalculateR2 } from "$lib/utils";
ChartJS.register(Title, Tooltip, Legend, LineElement, LinearScale, PointElement, CategoryScale);
@@ -20,17 +21,18 @@
let reversedItems = items.slice().reverse();
const defaultOptions = {
backgroundColor: "rgba(225, 204,230, .3)",
borderColor: "rgb(205, 130, 158)",
pointBorderColor: "rgb(205, 130, 158)",
backgroundColor: "rgba(59, 130, 246, 0.1)",
borderColor: "rgb(59, 130, 246)",
pointBorderColor: "rgb(59, 130, 246)",
pointBackgroundColor: "rgb(255, 255, 255)",
pointBorderWidth: 10,
pointHoverRadius: 5,
pointHoverBackgroundColor: "rgb(0, 157, 123)",
pointHoverBorderColor: "rgba(220, 220, 220, 1)",
pointBorderWidth: 2,
pointHoverRadius: 6,
pointHoverBackgroundColor: "rgb(59, 130, 246)",
pointHoverBorderColor: "rgb(255, 255, 255)",
pointHoverBorderWidth: 2,
pointRadius: 1,
pointHitRadius: 10,
pointRadius: 3,
pointHitRadius: 20,
tension: 0.4,
};
const data: ChartData<"line", (number | Point)[]> = {
labels: reversedItems.map((f) => f.period),
@@ -44,26 +46,81 @@
},
],
};
data.datasets.push({
...defaultOptions,
label: "R2",
data: CalculateR2(reversedItems.map((f, i) => [i, f.amount])),
borderColor: "#04d1d1",
pointBorderColor: "#04d1d1",
pointRadius: 0,
});
</script>
<template>
<Line {data} class="max-h-[50vh] h-[50vh]" />
<div class="relative flex flex-col h-[43vh]" data-vaul-drawer-wrapper id="page">
<div class="relative overflow-x-auto shadow-md sm:rounded-lg">
<table class="w-full text-sm text-left rtl:text-right text-gray-500 dark:text-gray-400">
<thead class="text-xs text-gray-700 uppercase dark:text-gray-400">
<tr>
<th class="px-6 py-3 bg-gray-50 dark:bg-gray-800 w-2/12" scope="col"> Period </th>
<th class="px-6 py-3" scope="col"> Amount </th>
</tr>
</thead>
<tbody>
{#each items as f}
<AggregatedWeightComp item={f} />
{/each}
</tbody>
</table>
<div class="flex flex-col gap-6 p-6 bg-gray-900 min-h-screen">
<div class="bg-gray-800 rounded-lg p-6 shadow-lg border border-gray-700">
<Line
{data}
options={{
responsive: true,
maintainAspectRatio: false,
plugins: {
legend: {
position: "top",
labels: {
padding: 20,
color: "rgb(229, 231, 235)",
font: {
size: 12,
family: "'Nunito', sans-serif",
},
},
},
},
scales: {
y: {
grid: {
color: "rgba(75, 85, 99, 0.2)",
},
ticks: {
color: "rgb(229, 231, 235)",
font: {
family: "'Nunito', sans-serif",
},
},
},
x: {
grid: {
color: "rgba(75, 85, 99, 0.2)",
},
ticks: {
color: "rgb(229, 231, 235)",
font: {
family: "'Nunito', sans-serif",
},
},
},
},
}}
class="max-h-[50vh] h-[50vh]"
/>
</div>
<div class="relative flex flex-col h-[43vh]" data-vaul-drawer-wrapper id="page">
<div class="relative overflow-x-auto shadow-md rounded-lg">
<table class="w-full text-sm text-left text-gray-400">
<thead class="text-xs uppercase bg-gray-800 text-gray-200 sticky top-0">
<tr>
<th class="px-6 py-4 font-semibold" scope="col">Period</th>
<th class="px-6 py-4 font-semibold" scope="col">Amount</th>
</tr>
</thead>
<tbody>
{#each items as f}
<AggregatedWeightComp item={f} />
{/each}
</tbody>
</table>
</div>
</div>
<div></div>
</div>
</template>

View File

@@ -1,34 +1,34 @@
<script lang="ts">
import { toast } from 'svelte-sonner'
import { toast } from "svelte-sonner";
import { main } from "$wails/models";
import { CreateWeight } from '$wails/main/App';
import { weightStore } from '$lib/store/Weight/weightStore';
import { CreateWeight } from "$wails/main/App";
import { weightStore } from "$lib/store/Weight/weightStore";
let item: main.Weight = {
weight: 0,
rowid: 0,
date: ''
}
let weight: string | null = null
id: 0,
date: "",
};
let weight: string | null = null;
async function update(event: KeyboardEvent & { currentTarget: (EventTarget & HTMLTableCellElement) }) {
if (!weight) return
weight = weight.trim()
async function update(event: KeyboardEvent & { currentTarget: EventTarget & HTMLTableCellElement }) {
if (!weight) return;
weight = weight.trim();
if (event.key == 'Enter') {
event.preventDefault()
item.weight = parseFloat(weight)
if (event.key == "Enter") {
event.preventDefault();
item.weight = parseFloat(weight);
if (isNaN(item.weight)) {
toast.error('Weight must be a number')
return
toast.error("Weight must be a number");
return;
}
const res = await CreateWeight(item)
weight = ''
const res = await CreateWeight(item);
weight = "";
if (!res.success) {
toast.error(`failed to create weight with error ${res.error}`)
return
toast.error(`failed to create weight with error ${res.error}`);
return;
}
console.log("update");
@@ -39,16 +39,20 @@
<template>
<tr class="border-b border-gray-200 dark:border-gray-700 text-lg font-bold">
<th class="px-6 py-4 font-medium text-gray-900 whitespace-nowrap bg-gray-50 dark:text-white dark:bg-gray-800"
scope="row">
<th
class="px-6 py-4 font-medium text-gray-900 whitespace-nowrap bg-gray-50 dark:text-white dark:bg-gray-800"
scope="row"
>
</th>
<td bind:innerText={weight}
class:border-[3px]={!weight}
class:border-red-600={!weight}
class="px-6 py-4 overflow-hidden"
contenteditable="true"
autofocus
on:keydown={update}>
<td
bind:innerText={weight}
class:border-[3px]={!weight}
class:border-red-600={!weight}
class="px-6 py-4 overflow-hidden"
contenteditable="true"
autofocus
on:keydown={update}
>
</td>
</tr>
</template>

View File

@@ -1,16 +1,29 @@
<script lang="ts">
import { main } from "$wails/models";
export let item: main.Weight
export let dateColor: string
export let item: main.Weight;
export let dateColor: string;
let date = new Date(item.date);
let dateString = new Intl.DateTimeFormat("en-CA", {
year: "numeric",
month: "2-digit",
day: "2-digit",
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
hour12: false,
}).format(date);
</script>
<template>
<tr class="border-b border-gray-200 dark:border-gray-700 font-bold text-lg">
<th class="px-6 py-4 font-medium text-gray-900 whitespace-nowrap bg-gray-50 dark:text-white dark:bg-gray-800"
style="color: {dateColor}"
scope="row">
{item.date}
<th
class="px-6 py-4 font-medium text-gray-900 whitespace-nowrap bg-gray-50 dark:text-white dark:bg-gray-800"
style="color: {dateColor}"
scope="row"
>
{dateString}
</th>
<td class="px-6 py-4">
{item.weight}

View File

@@ -1,10 +1,11 @@
<script lang="ts">
import { GenerateColor } from "$lib/utils";
import { GenerateColor, RemoveExistingColors } from "$lib/utils";
import EmptyWeightComp from "$components/Weight/EmptyWeightComp.svelte";
import WeightComp from "$components/Weight/WeightComp.svelte";
import { main } from "$wails/models";
export let items: main.Weight[] = [];
RemoveExistingColors();
const dateColors: Map<string, string> = new Map<string, string>();
@@ -14,25 +15,22 @@
const date = item.date.toString().split("T")[0];
if (!date) return GenerateColor();
if (!dateColors.has(date)) dateColors.set(date, GenerateColor());
// THERE'S NOTHING UNDEFINED HERE
// WE GOT RID OF UNDEFINED ON LINE 33
// ASSHOLE
// @ts-ignore
return dateColors.get(date);
}
</script>
<template>
<div class="relative flex flex-col flex-grow h-[93vh]" data-vaul-drawer-wrapper id="page">
<div class="relative overflow-auto h-full shadow-md sm:rounded-lg">
<table class="w-full text-sm text-left rtl:text-right text-gray-500 dark:text-gray-400">
<thead class="text-xs text-gray-700 uppercase dark:text-gray-400">
<div class="relative flex flex-col flex-grow h-[93vh] select-none bg-gray-900" data-vaul-drawer-wrapper id="page">
<div class="relative overflow-auto h-full shadow-md rounded-lg">
<table class="w-full text-sm text-left text-gray-400">
<thead class="text-xs uppercase bg-gray-800 text-gray-200 sticky top-0">
<tr>
<th class="px-6 py-3 bg-gray-50 dark:bg-gray-800 w-2/12" scope="col"> Date </th>
<th class="px-6 py-3 w-10/12" scope="col"> Weight </th>
<th class="px-6 py-4 font-semibold w-2/12" scope="col">Date</th>
<th class="px-6 py-4 font-semibold" scope="col">Weight</th>
</tr>
</thead>
<tbody>
<tbody class="divide-y divide-gray-700">
<EmptyWeightComp />
{#each items as f}
<WeightComp item={f} dateColor={getDateColor(f)} />
@@ -40,6 +38,5 @@
</tbody>
</table>
</div>
<div></div>
</div>
</template>

View File

@@ -1,28 +1,29 @@
<script lang="ts">
import Router from 'svelte-spa-router'
import Energy from './routes/Energy/Energy.svelte'
import Daily from './routes/Energy/Daily.svelte'
import Weekly from './routes/Energy/Weekly.svelte'
import Monthly from './routes/Energy/Monthly.svelte'
import Yearly from './routes/Energy/Yearly.svelte'
import Weight from './routes/Weight/Weight.svelte'
import WDaily from './routes/Weight/WDaily.svelte'
import WWeekly from './routes/Weight/WWeekly.svelte'
import WMonthly from './routes/Weight/WMonthly.svelte'
import WYearly from './routes/Weight/WYearly.svelte'
import Router from "svelte-spa-router";
import Energy from "./routes/Energy/Energy.svelte";
import Daily from "./routes/Energy/Daily.svelte";
import Weekly from "./routes/Energy/Weekly.svelte";
import Monthly from "./routes/Energy/Monthly.svelte";
import Yearly from "./routes/Energy/Yearly.svelte";
import Weight from "./routes/Weight/Weight.svelte";
import WDaily from "./routes/Weight/WDaily.svelte";
import WWeekly from "./routes/Weight/WWeekly.svelte";
import WMonthly from "./routes/Weight/WMonthly.svelte";
import WYearly from "./routes/Weight/WYearly.svelte";
const routes = {
'/': Energy,
'/Energy/daily': Daily,
'/Energy/weekly': Weekly,
'/Energy/monthly': Monthly,
'/Energy/yearly': Yearly,
'/Weight': Weight,
'/Weight/daily': WDaily,
'/Weight/weekly': WWeekly,
'/Weight/monthly': WMonthly,
'/Weight/yearly': WYearly
}
"/": Energy,
"/Energy": Energy,
"/Energy/daily": Daily,
"/Energy/weekly": Weekly,
"/Energy/monthly": Monthly,
"/Energy/yearly": Yearly,
"/Weight": Weight,
"/Weight/daily": WDaily,
"/Weight/weekly": WWeekly,
"/Weight/monthly": WMonthly,
"/Weight/yearly": WYearly,
};
</script>
<Router {routes} />

View File

@@ -1,5 +1,7 @@
<script lang="ts">
import FoodTable from "$lib/components/Energy/FoodTable.svelte";
import * as srouter from "svelte-spa-router";
import { location } from "svelte-spa-router";
import { foodStore } from "$lib/store/Energy/foodStore";
// Fuck this hacky shit
@@ -9,16 +11,16 @@
// Not when pushing unshifting the store
// This is the only thing that works
// Hacky ass shit
let forceUpdate = false;
foodStore.subscribe(() => {
forceUpdate = !forceUpdate;
});
let forceUpdate = false;
foodStore.subscribe(() => {
forceUpdate = !forceUpdate;
});
</script>
<template>
{#if forceUpdate}
<FoodTable items={$foodStore} />
{:else}
<FoodTable items={$foodStore} />
{/if}
{#if forceUpdate}
<FoodTable items={$foodStore} />
{:else}
<FoodTable items={$foodStore} />
{/if}
</template>

View File

@@ -1,8 +1,8 @@
<script lang="ts">
import AggregatedWeightTable from '$components/Weight/Aggregated/AggregatedWeightTable.svelte'
import { dailyWeightStore } from '$lib/store/Weight/dailyWeightStore'
import AggregatedWeightTable from "$components/Weight/Aggregated/AggregatedWeightTable.svelte";
import { dailyWeightStore } from "$lib/store/Weight/dailyWeightStore";
</script>
<template>
<AggregatedWeightTable items="{$dailyWeightStore}" />
<AggregatedWeightTable items={$dailyWeightStore} />
</template>

View File

@@ -1,8 +1,8 @@
<script lang="ts">
import { monthlyWeightStore } from '$lib/store/Weight/monthlyWeightStore'
import AggregatedWeightTable from '$components/Weight/Aggregated/AggregatedWeightTable.svelte'
import { monthlyWeightStore } from "$lib/store/Weight/monthlyWeightStore";
import AggregatedWeightTable from "$components/Weight/Aggregated/AggregatedWeightTable.svelte";
</script>
<template>
<AggregatedWeightTable items="{$monthlyWeightStore}" />
<AggregatedWeightTable items={$monthlyWeightStore} />
</template>

View File

@@ -1,8 +1,8 @@
<script lang="ts">
import { weeklyWeightStore } from '$lib/store/Weight/weeklyWeightStore'
import AggregatedWeightTable from '$components/Weight/Aggregated/AggregatedWeightTable.svelte'
import { weeklyWeightStore } from "$lib/store/Weight/weeklyWeightStore";
import AggregatedWeightTable from "$components/Weight/Aggregated/AggregatedWeightTable.svelte";
</script>
<template>
<AggregatedWeightTable items="{$weeklyWeightStore}" />
<AggregatedWeightTable items={$weeklyWeightStore} />
</template>

View File

@@ -1,8 +1,8 @@
<script lang="ts">
import AggregatedWeightTable from '$components/Weight/Aggregated/AggregatedWeightTable.svelte'
import { yearlyWeightStore } from '$lib/store/Weight/yearlyWeightStore'
import AggregatedWeightTable from "$components/Weight/Aggregated/AggregatedWeightTable.svelte";
import { yearlyWeightStore } from "$lib/store/Weight/yearlyWeightStore";
</script>
<template>
<AggregatedWeightTable items="{$yearlyWeightStore}" />
<AggregatedWeightTable items={$yearlyWeightStore} />
</template>

View File

@@ -4,7 +4,6 @@
let forceUpdate = false;
weightStore.subscribe(() => {
console.log("updte");
forceUpdate = !forceUpdate;
});
</script>

View File

@@ -1,5 +1,6 @@
import { cubicOut } from "svelte/easing";
import type { TransitionConfig } from "svelte/transition";
import regression from "regression";
type FlyAndScaleParams = {
y?: number;
@@ -68,8 +69,11 @@ function GenerateRandomHSL(): Color {
const existingColors: Color[] = [];
function RemoveExistingColors() {
existingColors.length = 0;
}
function GenerateColor(): string {
const minDistance = 15;
const minDistance = 5;
let newColor: Color;
let isDistinct = false;
@@ -89,7 +93,9 @@ function GenerateColor(): string {
break;
}
}
existingColors.push(newColor);
if (isDistinct) {
existingColors.push(newColor);
}
}
// We can not reach this point without having a color generated
@@ -104,5 +110,10 @@ function LerpColor(color1: Color, color2: Color, t: number): Color {
return { h, s, l };
}
export { GenerateColor, LerpColor };
function CalculateR2(input: [number, number][]): number[] {
const reg = regression.linear(input);
return reg.points.map((point: [number, number]) => point[1]);
}
export { GenerateColor, LerpColor, RemoveExistingColors, CalculateR2 };
export type { Color };

View File

@@ -1,8 +1,8 @@
import './style.css'
import App from './App.svelte'
import "./style.css";
import App from "./App.svelte";
const app = new App({
target: document.getElementById('app')
})
target: document.getElementById("app"),
});
export default app
export default app;

View File

@@ -3,28 +3,26 @@
@tailwind utilities;
html {
background-color: rgba(27, 38, 54, 1);
text-align: center;
color: white;
background-color: rgba(27, 38, 54, 1);
text-align: center;
color: white;
}
body {
margin: 0;
color: white;
font-family: "Nunito", -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto",
"Oxygen", "Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue",
sans-serif;
margin: 0;
color: white;
font-family: "Nunito", -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen", "Ubuntu", "Cantarell",
"Fira Sans", "Droid Sans", "Helvetica Neue", sans-serif;
}
@font-face {
font-family: "Nunito";
font-style: normal;
font-weight: 400;
src: local(""),
url("assets/fonts/nunito-v16-latin-regular.woff2") format("woff2");
font-family: "Nunito";
font-style: normal;
font-weight: 400;
src: local(""), url("assets/fonts/nunito-v16-latin-regular.woff2") format("woff2");
}
#app {
height: 100vh;
text-align: center;
height: 100vh;
text-align: center;
}

View File

@@ -2,6 +2,8 @@
// This file is automatically generated. DO NOT EDIT
import {main} from '../models';
export function Close():Promise<void>;
export function CreateFood(arg1:main.Food):Promise<main.WailsFood1>;
export function CreateWeight(arg1:main.Weight):Promise<main.WailsWeight1>;
@@ -12,7 +14,7 @@ export function GetDailyWeight():Promise<main.WailsAggregateWeight>;
export function GetFood():Promise<main.WailsFood>;
export function GetLastPer100(arg1:string):Promise<main.WailsPer100>;
export function GetLastPer100(arg1:string):Promise<main.WailsFoodSearch>;
export function GetMonthlyFood():Promise<main.WailsAggregateFood>;

View File

@@ -2,6 +2,10 @@
// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL
// This file is automatically generated. DO NOT EDIT
export function Close() {
return window['go']['main']['App']['Close']();
}
export function CreateFood(arg1) {
return window['go']['main']['App']['CreateFood'](arg1);
}

View File

@@ -33,7 +33,7 @@ export namespace main {
}
}
export class Food {
rowid: number;
id: number;
date: string;
food: string;
description: string;
@@ -47,7 +47,7 @@ export namespace main {
constructor(source: any = {}) {
if ('string' === typeof source) source = JSON.parse(source);
this.rowid = source["rowid"];
this.id = source["id"];
this.date = source["date"];
this.food = source["food"];
this.description = source["description"];
@@ -56,6 +56,32 @@ export namespace main {
this.energy = source["energy"];
}
}
export class FoodSearch {
id: number;
date: string;
food: string;
description: string;
amount: number;
per100: number;
energy: number;
score: number;
static createFrom(source: any = {}) {
return new FoodSearch(source);
}
constructor(source: any = {}) {
if ('string' === typeof source) source = JSON.parse(source);
this.id = source["id"];
this.date = source["date"];
this.food = source["food"];
this.description = source["description"];
this.amount = source["amount"];
this.per100 = source["per100"];
this.energy = source["energy"];
this.score = source["score"];
}
}
export class WailsAggregateFood {
data: AggregatedFood[];
success: boolean;
@@ -192,6 +218,40 @@ export namespace main {
return a;
}
}
export class WailsFoodSearch {
data: FoodSearch[];
success: boolean;
error?: string;
static createFrom(source: any = {}) {
return new WailsFoodSearch(source);
}
constructor(source: any = {}) {
if ('string' === typeof source) source = JSON.parse(source);
this.data = this.convertValues(source["data"], FoodSearch);
this.success = source["success"];
this.error = source["error"];
}
convertValues(a: any, classs: any, asMap: boolean = false): any {
if (!a) {
return a;
}
if (a.slice && a.map) {
return (a as any[]).map(elem => this.convertValues(elem, classs));
} else if ("object" === typeof a) {
if (asMap) {
for (const key of Object.keys(a)) {
a[key] = new classs(a[key]);
}
return a;
}
return new classs(a);
}
return a;
}
}
export class WailsGenericAck {
success: boolean;
error?: string;
@@ -206,24 +266,8 @@ export namespace main {
this.error = source["error"];
}
}
export class WailsPer100 {
data: number;
success: boolean;
error?: string;
static createFrom(source: any = {}) {
return new WailsPer100(source);
}
constructor(source: any = {}) {
if ('string' === typeof source) source = JSON.parse(source);
this.data = source["data"];
this.success = source["success"];
this.error = source["error"];
}
}
export class Weight {
rowid: number;
id: number;
date: string;
weight: number;
@@ -233,7 +277,7 @@ export namespace main {
constructor(source: any = {}) {
if ('string' === typeof source) source = JSON.parse(source);
this.rowid = source["rowid"];
this.id = source["id"];
this.date = source["date"];
this.weight = source["weight"];
}
@@ -322,6 +366,7 @@ export namespace main {
weightYearlyLookback: number;
target: number;
limit: number;
searchLimit: number;
static createFrom(source: any = {}) {
return new settings(source);
@@ -343,6 +388,7 @@ export namespace main {
this.weightYearlyLookback = source["weightYearlyLookback"];
this.target = source["target"];
this.limit = source["limit"];
this.searchLimit = source["searchLimit"];
}
}

4
go.mod
View File

@@ -6,7 +6,7 @@ toolchain go1.22.4
require (
github.com/mattn/go-sqlite3 v1.14.22
github.com/wailsapp/wails/v2 v2.9.1
github.com/wailsapp/wails/v2 v2.9.2
)
require (
@@ -30,7 +30,7 @@ require (
github.com/tkrajina/go-reflector v0.5.6 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
github.com/wailsapp/go-webview2 v1.0.10 // indirect
github.com/wailsapp/go-webview2 v1.0.16 // indirect
github.com/wailsapp/mimetype v1.4.1 // indirect
golang.org/x/crypto v0.23.0 // indirect
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 // indirect

8
go.sum
View File

@@ -59,12 +59,12 @@ github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyC
github.com/valyala/fasttemplate v1.2.1/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo=
github.com/valyala/fasttemplate v1.2.2/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
github.com/wailsapp/go-webview2 v1.0.10 h1:PP5Hug6pnQEAhfRzLCoOh2jJaPdrqeRgJKZhyYyDV/w=
github.com/wailsapp/go-webview2 v1.0.10/go.mod h1:Uk2BePfCRzttBBjFrBmqKGJd41P6QIHeV9kTgIeOZNo=
github.com/wailsapp/go-webview2 v1.0.16 h1:wffnvnkkLvhRex/aOrA3R7FP7rkvOqL/bir1br7BekU=
github.com/wailsapp/go-webview2 v1.0.16/go.mod h1:Uk2BePfCRzttBBjFrBmqKGJd41P6QIHeV9kTgIeOZNo=
github.com/wailsapp/mimetype v1.4.1 h1:pQN9ycO7uo4vsUUuPeHEYoUkLVkaRntMnHJxVwYhwHs=
github.com/wailsapp/mimetype v1.4.1/go.mod h1:9aV5k31bBOv5z6u+QP8TltzvNGJPmNJD4XlAL3U+j3o=
github.com/wailsapp/wails/v2 v2.9.1 h1:irsXnoQrCpeKzKTYZ2SUVlRRyeMR6I0vCO9Q1cvlEdc=
github.com/wailsapp/wails/v2 v2.9.1/go.mod h1:7maJV2h+Egl11Ak8QZN/jlGLj2wg05bsQS+ywJPT0gI=
github.com/wailsapp/wails/v2 v2.9.2 h1:Xb5YRTos1w5N7DTMyYegWaGukCP2fIaX9WF21kPPF2k=
github.com/wailsapp/wails/v2 v2.9.2/go.mod h1:uehvlCwJSFcBq7rMCGfk4rxca67QQGsbg5Nm4m9UnBs=
golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI=
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 h1:k/i9J1pBpvlfR+9QsetwPyERsqu1GIbi967PQMq3Ivc=

41
main.go
View File

@@ -7,6 +7,7 @@ import (
"io"
"log"
"os"
_ "embed"
"github.com/wailsapp/wails/v2"
"github.com/wailsapp/wails/v2/pkg/options"
@@ -23,7 +24,7 @@ func init() {
logFile, err := os.Create("main.log")
if err != nil {
log.Printf("Error creating log file: %v", err)
os.Exit(1)
return
}
logger := io.MultiWriter(os.Stdout, logFile)
log.SetOutput(logger)
@@ -39,23 +40,55 @@ var (
weightService *WeightService
)
//go:embed food.ddl
var foodDDL string
//go:embed spellfix.dll
var spellfixDll []byte
// TODO: Embed food.ddl and create DB if no exists
// TODO: Add averages to graphs (ie. R2) https://stackoverflow.com/questions/60622195/how-to-draw-a-linear-regression-line-in-chart-js
func main() {
_, err := os.Open("spellfix.dll")
if err != nil && !os.IsNotExist(err) {
Error.Printf("Error looking for spellfix.dll: %v", err)
return
}
if err != nil && os.IsNotExist(err) {
log.Printf("No spellfix.dll found, creating...")
file, err := os.Create("spellfix.dll")
if err != nil {
Error.Printf("Error creating spellfix.dll: %v", err)
return
}
_, err = file.Write(spellfixDll)
if err != nil {
Error.Printf("Error writing spellfix.dll: %v", err)
return
}
file.Close()
}
dbpath := flag.String("db", "food.db", "Path to the database file")
flag.Parse()
db := DB{path: *dbpath}
err := db.Open()
err = db.Open()
if err != nil {
Error.Printf("%++v", err)
os.Exit(1)
return
}
defer db.Close()
err = db.Init(foodDDL)
if err != nil {
Error.Printf("Error initializing database: %++v", err)
return
}
settingsService = &SettingsService{db: &db}
err = settingsService.LoadSettings()
if err != nil {
Error.Printf("%++v", err)
os.Exit(1)
return
}
log.Printf("Loaded settings as: %++v", Settings)

189
migratev2.sql Normal file
View File

@@ -0,0 +1,189 @@
drop index weightDateIdx;
drop index weightDailyIdx;
drop index weightWeeklyIdx;
drop index weightMonthlyIdx;
drop index weightYearlyIdx;
create table weight2 (
id integer primary key,
date datetime default (datetime('now')),
weight real not null
);
insert into weight2 select * from weight;
drop table weight;
drop view if exists weightView;
drop view if exists weightDaily;
drop view if exists weightWeekly;
drop view if exists weightMonthly;
drop view if exists weightYearly;
alter table weight2 rename to weight;
create index weightDateIdx on weight(date);
create index weightDailyIdx on weight(strftime('%Y-%m-%d', date));
create index weightWeeklyIdx on weight(strftime('%Y-%W', date));
create index weightMonthlyIdx on weight(strftime('%Y-%m', date));
create index weightYearlyIdx on weight(strftime('%Y', date));
create view weightDaily as
select strftime('%Y-%m-%d', date) as period,
round(avg(weight), 2) as amount
from weight
group by strftime('%Y-%m-%d', date)
order by date desc;
create view weightWeekly as
select strftime('%Y-%W', date) as period,
round(avg(weight), 2) as amount
from weight
group by strftime('%Y-%W', date)
order by date desc;
create view weightMonthly as
select strftime('%Y-%m', date) as period,
round(avg(weight), 2) as amount
from weight
group by strftime('%Y-%m', date)
order by date desc;
create view weightYearly as
select strftime('%Y', date) as period,
round(avg(weight), 2) as amount
from weight
group by strftime('%Y', date)
order by date desc;
create table food2 (
id integer primary key,
date datetime default (datetime('now')),
food varchar not null,
description varchar,
amount real not null,
per100 real default 0,
energy generated always as (coalesce(amount, 0) * coalesce(per100, 0) / 100) stored
);
drop index dailyIdx;
drop index weeklyIdx;
drop index monthlyIdx;
drop index yearlyIdx;
drop index dateIdx;
drop index foodIdx;
drop view if exists foodView;
drop view if exists foodDaily;
drop view if exists foodWeekly;
drop view if exists foodMonthly;
drop view if exists foodYearly;
drop view if exists foodRecent;
insert into food2(date, food, description, amount) select date, food, description, amount from food;
drop table food;
alter table food2 rename to food;
create index dailyIdx on food(strftime('%Y-%m-%d', date));
create index weeklyIdx on food(strftime('%Y-%W', date));
create index monthlyIdx on food(strftime('%Y-%m', date));
create index yearlyIdx on food(strftime('%Y', date));
create index dateIdx on food(date);
create index foodIdx on food(food);
create view foodView as
select id,
date,
food,
description,
round(amount, 2) as amount,
round(per100, 2) as per100,
round(energy, 2) as energy
from food;
create view foodDaily as
select strftime('%Y-%m-%d', date) as period,
round(sum(amount), 2) as amount,
round(avg(per100), 2) as avgPer100,
round(sum(energy), 2) as energy
from food
group by strftime('%Y-%m-%d', date)
order by date desc;
create view foodWeekly as
select strftime('%Y-%W', date) as period,
round(sum(amount), 2) as amount,
round(avg(per100), 2) as avgPer100,
round(sum(energy), 2) as energy
from food
group by strftime('%Y-%W', date)
order by date desc;
create view foodMonthly as
select strftime('%Y-%m', date) as period,
round(sum(amount), 2) as amount,
round(avg(per100), 2) as avgPer100,
round(sum(energy), 2) as energy
from food
group by strftime('%Y-%m', date)
order by date desc;
create view foodYearly as
select strftime('%Y', date) as period,
round(sum(amount), 2) as amount,
round(avg(per100), 2) as avgPer100,
round(sum(energy), 2) as energy
from food
group by strftime('%Y', date)
order by date desc;
create view foodRecent as
select *
from food
order by date desc
limit 10;
drop trigger if exists food_foodfix_insert;
create trigger food_foodfix_insert AFTER
insert on food for EACH row
begin
update foodfix
set rank = rank + 1
where word = new.food;
insert into foodfix (word, rank)
select new.food,
1
where not exists (
select 1
from foodfix
where word = new.food
);
end;
drop trigger if exists food_foodfix_delete;
create trigger food_foodfix_delete AFTER
delete on food for EACH row
begin
update foodfix
set rank = rank - 1
where word = old.food;
delete from foodfix
where word = old.food
and rank <= 0;
end;
drop trigger if exists food_foodfix_update;
create trigger food_foodfix_update AFTER
update on food for EACH row
begin
update foodfix
set rank = rank - 1
where word = old.food;
end;
delete from foodfix;
insert into foodfix (word, rank)
select food as word,
count(*) as rank
from food
group by food;

View File

@@ -28,8 +28,9 @@ type settings struct {
WeightMonthlyLookback int `default:"4" json:"weightMonthlyLookback"`
WeightYearlyLookback int `default:"2" json:"weightYearlyLookback"`
Target int `default:"2000" json:"target"`
Limit int `default:"2500" json:"limit"`
Target int `default:"2000" json:"target"`
Limit int `default:"2500" json:"limit"`
SearchLimit int `default:"5" json:"searchLimit"`
}
var Settings settings

BIN
spellfix.dll Normal file

Binary file not shown.

View File

@@ -28,6 +28,11 @@ type (
Success bool `json:"success"`
Error string `json:"error,omitempty"`
}
WailsFoodSearch struct {
Data []FoodSearch `json:"data"`
Success bool `json:"success"`
Error string `json:"error,omitempty"`
}
WailsWeight struct {
Data []Weight `json:"data"`

View File

@@ -10,7 +10,7 @@ type (
db *DB
}
Weight struct {
Rowid int64 `json:"rowid"`
Id int64 `json:"id"`
Date string `json:"date"`
Weight float32 `json:"weight"`
}
@@ -20,24 +20,23 @@ type (
}
)
const weightcolumns = "rowid, date, weight"
const weightcolumns = "id, date, weight"
const weightAggregatedColumns = "period, amount"
func (w *WeightService) GetRecent() ([]Weight, error) {
var res []Weight
func (w *WeightService) GetRecent() (res []Weight, err error) {
log.Printf("Getting recent weight")
if w.db == nil || !w.db.Ready {
return res, fmt.Errorf("cannot get recent weight, db is nil or is not ready")
}
row, err := w.db.readConn.Query(fmt.Sprintf("SELECT %s from weightView WHERE date > datetime('now', '-%d days') order by date desc;", weightcolumns, Settings.WeightDaysLookback))
if err != nil {
log.Printf("error getting daily weight: %v", err)
return res, err
return res, fmt.Errorf("error getting recent weight: %w", err)
}
for row.Next() {
var weight Weight
err := row.Scan(&weight.Rowid, &weight.Date, &weight.Weight)
err = row.Scan(&weight.Id, &weight.Date, &weight.Weight)
if err != nil {
log.Printf("error scanning row: %v", err)
continue
@@ -45,11 +44,12 @@ func (w *WeightService) GetRecent() ([]Weight, error) {
res = append(res, weight)
}
return res, nil
log.Printf("Got %d recent weights", len(res))
return
}
func (w *WeightService) Create(weight Weight) (Weight, error) {
func (w *WeightService) Create(weight Weight) (res Weight, err error) {
log.Printf("Creating weight: %v", weight)
if w.db == nil || !w.db.Ready {
return weight, fmt.Errorf("cannot create weight, db is nil or is not ready")
}
@@ -57,46 +57,44 @@ func (w *WeightService) Create(weight Weight) (Weight, error) {
return weight, fmt.Errorf("cannot create weight, weight is empty")
}
res, err := w.db.writeConn.Exec("INSERT INTO weight (weight) VALUES (?)", weight.Weight)
dbres, err := w.db.writeConn.Exec("INSERT INTO weight (weight) VALUES (?)", weight.Weight)
if err != nil {
return weight, fmt.Errorf("error inserting weight: %v", err)
return weight, fmt.Errorf("error inserting weight: %w", err)
}
rowid, err := res.LastInsertId()
id, err := dbres.LastInsertId()
if err != nil {
return weight, fmt.Errorf("error getting last insert id: %v", err)
return weight, fmt.Errorf("error getting last insert id: %w", err)
}
return w.GetByRowid(rowid)
return w.GetById(id)
}
func (w *WeightService) GetByRowid(rowid int64) (Weight, error) {
var res Weight
func (w *WeightService) GetById(id int64) (res Weight, err error) {
log.Printf("Getting weight by id: %d", id)
if w.db == nil || !w.db.Ready {
return res, fmt.Errorf("cannot get weight by rowid, db is nil or is not ready")
return res, fmt.Errorf("cannot get weight by id, db is nil or is not ready")
}
row := w.db.readConn.QueryRow(fmt.Sprintf("SELECT %s from weightView WHERE rowid = ?", weightcolumns), rowid)
err := row.Scan(&res.Rowid, &res.Date, &res.Weight)
row := w.db.readConn.QueryRow(fmt.Sprintf("SELECT %s from weightView WHERE id = ?", weightcolumns), id)
err = row.Scan(&res.Id, &res.Date, &res.Weight)
if err != nil {
return res, fmt.Errorf("error scanning row: %v", err)
return res, fmt.Errorf("error scanning row: %w", err)
}
return res, nil
return
}
// I could probably refactor this to be less of a disaster...
// But I think it'll work for now
func (w *WeightService) GetDaily() ([]AggregatedWeight, error) {
var res []AggregatedWeight
func (w *WeightService) GetDaily() (res []AggregatedWeight, err error) {
log.Printf("Getting daily weight")
if w.db == nil || !w.db.Ready {
return res, fmt.Errorf("cannot get daily weight, db is nil or is not ready")
}
row, err := w.db.readConn.Query(fmt.Sprintf("SELECT %s from weightDaily LIMIT %d", weightAggregatedColumns, Settings.WeightDailyLookback))
if err != nil {
log.Printf("error getting daily weight: %v", err)
return res, err
return res, fmt.Errorf("error getting daily weight: %w", err)
}
for row.Next() {
@@ -109,25 +107,23 @@ func (w *WeightService) GetDaily() ([]AggregatedWeight, error) {
res = append(res, weight)
}
return res, nil
log.Printf("Got %d daily weights", len(res))
return
}
func (w *WeightService) GetWeekly() ([]AggregatedWeight, error) {
var res []AggregatedWeight
func (w *WeightService) GetWeekly() (res []AggregatedWeight, err error) {
if w.db == nil || !w.db.Ready {
return res, fmt.Errorf("cannot get weekly weight, db is nil or is not ready")
}
row, err := w.db.readConn.Query(fmt.Sprintf("SELECT %s from weightWeekly LIMIT %d", weightAggregatedColumns, Settings.WeightWeeklyLookback))
if err != nil {
log.Printf("error getting weekly weight: %v", err)
return res, err
return res, fmt.Errorf("error getting weekly weight: %w", err)
}
for row.Next() {
var weight AggregatedWeight
err := row.Scan(&weight.Period, &weight.Amount)
err = row.Scan(&weight.Period, &weight.Amount)
if err != nil {
log.Printf("error scanning row: %v", err)
continue
@@ -135,20 +131,18 @@ func (w *WeightService) GetWeekly() ([]AggregatedWeight, error) {
res = append(res, weight)
}
return res, nil
log.Printf("Got %d weekly weights", len(res))
return
}
func (w *WeightService) GetMonthly() ([]AggregatedWeight, error) {
var res []AggregatedWeight
func (w *WeightService) GetMonthly() (res []AggregatedWeight, err error) {
if w.db == nil || !w.db.Ready {
return res, fmt.Errorf("cannot get monthly weight, db is nil or is not ready")
}
row, err := w.db.readConn.Query(fmt.Sprintf("SELECT %s from weightMonthly LIMIT %d", weightAggregatedColumns, Settings.WeightMonthlyLookback))
if err != nil {
log.Printf("error getting monthly weight: %v", err)
return res, err
return res, fmt.Errorf("error getting monthly weight: %w", err)
}
for row.Next() {
@@ -161,25 +155,23 @@ func (w *WeightService) GetMonthly() ([]AggregatedWeight, error) {
res = append(res, weight)
}
return res, nil
log.Printf("Got %d monthly weights", len(res))
return
}
func (w *WeightService) GetYearly() ([]AggregatedWeight, error) {
var res []AggregatedWeight
func (w *WeightService) GetYearly() (res []AggregatedWeight, err error) {
if w.db == nil || !w.db.Ready {
return res, fmt.Errorf("cannot get yearly weight, db is nil or is not ready")
}
row, err := w.db.readConn.Query(fmt.Sprintf("SELECT %s from weightYearly LIMIT %d", weightAggregatedColumns, Settings.WeightYearlyLookback))
if err != nil {
log.Printf("error getting yearly weight: %v", err)
return res, err
return res, fmt.Errorf("error getting yearly weight: %w", err)
}
for row.Next() {
var weight AggregatedWeight
err := row.Scan(&weight.Period, &weight.Amount)
err = row.Scan(&weight.Period, &weight.Amount)
if err != nil {
log.Printf("error scanning row: %v", err)
continue
@@ -188,5 +180,6 @@ func (w *WeightService) GetYearly() ([]AggregatedWeight, error) {
res = append(res, weight)
}
return res, nil
log.Printf("Got %d yearly weights", len(res))
return
}