2 Commits
2.1.1 ... 2.2.1

Author SHA1 Message Date
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
3 changed files with 32 additions and 8 deletions

View File

@@ -68,14 +68,15 @@ func (s *FoodService) GetLastPer100(name string) ([]FoodSearch, error) {
query := fmt.Sprintf(`
with search_results as (
select word, score, f.rowid, f.*
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
group by food
where rn = 1
order by score asc, date desc
limit %d
`, foodColumns, Settings.SearchLimit)

View File

@@ -106,6 +106,7 @@
function setInputVal(food: main.Food) {
name = food.food;
per100 = String(food.per100);
amount = String(food.amount);
hiLiteIndex = null;
foodSearch = [];
}

34
main.go
View File

@@ -24,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)
@@ -42,37 +42,59 @@ var (
//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)
os.Exit(1)
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)
foodService = &FoodService{db: &db}
weightService = &WeightService{db: &db}
// Create an instance of the app structure
app := NewApp()