mirror of
https://github.com/thebaer/tildes.git
synced 2018-07-20 07:15:21 +00:00
Move scores data file writing to store pkg
This commit is contained in:
parent
66c236ccb3
commit
cdf7edee68
57
store/store.go
Normal file
57
store/store.go
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
package store
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"bufio"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Row struct {
|
||||||
|
Data []string
|
||||||
|
}
|
||||||
|
|
||||||
|
func ReadRows(path, delimiter string) *[]Row {
|
||||||
|
f, _ := os.Open(path)
|
||||||
|
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
rows := []Row{}
|
||||||
|
s := bufio.NewScanner(f)
|
||||||
|
s.Split(bufio.ScanLines)
|
||||||
|
|
||||||
|
for s.Scan() {
|
||||||
|
data := strings.Split(s.Text(), delimiter)
|
||||||
|
r := &Row{Data: data}
|
||||||
|
rows = append(rows, *r)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &rows
|
||||||
|
}
|
||||||
|
|
||||||
|
func WriteData(path, data, delimeter string) {
|
||||||
|
f, err := os.OpenFile(path, os.O_CREATE | os.O_RDWR, 0644)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
_, err = f.WriteString(data)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func WriteRows(path string, rows *[]Row, delimeter string) {
|
||||||
|
f, err := os.OpenFile(path, os.O_CREATE | os.O_RDWR, 0644)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
for _, r := range rows {
|
||||||
|
_, err = f.WriteString(strings.Join(r.Data, delimeter))
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -12,6 +12,8 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
|
"github.com/thebaer/tildes/store"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -41,8 +43,8 @@ func main() {
|
|||||||
|
|
||||||
headers := []string{ "User", "Tildes", "Last Collected", "Addiction", "# Asks", "Avg.", "Last Amt." }
|
headers := []string{ "User", "Tildes", "Last Collected", "Addiction", "# Asks", "Avg.", "Last Amt." }
|
||||||
|
|
||||||
scoresData := readData(scoresPath, "&^%")
|
scoresData := store.ReadRows(scoresPath, "&^%")
|
||||||
updatesData := readData(scoreDeltasPath, deltaDelimiter)
|
updatesData := store.ReadRows(scoreDeltasPath, deltaDelimiter)
|
||||||
|
|
||||||
scoresData = checkScoreDelta(scoresData, updatesData)
|
scoresData = checkScoreDelta(scoresData, updatesData)
|
||||||
scoresTable := buildScoresTable(scoresData, headers)
|
scoresTable := buildScoresTable(scoresData, headers)
|
||||||
@ -52,15 +54,11 @@ func main() {
|
|||||||
|
|
||||||
type Table struct {
|
type Table struct {
|
||||||
Headers []string
|
Headers []string
|
||||||
Rows []Row
|
Rows []store.Row
|
||||||
}
|
}
|
||||||
|
|
||||||
type Row struct {
|
type By func(r1, r2 *store.Row) bool
|
||||||
Data []string
|
func (by By) Sort(rows []store.Row) {
|
||||||
}
|
|
||||||
|
|
||||||
type By func(r1, r2 *Row) bool
|
|
||||||
func (by By) Sort(rows []Row) {
|
|
||||||
rs := &rowSorter {
|
rs := &rowSorter {
|
||||||
rows: rows,
|
rows: rows,
|
||||||
by: by,
|
by: by,
|
||||||
@ -68,8 +66,8 @@ func (by By) Sort(rows []Row) {
|
|||||||
sort.Sort(rs)
|
sort.Sort(rs)
|
||||||
}
|
}
|
||||||
type rowSorter struct {
|
type rowSorter struct {
|
||||||
rows []Row
|
rows []store.Row
|
||||||
by func(r1, r2 *Row) bool
|
by func(r1, r2 *store.Row) bool
|
||||||
}
|
}
|
||||||
func (r *rowSorter) Len() int {
|
func (r *rowSorter) Len() int {
|
||||||
return len(r.rows)
|
return len(r.rows)
|
||||||
@ -82,12 +80,12 @@ func (r *rowSorter) Less(i, j int) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func sortScore(table *Table) *Table {
|
func sortScore(table *Table) *Table {
|
||||||
score := func(r1, r2 *Row) bool {
|
score := func(r1, r2 *store.Row) bool {
|
||||||
s1, _ := strconv.Atoi(r1.Data[1])
|
s1, _ := strconv.Atoi(r1.Data[1])
|
||||||
s2, _ := strconv.Atoi(r2.Data[1])
|
s2, _ := strconv.Atoi(r2.Data[1])
|
||||||
return s1 < s2
|
return s1 < s2
|
||||||
}
|
}
|
||||||
decScore := func(r1, r2 *Row) bool {
|
decScore := func(r1, r2 *store.Row) bool {
|
||||||
return !score(r1, r2)
|
return !score(r1, r2)
|
||||||
}
|
}
|
||||||
By(decScore).Sort(table.Rows)
|
By(decScore).Sort(table.Rows)
|
||||||
@ -136,7 +134,7 @@ type LastScore struct {
|
|||||||
Addiction int
|
Addiction int
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkScoreDelta(scoreRows *[]Row, deltaRows *[]Row) *[]Row {
|
func checkScoreDelta(scoreRows, deltaRows *[]store.Row) *[]store.Row {
|
||||||
users := make(map[string]LastScore)
|
users := make(map[string]LastScore)
|
||||||
|
|
||||||
// Read score delta data
|
// Read score delta data
|
||||||
@ -261,38 +259,20 @@ func checkScoreDelta(scoreRows *[]Row, deltaRows *[]Row) *[]Row {
|
|||||||
return scoreRows
|
return scoreRows
|
||||||
}
|
}
|
||||||
|
|
||||||
func buildScoresTable(rows *[]Row, headers []string) *Table {
|
func buildScoresTable(rows *[]store.Row, headers []string) *Table {
|
||||||
table := &Table{Headers: headers, Rows: nil}
|
t := &Table{Headers: headers, Rows: nil}
|
||||||
|
|
||||||
const layout = "Jan 2, 2006 3:04pm MST"
|
const layout = "Jan 2, 2006 3:04pm MST"
|
||||||
for i, r := range *rows {
|
for i, r := range *rows {
|
||||||
data := r.Data
|
data := r.Data
|
||||||
t := parseTimestamp(r.Data[2])
|
time := parseTimestamp(r.Data[2])
|
||||||
r.Data[2] = t.UTC().Format(layout)
|
r.Data[2] = time.UTC().Format(layout)
|
||||||
row := &Row{Data: data}
|
outRow := &store.Row{Data: data}
|
||||||
(*rows)[i] = *row
|
(*rows)[i] = *outRow
|
||||||
}
|
}
|
||||||
table.Rows = *rows
|
t.Rows = *rows
|
||||||
|
|
||||||
return table
|
return t
|
||||||
}
|
|
||||||
|
|
||||||
func readData(path string, delimiter string) *[]Row {
|
|
||||||
f, _ := os.Open(path)
|
|
||||||
|
|
||||||
defer f.Close()
|
|
||||||
|
|
||||||
rows := []Row{}
|
|
||||||
s := bufio.NewScanner(f)
|
|
||||||
s.Split(bufio.ScanLines)
|
|
||||||
|
|
||||||
for s.Scan() {
|
|
||||||
data := strings.Split(s.Text(), delimiter)
|
|
||||||
row := &Row{Data: data}
|
|
||||||
rows = append(rows, *row)
|
|
||||||
}
|
|
||||||
|
|
||||||
return &rows
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func getFile(path string) string {
|
func getFile(path string) string {
|
||||||
|
Loading…
Reference in New Issue
Block a user