made to suck less, including:
All checks were successful
continuous-integration/drone/push Build is passing

* `make` works again, exporting correct database file path for local dev
* better formatting of durations
* refactored duration math to misc functions
* now tracks and displays score
* displays short-lifetime fp wipeouts in red
This commit is contained in:
2020-03-25 07:50:10 -07:00
parent fe1c4df4f1
commit 0d9ed8874f
9 changed files with 84 additions and 29 deletions

View File

@@ -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
}

View File

@@ -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)
}

View File

@@ -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,
})
}

20
hn/misc.go Normal file
View File

@@ -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()))
}