1
0
mirror of https://github.com/thebaer/tildes.git synced 2018-07-20 07:15:21 +00:00

Show time between asks / addiction

This commit is contained in:
Matt Baer 2015-02-01 00:07:10 -05:00
parent d866615034
commit f1ef0a7bc8

View File

@ -36,7 +36,7 @@ func main() {
addictionData = "/home/bear/addicted.sh" addictionData = "/home/bear/addicted.sh"
} }
headers := []string{ "User", "Tildes", "Last Collected", "# Asks", "Avg.", "Last Amt." } headers := []string{ "User", "Tildes", "Last Collected", "Addiction", "# Asks", "Avg.", "Last Amt." }
scoresData := readData(scoresPath, "&^%") scoresData := readData(scoresPath, "&^%")
updatesData := readData(scoreDeltasPath, deltaDelimiter) updatesData := readData(scoreDeltasPath, deltaDelimiter)
@ -100,12 +100,37 @@ func parseTimestamp(ts string) time.Time {
return time.Unix(t, 0) return time.Unix(t, 0)
} }
func trimTrailingZeros(n float64) string {
return strings.TrimRight(strings.TrimRight(fmt.Sprintf("%.2f", n), "0"), ".")
}
func trimTrailingZerosShort(n float64) string {
return strings.TrimRight(strings.TrimRight(fmt.Sprintf("%.1f", n), "0"), ".")
}
func niceTime(sec int) string {
if sec == 0 {
return "-"
}
if sec < 60 {
return fmt.Sprintf("%dsec", sec)
} else if sec < 3600 {
return fmt.Sprintf("%smin", trimTrailingZerosShort(float64(sec) / 60.0))
} else if sec < 86400 {
return fmt.Sprintf("%shr", trimTrailingZerosShort(float64(sec) / 3600.0))
} else {
return fmt.Sprintf("%sdy", trimTrailingZerosShort(float64(sec) / 86400.0))
}
}
type LastScore struct { type LastScore struct {
LastUpdate int LastUpdate int
LastScore int LastScore int
LastIncrement int LastIncrement int
Times int Times int
ScoreOffset int ScoreOffset int
Addiction int
} }
func checkScoreDelta(scoreRows *[]Row, deltaRows *[]Row) *[]Row { func checkScoreDelta(scoreRows *[]Row, deltaRows *[]Row) *[]Row {
@ -143,27 +168,36 @@ func checkScoreDelta(scoreRows *[]Row, deltaRows *[]Row) *[]Row {
scanner := bufio.NewScanner(r) scanner := bufio.NewScanner(r)
for scanner.Scan() { for scanner.Scan() {
logRow := strings.Split(scanner.Text(), "\t") logRow := strings.Split(scanner.Text(), "\t")
uname := strings.TrimSpace(logRow[0]) uname := strings.TrimSpace(logRow[0])
u, exists := users[uname]
asks, _ := strconv.Atoi(strings.TrimSpace(logRow[3])) asks, _ := strconv.Atoi(strings.TrimSpace(logRow[3]))
addiction, _ := strconv.Atoi(strings.TrimSpace(logRow[1]))
u, exists := users[uname]
if !exists { if !exists {
u = LastScore{ ScoreOffset: 0 } u = LastScore{ ScoreOffset: 0 }
} }
u.ScoreOffset = 0 u.ScoreOffset = 0
u.Times = asks u.Times = asks
u.Addiction = addiction
fmt.Println(fmt.Sprintf("%d", u.Times))
users[uname] = u users[uname] = u
} }
// Add in scores data
fmt.Println(fmt.Sprintf("Reading scores data on %d users", len(*scoreRows)))
for i := range *scoreRows { for i := range *scoreRows {
r := (*scoreRows)[i] r := (*scoreRows)[i]
u, exists := users[r.Data[0]] u, exists := users[r.Data[0]]
fmt.Println(u)
score, _ := strconv.Atoi(r.Data[1]) score, _ := strconv.Atoi(r.Data[1])
update, _ := strconv.Atoi(r.Data[2]) update, _ := strconv.Atoi(r.Data[2])
// Fill in any missing users // Fill in any missing users
if !exists { if !exists {
u = LastScore{ LastScore: score, LastIncrement: -1, LastUpdate: update, Times: 0, ScoreOffset: score } u = LastScore{ LastScore: score, LastIncrement: -1, LastUpdate: update, Times: 0, ScoreOffset: score, Addiction: 0 }
users[r.Data[0]] = u users[r.Data[0]] = u
} }
@ -174,6 +208,8 @@ func checkScoreDelta(scoreRows *[]Row, deltaRows *[]Row) *[]Row {
u.LastScore = score u.LastScore = score
u.Times++ u.Times++
} }
r.Data = append(r.Data, niceTime(u.Addiction))
var asksStr string var asksStr string
if u.Times > 0 { if u.Times > 0 {
@ -186,9 +222,7 @@ func checkScoreDelta(scoreRows *[]Row, deltaRows *[]Row) *[]Row {
var avgStr string var avgStr string
if u.Times > 0 { if u.Times > 0 {
avg := float64(score - u.ScoreOffset) / float64(u.Times) avg := float64(score - u.ScoreOffset) / float64(u.Times)
avgStr = fmt.Sprintf("%.2f", avg) avgStr = trimTrailingZeros(avg)
avgStr = strings.TrimRight(avgStr, "0")
avgStr = strings.TrimRight(avgStr, ".")
} else { } else {
avgStr = "-" avgStr = "-"
} }