Did some easy refactoring.

This commit is contained in:
A. Svensson 2015-05-21 17:25:08 +02:00
parent 9ff797b539
commit 1d92d6467e

View File

@ -58,26 +58,34 @@ func main() {
for rows.Next() { for rows.Next() {
err := rows.Scan(&id, &title) err := rows.Scan(&id, &title)
checkerror(err) checkerror(err)
creategraphs(db, id, title) creategraphs(db, "week-", id, title)
createweekdaygraph(db, id, title) createweekdaygraph(db, "avg_days-", id, title)
} }
err = rows.Err() err = rows.Err()
checkerror(err) checkerror(err)
} }
func creategraphs(db *sql.DB, id int, title string) { func createtempfile(prefix string) (f *os.File, name string) {
prefix := "week-"
// create a tmp file // create a tmp file
ifile, err := ioutil.TempFile("", prefix) file, err := ioutil.TempFile("", prefix)
checkerror(err) checkerror(err)
defer ifile.Close() return file, file.Name()
ifilename := ifile.Name() }
// Make sure we have somewhere to save the stored graphs in func getgraphpath(title string, prefix string) (path string) {
err = os.MkdirAll(save_dir, 0777) err := os.MkdirAll(save_dir, 0777)
checkerror(err) checkerror(err)
hash := fmt.Sprintf("%x", sha256.Sum256([]byte(title))) hash := fmt.Sprintf("%x", sha256.Sum256([]byte(title)))
ofilename := filepath.Join(save_dir, fmt.Sprintf("%s%s", prefix, hash)) return filepath.Join(save_dir, fmt.Sprintf("%s%s", prefix, hash))
}
func creategraphs(db *sql.DB, prefix string, id int, title string) {
// create a tmp file
ifile, ifilename := createtempfile(prefix)
defer ifile.Close()
// Make sure we have somewhere to save the stored graphs in
ofilename := getgraphpath(title, prefix)
// get the server's data and write it to the file // get the server's data and write it to the file
rows, err := db.Query("select created,players from gameservers_serverhistory where server_id = ? and created >= ? order by created asc", id, last_week) rows, err := db.Query("select created,players from gameservers_serverhistory where server_id = ? and created >= ? order by created asc", id, last_week)
@ -106,19 +114,13 @@ func creategraphs(db *sql.DB, id int, title string) {
os.Remove(ifilename) os.Remove(ifilename)
} }
func createweekdaygraph(db *sql.DB, id int, title string) { func createweekdaygraph(db *sql.DB, prefix string, id int, title string) {
prefix := "avg_days-"
// create a tmp file // create a tmp file
ifile, err := ioutil.TempFile("", prefix) ifile, ifilename := createtempfile(prefix)
checkerror(err)
defer ifile.Close() defer ifile.Close()
ifilename := ifile.Name()
// Make sure we have somewhere to save the stored graphs in // Make sure we have somewhere to save the stored graphs in
err = os.MkdirAll(save_dir, 0777) ofilename := getgraphpath(title, prefix)
checkerror(err)
hash := fmt.Sprintf("%x", sha256.Sum256([]byte(title)))
ofilename := filepath.Join(save_dir, fmt.Sprintf("%s%s", prefix, hash))
// get the server's data and write it to the file // get the server's data and write it to the file
// TODO: Move sunday (first day in list at 0) to the end... // TODO: Move sunday (first day in list at 0) to the end...