Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
80fb660677 | |||
de461cb031 | |||
6d684b34ef |
53
db.go
53
db.go
@@ -3,6 +3,7 @@ package main
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
@@ -44,6 +45,58 @@ 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)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (db *DB) Close() error {
|
||||
err := db.writeConn.Close()
|
||||
if err != nil {
|
||||
|
1
food.ddl
1
food.ddl
@@ -5,7 +5,6 @@ create table weight (
|
||||
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));
|
||||
|
@@ -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();
|
||||
}
|
||||
|
@@ -1,9 +1,10 @@
|
||||
<script lang="ts">
|
||||
import { link, location } from "svelte-spa-router";
|
||||
import { faGear } from "@fortawesome/free-solid-svg-icons";
|
||||
import { faGear} from "@fortawesome/free-solid-svg-icons";
|
||||
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 = {
|
||||
@@ -88,6 +89,7 @@
|
||||
<Fa icon={faGear} scale={2} />
|
||||
</button>
|
||||
</div>
|
||||
<RefreshComponent />
|
||||
<EnergyToday />
|
||||
</header>
|
||||
|
||||
|
42
frontend/src/lib/components/RefreshComponent.svelte
Normal file
42
frontend/src/lib/components/RefreshComponent.svelte
Normal 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>
|
||||
<Fa icon={faRefresh} scale={2} />
|
||||
</button>
|
||||
</div>
|
||||
</template>
|
5
main.go
5
main.go
@@ -7,6 +7,7 @@ import (
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
_ "embed"
|
||||
|
||||
"github.com/wailsapp/wails/v2"
|
||||
"github.com/wailsapp/wails/v2/pkg/options"
|
||||
@@ -39,6 +40,9 @@ var (
|
||||
weightService *WeightService
|
||||
)
|
||||
|
||||
//go:embed food.ddl
|
||||
var foodDDL string
|
||||
|
||||
// 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() {
|
||||
@@ -52,6 +56,7 @@ func main() {
|
||||
os.Exit(1)
|
||||
}
|
||||
defer db.Close()
|
||||
db.Init(foodDDL)
|
||||
|
||||
settingsService = &SettingsService{db: &db}
|
||||
err = settingsService.LoadSettings()
|
||||
|
Reference in New Issue
Block a user