Do not attempt to make new graphs for servers with no data.

This commit is contained in:
A. Svensson 2015-05-25 12:49:44 +02:00
parent 4483137198
commit 2bd111e568

View File

@ -91,7 +91,10 @@ func weeklyhistorygraph(db *sql.DB, id int, title string) {
prefix := "week-time-" prefix := "week-time-"
// Create a new temp file, get it's filepath and get the final storage path // Create a new temp file, get it's filepath and get the final storage path
ifile, ifilename, ofilename := setuptemppaths(prefix, title) ifile, ifilename, ofilename := setuptemppaths(prefix, title)
defer ifile.Close() defer func() {
ifile.Close()
os.Remove(ifilename)
}()
// Rows for server, newer then LAST_WEEK and only on the hour // Rows for server, newer then LAST_WEEK and only on the hour
rows, err := db.Query("select created,players "+ rows, err := db.Query("select created,players "+
@ -105,6 +108,7 @@ func weeklyhistorygraph(db *sql.DB, id int, title string) {
var ( var (
created time.Time created time.Time
players int players int
gotrow bool = false
) )
// Scan in each row and write it to the tmp file // Scan in each row and write it to the tmp file
for rows.Next() { for rows.Next() {
@ -112,16 +116,17 @@ func weeklyhistorygraph(db *sql.DB, id int, title string) {
checkerror(err) checkerror(err)
_, err = ifile.WriteString(fmt.Sprintf("%d, %d\n", created.Unix(), players)) _, err = ifile.WriteString(fmt.Sprintf("%d, %d\n", created.Unix(), players))
checkerror(err) checkerror(err)
gotrow = true
} }
err = rows.Err() err = rows.Err()
checkerror(err) checkerror(err)
if gotrow == false {
return
}
// run the plotter against the tmp file // run the plotter against the tmp file
runcommand("./plot_time.sh", title, ifilename, ofilename) runcommand("./plot_time.sh", title, ifilename, ofilename)
// close and remove the file
ifile.Close()
os.Remove(ifilename)
} }
func monthlyhistorygraph(db *sql.DB, id int, title string) { func monthlyhistorygraph(db *sql.DB, id int, title string) {
@ -142,17 +147,22 @@ func monthlyhistorygraph(db *sql.DB, id int, title string) {
var ( var (
created time.Time created time.Time
players int players int
gotrow bool = false
) )
for rows.Next() { for rows.Next() {
err := rows.Scan(&created, &players) err := rows.Scan(&created, &players)
checkerror(err) checkerror(err)
_, err = ifile.WriteString(fmt.Sprintf("%d, %d\n", created.Unix(), players)) _, err = ifile.WriteString(fmt.Sprintf("%d, %d\n", created.Unix(), players))
checkerror(err) checkerror(err)
gotrow = true
} }
err = rows.Err() err = rows.Err()
checkerror(err) checkerror(err)
if gotrow {
// run the plotter against the tmp file
runcommand("./plot_time.sh", title, ifilename, ofilename) runcommand("./plot_time.sh", title, ifilename, ofilename)
}
ifile.Close() ifile.Close()
os.Remove(ifilename) os.Remove(ifilename)
@ -175,16 +185,19 @@ func monthlyaveragedaygraph(db *sql.DB, id int, title string) {
day int day int
players float64 players float64
avg_players [7]float64 avg_players [7]float64
gotrow bool = false
) )
for rows.Next() { for rows.Next() {
err := rows.Scan(&day, &players) err := rows.Scan(&day, &players)
checkerror(err) checkerror(err)
avg_players[day] = players avg_players[day] = players
gotrow = true
} }
err = rows.Err() err = rows.Err()
checkerror(err) checkerror(err)
if gotrow {
// Write each day's average to the file // Write each day's average to the file
for i := 1; i <= 6; i++ { for i := 1; i <= 6; i++ {
_, err = ifile.WriteString(fmt.Sprintf("%s, %f\n", WEEK_DAYS[i], avg_players[i])) _, err = ifile.WriteString(fmt.Sprintf("%s, %f\n", WEEK_DAYS[i], avg_players[i]))
@ -196,6 +209,7 @@ func monthlyaveragedaygraph(db *sql.DB, id int, title string) {
// Fucking wankers and their stupid usage of sunday as the first day of week... // Fucking wankers and their stupid usage of sunday as the first day of week...
runcommand("./plot_bar.sh", title, ifilename, ofilename) runcommand("./plot_bar.sh", title, ifilename, ofilename)
}
ifile.Close() ifile.Close()
os.Remove(ifilename) os.Remove(ifilename)