mirror of
https://gitlab.dit.htwk-leipzig.de/htwk-software/htwkalender-pwa.git
synced 2025-08-14 07:33:53 +02:00
Merge remote-tracking branch 'htwk-org/development'
# Conflicts: # backend/go.mod # frontend/index.html # frontend/package-lock.json # frontend/package.json # frontend/public/themes/lara-dark-blue/theme.css # frontend/public/themes/lara-dark-blue/theme.css.map # frontend/public/themes/lara-light-blue/theme.css # frontend/public/themes/lara-light-blue/theme.css.map # frontend/src/App.vue # frontend/src/components/DarkModeSwitcher.vue # frontend/src/i18n/index.ts # frontend/src/main.ts # frontend/src/router/index.ts # frontend/src/view/CalendarLink.vue # frontend/src/view/edit/EditCalendar.vue # frontend/vite.config.ts # reverseproxy.conf # reverseproxy.local.conf # services/data-manager/main.go # services/data-manager/model/roomOccupancyModel.go # services/data-manager/service/addRoute.go # services/data-manager/service/addSchedule.go # services/data-manager/service/db/dbGroups.go # services/data-manager/service/feed/feedFunctions.go # services/data-manager/service/fetch/sport/sportFetcher.go # services/data-manager/service/fetch/v1/fetchSeminarEventService.go # services/data-manager/service/fetch/v1/fetchSeminarGroupService.go # services/data-manager/service/fetch/v2/fetcher.go # services/data-manager/service/functions/filter.go # services/data-manager/service/functions/filter_test.go # services/data-manager/service/functions/time/parse.go # services/data-manager/service/room/roomService.go # services/data-manager/service/room/roomService_test.go # services/go.sum # services/ical/service/connector/grpc/client.go
This commit is contained in:
295
services/data-manager/service/room/roomService.go
Normal file
295
services/data-manager/service/room/roomService.go
Normal file
@@ -0,0 +1,295 @@
|
||||
//Calendar implementation for the HTWK Leipzig timetable. Evaluation and display of the individual dates in iCal format.
|
||||
//Copyright (C) 2024 HTWKalender support@htwkalender.de
|
||||
|
||||
//This program is free software: you can redistribute it and/or modify
|
||||
//it under the terms of the GNU Affero General Public License as published by
|
||||
//the Free Software Foundation, either version 3 of the License, or
|
||||
//(at your option) any later version.
|
||||
|
||||
//This program is distributed in the hope that it will be useful,
|
||||
//but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
//GNU Affero General Public License for more details.
|
||||
|
||||
//You should have received a copy of the GNU Affero General Public License
|
||||
//along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package room
|
||||
|
||||
import (
|
||||
"github.com/pocketbase/pocketbase"
|
||||
"htwkalender/data-manager/model"
|
||||
"htwkalender/data-manager/service/db"
|
||||
"htwkalender/data-manager/service/functions"
|
||||
"htwkalender/model"
|
||||
"htwkalender/service/db"
|
||||
"htwkalender/service/functions"
|
||||
"math"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/pocketbase/pocketbase"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
// maximum number of blocks is around 6 months with 15 minute granularity (180 * 24 * 4 = 17280)
|
||||
const MaxNumberOfBlocks = 1728000
|
||||
|
||||
func GetRooms(app *pocketbase.PocketBase) ([]string, error) {
|
||||
rooms, err := db.GetRooms(app)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return rooms, nil
|
||||
}
|
||||
}
|
||||
|
||||
func GetRoomScheduleForDay(app *pocketbase.PocketBase, room string, date string) ([]model.AnonymizedEventDTO, error) {
|
||||
roomSchedule, err := db.GetRoomScheduleForDay(app, room, date)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
anonymizedRoomSchedule := anonymizeRooms(roomSchedule)
|
||||
return anonymizedRoomSchedule, nil
|
||||
}
|
||||
|
||||
func GetRoomSchedule(app *pocketbase.PocketBase, room string, from string, to string) ([]model.AnonymizedEventDTO, error) {
|
||||
roomSchedule, err := db.GetRoomSchedule(app, room, from, to)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
anonymizedRoomSchedule := anonymizeRooms(roomSchedule)
|
||||
return anonymizedRoomSchedule, nil
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the room occupancy for all rooms within a given time range
|
||||
* @param app pocketbase instance
|
||||
* @param from start time of the occupancy list
|
||||
* @param to end time of the occupancy list
|
||||
* @param granularity number of minutes for one block
|
||||
* @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
|
||||
}
|
||||
|
||||
// calculate the number of blocks for the given time range and granularity
|
||||
timeDifference := toTime.Sub(fromTime)
|
||||
numberOfBlocks := int(math.Ceil(timeDifference.Minutes() / float64(granularity)))
|
||||
|
||||
numberOfBlocks = min(numberOfBlocks, MaxNumberOfBlocks)
|
||||
|
||||
roomOccupancyList := emptyRoomOccupancyList(fromTime, granularity, numberOfBlocks)
|
||||
|
||||
// get all events within the time range
|
||||
events, err := db.GetEventsThatCollideWithTimeRange(app, fromTime, toTime)
|
||||
if err != nil {
|
||||
return model.RoomOccupancyList{}, err
|
||||
}
|
||||
|
||||
rooms, err := getRelevantRooms(app)
|
||||
if err != nil {
|
||||
return model.RoomOccupancyList{}, err
|
||||
}
|
||||
|
||||
for _, room := range rooms {
|
||||
// get the schedule for only this room
|
||||
roomEvents := functions.Filter(events, func(event model.Event) bool {
|
||||
return strings.Contains(event.Rooms, room)
|
||||
})
|
||||
|
||||
// encode the room schedule binary and add it to the list
|
||||
roomOccupancy, err := createRoomOccupancy(room, roomEvents, fromTime, granularity, numberOfBlocks)
|
||||
if err != nil {
|
||||
return model.RoomOccupancyList{}, err
|
||||
}
|
||||
roomOccupancyList.Rooms = append(roomOccupancyList.Rooms, roomOccupancy)
|
||||
}
|
||||
|
||||
return roomOccupancyList, nil
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all rooms from the database and filter them by a regex
|
||||
* @param app pocketbase instance
|
||||
* @return all rooms that match the regex
|
||||
* @return error if the database query fails
|
||||
*/
|
||||
func getRelevantRooms(app *pocketbase.PocketBase) ([]string, error) {
|
||||
// get all rooms
|
||||
rooms, err := db.GetRooms(app)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// filter by regex for the room name
|
||||
roomRegex := regexp.MustCompile(".*")
|
||||
return functions.Filter(rooms, roomRegex.MatchString), nil
|
||||
}
|
||||
|
||||
/*
|
||||
* Create an empty room occupancy list
|
||||
* @param from start time of the occupancy list
|
||||
* @param granularity number of minutes for one block
|
||||
* @param blockCount number of blocks that can be either occupied or free
|
||||
*/
|
||||
func emptyRoomOccupancyList(from time.Time, granularity int, blockCount int) model.RoomOccupancyList {
|
||||
return model.RoomOccupancyList{
|
||||
Start: from,
|
||||
Granularity: granularity,
|
||||
Blocks: blockCount,
|
||||
Rooms: []model.RoomOccupancy{},
|
||||
}
|
||||
}
|
||||
|
||||
/*+
|
||||
* Create the room occupancy for a given room
|
||||
* @param room room name
|
||||
* @param events events that could block the room (or be free)
|
||||
* @param start start time of the schedule
|
||||
* @param granularity number of minutes for one block
|
||||
* @param blockCount number of blocks
|
||||
* @return room occupancy for the given room
|
||||
* @return error if encoding the room schedule fails
|
||||
*/
|
||||
func createRoomOccupancy(room string, events []model.Event, start time.Time, granularity int, blockCount int) (model.RoomOccupancy, error) {
|
||||
roomSchedule := anonymizeRooms(events)
|
||||
occupancy, err := encodeRoomSchedule(roomSchedule, start, granularity, blockCount)
|
||||
|
||||
if err != nil {
|
||||
return model.RoomOccupancy{}, err
|
||||
}
|
||||
|
||||
return model.RoomOccupancy{
|
||||
Name: room,
|
||||
Occupancy: primitive.Binary{
|
||||
Subtype: 0,
|
||||
Data: occupancy,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
/*
|
||||
* Transform the events to anonymized events throwing away all unnecessary information
|
||||
* @param events events to be anonymized
|
||||
* @see Event.AnonymizeEvent
|
||||
* @return anonymized events
|
||||
*/
|
||||
func anonymizeRooms(events []model.Event) []model.AnonymizedEventDTO {
|
||||
var anonymizedEvents []model.AnonymizedEventDTO
|
||||
for _, event := range events {
|
||||
anonymizedEvents = append(anonymizedEvents, event.AnonymizeEvent())
|
||||
}
|
||||
return anonymizedEvents
|
||||
}
|
||||
|
||||
func GetFreeRooms(app *pocketbase.PocketBase, from time.Time, to time.Time) ([]string, error) {
|
||||
rooms, err := db.GetRooms(app)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var events model.Events
|
||||
events, err = db.GetEventsThatCollideWithTimeRange(app, from, to)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
freeRooms := removeRoomsThatHaveEvents(rooms, events)
|
||||
return freeRooms, nil
|
||||
}
|
||||
|
||||
// Remove all rooms from the list that have events in the given time range
|
||||
func removeRoomsThatHaveEvents(rooms []string, schedule []model.Event) []string {
|
||||
var freeRooms []string
|
||||
for _, room := range rooms {
|
||||
if !isRoomInSchedule(room, schedule) {
|
||||
freeRooms = append(freeRooms, room)
|
||||
}
|
||||
}
|
||||
return freeRooms
|
||||
}
|
||||
|
||||
// Check if a room is in the schedule
|
||||
func isRoomInSchedule(room string, schedule []model.Event) bool {
|
||||
for _, event := range schedule {
|
||||
if event.Course != "Sport" {
|
||||
rooms := functions.SeperateRoomString(event.Rooms)
|
||||
// check if room is in rooms
|
||||
for _, r := range rooms {
|
||||
if r == room {
|
||||
return true
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if event.Rooms == room {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode the room schedule to a byte array
|
||||
*
|
||||
* @param roomSchedule events that block the room
|
||||
* @param start start time of the schedule
|
||||
* @param granularity number of minutes for one block
|
||||
* @param blockCount number of blocks
|
||||
*
|
||||
* @return byte array of the encoded room schedule
|
||||
* @return error if encoding fails
|
||||
*/
|
||||
func encodeRoomSchedule(roomSchedule []model.AnonymizedEventDTO, start time.Time, granularity int, blockCount int) ([]byte, error) {
|
||||
// Create empty occupancy array with blockCount bits
|
||||
byteCount := int(math.Ceil(float64(blockCount) / 8))
|
||||
occupancy := make([]byte, byteCount)
|
||||
|
||||
// Iterate over all events in the schedule
|
||||
for _, event := range roomSchedule {
|
||||
// skip if room is not occupied or end time is not after start time
|
||||
if event.Free || !event.Start.Time().Before(event.End.Time()) {
|
||||
continue
|
||||
}
|
||||
|
||||
// Calculate the start and end block of the event
|
||||
startBlock := int(
|
||||
math.Floor(event.Start.Time().Sub(start).Minutes() / float64(granularity)),
|
||||
)
|
||||
endBlock := int(
|
||||
math.Ceil(event.End.Time().Sub(start).Minutes() / float64(granularity)),
|
||||
)
|
||||
|
||||
occupyBlocks(occupancy, startBlock, endBlock, blockCount)
|
||||
}
|
||||
|
||||
return occupancy, nil
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the bits of the occupancy array for the given block range
|
||||
* to 1
|
||||
*
|
||||
* @param occupancy byte array of the occupancy
|
||||
* @param startBlock start block (bit defined by granularity) of the event
|
||||
* @param endBlock end block of the event
|
||||
* @param blockCount number of blocks (bits) in the occupancy array
|
||||
*/
|
||||
func occupyBlocks(occupancy []byte, startBlock int, endBlock int, blockCount int) {
|
||||
lowerBound := max(0, min(startBlock, blockCount))
|
||||
upperBound := min(max(endBlock, lowerBound), blockCount)
|
||||
|
||||
// Iterate over all blocks of the event
|
||||
for i := lowerBound; i < upperBound; i++ {
|
||||
occupancy[i/8] |= 1 << (7 - i%8)
|
||||
}
|
||||
}
|
625
services/data-manager/service/room/roomService_test.go
Normal file
625
services/data-manager/service/room/roomService_test.go
Normal file
@@ -0,0 +1,625 @@
|
||||
//Calendar implementation for the HTWK Leipzig timetable. Evaluation and display of the individual dates in iCal format.
|
||||
//Copyright (C) 2024 HTWKalender support@htwkalender.de
|
||||
|
||||
//This program is free software: you can redistribute it and/or modify
|
||||
//it under the terms of the GNU Affero General Public License as published by
|
||||
//the Free Software Foundation, either version 3 of the License, or
|
||||
//(at your option) any later version.
|
||||
|
||||
//This program is distributed in the hope that it will be useful,
|
||||
//but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
//GNU Affero General Public License for more details.
|
||||
|
||||
//You should have received a copy of the GNU Affero General Public License
|
||||
//along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
package room
|
||||
|
||||
import (
|
||||
"htwkalender/data-manager/model"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/pocketbase/pocketbase/tools/types"
|
||||
)
|
||||
|
||||
func TestAnonymizeRooms(t *testing.T) {
|
||||
type args struct {
|
||||
events []model.Event
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want []model.AnonymizedEventDTO
|
||||
}{
|
||||
{
|
||||
name: "anonymize single event",
|
||||
args: args{
|
||||
events: []model.Event{
|
||||
{
|
||||
UUID: "testUUID",
|
||||
Day: "Montag",
|
||||
Week: "52",
|
||||
Start: types.DateTime{},
|
||||
End: types.DateTime{},
|
||||
Name: "Secret",
|
||||
EventType: "V",
|
||||
Prof: "Prof. Dr. Secret",
|
||||
Rooms: "Room",
|
||||
Notes: "Secret",
|
||||
BookedAt: "Secret",
|
||||
Course: "42INM-3",
|
||||
Semester: "ws",
|
||||
Compulsory: "p",
|
||||
},
|
||||
},
|
||||
},
|
||||
want: []model.AnonymizedEventDTO{
|
||||
{
|
||||
Day: "Montag",
|
||||
Week: "52",
|
||||
Start: types.DateTime{},
|
||||
End: types.DateTime{},
|
||||
Rooms: "Room",
|
||||
Free: false,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "anonymize empty list",
|
||||
args: args{
|
||||
events: []model.Event{},
|
||||
},
|
||||
want: nil,
|
||||
},
|
||||
{
|
||||
name: "anonymize multiple events",
|
||||
args: args{
|
||||
events: []model.Event{
|
||||
{
|
||||
UUID: "testUUID1",
|
||||
Day: "Montag",
|
||||
Week: "51",
|
||||
Start: types.DateTime{},
|
||||
End: types.DateTime{},
|
||||
Name: "Incognito",
|
||||
EventType: "V",
|
||||
Prof: "Prof. Dr. Incognito",
|
||||
Rooms: "Room",
|
||||
Notes: "Incognito",
|
||||
BookedAt: "Incognito",
|
||||
Course: "69INM-2",
|
||||
Semester: "sose",
|
||||
Compulsory: "p",
|
||||
},
|
||||
{
|
||||
UUID: "testUUID2",
|
||||
Day: "Dienstag",
|
||||
Week: "52",
|
||||
Start: types.DateTime{},
|
||||
End: types.DateTime{},
|
||||
Name: "Private",
|
||||
EventType: "S",
|
||||
Prof: "Prof.In. Dr.-Ing. Private",
|
||||
Rooms: "Room",
|
||||
Notes: "Private",
|
||||
BookedAt: "Private",
|
||||
Course: "42MIM-3",
|
||||
Semester: "ws",
|
||||
Compulsory: "w",
|
||||
},
|
||||
},
|
||||
},
|
||||
want: []model.AnonymizedEventDTO{
|
||||
{
|
||||
Day: "Montag",
|
||||
Week: "51",
|
||||
Start: types.DateTime{},
|
||||
End: types.DateTime{},
|
||||
Rooms: "Room",
|
||||
Free: false,
|
||||
},
|
||||
{
|
||||
Day: "Dienstag",
|
||||
Week: "52",
|
||||
Start: types.DateTime{},
|
||||
End: types.DateTime{},
|
||||
Rooms: "Room",
|
||||
Free: false,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := anonymizeRooms(tt.args.events); !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("anonymizeRooms() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsRoomInSchedule(t *testing.T) {
|
||||
type args struct {
|
||||
room string
|
||||
schedule []model.Event
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want bool
|
||||
}{
|
||||
{
|
||||
name: "room is in schedule",
|
||||
args: args{
|
||||
room: "Room",
|
||||
schedule: []model.Event{
|
||||
{
|
||||
UUID: "testUUID",
|
||||
Day: "Montag",
|
||||
Week: "52",
|
||||
Start: types.DateTime{},
|
||||
End: types.DateTime{},
|
||||
Name: "Secret",
|
||||
EventType: "V",
|
||||
Prof: "Prof. Dr. Secret",
|
||||
Rooms: "Room",
|
||||
Notes: "Secret",
|
||||
},
|
||||
},
|
||||
},
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "room is not in schedule",
|
||||
args: args{
|
||||
room: "Z324",
|
||||
schedule: []model.Event{
|
||||
{
|
||||
UUID: "testUUID",
|
||||
Day: "Montag",
|
||||
Week: "52",
|
||||
Start: types.DateTime{},
|
||||
End: types.DateTime{},
|
||||
Name: "Secret",
|
||||
EventType: "V",
|
||||
Prof: "Prof. Dr. Bond",
|
||||
Rooms: "LI007",
|
||||
Notes: "Keine Zeit für die Uni",
|
||||
},
|
||||
},
|
||||
},
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
name: "schedule event.Course is sport",
|
||||
args: args{
|
||||
room: "Klettergerüst",
|
||||
schedule: []model.Event{
|
||||
{
|
||||
UUID: "903784265784639527",
|
||||
Day: "Montag",
|
||||
Week: "52",
|
||||
Start: types.DateTime{},
|
||||
End: types.DateTime{},
|
||||
Name: "Hampelmann",
|
||||
EventType: "S",
|
||||
Prof: "Prof. Dr. Bewegung",
|
||||
Rooms: "Klettergerüst",
|
||||
Notes: "A apple a day keeps the doctor away",
|
||||
Course: "Sport",
|
||||
},
|
||||
},
|
||||
},
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "schedule event.Course is sport with different room",
|
||||
args: args{
|
||||
room: "HTWK Sportplatz",
|
||||
schedule: []model.Event{
|
||||
{
|
||||
UUID: "903784265784639527",
|
||||
Day: "Montag",
|
||||
Week: "52",
|
||||
Start: types.DateTime{},
|
||||
End: types.DateTime{},
|
||||
Name: "Hampelmann",
|
||||
EventType: "S",
|
||||
Prof: "Prof. Dr. Bewegung",
|
||||
Rooms: "Klettergerüst",
|
||||
Notes: "A apple a day keeps the doctor away",
|
||||
Course: "Sport",
|
||||
},
|
||||
},
|
||||
},
|
||||
want: false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := isRoomInSchedule(tt.args.room, tt.args.schedule); got != tt.want {
|
||||
t.Errorf("isRoomInSchedule() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetFreeRooms(t *testing.T) {
|
||||
type args struct {
|
||||
rooms []string
|
||||
schedule []model.Event
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want []string
|
||||
}{
|
||||
{
|
||||
name: "remove room1 from list",
|
||||
args: args{
|
||||
rooms: []string{
|
||||
"Room1",
|
||||
"Room2",
|
||||
"Room3",
|
||||
},
|
||||
schedule: []model.Event{
|
||||
{
|
||||
UUID: "testUUID",
|
||||
Day: "Montag",
|
||||
Week: "52",
|
||||
Start: types.DateTime{},
|
||||
End: types.DateTime{},
|
||||
Name: "Secret",
|
||||
EventType: "V",
|
||||
Prof: "Prof. Dr. Secret",
|
||||
Rooms: "Room1",
|
||||
Notes: "Secret",
|
||||
},
|
||||
},
|
||||
},
|
||||
want: []string{
|
||||
"Room2",
|
||||
"Room3",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "remove room2 from list",
|
||||
args: args{
|
||||
rooms: []string{
|
||||
"Room1",
|
||||
"Room2",
|
||||
"Room3",
|
||||
},
|
||||
schedule: []model.Event{
|
||||
{
|
||||
UUID: "testUUID",
|
||||
Day: "Montag",
|
||||
Week: "52",
|
||||
Start: types.DateTime{},
|
||||
End: types.DateTime{},
|
||||
Name: "Secret",
|
||||
EventType: "V",
|
||||
Prof: "Prof. Dr. Secret",
|
||||
Rooms: "Room3",
|
||||
Notes: "Secret",
|
||||
},
|
||||
},
|
||||
},
|
||||
want: []string{
|
||||
"Room1",
|
||||
"Room2",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "remove no room from list",
|
||||
args: args{
|
||||
rooms: []string{
|
||||
"Room1",
|
||||
"Room2",
|
||||
"Room3",
|
||||
},
|
||||
schedule: []model.Event{
|
||||
{
|
||||
UUID: "testUUID",
|
||||
Day: "Montag",
|
||||
Week: "52",
|
||||
Start: types.DateTime{},
|
||||
End: types.DateTime{},
|
||||
Name: "Secret",
|
||||
EventType: "V",
|
||||
Prof: "Prof. Dr. Secret",
|
||||
Rooms: "Room4",
|
||||
Notes: "Secret",
|
||||
},
|
||||
},
|
||||
},
|
||||
want: []string{
|
||||
"Room1",
|
||||
"Room2",
|
||||
"Room3",
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := removeRoomsThatHaveEvents(tt.args.rooms, tt.args.schedule); !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("removeRoomsThatHaveEvents() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func Test_encodeRoomSchedule(t *testing.T) {
|
||||
testTime, _ := time.Parse(time.RFC3339, "2024-12-24T12:00:00Z")
|
||||
testDateTime, _ := types.ParseDateTime(testTime)
|
||||
testDateTime_m15, _ := types.ParseDateTime(testTime.Add(-time.Minute * 15))
|
||||
testDateTime_p10, _ := types.ParseDateTime(testTime.Add(time.Minute * 10))
|
||||
testDateTime_p15, _ := types.ParseDateTime(testTime.Add(time.Minute * 15))
|
||||
testDateTime_p30, _ := types.ParseDateTime(testTime.Add(time.Minute * 30))
|
||||
testDateTime_p45, _ := types.ParseDateTime(testTime.Add(time.Minute * 45))
|
||||
testDateTime_late, _ := types.ParseDateTime(testTime.Add(time.Hour * 100))
|
||||
|
||||
type args struct {
|
||||
roomSchedule []model.AnonymizedEventDTO
|
||||
start time.Time
|
||||
granularity int
|
||||
blockCount int
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want []byte
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "encode event without length",
|
||||
args: args{
|
||||
roomSchedule: []model.AnonymizedEventDTO{
|
||||
{
|
||||
Day: "Montag",
|
||||
Week: "52",
|
||||
Start: testDateTime_p10,
|
||||
End: testDateTime_p10,
|
||||
Rooms: "Room",
|
||||
Free: false,
|
||||
},
|
||||
},
|
||||
start: testTime,
|
||||
granularity: 15,
|
||||
blockCount: 4,
|
||||
},
|
||||
want: []byte{
|
||||
0x00,
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "ignore event with start time after end time",
|
||||
args: args{
|
||||
roomSchedule: []model.AnonymizedEventDTO{
|
||||
{
|
||||
Day: "Montag",
|
||||
Week: "52",
|
||||
Start: testDateTime_p30,
|
||||
End: testDateTime_p10,
|
||||
Rooms: "Room",
|
||||
Free: false,
|
||||
},
|
||||
},
|
||||
start: testTime,
|
||||
granularity: 15,
|
||||
blockCount: 4,
|
||||
},
|
||||
want: []byte{
|
||||
0x00,
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "encode time table without length",
|
||||
args: args{
|
||||
roomSchedule: []model.AnonymizedEventDTO{
|
||||
{
|
||||
Day: "Montag",
|
||||
Week: "52",
|
||||
Start: testDateTime,
|
||||
End: testDateTime_p10,
|
||||
Rooms: "Room",
|
||||
Free: false,
|
||||
},
|
||||
},
|
||||
start: testTime,
|
||||
granularity: 15,
|
||||
blockCount: 0,
|
||||
},
|
||||
want: []byte{},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "encode time table without events",
|
||||
args: args{
|
||||
roomSchedule: []model.AnonymizedEventDTO{},
|
||||
start: testTime,
|
||||
granularity: 15,
|
||||
blockCount: 24,
|
||||
},
|
||||
want: []byte{
|
||||
0x00, 0x00, 0x00,
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "encode time table with single event",
|
||||
args: args{
|
||||
roomSchedule: []model.AnonymizedEventDTO{
|
||||
{
|
||||
Day: "Montag",
|
||||
Week: "52",
|
||||
Start: testDateTime_p30,
|
||||
End: testDateTime_late,
|
||||
Rooms: "Room",
|
||||
Free: false,
|
||||
},
|
||||
},
|
||||
start: testTime,
|
||||
granularity: 30,
|
||||
blockCount: 50,
|
||||
},
|
||||
want: []byte{
|
||||
0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "ignore free event",
|
||||
args: args{
|
||||
roomSchedule: []model.AnonymizedEventDTO{
|
||||
{
|
||||
Day: "Montag",
|
||||
Week: "52",
|
||||
Start: testDateTime_p15,
|
||||
End: testDateTime_p45,
|
||||
Rooms: "Room",
|
||||
Free: false,
|
||||
},
|
||||
{
|
||||
Day: "Montag",
|
||||
Week: "52",
|
||||
Start: testDateTime,
|
||||
End: testDateTime_p30,
|
||||
Rooms: "Room",
|
||||
Free: true,
|
||||
},
|
||||
},
|
||||
start: testTime,
|
||||
granularity: 15,
|
||||
blockCount: 50,
|
||||
},
|
||||
want: []byte{
|
||||
0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "encode time table with multiple events",
|
||||
args: args{
|
||||
roomSchedule: []model.AnonymizedEventDTO{
|
||||
{
|
||||
Day: "Montag",
|
||||
Week: "52",
|
||||
Start: testDateTime,
|
||||
End: testDateTime_p15,
|
||||
Rooms: "Room",
|
||||
Free: false,
|
||||
},
|
||||
{
|
||||
Day: "Montag",
|
||||
Week: "52",
|
||||
Start: testDateTime_p30,
|
||||
End: testDateTime_p45,
|
||||
Rooms: "Room",
|
||||
Free: false,
|
||||
},
|
||||
},
|
||||
start: testTime,
|
||||
granularity: 15,
|
||||
blockCount: 4,
|
||||
},
|
||||
want: []byte{
|
||||
0xA0,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "encode time table with multiple unordered events",
|
||||
args: args{
|
||||
roomSchedule: []model.AnonymizedEventDTO{
|
||||
{
|
||||
Day: "Montag",
|
||||
Week: "52",
|
||||
Start: testDateTime_p30,
|
||||
End: testDateTime_p45,
|
||||
Rooms: "Room",
|
||||
Free: false,
|
||||
},
|
||||
{
|
||||
Day: "Montag",
|
||||
Week: "52",
|
||||
Start: testDateTime,
|
||||
End: testDateTime_p15,
|
||||
Rooms: "Room",
|
||||
Free: false,
|
||||
},
|
||||
},
|
||||
start: testTime,
|
||||
granularity: 15,
|
||||
blockCount: 4,
|
||||
},
|
||||
want: []byte{
|
||||
0xA0,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "encode time table with overlapping events",
|
||||
args: args{
|
||||
roomSchedule: []model.AnonymizedEventDTO{
|
||||
{
|
||||
Day: "Montag",
|
||||
Week: "52",
|
||||
Start: testDateTime_p15,
|
||||
End: testDateTime_p30,
|
||||
Rooms: "Room",
|
||||
Free: false,
|
||||
},
|
||||
{
|
||||
Day: "Montag",
|
||||
Week: "52",
|
||||
Start: testDateTime,
|
||||
End: testDateTime_p45,
|
||||
Rooms: "Room",
|
||||
Free: false,
|
||||
},
|
||||
},
|
||||
start: testTime,
|
||||
granularity: 15,
|
||||
blockCount: 4,
|
||||
},
|
||||
want: []byte{
|
||||
0xE0,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "consider events starting before the start time",
|
||||
args: args{
|
||||
roomSchedule: []model.AnonymizedEventDTO{
|
||||
{
|
||||
Day: "Montag",
|
||||
Week: "52",
|
||||
Start: testDateTime_m15,
|
||||
End: testDateTime_p15,
|
||||
Rooms: "Room",
|
||||
Free: false,
|
||||
},
|
||||
},
|
||||
start: testTime,
|
||||
granularity: 15,
|
||||
blockCount: 4,
|
||||
},
|
||||
want: []byte{
|
||||
0x80,
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, err := encodeRoomSchedule(tt.args.roomSchedule, tt.args.start, tt.args.granularity, tt.args.blockCount)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("encodeRoomSchedule() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("encodeRoomSchedule() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user