Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ID-85]Webtools fixes #91

Merged
merged 36 commits into from
May 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
f04c4d0
in progress
Apr 25, 2024
02d9d6c
in progress
Apr 29, 2024
ebd487c
in progress
Apr 30, 2024
abb293d
fix lint issues
Apr 30, 2024
7ed283e
fix duration
May 1, 2024
d0d5d91
resolve comment
May 1, 2024
970c636
set the imageAdapter
May 1, 2024
2ab40a5
add new model and new mongo collection for the images
May 1, 2024
bfb704b
FindAllImages from the database
May 1, 2024
2c12949
download image from webtools API in progress
May 1, 2024
66ab100
upload image
May 1, 2024
0d6f519
in progress
May 1, 2024
996020e
in progress
May 1, 2024
ac82d8f
fix the url
May 1, 2024
a386d80
fix lint issues
May 1, 2024
b7eaf79
fix lint issues
May 1, 2024
475e489
fix comment
May 1, 2024
ba01c3c
Remove image png
petyos May 2, 2024
01e99fb
Use prefixes
petyos May 2, 2024
c86139d
Align a bit
petyos May 2, 2024
01d01b3
Fix priv key?
petyos May 2, 2024
7a69889
Align code
petyos May 2, 2024
65518f0
Define process images function
petyos May 2, 2024
0125a9c
Remove useless loop and fix log
petyos May 2, 2024
6eef7d2
Handle when error occured
petyos May 2, 2024
9c0a151
Use the library when calling bbs API!
petyos May 3, 2024
2250262
Return the url
petyos May 3, 2024
e608ce1
In progress
petyos May 3, 2024
967f08a
Why do you call this API two times??
petyos May 3, 2024
75a8778
More stuff
petyos May 4, 2024
9271505
Get the events for images processing
petyos May 5, 2024
9fa7bcf
Get the events which are not processed
petyos May 5, 2024
28e8ea2
Apply process images
petyos May 5, 2024
c9a4a3e
Work with the processed image data
petyos May 5, 2024
fb0a520
Pix path
petyos May 5, 2024
52a992c
Fix timer
petyos May 5, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion core/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type Application struct {
storage Storage

eventsBBAdapter EventsBBAdapter
imageAdapter ImageAdapter

//events logic
eventsLogic eventsLogic
Expand Down Expand Up @@ -92,9 +93,10 @@ func (a *Application) GetEnvConfigs() (*model.EnvConfigData, error) {
func NewApplication(version string, build string,
storage Storage,
eventsBBAdapter EventsBBAdapter,
imageAdapter ImageAdapter,
appntAdapters map[string]Appointments,
logger *logs.Logger) *Application {
application := Application{version: version, build: build, storage: storage, eventsBBAdapter: eventsBBAdapter, logger: logger, AppointmentAdapters: appntAdapters}
application := Application{version: version, build: build, storage: storage, eventsBBAdapter: eventsBBAdapter, imageAdapter: imageAdapter, logger: logger, AppointmentAdapters: appntAdapters}

//add the drivers ports/interfaces
application.Default = newAppDefault(&application)
Expand Down
8 changes: 8 additions & 0 deletions core/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ type Storage interface {
FindWebtoolsBlacklistData() ([]model.WebToolsItem, error)
AddWebtoolsBlacklistData(dataSourceIDs []string, dataCalendarIDs []string) error
RemoveWebtoolsBlacklistData(dataSourceIDs []string, dataCalendarIDs []string) error

FindImageItems() ([]model.ContentImagesURL, error)
InsertImageItem(items model.ContentImagesURL) error
}

// StorageListener represents storage listener
Expand Down Expand Up @@ -187,3 +190,8 @@ type SuccessTeam interface {
GetPrimaryCareProvider(uin string, accesstoken string, conf *model.EnvConfigData) (*[]model.SuccessTeamMember, int, error)
GetAcademicAdvisors(uin string, calendars *[]model.UnitCalendar, accesstoken string, conf *model.EnvConfigData) (*[]model.SuccessTeamMember, int, error)
}

// ImageAdapter is used to precess images
type ImageAdapter interface {
ProcessImage(item model.WebToolsEvent) (*model.ContentImagesURL, error)
}
119 changes: 116 additions & 3 deletions core/logic_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ func (e eventsLogic) processWebToolsEvents() {
e.logger.Errorf("error on loading web tools events - %s", err)
return
}

webToolsCount := len(allWebToolsEvents)
if webToolsCount == 0 {
e.logger.Error("web tools are nil")
Expand All @@ -255,6 +256,13 @@ func (e eventsLogic) processWebToolsEvents() {

e.logger.Infof("we loaded %d web tools events", webToolsCount)

//process the images before the main processing
imagesData, err := e.processImages(allWebToolsEvents)
if err != nil {
e.logger.Errorf("error on processing images - %s", err)
return
}

now := time.Now()

//in transaction
Expand Down Expand Up @@ -296,7 +304,7 @@ func (e eventsLogic) processWebToolsEvents() {
//prepare the id
id := e.prepareID(wt.EventID, existingLegacyIdsMap)

le := e.constructLegacyEvent(wt, id, now)
le := e.constructLegacyEvent(wt, id, now, imagesData)
newLegacyEvents = append(newLegacyEvents, le)
}

Expand All @@ -317,6 +325,99 @@ func (e eventsLogic) processWebToolsEvents() {
}
}

func (e eventsLogic) processImages(allWebtoolsEvents []model.WebToolsEvent) ([]model.ContentImagesURL, error) {
//get the events for images processing
forProcessingEvents, err := e.getEventsForImagesProcessing(allWebtoolsEvents)
if err != nil {
return nil, err
}

e.logger.Infof("there are %d events for images processing", len(forProcessingEvents))

//get the events which are not processed
notProccesed, err := e.getNotProcessedEvents(forProcessingEvents)
if err != nil {
return nil, err
}

e.logger.Infof("there are %d events to be processed as not proccesed", len(notProccesed))

//process the images which have not been processed
err = e.applyProcessImages(notProccesed)
if err != nil {
e.logger.Error("Error on processing images")
return nil, err
}

//as we already ahve processed all iages just return this dtaa to be used
imagesData, err := e.app.storage.FindImageItems()
if err != nil {
return nil, err
}

return imagesData, nil
}

func (e eventsLogic) getEventsForImagesProcessing(allWebtoolsEvents []model.WebToolsEvent) ([]model.WebToolsEvent, error) {
res := []model.WebToolsEvent{}
for _, w := range allWebtoolsEvents {
if w.LargeImageUploaded == "true" {
res = append(res, w)
}

}
return res, nil
}

func (e eventsLogic) getNotProcessedEvents(eventsForProcessing []model.WebToolsEvent) ([]model.WebToolsEvent, error) {
allProcessed, err := e.app.storage.FindImageItems()
if err != nil {
return nil, err
}

processedMap := make(map[string]bool) // map to keep track of processed events
for _, item := range allProcessed {
processedMap[item.ID] = true
}

var notProcessedEvents []model.WebToolsEvent
for _, event := range eventsForProcessing {
if _, processed := processedMap[event.EventID]; !processed {
notProcessedEvents = append(notProcessedEvents, event)
}
}

return notProcessedEvents, nil
}

func (e eventsLogic) applyProcessImages(item []model.WebToolsEvent) error {
i := 0
for _, w := range item {

//process image
res, err := e.app.imageAdapter.ProcessImage(w)
if err != nil {
return err
}

if res == nil {
continue
}

//mark as processed
err = e.app.storage.InsertImageItem(*res)
if err != nil {
return err
}

e.logger.Infof("%d - %s image was processed: %s", i, res.ID, res.ImageURL)

i++
}
return nil

}

// ignore or modify webtools events
func (e eventsLogic) modifyWebtoolsEventsList(allWebtoolsEvents []model.WebToolsEvent) ([]model.WebToolsEvent, error) {
modifiedList := []model.WebToolsEvent{}
Expand Down Expand Up @@ -432,7 +533,7 @@ func (e eventsLogic) loadAllWebToolsEvents() ([]model.WebToolsEvent, error) {
return allWebToolsEvents, nil
}

func (e eventsLogic) constructLegacyEvent(g model.WebToolsEvent, id string, now time.Time) model.LegacyEventItem {
func (e eventsLogic) constructLegacyEvent(g model.WebToolsEvent, id string, now time.Time, imagesData []model.ContentImagesURL) model.LegacyEventItem {
syncProcessSource := "webtools-direct"

createdBy := g.CreatedBy
Expand Down Expand Up @@ -555,14 +656,26 @@ func (e eventsLogic) constructLegacyEvent(g model.WebToolsEvent, id string, now
}
//end target audience

//image url
imageURL := e.getImageURL(g.EventID, imagesData)

return model.LegacyEventItem{SyncProcessSource: syncProcessSource, SyncDate: now,
Item: model.LegacyEvent{ID: id, Category: g.EventType, CreatedBy: createdBy, OriginatingCalendarID: g.OriginatingCalendarID, IsVirtial: isVirtual,
DataModified: modifiedDate, DateCreated: createdDate,
Sponsor: g.Sponsor, Title: g.Title, CalendarID: g.CalendarID, SourceID: "0", AllDay: allDay, IsEventFree: costFree, Cost: g.Cost, LongDescription: g.Description,
TitleURL: g.TitleURL, RegistrationURL: g.RegistrationURL, RecurringFlag: Recurrence, IcalURL: icalURL, OutlookURL: outlookURL,
RecurrenceID: recurrenceID, Location: &location, Contacts: contatsLegacy,
DataSourceEventID: g.EventID, StartDate: startDateStr, EndDate: endDateStr,
Tags: tags, TargetAudience: targetAudience}}
Tags: tags, TargetAudience: targetAudience, ImageURL: imageURL}}
}

func (e eventsLogic) getImageURL(eventID string, imageData []model.ContentImagesURL) *string {
for _, image := range imageData {
if image.ID == eventID {
return &image.ImageURL
}
}
return nil
}

func (e eventsLogic) formatDate(wtDate string) string {
Expand Down
31 changes: 31 additions & 0 deletions core/model/images.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2022 Board of Trustees of the University of Illinois.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package model

// ContentImagesURL is used to keep the imageURL from ContentBB
type ContentImagesURL struct {
ID string `json:"id" bson:"_id"`
ImageURL string `json:"imageURL" bson:"imageURL"`
}

// ImageData is used to keep the the image thata from webtools
type ImageData struct {
ImageData []byte `json:"image_data"`
Height int `json:"height"`
Width int `json:"width"`
Quality int `json:"quality"`
Path string `json:"path"`
FileName string `json:"fileName"`
}
Loading
Loading