Skip to content

Commit 9268da3

Browse files
authored
Fix/csv (#1170)
* WIP * Fix CSV export * Fix linter errors * Fixed date formatting for range * Fix/tz fix 2 (#1169) * Use completely different TZ in tests * Second fix for wrong timezones of check ins * Bump go version * US/Pacific not working in GitHub runner * Fix linter error * Fix double conversion to UTC without TZ * Fix tests * Fix linter error and remove comments * Fix linter error * Fix too many arguments * WIP * Fix CSV export * Fix linter errors * Fixed date formatting for range * Fix too many arguments * Fix linter error
1 parent ce81714 commit 9268da3

File tree

3 files changed

+57
-31
lines changed

3 files changed

+57
-31
lines changed

api/cmd/api/locations.go

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"fmt"
55
"net/http"
6+
"sort"
67
"strconv"
78

89
httptools "github.com/XDoubleU/essentia/pkg/communication/http"
@@ -110,11 +111,19 @@ func (app *Application) getLocationCheckInsDayHandler(w http.ResponseWriter,
110111
Format(constants.CSVFileNameFormat)
111112
filename = "Day-" + filename
112113

114+
orderedSchoolNames := make([]string, 0, len(valueMap))
115+
for schoolName := range valueMap {
116+
orderedSchoolNames = append(orderedSchoolNames, schoolName)
117+
}
118+
sort.Slice(orderedSchoolNames, func(i, j int) bool {
119+
return orderedSchoolNames[i] < orderedSchoolNames[j]
120+
})
121+
113122
err = httptools.WriteCSV(
114123
w,
115124
filename,
116-
getCSVHeaders(valueMap),
117-
getCSVData(dateStrings, capacities, valueMap),
125+
getCSVHeaders(orderedSchoolNames),
126+
getCSVData(dateStrings, capacities, valueMap, orderedSchoolNames),
118127
)
119128
} else {
120129
err = httptools.WriteJSON(w, http.StatusOK, dtos.CheckInsGraphDto{
@@ -197,11 +206,19 @@ func (app *Application) getLocationCheckInsRangeHandler(
197206
Format(constants.CSVFileNameFormat)
198207
filename = "Range-" + filename
199208

209+
orderedSchoolNames := make([]string, 0, len(valueMap))
210+
for schoolName := range valueMap {
211+
orderedSchoolNames = append(orderedSchoolNames, schoolName)
212+
}
213+
sort.Slice(orderedSchoolNames, func(i, j int) bool {
214+
return orderedSchoolNames[i] < orderedSchoolNames[j]
215+
})
216+
200217
err = httptools.WriteCSV(
201218
w,
202219
filename,
203-
getCSVHeaders(valueMap),
204-
getCSVData(dateStrings, capacities, valueMap),
220+
getCSVHeaders(orderedSchoolNames),
221+
getCSVData(dateStrings, capacities, valueMap, orderedSchoolNames),
205222
)
206223
} else {
207224
err = httptools.WriteJSON(w, http.StatusOK, dtos.CheckInsGraphDto{
@@ -217,16 +234,14 @@ func (app *Application) getLocationCheckInsRangeHandler(
217234
}
218235

219236
func getCSVHeaders(
220-
valueMap map[string][]int,
237+
schoolNames []string,
221238
) []string {
222239
headers := []string{
223240
"datetime",
224241
"capacity",
225242
}
226243

227-
for schoolName := range valueMap {
228-
headers = append(headers, schoolName)
229-
}
244+
headers = append(headers, schoolNames...)
230245

231246
return headers
232247
}
@@ -235,23 +250,26 @@ func getCSVData(
235250
dateStrings []string,
236251
capacities map[string][]int,
237252
valuesPerSchool map[string][]int,
253+
orderedSchoolNames []string,
238254
) [][]string {
239255
var output [][]string
240256

241257
for i, dateString := range dateStrings {
242-
for _, values := range valuesPerSchool {
243-
var entry []string
244-
245-
var totalCapacity int
246-
for _, capacity := range capacities {
247-
totalCapacity += capacity[i]
248-
}
249-
250-
entry = append(entry, dateString)
251-
entry = append(entry, fmt.Sprintf("%d", totalCapacity))
252-
entry = append(entry, strconv.Itoa(values[i]))
253-
output = append(output, entry)
258+
var entry []string
259+
260+
var totalCapacity int
261+
for _, capacity := range capacities {
262+
totalCapacity += capacity[i]
254263
}
264+
265+
entry = append(entry, dateString)
266+
entry = append(entry, fmt.Sprintf("%d", totalCapacity))
267+
268+
for _, schoolName := range orderedSchoolNames {
269+
entry = append(entry, strconv.Itoa(valuesPerSchool[schoolName][i]))
270+
}
271+
272+
output = append(output, entry)
255273
}
256274

257275
return output

api/cmd/api/locations_test.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,11 @@ func TestGetCheckInsLocationRangeCSV(t *testing.T) {
224224
testEnv, testApp := setup(t)
225225
defer testEnv.teardown()
226226

227-
amount := 10
228-
testEnv.createCheckIns(testEnv.fixtures.DefaultLocation, 1, amount)
227+
extraSchool := testEnv.createSchools(1)[0]
228+
229+
amount1, amount2 := 10, 5
230+
testEnv.createCheckIns(testEnv.fixtures.DefaultLocation, 1, amount1)
231+
testEnv.createCheckIns(testEnv.fixtures.DefaultLocation, extraSchool.ID, amount2)
229232

230233
startDate := testApp.getTimeNowUTC().AddDate(0, 0, -1).Format(constants.DateFormat)
231234
endDate := testApp.getTimeNowUTC().AddDate(0, 0, 1).Format(constants.DateFormat)
@@ -255,21 +258,22 @@ func TestGetCheckInsLocationRangeCSV(t *testing.T) {
255258

256259
rsData, _ := httptools.ReadCSV(rs.Body)
257260

258-
expectedHeaders := []string{"datetime", "capacity", "Andere"}
261+
expectedHeaders := []string{"datetime", "capacity", "Andere", extraSchool.Name}
259262

260263
assert.Equal(t, http.StatusOK, rs.StatusCode)
261264
assert.Equal(t, "text/csv", rs.Header.Get("content-type"))
262265
assert.Equal(t, expectedHeaders, rsData[0])
263266
assert.Equal(t, 4, len(rsData))
267+
assert.Equal(t, len(rsData[0]), len(rsData[1]))
264268

265269
// yesterday
266-
fetchedTimeYesterday, _ := time.Parse(time.RFC3339, rsData[1][0])
270+
fetchedTimeYesterday, _ := time.Parse(constants.DateFormat, rsData[1][0])
267271
assert.Equal(t, startDate, fetchedTimeYesterday.Format(constants.DateFormat))
268272
assert.Equal(t, "0", rsData[1][1])
269273
assert.Equal(t, "0", rsData[1][2])
270274

271275
// today
272-
fetchedTimeToday, _ := time.Parse(time.RFC3339, rsData[2][0])
276+
fetchedTimeToday, _ := time.Parse(constants.DateFormat, rsData[2][0])
273277
assert.Equal(
274278
t,
275279
testApp.getTimeNowUTC().Format(constants.DateFormat),
@@ -280,10 +284,11 @@ func TestGetCheckInsLocationRangeCSV(t *testing.T) {
280284
strconv.Itoa(int(testEnv.fixtures.DefaultLocation.Capacity)),
281285
rsData[2][1],
282286
)
283-
assert.Equal(t, strconv.Itoa(amount), rsData[2][2])
287+
assert.Equal(t, strconv.Itoa(amount1), rsData[2][2])
288+
assert.Equal(t, strconv.Itoa(amount2), rsData[2][3])
284289

285290
// tomorrow
286-
fetchedTimeTomorrow, _ := time.Parse(time.RFC3339, rsData[3][0])
291+
fetchedTimeTomorrow, _ := time.Parse(constants.DateFormat, rsData[3][0])
287292
assert.Equal(t, endDate, fetchedTimeTomorrow.Format(constants.DateFormat))
288293
assert.Equal(t, "0", rsData[3][1])
289294
assert.Equal(t, "0", rsData[3][2])

api/internal/services/locations.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/XDoubleU/essentia/pkg/grapher"
1212
timetools "github.com/XDoubleU/essentia/pkg/time"
1313

14+
"check-in/api/internal/constants"
1415
"check-in/api/internal/dtos"
1516
"check-in/api/internal/models"
1617
"check-in/api/internal/repositories"
@@ -127,14 +128,16 @@ func (service LocationService) GetCheckInsEntriesRange(
127128
g := grapher.New[int](
128129
grapher.CumulativeSameDate,
129130
grapher.None,
130-
time.RFC3339,
131-
time.Second,
131+
constants.DateFormat,
132+
//nolint:mnd //granularity of days
133+
24*time.Hour,
132134
)
133135
capacitiesGrapher := grapher.New[int](
134136
grapher.Normal,
135137
grapher.None,
136-
time.RFC3339,
137-
time.Second,
138+
constants.DateFormat,
139+
//nolint:mnd //granularity of days
140+
24*time.Hour,
138141
)
139142

140143
for i := startDate; i.Before(endDate); i = i.AddDate(0, 0, 1) {

0 commit comments

Comments
 (0)