diff --git a/.gitignore b/.gitignore index 5b604d3..095092d 100644 --- a/.gitignore +++ b/.gitignore @@ -13,4 +13,4 @@ *.out server -storage.db +storage.sqlite diff --git a/Makefile b/Makefile index 15633c0..10fbef2 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,6 @@ +# for development, db in cwd +export DATABASE_PATH := ./storage.sqlite + VERSION := $(shell git rev-parse --short HEAD) BUILDTIME := $(shell date -u '+%Y-%m-%dT%H:%M:%SZ') BUILDTIMEFILENAME := $(shell date -u '+%Y%m%d-%H%M%SZ') diff --git a/go.mod b/go.mod index 9c62757..1a5e2b1 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/UnnoTed/fileb0x v1.1.4 // indirect github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect github.com/flosch/pongo2 v0.0.0-20190707114632-bbf5a6c351f4 + github.com/hako/durafmt v0.0.0-20191009132224-3f39dc1ed9f4 github.com/jinzhu/gorm v1.9.12 github.com/k0kubun/pp v3.0.1+incompatible github.com/labstack/echo v3.3.10+incompatible diff --git a/go.sum b/go.sum index ec67a62..f64f168 100644 --- a/go.sum +++ b/go.sum @@ -20,6 +20,8 @@ github.com/go-check/check v0.0.0-20180628173108-788fd7840127/go.mod h1:9ES+weclK github.com/go-sql-driver/mysql v1.4.1/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w= github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/hako/durafmt v0.0.0-20191009132224-3f39dc1ed9f4 h1:60gBOooTSmNtrqNaRvrDbi8VAne0REaek2agjnITKSw= +github.com/hako/durafmt v0.0.0-20191009132224-3f39dc1ed9f4/go.mod h1:5Scbynm8dF1XAPwIwkGPqzkM/shndPm79Jd1003hTjE= github.com/jinzhu/gorm v1.9.12 h1:Drgk1clyWT9t9ERbzHza6Mj/8FY/CqMyVzOiHviMo6Q= github.com/jinzhu/gorm v1.9.12/go.mod h1:vhTjlKSJUTWNtcbQtrMBFCxy7eXTzeCAzfL5fBZT/Qs= github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= diff --git a/hn/db.go b/hn/db.go index c6f8438..1358865 100644 --- a/hn/db.go +++ b/hn/db.go @@ -17,6 +17,7 @@ type HNFrontPage struct { HighestRank uint // frontpage index Rank uint // frontpage index Title string // submission title + Score uint // updoots URL string // duh } @@ -27,6 +28,7 @@ type HNStoryRank struct { Title string // submission title URL string // duh Rank uint // frontpage index + Score uint // updoots FetchedAt time.Time // identical within fetchid } diff --git a/hn/fetcher.go b/hn/fetcher.go index ca30f5e..3a09a3b 100644 --- a/hn/fetcher.go +++ b/hn/fetcher.go @@ -88,6 +88,7 @@ func (f *Fetcher) StoreFrontPage() error { Rank: uint(i + 1), URL: item.URL, Title: item.Title, + Score: item.Score, FetchedAt: t, } */ @@ -109,6 +110,7 @@ func (f *Fetcher) StoreFrontPage() error { HighestRank: uint(i + 1), Rank: uint(i + 1), Title: item.Title, + Score: uint(item.Score), URL: item.URL, } f.db.Create(&r) @@ -116,6 +118,7 @@ func (f *Fetcher) StoreFrontPage() error { Uint("hnid", uint(id)). Uint("rank", uint(i+1)). Str("title", item.Title). + Int("score", item.Score). Str("url", item.URL). Msg("HN new story on frontpage") } else { @@ -129,10 +132,12 @@ func (f *Fetcher) StoreFrontPage() error { Uint("hnid", uint(id)). Uint("oldrank", old.Rank). Uint("newrank", uint(i+1)). + Int("score", item.Score). Str("title", item.Title). Str("url", item.URL). Msg("HN story rank changed, recording new rank") old.Rank = uint(i + 1) + old.Score = uint(item.Score) needSave = true } @@ -145,6 +150,12 @@ func (f *Fetcher) StoreFrontPage() error { old.HighestRank = uint(i + 1) needSave = true } + + if old.Score != uint(item.Score) { + old.Score = uint(item.Score) + needSave = true + } + if needSave { f.db.Save(&old) } diff --git a/hn/handlers.go b/hn/handlers.go index 1388106..bb5d511 100644 --- a/hn/handlers.go +++ b/hn/handlers.go @@ -24,33 +24,41 @@ func (r *RequestHandlerSet) indexHandler(c echo.Context) error { r.db.Where("disappeared is not ?", SQLITE_NULL_DATETIME).Order("disappeared desc").Find(&fpi) type fprow struct { - Duration string - URL string - Title string - HighestRank uint - HNID uint - TimeGone string + Duration string + DurationSecs uint + URL string + Title string + HighestRank uint + HNID uint + Score uint + TimeGone string + TimeGoneSecs uint } var fprows []fprow for _, item := range fpi { fprows = append(fprows, fprow{ - Duration: item.Disappeared.Round(time.Minute).Sub(item.Appeared.Round(time.Minute)).String(), - URL: item.URL, - HNID: item.HNID, - Title: item.Title, - HighestRank: item.HighestRank, - TimeGone: time.Now().Round(time.Minute).Sub(item.Disappeared.Round(time.Minute)).String(), + Duration: timeDiffHuman(item.Disappeared, item.Appeared), + DurationSecs: timeDiffAbsSeconds(item.Disappeared, item.Appeared), + URL: item.URL, + HNID: item.HNID, + Score: item.Score, + Title: item.Title, + HighestRank: item.HighestRank, + TimeGone: timeDiffHuman(time.Now(), item.Disappeared), + TimeGoneSecs: timeDiffAbsSeconds(time.Now(), item.Disappeared), }) } type rowtwo struct { - Duration string - URL string - Title string - HighestRank uint - HNID uint - Rank uint + Duration string + DurationSecs uint + URL string + Title string + Score uint + HighestRank uint + HNID uint + Rank uint } var currentfp []rowtwo @@ -59,12 +67,14 @@ func (r *RequestHandlerSet) indexHandler(c echo.Context) error { for _, item := range cur { currentfp = append(currentfp, rowtwo{ - Duration: time.Now().Round(time.Minute).Sub(item.Appeared.Round(time.Minute)).String(), - URL: item.URL, - HNID: item.HNID, - Title: item.Title, - HighestRank: item.HighestRank, - Rank: item.Rank, + Duration: timeDiffHuman(time.Now(), item.Appeared), + DurationSecs: timeDiffAbsSeconds(time.Now(), item.Appeared), + URL: item.URL, + HNID: item.HNID, + Score: item.Score, + Title: item.Title, + HighestRank: item.HighestRank, + Rank: item.Rank, }) } diff --git a/hn/misc.go b/hn/misc.go new file mode 100644 index 0000000..1e2095e --- /dev/null +++ b/hn/misc.go @@ -0,0 +1,20 @@ +package hn + +import ( + "math" + "time" + + "github.com/hako/durafmt" +) + +func timeDiffHuman(first time.Time, second time.Time) string { + if first.Before(second) { + return durafmt.ParseShort(second.Sub(first)).String() + } else { + return durafmt.ParseShort(first.Sub(second)).String() + } +} + +func timeDiffAbsSeconds(first time.Time, second time.Time) uint { + return uint(math.Abs(first.Sub(second).Truncate(time.Second).Seconds())) +} diff --git a/view/index.html b/view/index.html index ae52af5..83f3211 100644 --- a/view/index.html +++ b/view/index.html @@ -11,7 +11,8 @@ Hang Time Title Highest Rank - Time Since Wipeout + Time Since Wipeout @@ -21,8 +22,13 @@ --> - {{exit.Duration}} - {{exit.Title}} ({{exit.Duration}} + {% else %} + {{exit.Duration}} + {% endif %} + {{exit.Title}} ({{exit.Score}} + points, comments) {{exit.HighestRank}} {{exit.TimeGone}} @@ -47,7 +53,7 @@ {{i.Duration}} - {{i.Title}} ({{i.Title}} ({{i.Score}} points, comments) {{i.HighestRank}}