generated from fun-stack/example
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstatspage.go
149 lines (123 loc) · 3.92 KB
/
statspage.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"context"
"database/sql"
"encoding/json"
"fmt"
"html/template"
"io"
"net/http"
"time"
"github.com/pkg/errors"
"github.com/johnwarden/httperror"
)
type StatsPageParams struct {
StoryID int `schema:"id,required"`
OptionalModelParams
}
type StatsPageData struct {
StatsPageParams
DefaultPageHeaderData
EstimatedUpvoteRate int
Story
RanksPlotDataJSON template.JS
UpvotesPlotDataJSON template.JS
PenaltyPlotDataJSON template.JS
MaxSampleTime int
}
func (s StatsPageData) MaxSampleTimeISOString() string {
return time.Unix(int64(s.MaxSampleTime), 0).UTC().Format("2006-01-02T15:04")
}
func (s StatsPageData) OriginalSubmissionTimeISOString() string {
return time.Unix(s.OriginalSubmissionTime, 0).UTC().Format("2006-01-02T15:04")
}
func (s StatsPageData) MaxAgeHours() int {
return (s.MaxSampleTime - int(s.OriginalSubmissionTime)) / 3600
}
var ErrStoryIDNotFound = httperror.New(404, "Story ID not found")
func (app app) statsPage(w io.Writer, r *http.Request, params StatsPageParams, userID sql.NullInt64) error {
ndb := app.ndb
s, err := ndb.selectStoryDetails(params.StoryID)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return ErrStoryIDNotFound
}
return err
}
modelParams := params.OptionalModelParams.WithDefaults()
s.UpvoteRate = modelParams.upvoteRate(s.CumulativeUpvotes, s.CumulativeExpectedUpvotes)
s.IsStatsPage = true
d := StatsPageData{
StatsPageParams: params, // pass through any and all URL parameters to the template
EstimatedUpvoteRate: 1.0,
Story: s,
DefaultPageHeaderData: DefaultPageHeaderData{UserID: userID},
}
maxSampleTime, err := maxSampleTime(ndb, params.StoryID)
if err != nil {
return errors.Wrap(err, "maxSampleTime")
}
d.MaxSampleTime = maxSampleTime
if s.Archived {
app.logger.Debug("Loading story data from archive", "storyID", params.StoryID)
err = app.loadStatsDataFromArchive(params.StoryID, &d)
if err != nil {
return errors.Wrap(err, "loading stats data from archive")
}
} else {
ranks, err := rankDatapoints(ndb, params.StoryID)
if err != nil {
return errors.Wrap(err, "rankDatapoints")
}
ranksJson, err := json.Marshal(ranks)
if err != nil {
return errors.Wrap(err, "json.Marshal RanksPlotData")
}
d.RanksPlotDataJSON = template.JS(string(ranksJson))
upvotes, err := upvotesDatapoints(ndb, params.StoryID, modelParams)
if err != nil {
return errors.Wrap(err, "upvotesDatapoints")
}
upvotesJson, err := json.Marshal(upvotes)
if err != nil {
return errors.Wrap(err, "json.Marshal UpvotesPlotData")
}
d.UpvotesPlotDataJSON = template.JS(string(upvotesJson))
}
err = templates.ExecuteTemplate(w, "stats.html.tmpl", d)
return errors.Wrap(err, "executing stats page template")
}
func (app app) loadStatsDataFromArchive(storyID int, d *StatsPageData) error {
// Create storage client
sc, err := NewStorageClient()
if err != nil {
return errors.Wrap(err, "creating storage client")
}
// Download JSON file
filename := fmt.Sprintf("%d.json", storyID)
jsonData, err := sc.DownloadFile(context.Background(), filename)
if err != nil {
return errors.Wrap(err, "downloading JSON file from archive")
}
// Unmarshal JSON data into StatsData struct
var statsData StatsData
err = json.Unmarshal(jsonData, &statsData)
if err != nil {
return errors.Wrap(err, "json.Unmarshal statsData")
}
// Set data into StatsPageData
d.MaxSampleTime = statsData.MaxSampleTime
// Marshal data to JSON strings and assign to template fields
ranksPlotDataJSON, err := json.Marshal(statsData.RanksPlotData)
if err != nil {
return errors.Wrap(err, "json.Marshal RanksPlotData")
}
upvotesPlotDataJSON, err := json.Marshal(statsData.UpvotesPlotData)
if err != nil {
return errors.Wrap(err, "json.Marshal UpvotesPlotData")
}
d.RanksPlotDataJSON = template.JS(ranksPlotDataJSON)
d.UpvotesPlotDataJSON = template.JS(upvotesPlotDataJSON)
// Handle PenaltyPlotData similarly
return nil
}