This repository has been archived by the owner on Jul 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add per realm stats Adds per realm stats, and a simple visualization of the codes issued and claimed for the past month. * Handle review feedback * More Co-authored-by: Jeremy Faller <jeremy@golang.org>
- Loading branch information
1 parent
19145ac
commit 6f4db71
Showing
12 changed files
with
273 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
{{define "realmstats"}} | ||
|
||
{{$realm := .realm}} | ||
{{$stats := .stats}} | ||
|
||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
{{template "head" .}} | ||
</head> | ||
|
||
<body> | ||
{{template "navbar" .}} | ||
|
||
<main role="main" class="container"> | ||
{{template "flash" .}} | ||
|
||
<h1>Realm stats</h1> | ||
|
||
<div class="card mb-3"> | ||
<div class="card-header">Statistics</div> | ||
<div class="card-body table-responsive"> | ||
{{if $stats}} | ||
<div id="chart" class="mb-3" style="height: 300px;"> | ||
<span>Loading chart...</span> | ||
</div> | ||
<table class="table table-bordered table-striped"> | ||
<thead> | ||
<tr> | ||
<th scope="col">Date</th> | ||
<th scope="col" width="125">Issued</th> | ||
<th scope="col" width="125">Claimed</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
{{range $stat := $stats}} | ||
<tr> | ||
<td>{{$stat.Date.Format "2006-01-02"}}</td> | ||
<td>{{$stat.CodesIssued}}</td> | ||
<td>{{$stat.CodesClaimed}}</td> | ||
</tr> | ||
{{end}} | ||
</tbody> | ||
</table> | ||
<div class="font-italic"> | ||
This data is refreshed every 5 minutes. | ||
</div> | ||
{{else}} | ||
<p>No codes have been issued in this realm.</p> | ||
{{end}} | ||
</div> | ||
</div> | ||
|
||
</main> | ||
|
||
{{template "scripts" .}} | ||
|
||
{{if $stats}} | ||
<script src="https://www.gstatic.com/charts/loader.js"></script> | ||
<script> | ||
google.charts.load('current', {packages: ['line']}); | ||
google.charts.setOnLoadCallback(drawChart) | ||
|
||
function drawChart() { | ||
var data = google.visualization.arrayToDataTable([ | ||
['Date', 'Issued', "Claimed"], | ||
{{range $stat := $stats}} | ||
['{{$stat.Date.Format "Jan 2"}}', {{$stat.CodesIssued}}, {{$stat.CodesClaimed}}], | ||
{{end}} | ||
]); | ||
|
||
var options = { | ||
colors: ['#007bff', '#ff7b00'], | ||
legend: {position: 'bottom'}, | ||
tooltip: {trigger: 'focus'}, | ||
}; | ||
|
||
var chart = new google.charts.Line(document.getElementById('chart')); | ||
chart.draw(data, google.charts.Line.convertOptions(options)); | ||
} | ||
</script> | ||
{{end}} | ||
</body> | ||
</html> | ||
{{end}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright 2020 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package realmadmin | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/google/exposure-notifications-verification-server/pkg/controller" | ||
"github.com/google/exposure-notifications-verification-server/pkg/database" | ||
) | ||
|
||
func (c *Controller) HandleShow() http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
ctx := r.Context() | ||
|
||
realm := controller.RealmFromContext(ctx) | ||
if realm == nil { | ||
controller.MissingRealm(w, r, c.h) | ||
return | ||
} | ||
|
||
// Get and cache the stats for this user. | ||
var stats []*database.RealmStats | ||
cacheKey := fmt.Sprintf("stats:realm:%d", realm.ID) | ||
if err := c.cacher.Fetch(ctx, cacheKey, &stats, 5*time.Minute, func() (interface{}, error) { | ||
now := time.Now().UTC() | ||
past := now.Add(-30 * 24 * time.Hour) | ||
return realm.Stats(c.db, past, now) | ||
}); err != nil { | ||
controller.InternalError(w, r, c.h, err) | ||
return | ||
} | ||
|
||
c.renderStats(ctx, w, realm, stats) | ||
}) | ||
} | ||
|
||
func (c *Controller) renderStats(ctx context.Context, w http.ResponseWriter, realm *database.Realm, stats []*database.RealmStats) { | ||
m := controller.TemplateMapFromContext(ctx) | ||
m["user"] = realm | ||
m["stats"] = stats | ||
c.h.RenderHTML(w, "realmstats", m) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright 2020 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package database | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
// RealmStats represents statistics related to a user in the database. | ||
type RealmStats struct { | ||
Date time.Time `gorm:"date; not null"` | ||
RealmID uint `gorm:"realm_id; not null"` | ||
CodesIssued uint `gorm:"codes_issued; default: 0"` | ||
CodesClaimed uint `gorm:"codes_claimed; default: 0"` | ||
} | ||
|
||
// TableName sets the RealmStats table name | ||
func (RealmStats) TableName() string { | ||
return "realm_stats" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.