/
stats-model.go
231 lines (205 loc) · 5.33 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
231
package volunteer
import (
"encoding/json"
"errors"
"strings"
"time"
)
// swagger:model Volunteer_Stat
type Volunteer_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 active volunteer by university
// in: json
// example: {"Lumiere": 1, "SciencePo": 4}
// read only: true
NbActivePerUniversity json.RawMessage `json:"nb_active_per_university"`
// Number of active volunteer by hr status
// in: json
// example: {"volunteer": 1, "employee": 4}
// read only: true
NbActivePerHrstatus json.RawMessage `json:"nb_active_per_hrstatus"`
// Number of new volunteer this month
// in: int
// example: 12
// read only: true
NbNewVlt int `json:"nb_new_vlt_this_month"`
// Number of alumnus
// in: int
// example: 12
// read only: true
NbAlumnus int `json:"nb_alumnus"`
// Number of volunteer in the bureau
// in: int
// example: 12
// read only: true
NbBureau int `json:"nb_bureau"`
// 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 Volunteer_Stats []Volunteer_Stat
func NewMonthlyStat(stat *Volunteer_Stat) {
var err error
stmt, _ := TheDb().Prepare(
"INSERT INTO " + db_name_monthly_stat + `
(archive_date, nb_active_per_university, nb_active_per_hrstatus, nb_new_vlt_this_month,
nb_alumnus, nb_bureau, created_at, updated_at)
VALUES (?,?,?,?,?,?,?,?);`,
)
_, err = stmt.Exec(
stat.ArchiveDate,
stat.NbActivePerUniversity,
stat.NbActivePerHrstatus,
stat.NbNewVlt,
stat.NbAlumnus,
stat.NbBureau,
stat.CreatedAt,
stat.UpdatedAt,
)
if err != nil {
TheLogger().LogInfo(log_name_monthly_stat, "can't create new stat.")
}
}
func UpdateMonthlyStat(stat *Volunteer_Stat) {
stmt, err := TheDb().Prepare(
"UPDATE " + db_name_monthly_stat + ` SET
archive_date=?, nb_active_per_university=?, nb_active_per_hrstatus=?,
nb_new_vlt_this_month=?, nb_alumnus=?, nb_bureau=?, updated_at=?
WHERE id=?;`,
)
if err != nil {
TheLogger().LogError(log_name_monthly_stat, "problem with the db.", err)
}
_, err = stmt.Exec(
stat.ArchiveDate,
stat.NbActivePerUniversity,
stat.NbActivePerHrstatus,
stat.NbNewVlt,
stat.NbAlumnus,
stat.NbBureau,
stat.UpdatedAt,
stat.Id,
)
if err != nil {
TheLogger().LogError(log_name_monthly_stat, "stat can't be updated.", err)
}
}
func AutoNewMonthlyStat() {
var stat Volunteer_Stat
var err error
stat.CreatedAt = time.Now()
stat.UpdatedAt = time.Now()
// Get data of the past
stat.ArchiveDate = stat.CreatedAt.AddDate(0, -1, 0).Format("2006-01")
month_stats := AllVolunteers()
if len(*month_stats) == 0 {
TheLogger().LogWarning(log_name_monthly_stat, "no data for this date.", errors.New("no data"))
return
}
// Merge data
countActiveUniversity := make(map[string]int)
countActiveStatus := make(map[string]int)
countNewVlt := 0
countAlumnus := 0
countBureau := 0
for _, volunteer := range *month_stats {
if volunteer.Actif {
countActiveUniversity[volunteer.University]++
countActiveStatus[volunteer.HrStatus]++
}
if volunteer.HrStatus == "volunteer" {
if strings.Contains(volunteer.StartedDate, stat.ArchiveDate) {
countNewVlt++
}
if !volunteer.Actif {
countAlumnus++
}
if volunteer.Bureau {
countBureau++
}
}
}
stat.NbActivePerUniversity, err = json.Marshal(countActiveUniversity)
if err != nil {
TheLogger().LogError(log_name_monthly_stat, "problem with encoder.", err)
}
stat.NbActivePerHrstatus, err = json.Marshal(countActiveStatus)
if err != nil {
TheLogger().LogError(log_name_monthly_stat, "problem with encoder.", err)
}
stat.NbNewVlt = countNewVlt
stat.NbAlumnus = countAlumnus
stat.NbBureau = countBureau
// 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) *Volunteer_Stat {
var stat Volunteer_Stat
rows := TheDb().QueryRow(
"SELECT * FROM "+db_name_monthly_stat+" WHERE archive_date = ?;",
archive_date,
)
err := rows.Scan(
&stat.Id,
&stat.ArchiveDate,
&stat.NbActivePerUniversity,
&stat.NbActivePerHrstatus,
&stat.NbNewVlt,
&stat.NbAlumnus,
&stat.NbBureau,
&stat.CreatedAt,
&stat.UpdatedAt,
)
if err != nil {
TheLogger().LogInfo(log_name_monthly_stat, "cards not found.")
}
return &stat
}
func AllMonthlyStats() *Volunteer_Stats {
var stats Volunteer_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 Volunteer_Stat
err := rows.Scan(
&stat.Id,
&stat.ArchiveDate,
&stat.NbActivePerUniversity,
&stat.NbActivePerHrstatus,
&stat.NbNewVlt,
&stat.NbAlumnus,
&stat.NbBureau,
&stat.CreatedAt,
&stat.UpdatedAt,
)
if err != nil {
TheLogger().LogInfo(log_name_monthly_stat, "stats not found.")
}
stats = append(stats, stat)
}
return &stats
}