3 add occupancy bson endpoint in backend

This commit is contained in:
survellow
2024-05-23 22:47:52 +02:00
parent d73f7e284b
commit 114a309e8b
9 changed files with 740 additions and 4 deletions

View File

@ -47,21 +47,29 @@ require (
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/snappy v0.0.1 // indirect
github.com/google/wire v0.5.0 // indirect
github.com/googleapis/gax-go/v2 v2.12.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
github.com/klauspost/compress v1.13.6 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-sqlite3 v1.14.19 // indirect
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/cobra v1.8.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
github.com/xdg-go/scram v1.1.2 // indirect
github.com/xdg-go/stringprep v1.0.4 // indirect
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect
go.mongodb.org/mongo-driver v1.15.0 // indirect
go.opencensus.io v0.24.0 // indirect
gocloud.dev v0.36.0 // indirect
golang.org/x/crypto v0.18.0 // indirect

View File

@ -118,6 +118,8 @@ github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaS
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4=
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
@ -161,6 +163,8 @@ github.com/jordic/goics v0.0.0-20210404174824-5a0337b716a0 h1:p+k2RozdR141dIkAbO
github.com/jordic/goics v0.0.0-20210404174824-5a0337b716a0/go.mod h1:YHaw6sOIeFRob8Y9q/blEAMfVcLpeE9+vdhrwyEMxoI=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8=
github.com/klauspost/compress v1.13.6 h1:P76CopJELS0TiO2mebmnzgWaajssP/EszplttgQxcgc=
github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
@ -181,6 +185,8 @@ github.com/mattn/go-sqlite3 v1.14.19/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE=
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d h1:5PJl274Y63IEHC+7izoQE9x6ikvDFZS2mDVS3drnohI=
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE=
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe h1:iruDEfMl2E6fbMZ9s0scYfZQ84/6SPL6zC8ACM2oIL0=
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pocketbase/dbx v1.10.1 h1:cw+vsyfCJD8YObOVeqb93YErnlxwYMkNZ4rwN0G0AaA=
@ -213,7 +219,17 @@ github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6Kllzaw
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo=
github.com/valyala/fasttemplate v1.2.2/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c=
github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI=
github.com/xdg-go/scram v1.1.2 h1:FHX5I5B4i4hKRVRBCFRxq1iQRej7WO3hhBuJf+UUySY=
github.com/xdg-go/scram v1.1.2/go.mod h1:RT/sEzTbU5y00aCK8UOx6R7YryM0iF1N2MOmC3kKLN4=
github.com/xdg-go/stringprep v1.0.4 h1:XLI/Ng3O1Atzq0oBs3TWm+5ZVgkq2aqdlvP9JtoZ6c8=
github.com/xdg-go/stringprep v1.0.4/go.mod h1:mPGuuIYwz7CmR2bT9j4GbQqutWS1zV24gijq1dTyGkM=
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d h1:splanxYIlg+5LfHAM6xpdFEAYOk8iySO56hMFq6uLyA=
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d/go.mod h1:rHwXgn7JulP+udvsHwJoVG1YGAP6VLg4y9I5dyZdqmA=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
go.mongodb.org/mongo-driver v1.15.0 h1:rJCKC8eEliewXjZGf0ddURtl7tTVy1TK3bfl0gkUSLc=
go.mongodb.org/mongo-driver v1.15.0/go.mod h1:Vzb0Mk/pa7e6cWw85R4F/endUC3u0U9jGcNU603k65c=
go.opencensus.io v0.24.0 h1:y73uSU6J157QMP2kn2r30vwW1A2W2WFwSCGnAVxeaD0=
go.opencensus.io v0.24.0/go.mod h1:vNK8G9p7aAivkbmorf4v+7Hgx+Zs0yY+0fOtgBfjQKo=
gocloud.dev v0.36.0 h1:q5zoXux4xkOZP473e1EZbG8Gq9f0vlg1VNH5Du/ybus=

View File

@ -0,0 +1,35 @@
//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 model
import (
"time"
"go.mongodb.org/mongo-driver/bson/primitive"
)
type RoomOccupancy struct {
Name string `bson:"name"`
Occupancy primitive.Binary `bson:"occupancy"`
}
type RoomOccupancyList struct {
Start time.Time `bson:"start"`
Granularity int `bson:"granularity"`
Blocks int `bson:"blocks"`
Rooms []RoomOccupancy `bson:"rooms"`
}

View File

@ -116,6 +116,31 @@ paths:
responses:
'200':
description: Successful response
/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
content:
application/bson:
schema:
$ref: '#/components/schemas/RoomOccupancy'
/api/createFeed:
post:
summary: Create iCal Feed
@ -262,4 +287,30 @@ components:
events:
type: array
items:
type: string
type: string
RoomOccupancy:
type: object
properties:
start:
type: string
format: date-time
granularity:
type: integer
blocks:
type: integer
rooms:
type: object
properties:
name:
type: string
occupancy:
type: string
format: binary
required:
- name
- occupancy
required:
- start
- granularity
- blocks
- rooms

View File

@ -31,8 +31,11 @@ import (
"github.com/pocketbase/pocketbase"
"github.com/pocketbase/pocketbase/apis"
"github.com/pocketbase/pocketbase/core"
"go.mongodb.org/mongo-driver/bson"
)
const RoomOccupancyGranularity = 5
func AddRoutes(app *pocketbase.PocketBase) {
app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
@ -172,6 +175,40 @@ func AddRoutes(app *pocketbase.PocketBase) {
return nil
})
// API Endpoint to get room occupancy for a time period for all rooms, when requested as BSON
app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
_, err := e.Router.AddRoute(echo.Route{
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)
if err != nil {
slog.Error("Failed to get room occupancy: %v", err)
return c.JSON(http.StatusBadRequest, "Failed to get room occupancy")
}
bson_coded, err := bson.Marshal(rooms)
if err != nil {
slog.Error("Failed to encode room occupancy to BSON: %v", err)
return c.JSON(http.StatusBadRequest, "Failed to encode room occupancy to BSON")
}
return c.Blob(http.StatusOK, "application/bson", bson_coded)
},
Middlewares: []echo.MiddlewareFunc{
apis.ActivityLogger(app),
},
})
if err != nil {
return err
}
return nil
})
// API Endpoint to create a new iCal feed
app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
_, err := e.Router.AddRoute(echo.Route{

View File

@ -0,0 +1,27 @@
//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 functions
// function to filter an array
func Filter[T any](ss []T, test func(T) bool) (ret []T) {
for _, s := range ss {
if test(s) {
ret = append(ret, s)
}
}
return
}

View File

@ -0,0 +1,103 @@
//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 functions
import (
"reflect"
"strings"
"testing"
)
func Test_Filter_number(t *testing.T) {
type args struct {
ss []int
test func(int) bool
}
tests := []struct {
name string
args args
wantRet []int
}{
{
"filter even numbers",
args{
[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
func(i int) bool {
return i%2 == 0
},
},
[]int{2, 4, 6, 8, 10},
},
{
"filter smaller than 5",
args{
[]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
func(i int) bool {
return i < 5
},
},
[]int{1, 2, 3, 4},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if gotRet := Filter(tt.args.ss, tt.args.test); !reflect.DeepEqual(gotRet, tt.wantRet) {
t.Errorf("filter() = %v, want %v", gotRet, tt.wantRet)
}
})
}
}
func Test_Filter_string(t *testing.T) {
type args struct {
ss []string
test func(string) bool
}
tests := []struct {
name string
args args
wantRet []string
}{
{
"filter contains a",
args{
[]string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"},
func(i string) bool {
return strings.Contains(i, "a")
},
},
[]string{"a"},
},
{
"filter starts with prefix 'a'",
args{
[]string{"alpha", "beta", "a", "ab", "ac", "delta"},
func(i string) bool {
return strings.HasPrefix(i, "a")
},
},
[]string{"alpha", "a", "ab", "ac"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if gotRet := Filter(tt.args.ss, tt.args.test); !reflect.DeepEqual(gotRet, tt.wantRet) {
t.Errorf("filter() = %v, want %v", gotRet, tt.wantRet)
}
})
}
}

View File

@ -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)
}
}

View File

@ -17,10 +17,12 @@
package room
import (
"github.com/pocketbase/pocketbase/tools/types"
"htwkalender/model"
"reflect"
"testing"
"time"
"github.com/pocketbase/pocketbase/tools/types"
)
func Test_anonymizeRooms(t *testing.T) {
@ -305,3 +307,275 @@ func Test_getFreeRooms(t *testing.T) {
})
}
}
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)
}
})
}
}