Skip to content

Commit

Permalink
Get users table hooked up
Browse files Browse the repository at this point in the history
  • Loading branch information
glinscott committed Feb 23, 2018
1 parent df41796 commit 196df22
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
30 changes: 23 additions & 7 deletions go/src/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"server/db"
"strconv"
"strings"
"time"

"github.com/gin-contrib/multitemplate"
"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -241,22 +242,37 @@ func getNetwork(c *gin.Context) {
}

func getActiveUsers() ([]gin.H, error) {
users := []db.User{}
err := db.GetDB().Find(&users).Error
rows, err := db.GetDB().Raw(`SELECT username, training_games.version, training_games.created_at, c.count FROM users
LEFT JOIN training_games
ON users.id = training_games.user_id
AND training_games.id = (SELECT MAX(training_games.id) FROM training_games WHERE training_games.user_id = users.id)
LEFT JOIN (SELECT user_id, count(*)
FROM training_games
WHERE created_at >= now() - INTERVAL '1 day'
GROUP BY user_id) as c
ON c.user_id = training_games.user_id`).Rows()
if err != nil {
return nil, err
}
defer rows.Close()

result := []gin.H{}
for _, user := range users {
for rows.Next() {
var username string
var version int
var created_at time.Time
var count uint64
rows.Scan(&username, &version, &created_at, &count)

result = append(result, gin.H{
"user": user.Username,
"games_hour": 0,
"user": username,
"games_today": count,
"system": "",
"version": "",
"last_updated": user.UpdatedAt,
"version": version,
"last_updated": created_at,
})
}

return result, nil
}

Expand Down
6 changes: 3 additions & 3 deletions go/src/server/templates/index.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<thead>
<tr>
<th>User</th>
<th>Games / Hour</th>
<th>Games / Day</th>
<th>System</th>
<th>Version</th>
<th>Last Updated</th>
Expand All @@ -31,10 +31,10 @@
{{range .Users}}
<tr>
<td><a href="/user/{{.user}}">{{.user}}</a></td>
<td>{{.games_hour}}</td>
<td>{{.games_today}}</td>
<td>{{.system}}</td>
<td>{{.version}}</td>
<td>{{.last_update}}</td>
<td>{{.last_updated}}</td>
</tr>
{{end}}
</tbody>
Expand Down

0 comments on commit 196df22

Please sign in to comment.