-
Notifications
You must be signed in to change notification settings - Fork 1
/
logbook.go
211 lines (193 loc) · 7.03 KB
/
logbook.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
package main
import (
"fmt"
"time"
)
type Logbook struct {
TimeFrame TimeFrame
Token Token
Entries LogbookEntries
DestFolder string
}
// https://serviceapp.amisgest.ca/8_2/breeze/Breeze/logbook_entry?beginDateTime=2024-01-23T05%3A00%3A00.000Z&endDateTime=2024-01-24T04%3A59%3A59.999Z&ownerPersonId=
type LogbookEntries []*LogbookEntry
type LogbookEntry struct {
ID uint `json:"id"`
OwnerPersonID uint `json:"owner_person_id"`
LogbookEntryStatusID uint `json:"logbook_entry_status_id"`
WriterPersonID uint `json:"writer_person_id"`
CreationDate string `json:"creation_date,omitempty"`
ModificationDate string `json:"modification_date,omitempty"`
PostedDate string `json:"posted_date,omitempty"`
LocaleID string `json:"locale_id"`
Activities Activities
Observations Observations
}
type Activities []*Activity
type Activity struct {
ID uint `json:"id"` // activity id
LogbookEntryID uint `json:"logbook_entry_id"` // coresponding logbook entry
Order uint `json:"order"` // frontend display order
ActivityTypeID uint `json:"activity_type_id"` // activity type id (ex: 1 for "meal", 2 for "diaper", 9 for "info")
Comment string `json:"comment"` // activity comment from the teacher
NumericValue uint `json:"numeric_value"` // associated numeric value with the activity. ex: when activity_type = 1 (0="poor apetite", 3="very good apetite")
ActivityMedias ActivityMedias
}
type ActivityMedias []*ActivityMedia
type ActivityMedia struct {
MediaID uint `json:"media_id"` // media id
ActivityID uint `json:"activity_id"` // activity id
Media Media
}
type Observations []*Observation
type Observation struct {
ID uint `json:"id"` // observation id
LogbookEntryID uint `json:"logbook_entry_id"` // coresponding logbook entry
Order uint `json:"order"` // frontend display order
Text string `json:"text,omitempty"` // comment
IsCompiled bool `json:"is_compiled"` // maybe: true if observation has been sent, false otherwise? (not sure) is it sometimes false?
CompileDate string `json:"compile_date"` // datetime timestamp
ObservationDate string `json:"observation_date"` // datetime
OwnerPersonID uint `json:"owner_person_id"` // kid's ID
WriterPersonID uint `json:"writer_person_id"` // group
ObservationMedias ObservationMedias
}
type ObservationMedias []*ObservationMedia
type ObservationMedia struct {
MediaID uint `json:"media_id"`
ObservationID uint `json:"observation_id"`
Media Media
}
// Search logbook entries by timeframe
func (l *Logbook) Search(timeframe TimeFrame) {
l.TimeFrame = timeframe
l.Entries = l.TimeFrame.getLogbookEntries(l.Token)
l.getActivities()
l.getObservations()
}
// Get all observations and observations medias for each logbook entry
func (l *Logbook) getObservations() {
observations := l.TimeFrame.getObservations(l.Token)
observationMedias := l.TimeFrame.getObservationMedias(l.Token)
// merge observations and observationMedias
for _, entry := range l.Entries {
for _, observation := range observations {
if observation.LogbookEntryID == entry.ID {
for _, observationMedia := range observationMedias {
if observation.ID == observationMedia.ObservationID {
observationMedia.Media.ID = observationMedia.MediaID
observationMedia.Media.getGuid(l.Token)
observation.ObservationMedias = append(observation.ObservationMedias, observationMedia)
}
}
entry.Observations = append(entry.Observations, observation)
}
}
}
}
// Get all observations and observations medias for each logbook entry
func (l *Logbook) getActivities() {
activities := l.TimeFrame.getActivities(l.Token)
activitymedias := l.TimeFrame.getActivityMedia(l.Token)
// merge activities and activityMedias
for _, entry := range l.Entries {
for _, activity := range activities {
if activity.LogbookEntryID == entry.ID {
for _, activityMedia := range activitymedias {
if activity.ID == activityMedia.ActivityID {
activityMedia.Media.ID = activityMedia.MediaID
activityMedia.Media.getGuid(l.Token)
activity.ActivityMedias = append(activity.ActivityMedias, activityMedia)
}
}
entry.Activities = append(entry.Activities, activity)
}
}
}
}
/* Download all pictures from their guids */
func (l *Logbook) DownloadAllPictures() {
fmt.Printf("Downloading %d pictures\n", l.countPictures())
l.downloadActivityPictures()
l.downloadObservationsPictures()
}
// Download all observations attached pictures
func (l *Logbook) downloadObservationsPictures() {
for _, entry := range l.Entries {
for _, observation := range entry.Observations {
for _, observationMedia := range observation.ObservationMedias {
fileDate, _ := time.Parse(time.RFC3339, observation.CompileDate)
// fmt.Println("downloading observation picture", fileDate, observation.Text, observationMedia.Media.ID)
err := observationMedia.Media.GetData(fileDate, l.DestFolder)
if err != nil {
fmt.Println("error downloading observation picture", err)
}
}
}
}
}
// Download all activities attached pictures
func (l *Logbook) downloadActivityPictures() {
for _, entry := range l.Entries {
for _, activity := range entry.Activities {
for _, activityMedia := range activity.ActivityMedias {
fileDate, _ := time.Parse(time.RFC3339, entry.PostedDate)
// fmt.Println("downloading activity picture", fileDate, activity.Comment, activityMedia.Media.ID)
err := activityMedia.Media.GetData(fileDate, l.DestFolder)
if err != nil {
fmt.Println("error downloading activity picture", err)
}
}
}
}
}
/*
Return all infos about the logbook
1. Timeframe
2. Number of entries
3. Number of observations
4. Number of activities
5. Number of pictures
*/
func (l *Logbook) Infos() {
fmt.Printf("Timeframe: %s - %s\n", l.TimeFrame.BeginDateTime, l.TimeFrame.EndDateTime)
fmt.Printf("Number of entries: %d\n", len(l.Entries))
fmt.Printf("Number of observations: %d\n", l.countObservations())
fmt.Printf("Number of activities: %d\n", l.countActivities())
fmt.Printf("Number of pictures: %d\n", l.countPictures())
}
func (l *Logbook) countObservations() int {
count := 0
for _, entry := range l.Entries {
count += len(entry.Observations)
}
return count
}
func (l *Logbook) countActivities() int {
count := 0
for _, entry := range l.Entries {
count += len(entry.Activities)
}
return count
}
func (l *Logbook) countActivityPictures() int {
count := 0
for _, entry := range l.Entries {
for _, activity := range entry.Activities {
count += len(activity.ActivityMedias)
}
}
return count
}
func (l *Logbook) countObservationPictures() int {
count := 0
for _, entry := range l.Entries {
for _, observation := range entry.Observations {
count += len(observation.ObservationMedias)
}
}
return count
}
func (l *Logbook) countPictures() int {
return l.countActivityPictures() + l.countObservationPictures()
}