Skip to content

Commit

Permalink
Stats page
Browse files Browse the repository at this point in the history
  • Loading branch information
aceberg committed Feb 25, 2024
1 parent 6e8f637 commit b624551
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 0 deletions.
13 changes: 13 additions & 0 deletions internal/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ type Check struct {
Count int `db:"COUNT"`
}

// Stat - statistic
type Stat struct {
Name string
Group string
DWeek int
DMonth int
DTotal int
CWeek int
CMonth int
CTotal int
}

// GuiData - web gui data
type GuiData struct {
Config Conf
Expand All @@ -44,4 +56,5 @@ type GuiData struct {
OnePlan Plan
Plans []Plan
Checks []Check
Stats map[string]Stat
}
45 changes: 45 additions & 0 deletions internal/web/stats.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package web

import (
// "log"
"net/http"

"github.com/gin-gonic/gin"

"github.com/aceberg/ClickAHabit/internal/db"
"github.com/aceberg/ClickAHabit/internal/models"
)

func statsHandler(c *gin.Context) {
var guiData models.GuiData
var key string

allChecks = db.Select(appConfig.DBPath)
guiData.Config = appConfig

statsMap := make(map[string]models.Stat)

for _, check := range allChecks {
if check.Count != 0 {
key = check.Group + ": " + check.Name
stat, exists := statsMap[key]

if exists {
stat.DTotal = stat.DTotal + 1
stat.CTotal = stat.CTotal + check.Count
} else {
stat.Name = check.Name
stat.Group = check.Group
stat.DTotal = 1
stat.CTotal = check.Count
}

statsMap[key] = stat
}
}

guiData.Stats = statsMap

c.HTML(http.StatusOK, "header.html", guiData)
c.HTML(http.StatusOK, "stats.html", guiData)
}
3 changes: 3 additions & 0 deletions internal/web/templates/header.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@
<li class="nav-item">
<a class="nav-link active" href="/history/today">History</a>
</li>
<li class="nav-item">
<a class="nav-link active" href="/stats/">Stats</a>
</li>
<li class="nav-item">
<a class="nav-link active" href="/config/">Config</a>
</li>
Expand Down
33 changes: 33 additions & 0 deletions internal/web/templates/stats.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{{ define "stats.html" }}

<body>
<div class="container-lg mt-3">
<div class="card border-primary">
<div class="card-header">
<h5>Stats</h5>
</div>
<div class="card-body table-responsive">
<table class="table table-striped table-hover">
<thead>
<th>Group</th>
<th>Name</th>
<th>Days Total</th>
<th>Count Total</th>
</thead>
<tbody>
{{ range .Stats }}
<tr>
<td>{{ .Group }}</td>
<td>{{ .Name }}</td>
<td>{{ .DTotal }}</td>
<td>{{ .CTotal }}</td>
</tr>
{{ end }}
</tbody>
</table>
</div>
</div>
</div>

{{ template "footer.html" }}
{{ end }}
1 change: 1 addition & 0 deletions internal/web/webgui.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func Gui(dirPath, nodePath string) {
router.GET("/plan/", planHandler) // plan.go
router.GET("/planedit/:id", editHandler) // plan-edit.go
router.GET("/plandel/:id", planDel) // plan.go
router.GET("/stats/", statsHandler) // stats.go
router.GET("/update/:date", updatePlan) // update.go

router.POST("/config/", saveConfigHandler) // config.go
Expand Down

0 comments on commit b624551

Please sign in to comment.