add -a and -b flags
This commit is contained in:
parent
cbd92a8d42
commit
2d742ce3f7
30
main.go
30
main.go
|
@ -34,28 +34,38 @@ func formula(p float64) int64 {
|
||||||
return int64(yearsAgo * 31556926)
|
return int64(yearsAgo * 31556926)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *TimelineDB) closestEvents(t int64, n int) ([]EventsRow, error) {
|
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
|
||||||
|
}
|
||||||
|
|
||||||
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 is not null
|
where timestamp between ? and ?
|
||||||
order by abs(? - timestamp) asc
|
order by abs(? - timestamp) asc
|
||||||
limit ?
|
limit ?
|
||||||
`
|
`
|
||||||
|
|
||||||
events := make([]EventsRow, n)
|
|
||||||
stmt, err := db.Prepare(query)
|
stmt, err := db.Prepare(query)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return events, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer stmt.Close()
|
defer stmt.Close()
|
||||||
|
|
||||||
rows, err := stmt.Query(t, n)
|
rows, err := stmt.Query(llimit, ulimit, t, n)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return events, err
|
return nil, 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
|
||||||
|
@ -96,8 +106,14 @@ func (event *EventsRow) Output() (int64, string, string, error) {
|
||||||
func main() {
|
func main() {
|
||||||
percent := flag.Float64("p", -1, "percentage")
|
percent := flag.Float64("p", -1, "percentage")
|
||||||
nevents := flag.Int("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 *after && *before {
|
||||||
|
log.Fatal("cannot pass both -a and -b")
|
||||||
|
}
|
||||||
|
|
||||||
if *percent < 0 || *percent > 1 {
|
if *percent < 0 || *percent > 1 {
|
||||||
log.Fatal("invalid percentage")
|
log.Fatal("invalid percentage")
|
||||||
} else if *nevents < 0 {
|
} else if *nevents < 0 {
|
||||||
|
@ -114,7 +130,7 @@ func main() {
|
||||||
}
|
}
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
|
|
||||||
events, err := db.closestEvents(t, *nevents)
|
events, err := db.closestEvents(t, *nevents, *after, *before)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue