mirror of
https://gitlab.dit.htwk-leipzig.de/htwk-software/htwkalender-pwa.git
synced 2025-08-07 12:19:17 +02:00
3 add occupancy bson endpoint in backend
This commit is contained in:
@@ -17,13 +17,21 @@
|
||||
package room
|
||||
|
||||
import (
|
||||
"github.com/pocketbase/pocketbase"
|
||||
"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 {
|
||||
@@ -51,7 +59,128 @@ func GetRoomSchedule(app *pocketbase.PocketBase, room string, from string, to st
|
||||
return anonymizedRoomSchedule, nil
|
||||
}
|
||||
|
||||
// Transform the events to anonymized events throwing away all unnecessary information
|
||||
/**
|
||||
* 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 {
|
||||
@@ -102,3 +231,59 @@ func isRoomInSchedule(room string, schedule []model.Event) bool {
|
||||
}
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user