/
stats-model.go
230 lines (205 loc) · 5.39 KB
/
stats-model.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package planning
import (
"encoding/json"
"errors"
"math"
"time"
)
// swagger:model Planning_Stat
type Planning_Stat struct {
// Id of the entry
// in: int
// read only: true
Id int `json:"id"`
// Archive date of the stat
// in: string
// example: 2023-01
// required: true
ArchiveDate string `json:"archive_date"`
// Number of planning by location
// in: json
// example: {"MDE": 12, "MDEL": 4}
// read only: true
NbPerLocation json.RawMessage `json:"nb_per_location"`
// Number of planning by type
// in: json
// example: {"permanence": 12, "event": 4}
// read only: true
NbPerType json.RawMessage `json:"nb_per_type"`
// Number of planning total
// in: int
// example: 12
// read only: true
NbTotal int `json:"nb_total"`
// Average attendee per type of planning
// in: json
// example: {"event": 12.5, "permanence": 5}
// read only: true
AvgAttPerType json.RawMessage `json:"nb_avg_att_per_type"`
// Number of planning attendees total
// in: int
// example: 12
// read only: true
NbAttTotal int `json:"nb_att_total"`
// CreatedAt date of the stat
// in: time.Time
// read only: true
CreatedAt time.Time `json:"created_at"`
// UpdatedAt date of the stat
// in: time.Time
// read only: true
UpdatedAt time.Time `json:"updated_at"`
}
type Planning_Stats []Planning_Stat
func NewMonthlyStat(stat *Planning_Stat) {
var err error
stmt, _ := TheDb().Prepare(
"INSERT INTO " + db_name_monthly_stat + `
(archive_date, nb_per_location, nb_per_type, nb_total, nb_avg_att_per_type,
nb_att_total, created_at, updated_at)
VALUES (?,?,?,?,?,?,?,?);`,
)
_, err = stmt.Exec(
stat.ArchiveDate,
stat.NbPerLocation,
stat.NbPerType,
stat.NbTotal,
stat.AvgAttPerType,
stat.NbAttTotal,
stat.CreatedAt,
stat.UpdatedAt,
)
if err != nil {
TheLogger().LogInfo(log_name_monthly_stat, "can't create new stat.")
}
}
func UpdateMonthlyStat(stat *Planning_Stat) {
stmt, err := TheDb().Prepare(
"UPDATE " + db_name_monthly_stat + ` SET
archive_date=?, nb_per_location=?, nb_per_type=?, nb_total=?, nb_avg_att_per_type=?,
nb_att_total=?, updated_at=?
WHERE id=?;`,
)
if err != nil {
TheLogger().LogError(log_name_monthly_stat, "problem with the db.", err)
}
_, err = stmt.Exec(
stat.ArchiveDate,
stat.NbPerLocation,
stat.NbPerType,
stat.NbTotal,
stat.AvgAttPerType,
stat.NbAttTotal,
stat.UpdatedAt,
stat.Id,
)
if err != nil {
TheLogger().LogError(log_name_monthly_stat, "stat can't be updated.", err)
}
}
func AutoNewMonthlyStat(archive_date ...string) {
var stat Planning_Stat
var err error
stat.CreatedAt = time.Now()
stat.UpdatedAt = time.Now()
// Get data of the past
if archive_date != nil {
stat.ArchiveDate = archive_date[0]
} else {
stat.ArchiveDate = stat.CreatedAt.AddDate(0, -1, 0).Format("2006-01")
}
month_stats := FindPlanningsByDate(stat.ArchiveDate)
if len(*month_stats) == 0 {
TheLogger().LogWarning(log_name_monthly_stat, "no data for this date.", errors.New("no data"))
return
}
// Merge data
countLocation := make(map[string]int)
countType := make(map[string]int)
avgAtt := make(map[string]float64)
avgAttType := make(map[string]int)
countAttTotal := 0
for _, planning := range *month_stats {
atts := FindAttendeeByPlanningId(planning.Id)
countLocation[planning.Location]++
countType[planning.Type]++
avgAtt[planning.Type] += float64(len(*atts))
countAttTotal += len(*atts)
}
for key, value := range countType {
avgAttType[key] = int(math.RoundToEven(avgAtt[key] / float64(value)))
}
stat.NbPerLocation, err = json.Marshal(countLocation)
if err != nil {
TheLogger().LogError(log_name_monthly_stat, "problem with encoder.", err)
}
stat.NbPerType, err = json.Marshal(countType)
if err != nil {
TheLogger().LogError(log_name_monthly_stat, "problem with encoder.", err)
}
stat.NbTotal = len(*month_stats)
stat.AvgAttPerType, err = json.Marshal(avgAttType)
if err != nil {
TheLogger().LogError(log_name_monthly_stat, "problem with encoder.", err)
}
stat.NbAttTotal = countAttTotal
// Add data into db
previous_gen := FindMonthlyStatByDate(stat.ArchiveDate)
if previous_gen.Id != 0 {
TheLogger().LogInfo(log_name_monthly_stat, "this date has already been generated.")
stat.Id = previous_gen.Id
UpdateMonthlyStat(&stat)
} else {
NewMonthlyStat(&stat)
}
}
func FindMonthlyStatByDate(archive_date string) *Planning_Stat {
var stat Planning_Stat
rows := TheDb().QueryRow(
"SELECT * FROM "+db_name_monthly_stat+" WHERE archive_date = ?;",
archive_date,
)
err := rows.Scan(
&stat.Id,
&stat.ArchiveDate,
&stat.NbPerLocation,
&stat.NbPerType,
&stat.NbTotal,
&stat.AvgAttPerType,
&stat.NbAttTotal,
&stat.CreatedAt,
&stat.UpdatedAt,
)
if err != nil {
TheLogger().LogInfo(log_name_monthly_stat, "cards not found.")
}
return &stat
}
func AllMonthlyStats() *Planning_Stats {
var stats Planning_Stats
rows, err := TheDb().Query("SELECT * FROM " + db_name_monthly_stat)
if err != nil {
TheLogger().LogError(log_name_monthly_stat, "problem with the db.", err)
}
// Close rows after all readed
defer rows.Close()
for rows.Next() {
var stat Planning_Stat
err := rows.Scan(
&stat.Id,
&stat.ArchiveDate,
&stat.NbPerLocation,
&stat.NbPerType,
&stat.NbTotal,
&stat.AvgAttPerType,
&stat.NbAttTotal,
&stat.CreatedAt,
&stat.UpdatedAt,
)
if err != nil {
TheLogger().LogInfo(log_name_monthly_stat, "stats not found.")
}
stats = append(stats, stat)
}
return &stats
}