mirror of
https://gitlab.dit.htwk-leipzig.de/htwk-software/htwkalender.git
synced 2025-08-03 18:29:14 +02:00
feat:#34 refactored function to intended service, fixed docker files
This commit is contained in:
126
services/data-manager/service/feed/feedFunctions.go
Normal file
126
services/data-manager/service/feed/feedFunctions.go
Normal file
@@ -0,0 +1,126 @@
|
||||
//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 feed
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"github.com/pocketbase/dbx"
|
||||
"github.com/pocketbase/pocketbase/daos"
|
||||
"htwkalender/model"
|
||||
database "htwkalender/service/db"
|
||||
localTime "htwkalender/service/functions/time"
|
||||
"log/slog"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func ClearFeeds(db *daos.Dao, months int, clock localTime.Clock) {
|
||||
feeds, err := database.GetAllFeeds(db)
|
||||
if err != nil {
|
||||
slog.Error("CleanFeeds: failed to get all feeds", "error", err)
|
||||
return
|
||||
}
|
||||
for _, feed := range feeds {
|
||||
// if retrieved time is older than a half year delete feed
|
||||
now := clock.Now()
|
||||
feedRetrievedTime := feed.Retrieved.Time()
|
||||
timeShift := now.AddDate(0, -months, 0)
|
||||
|
||||
if feedRetrievedTime.Before(timeShift) {
|
||||
// delete feed
|
||||
var sqlResult sql.Result
|
||||
sqlResult, err = db.DB().Delete("feeds", dbx.NewExp("id = {:id}", dbx.Params{"id": feed.GetId()})).Execute()
|
||||
if err != nil {
|
||||
slog.Error("CleanFeeds: delete feed "+feed.GetId()+" failed", "error", err)
|
||||
slog.Error("SQL Result: ", "error", sqlResult)
|
||||
} else {
|
||||
slog.Info("CleanFeeds: delete feed " + feed.GetId() + " successful")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func CombineEventsInFeed(events model.Events) model.Events {
|
||||
// Combine events with the same name, start, end and course
|
||||
// check if there are events
|
||||
if len(events) > 0 {
|
||||
combinedEvents := model.Events{events[0]}
|
||||
|
||||
for i := 1; i < len(events); i++ {
|
||||
// check if the event is already in the combinedEvents
|
||||
alreadyInCombinedEvents := false
|
||||
|
||||
for j := 0; j < len(combinedEvents); j++ {
|
||||
if events[i].Name == combinedEvents[j].Name &&
|
||||
events[i].Start == combinedEvents[j].Start &&
|
||||
events[i].End == combinedEvents[j].End &&
|
||||
events[i].Course == combinedEvents[j].Course {
|
||||
alreadyInCombinedEvents = true
|
||||
combinedEvents[j].Notes = addNotesIfAlreadyRoomsAdded(events, combinedEvents, j, i)
|
||||
combinedEvents[j].Prof = combineProfs(events, i, combinedEvents, j)
|
||||
combinedEvents[j].Rooms = combineRooms(events, i, combinedEvents, j)
|
||||
break
|
||||
}
|
||||
}
|
||||
if !alreadyInCombinedEvents {
|
||||
combinedEvents = append(combinedEvents, events[i])
|
||||
}
|
||||
}
|
||||
return combinedEvents
|
||||
}
|
||||
return model.Events{}
|
||||
}
|
||||
|
||||
func addNotesIfAlreadyRoomsAdded(events model.Events, combinedEvents model.Events, index2 int, index1 int) string {
|
||||
// check if combinedEvents[index2].Rooms string contains comma "," ,
|
||||
if !strings.Contains(combinedEvents[index2].Rooms, ",") {
|
||||
return descriptionString(combinedEvents[index2]) + "\n" + descriptionString(events[index1])
|
||||
} else {
|
||||
return combinedEvents[index2].Notes + "\n" + descriptionString(events[index1])
|
||||
}
|
||||
}
|
||||
|
||||
func combineProfs(events model.Events, index1 int, combinedEvents model.Events, index2 int) string {
|
||||
// combine the profs
|
||||
if events[index1].Prof != "" {
|
||||
if combinedEvents[index2].Prof == "" {
|
||||
return events[index1].Prof
|
||||
} else {
|
||||
if !strings.Contains(combinedEvents[index2].Prof, events[index1].Prof) {
|
||||
return combinedEvents[index2].Prof + ", " + events[index1].Prof
|
||||
}
|
||||
}
|
||||
}
|
||||
return combinedEvents[index2].Prof
|
||||
}
|
||||
|
||||
func descriptionString(event model.Event) string {
|
||||
return event.Rooms + " - " + event.Notes + " (" + event.Prof + ")"
|
||||
}
|
||||
|
||||
func combineRooms(events model.Events, index1 int, combinedEvents model.Events, index2 int) string {
|
||||
// combine the rooms
|
||||
if events[index1].Rooms != "" {
|
||||
if combinedEvents[index2].Rooms == "" {
|
||||
return events[index1].Rooms
|
||||
} else {
|
||||
if !strings.Contains(combinedEvents[index2].Rooms, events[index1].Rooms) {
|
||||
return combinedEvents[index2].Rooms + ", " + events[index1].Rooms
|
||||
}
|
||||
}
|
||||
}
|
||||
return combinedEvents[index2].Rooms
|
||||
}
|
200
services/data-manager/service/feed/feedFunctions_test.go
Normal file
200
services/data-manager/service/feed/feedFunctions_test.go
Normal file
@@ -0,0 +1,200 @@
|
||||
//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 feed
|
||||
|
||||
import (
|
||||
"github.com/pocketbase/pocketbase/daos"
|
||||
"github.com/pocketbase/pocketbase/tests"
|
||||
"htwkalender/model"
|
||||
mockTime "htwkalender/service/functions/time"
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
const testDataDir = "./mockData"
|
||||
|
||||
func TestClearFeeds(t *testing.T) {
|
||||
|
||||
setupTestApp := func(t *testing.T) *daos.Dao {
|
||||
testApp, err := tests.NewTestApp(testDataDir)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
dao := daos.New(testApp.Dao().DB())
|
||||
return dao
|
||||
}
|
||||
|
||||
type args struct {
|
||||
db *daos.Dao
|
||||
months int
|
||||
mockClock mockTime.MockClock
|
||||
}
|
||||
testCases := []struct {
|
||||
name string
|
||||
args args
|
||||
want int
|
||||
}{
|
||||
{
|
||||
name: "TestClearFeeds",
|
||||
args: args{
|
||||
db: setupTestApp(t),
|
||||
months: 6,
|
||||
mockClock: mockTime.MockClock{
|
||||
NowTime: time.Date(2023, 12, 1, 0, 0, 0, 0, time.UTC),
|
||||
},
|
||||
},
|
||||
want: 1,
|
||||
},
|
||||
{
|
||||
name: "TestClearAllFeeds",
|
||||
args: args{
|
||||
db: setupTestApp(t),
|
||||
months: 1,
|
||||
mockClock: mockTime.MockClock{
|
||||
NowTime: time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC),
|
||||
},
|
||||
},
|
||||
want: 0,
|
||||
},
|
||||
{
|
||||
name: "TestClearFeedsClearBeforeRetrievedTime",
|
||||
args: args{
|
||||
db: setupTestApp(t),
|
||||
months: 1,
|
||||
mockClock: mockTime.MockClock{
|
||||
NowTime: time.Date(2010, 1, 1, 0, 0, 0, 0, time.UTC),
|
||||
},
|
||||
},
|
||||
want: 3,
|
||||
},
|
||||
}
|
||||
for _, tt := range testCases {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
ClearFeeds(tt.args.db, tt.args.months, tt.args.mockClock)
|
||||
// count all feeds in db
|
||||
var feeds []*model.Feed
|
||||
err := tt.args.db.DB().Select("id").From("feeds").All(&feeds)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := len(feeds); got != tt.want {
|
||||
t.Errorf("ClearFeeds() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCombineEventsInFeed(t *testing.T) {
|
||||
type args struct {
|
||||
events model.Events
|
||||
}
|
||||
testCases := []struct {
|
||||
name string
|
||||
args args
|
||||
want model.Events
|
||||
}{
|
||||
{
|
||||
name: "TestCombineEventsInFeed",
|
||||
args: args{
|
||||
events: model.Events{
|
||||
{
|
||||
Name: "Modellierung",
|
||||
Start: mockTime.ParseAsTypesDatetime(time.Date(2023, 12, 1, 0, 0, 0, 0, time.UTC)),
|
||||
End: mockTime.ParseAsTypesDatetime(time.Date(2023, 12, 1, 4, 0, 0, 0, time.UTC)),
|
||||
Prof: "Prof. Bunt",
|
||||
Rooms: "LI001",
|
||||
Notes: "Gruppe 2",
|
||||
},
|
||||
{
|
||||
Name: "Modellierung",
|
||||
Start: mockTime.ParseAsTypesDatetime(time.Date(2023, 12, 1, 0, 0, 0, 0, time.UTC)),
|
||||
End: mockTime.ParseAsTypesDatetime(time.Date(2023, 12, 1, 4, 0, 0, 0, time.UTC)),
|
||||
Prof: "Prof. Bunt",
|
||||
Rooms: "LI002",
|
||||
Notes: "Gruppe 1",
|
||||
},
|
||||
},
|
||||
},
|
||||
want: model.Events{
|
||||
{
|
||||
Name: "Modellierung",
|
||||
Start: mockTime.ParseAsTypesDatetime(time.Date(2023, 12, 1, 0, 0, 0, 0, time.UTC)),
|
||||
End: mockTime.ParseAsTypesDatetime(time.Date(2023, 12, 1, 4, 0, 0, 0, time.UTC)),
|
||||
Prof: "Prof. Bunt",
|
||||
Rooms: "LI001, LI002",
|
||||
Notes: "LI001 - Gruppe 2 (Prof. Bunt)\nLI002 - Gruppe 1 (Prof. Bunt)",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "CannotCombineEventsInFeed",
|
||||
args: args{
|
||||
events: model.Events{
|
||||
{
|
||||
Name: "Modellierung",
|
||||
Start: mockTime.ParseAsTypesDatetime(time.Date(2023, 12, 1, 0, 0, 0, 0, time.UTC)),
|
||||
End: mockTime.ParseAsTypesDatetime(time.Date(2023, 12, 1, 4, 0, 0, 0, time.UTC)),
|
||||
Prof: "Prof. Bunt",
|
||||
Rooms: "LI001",
|
||||
Notes: "Gruppe 2",
|
||||
},
|
||||
{
|
||||
Name: "Modellierung - 2",
|
||||
Start: mockTime.ParseAsTypesDatetime(time.Date(2023, 12, 1, 0, 0, 0, 0, time.UTC)),
|
||||
End: mockTime.ParseAsTypesDatetime(time.Date(2023, 12, 1, 4, 0, 0, 0, time.UTC)),
|
||||
Prof: "Prof. Bunt",
|
||||
Rooms: "LI002",
|
||||
Notes: "Gruppe 1",
|
||||
},
|
||||
},
|
||||
},
|
||||
want: model.Events{
|
||||
{
|
||||
Name: "Modellierung",
|
||||
Start: mockTime.ParseAsTypesDatetime(time.Date(2023, 12, 1, 0, 0, 0, 0, time.UTC)),
|
||||
End: mockTime.ParseAsTypesDatetime(time.Date(2023, 12, 1, 4, 0, 0, 0, time.UTC)),
|
||||
Prof: "Prof. Bunt",
|
||||
Rooms: "LI001",
|
||||
Notes: "Gruppe 2",
|
||||
},
|
||||
{
|
||||
Name: "Modellierung - 2",
|
||||
Start: mockTime.ParseAsTypesDatetime(time.Date(2023, 12, 1, 0, 0, 0, 0, time.UTC)),
|
||||
End: mockTime.ParseAsTypesDatetime(time.Date(2023, 12, 1, 4, 0, 0, 0, time.UTC)),
|
||||
Prof: "Prof. Bunt",
|
||||
Rooms: "LI002",
|
||||
Notes: "Gruppe 1",
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "NoEventsInFeed",
|
||||
args: args{
|
||||
events: model.Events{},
|
||||
},
|
||||
want: model.Events{},
|
||||
},
|
||||
}
|
||||
for _, tt := range testCases {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := CombineEventsInFeed(tt.args.events); !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("CombineEventsInFeed() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
BIN
services/data-manager/service/feed/mockData/data.db
Normal file
BIN
services/data-manager/service/feed/mockData/data.db
Normal file
Binary file not shown.
BIN
services/data-manager/service/feed/mockData/logs.db
Normal file
BIN
services/data-manager/service/feed/mockData/logs.db
Normal file
Binary file not shown.
Reference in New Issue
Block a user