feat:#22 refactor to cache room schedule and webworker config

This commit is contained in:
survellow
2024-08-02 23:55:58 +02:00
parent e4bde20397
commit 4fcfa76e1c
14 changed files with 164 additions and 83 deletions

View File

@ -29,6 +29,7 @@ type RoomOccupancy struct {
type RoomOccupancyList struct {
Start time.Time `bson:"start"`
Updated time.Time `bson:"updated"`
Granularity int `bson:"granularity"`
Blocks int `bson:"blocks"`
Rooms []RoomOccupancy `bson:"rooms"`

View File

@ -119,21 +119,6 @@ paths:
/api/schedule/rooms:
get:
summary: Get Room Occupancy
parameters:
- name: from
in: query
description: date
example: "2024-12-24T00:00:00.000Z"
required: true
schema:
type: string
- name: to
in: query
description: date
example: "2024-12-25T00:00:00.000Z"
required: true
schema:
type: string
responses:
'200':
description: Successful response
@ -294,6 +279,9 @@ components:
start:
type: string
format: date-time
updated:
type: string
format: date-time
granularity:
type: integer
blocks:
@ -306,9 +294,9 @@ components:
occupancy:
type: string
format: binary
required:
- name
- occupancy
required:
- name
- occupancy
required:
- start
- granularity

View File

@ -181,9 +181,7 @@ func AddRoutes(app *pocketbase.PocketBase) {
Method: http.MethodGet,
Path: "/api/schedule/rooms",
Handler: func(c echo.Context) error {
from := c.QueryParam("from")
to := c.QueryParam("to")
rooms, err := room.GetRoomOccupancyList(app, from, to, RoomOccupancyGranularity)
rooms, err := room.GetRoomOccupancyList(app, RoomOccupancyGranularity)
if err != nil {
slog.Error("Failed to get room occupancy: %v", "error", err)

View File

@ -16,15 +16,41 @@
package functions
import "time"
import (
"time"
)
const START_OF_SUMMER_SEMESTER_MONTH = time.April
const START_OF_WINTER_SEMESTER_MONTH = time.October
// GetCurrentSemesterString returns the current semester as string
// if current month is between 10 and 03 -> winter semester "ws"
func GetCurrentSemesterString() string {
if time.Now().Month() >= 10 || time.Now().Month() <= 3 {
if now := time.Now(); isBeforeSummerSemester(now) || isAfterSummerSemester(now) {
return "ws"
} else {
return "ss"
}
}
// GetSemesterStart gibt das Startdatum des aktuellen Semesters zurück
func GetSemesterStart(date time.Time) time.Time {
if isBeforeSummerSemester(date) {
return time.Date(date.Year()-1, START_OF_WINTER_SEMESTER_MONTH, 1, 0, 0, 0, 0, date.Location())
} else if isAfterSummerSemester(date) {
return time.Date(date.Year(), START_OF_WINTER_SEMESTER_MONTH, 1, 0, 0, 0, 0, date.Location())
} else {
return time.Date(date.Year(), START_OF_SUMMER_SEMESTER_MONTH, 1, 0, 0, 0, 0, date.Location())
}
}
// Check if the given date is before the start of summer semester
func isBeforeSummerSemester(date time.Time) bool {
return date.Month() < START_OF_SUMMER_SEMESTER_MONTH
}
// Check if the given date is after the end of summer semester
func isAfterSummerSemester(date time.Time) bool {
return date.Month() >= START_OF_WINTER_SEMESTER_MONTH
}

View File

@ -68,16 +68,11 @@ func GetRoomSchedule(app *pocketbase.PocketBase, room string, from string, to st
* @return room occupancy list
* @return error if the database query fails
*/
func GetRoomOccupancyList(app *pocketbase.PocketBase, from string, to string, granularity int) (model.RoomOccupancyList, error) {
// try parsing the time strings
fromTime, err := time.Parse(time.RFC3339, from)
if err != nil {
return model.RoomOccupancyList{}, err
}
toTime, err := time.Parse(time.RFC3339, to)
if err != nil {
return model.RoomOccupancyList{}, err
}
func GetRoomOccupancyList(app *pocketbase.PocketBase, granularity int) (model.RoomOccupancyList, error) {
now := time.Now()
fromTime := functions.GetSemesterStart(now)
toTime := functions.GetSemesterStart(now.AddDate(0, 6, 0))
// calculate the number of blocks for the given time range and granularity
timeDifference := toTime.Sub(fromTime)
@ -142,6 +137,7 @@ func getRelevantRooms(app *pocketbase.PocketBase) ([]string, error) {
func emptyRoomOccupancyList(from time.Time, granularity int, blockCount int) model.RoomOccupancyList {
return model.RoomOccupancyList{
Start: from,
Updated: time.Now(),
Granularity: granularity,
Blocks: blockCount,
Rooms: []model.RoomOccupancy{},