Compare commits

..

No commits in common. "3f45e0fbc0d638f951711c59d0f3a3ccae1c772c" and "35cd759205004e85710a0dc1f33e0b99bb0f1d9a" have entirely different histories.

3 changed files with 29 additions and 42 deletions

View File

@ -1,11 +0,0 @@
.PHONY: all build fmt
all: timeline.db xbit
xbit: build
build:
go build
timeline.db: timeline.sql
sqlite3 timeline.db < timeline.sql

2
go.mod
View File

@ -1,4 +1,4 @@
module scm.dairydemon.net/filifa/xbit module dairydemon.net/xbit
go 1.19 go 1.19

58
main.go
View File

@ -2,7 +2,6 @@ package main
import ( import (
"database/sql" "database/sql"
"errors"
"flag" "flag"
"fmt" "fmt"
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
@ -34,22 +33,28 @@ func formula(p float64) int64 {
return int64(yearsAgo * 31556926) return int64(yearsAgo * 31556926)
} }
func (db *TimelineDB) closestEvents(t int64, n uint, llimit int64, ulimit int64) ([]EventsRow, error) { func (db *TimelineDB) closestEvents(t int64, n int) ([]EventsRow, error) {
query := ` query := `
select description, timestamp, yearknown, monthknown, dayknown, hourknown, minuteknown, secondknown select description, timestamp, yearknown, monthknown, dayknown, hourknown, minuteknown, secondknown
from events from events
where timestamp between ? and ? where timestamp is not null
order by abs(? - timestamp) asc order by abs(? - timestamp) asc
limit ? limit ?
` `
rows, err := db.Query(query, llimit, ulimit, t, n) events := make([]EventsRow, n)
stmt, err := db.Prepare(query)
if err != nil { if err != nil {
return nil, err return events, err
}
defer stmt.Close()
rows, err := stmt.Query(t, n)
if err != nil {
return events, err
} }
defer rows.Close() defer rows.Close()
events := make([]EventsRow, n)
i := 0 i := 0
for ; rows.Next(); i++ { for ; rows.Next(); i++ {
var event EventsRow var event EventsRow
@ -65,33 +70,36 @@ func (db *TimelineDB) closestEvents(t int64, n uint, llimit int64, ulimit int64)
} }
func (event *EventsRow) Output() (int64, string, string, error) { func (event *EventsRow) Output() (int64, string, string, error) {
if !event.timestamp.Valid { timestamp, err := event.timestamp.Value()
return 0, "", "", errors.New("null timestamp") if err != nil {
} else if !event.description.Valid { return 0, "", "", err
return 0, "", "", errors.New("null description")
} }
timestamp := event.timestamp.Int64 desc, err := event.description.Value()
desc := event.description.String if err != nil {
yearknown := event.yearknown.Valid && event.yearknown.Bool return 0, "", "", err
}
yearknown, err := event.yearknown.Value()
if err != nil {
return 0, "", "", err
}
var ago string var ago string
date := time.Unix(timestamp, 0) date := time.Unix(timestamp.(int64), 0)
if !yearknown { if yearknown == nil || !yearknown.(bool) {
yeardiff := time.Now().Year() - date.Year() yeardiff := time.Now().Year() - date.Year()
ago = strconv.Itoa(yeardiff) + " years ago" ago = strconv.Itoa(yeardiff) + " years ago"
} else { } else {
ago = date.String() ago = date.String()
} }
return timestamp, ago, desc, nil return timestamp.(int64), ago, desc.(string), nil
} }
func main() { func main() {
percent := flag.Float64("p", -1, "percentage") percent := flag.Float64("p", -1, "percentage")
nevents := flag.Uint("n", 1, "number of events") nevents := flag.Int("n", 1, "number of events")
after := flag.Bool("a", false, "only return events occurring after computed timestamp")
before := flag.Bool("b", false, "only return events occurring before computed timestamp")
flag.Parse() flag.Parse()
if *percent < 0 || *percent > 1 { if *percent < 0 || *percent > 1 {
@ -100,16 +108,6 @@ func main() {
t := time.Now().Unix() - formula(*percent) t := time.Now().Unix() - formula(*percent)
llimit := int64(math.MinInt64)
ulimit := int64(math.MaxInt64)
if *after && *before {
log.Fatal("cannot pass both -a and -b")
} else if *after {
llimit = t
} else if *before {
ulimit = t
}
var db TimelineDB var db TimelineDB
var err error var err error
db.DB, err = sql.Open("sqlite3", "./timeline.db") db.DB, err = sql.Open("sqlite3", "./timeline.db")
@ -118,7 +116,7 @@ func main() {
} }
defer db.Close() defer db.Close()
events, err := db.closestEvents(t, *nevents, llimit, ulimit) events, err := db.closestEvents(t, *nevents)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }