2024-02-22 03:35:55 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2024-02-24 06:47:45 +00:00
|
|
|
"errors"
|
2024-02-22 03:35:55 +00:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
|
|
"log"
|
|
|
|
"math"
|
2024-02-23 04:54:45 +00:00
|
|
|
"strconv"
|
2024-02-22 03:35:55 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2024-02-23 04:24:32 +00:00
|
|
|
type EventsRow struct {
|
|
|
|
description sql.NullString
|
|
|
|
timestamp sql.NullInt64
|
|
|
|
interval sql.NullString
|
|
|
|
period sql.NullString
|
|
|
|
yearknown sql.NullBool
|
|
|
|
monthknown sql.NullBool
|
|
|
|
dayknown sql.NullBool
|
|
|
|
hourknown sql.NullBool
|
|
|
|
minuteknown sql.NullBool
|
|
|
|
secondknown sql.NullBool
|
|
|
|
}
|
|
|
|
|
|
|
|
type TimelineDB struct {
|
|
|
|
*sql.DB
|
2024-02-22 03:35:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func formula(p float64) int64 {
|
|
|
|
yearsAgo := math.Exp(20.3444*math.Pow(p, 3)+3) - math.Exp(3)
|
|
|
|
return int64(yearsAgo * 31556926)
|
|
|
|
}
|
|
|
|
|
2024-02-24 23:59:36 +00:00
|
|
|
func (db *TimelineDB) closestEvents(t int64, n int, after bool, before bool) ([]EventsRow, error) {
|
|
|
|
ulimit := int64(math.MaxInt64)
|
|
|
|
llimit := int64(math.MinInt64)
|
|
|
|
if after && before {
|
|
|
|
return nil, errors.New("after and before can't both be true")
|
|
|
|
} else if after {
|
|
|
|
llimit = t
|
|
|
|
} else if before {
|
|
|
|
ulimit = t
|
|
|
|
}
|
|
|
|
|
2024-02-22 03:35:55 +00:00
|
|
|
query := `
|
2024-02-23 04:24:32 +00:00
|
|
|
select description, timestamp, yearknown, monthknown, dayknown, hourknown, minuteknown, secondknown
|
2024-02-22 03:35:55 +00:00
|
|
|
from events
|
2024-02-24 23:59:36 +00:00
|
|
|
where timestamp between ? and ?
|
2024-02-22 03:35:55 +00:00
|
|
|
order by abs(? - timestamp) asc
|
2024-02-24 05:04:35 +00:00
|
|
|
limit ?
|
2024-02-22 03:35:55 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
stmt, err := db.Prepare(query)
|
|
|
|
if err != nil {
|
2024-02-24 23:59:36 +00:00
|
|
|
return nil, err
|
2024-02-22 03:35:55 +00:00
|
|
|
}
|
|
|
|
defer stmt.Close()
|
|
|
|
|
2024-02-24 23:59:36 +00:00
|
|
|
rows, err := stmt.Query(llimit, ulimit, t, n)
|
2024-02-24 05:04:35 +00:00
|
|
|
if err != nil {
|
2024-02-24 23:59:36 +00:00
|
|
|
return nil, err
|
2024-02-24 05:04:35 +00:00
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
2024-02-24 23:59:36 +00:00
|
|
|
events := make([]EventsRow, n)
|
2024-02-24 05:54:54 +00:00
|
|
|
i := 0
|
|
|
|
for ; rows.Next(); i++ {
|
2024-02-24 05:04:35 +00:00
|
|
|
var event EventsRow
|
|
|
|
err = rows.Scan(&event.description, &event.timestamp, &event.yearknown, &event.monthknown, &event.dayknown, &event.hourknown, &event.minuteknown, &event.secondknown)
|
|
|
|
if err != nil {
|
|
|
|
return events, err
|
|
|
|
}
|
|
|
|
|
|
|
|
events[i] = event
|
|
|
|
}
|
|
|
|
|
2024-02-24 05:54:54 +00:00
|
|
|
return events[:i], err
|
2024-02-22 03:35:55 +00:00
|
|
|
}
|
|
|
|
|
2024-02-23 05:36:57 +00:00
|
|
|
func (event *EventsRow) Output() (int64, string, string, error) {
|
2024-02-24 06:47:45 +00:00
|
|
|
if !event.timestamp.Valid {
|
|
|
|
return 0, "", "", errors.New("null timestamp")
|
|
|
|
} else if !event.description.Valid {
|
|
|
|
return 0, "", "", errors.New("null description")
|
2024-02-23 04:33:54 +00:00
|
|
|
}
|
|
|
|
|
2024-02-24 06:47:45 +00:00
|
|
|
timestamp := event.timestamp.Int64
|
|
|
|
desc := event.description.String
|
|
|
|
yearknown := event.yearknown.Valid && event.yearknown.Bool
|
2024-02-23 04:54:45 +00:00
|
|
|
|
|
|
|
var ago string
|
2024-02-24 06:47:45 +00:00
|
|
|
date := time.Unix(timestamp, 0)
|
|
|
|
if !yearknown {
|
2024-02-23 04:54:45 +00:00
|
|
|
yeardiff := time.Now().Year() - date.Year()
|
|
|
|
ago = strconv.Itoa(yeardiff) + " years ago"
|
|
|
|
} else {
|
|
|
|
ago = date.String()
|
|
|
|
}
|
2024-02-23 04:33:54 +00:00
|
|
|
|
2024-02-24 06:47:45 +00:00
|
|
|
return timestamp, ago, desc, nil
|
2024-02-23 04:33:54 +00:00
|
|
|
}
|
|
|
|
|
2024-02-22 03:35:55 +00:00
|
|
|
func main() {
|
|
|
|
percent := flag.Float64("p", -1, "percentage")
|
2024-02-24 05:04:35 +00:00
|
|
|
nevents := flag.Int("n", 1, "number of events")
|
2024-02-24 23:59:36 +00:00
|
|
|
after := flag.Bool("a", false, "only return events occurring after computed timestamp")
|
|
|
|
before := flag.Bool("b", false, "only return events occurring before computed timestamp")
|
2024-02-22 03:35:55 +00:00
|
|
|
flag.Parse()
|
|
|
|
|
2024-02-24 23:59:36 +00:00
|
|
|
if *after && *before {
|
|
|
|
log.Fatal("cannot pass both -a and -b")
|
|
|
|
}
|
|
|
|
|
2024-02-22 03:35:55 +00:00
|
|
|
if *percent < 0 || *percent > 1 {
|
|
|
|
log.Fatal("invalid percentage")
|
2024-02-24 07:04:11 +00:00
|
|
|
} else if *nevents < 0 {
|
|
|
|
log.Fatal("number of events must be non-negative")
|
2024-02-22 03:35:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
t := time.Now().Unix() - formula(*percent)
|
|
|
|
|
2024-02-23 04:24:32 +00:00
|
|
|
var db TimelineDB
|
|
|
|
var err error
|
|
|
|
db.DB, err = sql.Open("sqlite3", "./timeline.db")
|
2024-02-22 03:35:55 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
defer db.Close()
|
|
|
|
|
2024-02-24 23:59:36 +00:00
|
|
|
events, err := db.closestEvents(t, *nevents, *after, *before)
|
2024-02-23 04:24:32 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2024-02-24 05:04:35 +00:00
|
|
|
for _, event := range events {
|
|
|
|
timestamp, date, desc, err := event.Output()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2024-02-23 05:36:57 +00:00
|
|
|
|
2024-02-24 05:04:35 +00:00
|
|
|
fmt.Printf("%v\t%v\t%v\n", timestamp, date, desc)
|
|
|
|
}
|
2024-02-22 03:35:55 +00:00
|
|
|
}
|