Compare commits
10 Commits
35cd759205
...
3f45e0fbc0
Author | SHA1 | Date |
---|---|---|
Nick Griffey | 3f45e0fbc0 | |
Nick Griffey | a4da825b8e | |
Nick Griffey | a286eb3759 | |
Nick Griffey | b09e915d9e | |
Nick Griffey | 9c4d4aa037 | |
Nick Griffey | 191b10175f | |
Nick Griffey | 831d4f78e3 | |
Nick Griffey | 2d742ce3f7 | |
Nick Griffey | cbd92a8d42 | |
Nick Griffey | fdd7a7bb98 |
|
@ -0,0 +1,11 @@
|
|||
.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
2
go.mod
|
@ -1,4 +1,4 @@
|
|||
module dairydemon.net/xbit
|
||||
module scm.dairydemon.net/filifa/xbit
|
||||
|
||||
go 1.19
|
||||
|
||||
|
|
58
main.go
58
main.go
|
@ -2,6 +2,7 @@ package main
|
|||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
|
@ -33,28 +34,22 @@ func formula(p float64) int64 {
|
|||
return int64(yearsAgo * 31556926)
|
||||
}
|
||||
|
||||
func (db *TimelineDB) closestEvents(t int64, n int) ([]EventsRow, error) {
|
||||
func (db *TimelineDB) closestEvents(t int64, n uint, llimit int64, ulimit int64) ([]EventsRow, error) {
|
||||
query := `
|
||||
select description, timestamp, yearknown, monthknown, dayknown, hourknown, minuteknown, secondknown
|
||||
from events
|
||||
where timestamp is not null
|
||||
where timestamp between ? and ?
|
||||
order by abs(? - timestamp) asc
|
||||
limit ?
|
||||
`
|
||||
|
||||
events := make([]EventsRow, n)
|
||||
stmt, err := db.Prepare(query)
|
||||
rows, err := db.Query(query, llimit, ulimit, t, n)
|
||||
if err != nil {
|
||||
return events, err
|
||||
}
|
||||
defer stmt.Close()
|
||||
|
||||
rows, err := stmt.Query(t, n)
|
||||
if err != nil {
|
||||
return events, err
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
events := make([]EventsRow, n)
|
||||
i := 0
|
||||
for ; rows.Next(); i++ {
|
||||
var event EventsRow
|
||||
|
@ -70,36 +65,33 @@ func (db *TimelineDB) closestEvents(t int64, n int) ([]EventsRow, error) {
|
|||
}
|
||||
|
||||
func (event *EventsRow) Output() (int64, string, string, error) {
|
||||
timestamp, err := event.timestamp.Value()
|
||||
if err != nil {
|
||||
return 0, "", "", err
|
||||
if !event.timestamp.Valid {
|
||||
return 0, "", "", errors.New("null timestamp")
|
||||
} else if !event.description.Valid {
|
||||
return 0, "", "", errors.New("null description")
|
||||
}
|
||||
|
||||
desc, err := event.description.Value()
|
||||
if err != nil {
|
||||
return 0, "", "", err
|
||||
}
|
||||
|
||||
yearknown, err := event.yearknown.Value()
|
||||
if err != nil {
|
||||
return 0, "", "", err
|
||||
}
|
||||
timestamp := event.timestamp.Int64
|
||||
desc := event.description.String
|
||||
yearknown := event.yearknown.Valid && event.yearknown.Bool
|
||||
|
||||
var ago string
|
||||
date := time.Unix(timestamp.(int64), 0)
|
||||
if yearknown == nil || !yearknown.(bool) {
|
||||
date := time.Unix(timestamp, 0)
|
||||
if !yearknown {
|
||||
yeardiff := time.Now().Year() - date.Year()
|
||||
ago = strconv.Itoa(yeardiff) + " years ago"
|
||||
} else {
|
||||
ago = date.String()
|
||||
}
|
||||
|
||||
return timestamp.(int64), ago, desc.(string), nil
|
||||
return timestamp, ago, desc, nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
percent := flag.Float64("p", -1, "percentage")
|
||||
nevents := flag.Int("n", 1, "number of events")
|
||||
nevents := flag.Uint("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()
|
||||
|
||||
if *percent < 0 || *percent > 1 {
|
||||
|
@ -108,6 +100,16 @@ func main() {
|
|||
|
||||
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 err error
|
||||
db.DB, err = sql.Open("sqlite3", "./timeline.db")
|
||||
|
@ -116,7 +118,7 @@ func main() {
|
|||
}
|
||||
defer db.Close()
|
||||
|
||||
events, err := db.closestEvents(t, *nevents)
|
||||
events, err := db.closestEvents(t, *nevents, llimit, ulimit)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue