feat: sending processing images on app load

This commit is contained in:
2025-07-02 12:04:16 +01:00
parent cb4a03015a
commit c632487d7e
3 changed files with 103 additions and 67 deletions

View File

@ -44,9 +44,7 @@ func ListenNewImageEvents(db *sql.DB, notifier *Notifier[Notification]) {
panic(err) panic(err)
} }
for { for parameters := range listener.Notify {
select {
case parameters := <-listener.Notify:
imageId := uuid.MustParse(parameters.Extra) imageId := uuid.MustParse(parameters.Extra)
databaseEventLog.Debug("Starting processing image", "ImageID", imageId) databaseEventLog.Debug("Starting processing image", "ImageID", imageId)
@ -84,7 +82,6 @@ func ListenNewImageEvents(db *sql.DB, notifier *Notifier[Notification]) {
}() }()
} }
} }
}
func ListenProcessingImageStatus(db *sql.DB, images models.ImageModel, notifier *Notifier[Notification]) { func ListenProcessingImageStatus(db *sql.DB, images models.ImageModel, notifier *Notifier[Notification]) {
listener := pq.NewListener(os.Getenv("DB_CONNECTION"), time.Second, time.Second, func(event pq.ListenerEventType, err error) { listener := pq.NewListener(os.Getenv("DB_CONNECTION"), time.Second, time.Second, func(event pq.ListenerEventType, err error) {
@ -100,9 +97,7 @@ func ListenProcessingImageStatus(db *sql.DB, images models.ImageModel, notifier
panic(err) panic(err)
} }
for { for data := range listener.Notify {
select {
case data := <-listener.Notify:
imageStringUuid := data.Extra[0:36] imageStringUuid := data.Extra[0:36]
status := data.Extra[36:] status := data.Extra[36:]
@ -130,39 +125,46 @@ func ListenProcessingImageStatus(db *sql.DB, images models.ImageModel, notifier
} }
} }
} }
}
/*
* TODO: We have channels open every a user sends an image.
* We never close these channels.
*
* What is a reasonable default? Close the channel after 1 minute of inactivity?
*/
func CreateEventsHandler(notifier *Notifier[Notification]) http.HandlerFunc { func CreateEventsHandler(notifier *Notifier[Notification]) http.HandlerFunc {
counter := 0 counter := 0
userSplitters := make(map[string]*ChannelSplitter[Notification]) userSplitters := make(map[string]*ChannelSplitter[Notification])
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
userId := r.Context().Value(USER_ID).(uuid.UUID) _userId := r.Context().Value(USER_ID).(uuid.UUID)
if userId == uuid.Nil { if _userId == uuid.Nil {
w.WriteHeader(http.StatusUnauthorized) w.WriteHeader(http.StatusUnauthorized)
return return
} }
userId := _userId.String()
w.Header().Set("Content-Type", "text/event-stream") w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache") w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive") w.Header().Set("Connection", "keep-alive")
// w.(http.Flusher).Flush() // w.(http.Flusher).Flush()
if _, exists := notifier.Listeners[userId.String()]; !exists { if _, exists := notifier.Listeners[userId]; !exists {
notifier.Create(userId.String()) notifier.Create(userId)
} }
userNotifications := notifier.Listeners[userId.String()] userNotifications := notifier.Listeners[userId]
if _, exists := userSplitters[userId.String()]; !exists { if _, exists := userSplitters[userId]; !exists {
splitter := NewChannelSplitter(userNotifications) splitter := NewChannelSplitter(userNotifications)
userSplitters[userId.String()] = &splitter userSplitters[userId] = &splitter
splitter.Listen() splitter.Listen()
} }
splitter := userSplitters[userId.String()] splitter := userSplitters[userId]
id := strconv.Itoa(counter) id := strconv.Itoa(counter)
counter += 1 counter += 1

View File

@ -135,14 +135,24 @@ func main() {
return return
} }
processingImages, err := imageModel.GetProcessing(r.Context(), userId)
if err != nil {
log.Println(err)
w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, "Something went wrong")
return
}
type ImagesReturn struct { type ImagesReturn struct {
UserImages []models.UserImageWithImage UserImages []models.UserImageWithImage
ImageProperties []models.TypedProperties ImageProperties []models.TypedProperties
ProcessingImages []models.UserProcessingImage
} }
imagesReturn := ImagesReturn{ imagesReturn := ImagesReturn{
UserImages: images, UserImages: images,
ImageProperties: models.GetTypedImageProperties(imageProperties), ImageProperties: models.GetTypedImageProperties(imageProperties),
ProcessingImages: processingImages,
} }
jsonImages, err := json.Marshal(imagesReturn) jsonImages, err := json.Marshal(imagesReturn)

View File

@ -5,6 +5,7 @@ import (
"database/sql" "database/sql"
"errors" "errors"
"fmt" "fmt"
"screenmark/screenmark/.gen/haystack/haystack/enum"
"screenmark/screenmark/.gen/haystack/haystack/model" "screenmark/screenmark/.gen/haystack/haystack/model"
. "screenmark/screenmark/.gen/haystack/haystack/table" . "screenmark/screenmark/.gen/haystack/haystack/table"
@ -29,6 +30,12 @@ type ProcessingImageData struct {
Image model.Image Image model.Image
} }
type UserProcessingImage struct {
model.UserImagesToProcess
Image model.Image
}
func (m ImageModel) Process(ctx context.Context, userId uuid.UUID, image model.Image) (model.UserImagesToProcess, error) { func (m ImageModel) Process(ctx context.Context, userId uuid.UUID, image model.Image) (model.UserImagesToProcess, error) {
tx, err := m.dbPool.BeginTx(ctx, nil) tx, err := m.dbPool.BeginTx(ctx, nil)
if err != nil { if err != nil {
@ -89,7 +96,7 @@ func (m ImageModel) GetToProcessWithData(ctx context.Context, imageId uuid.UUID)
err := stmt.QueryContext(ctx, m.dbPool, &images) err := stmt.QueryContext(ctx, m.dbPool, &images)
if len(images) != 1 { if len(images) != 1 {
return ProcessingImageData{}, errors.New(fmt.Sprintf("Expected 1, got %d\n", len(images))) return ProcessingImageData{}, fmt.Errorf("Expected 1, got %d\n", len(images))
} }
return images[0], err return images[0], err
@ -171,6 +178,23 @@ func (m ImageModel) Get(ctx context.Context, imageId uuid.UUID) (model.Image, er
return image, err return image, err
} }
func (m ImageModel) GetProcessing(ctx context.Context, userId uuid.UUID) ([]UserProcessingImage, error) {
getProcessingStmt := SELECT(UserImagesToProcess.AllColumns, Image.ID, Image.ImageName).
FROM(
UserImagesToProcess.INNER_JOIN(
Image, Image.ID.EQ(UserImagesToProcess.ImageID),
),
).WHERE(
UserImagesToProcess.UserID.EQ(UUID(userId)).
AND(UserImagesToProcess.Status.NOT_EQ(enum.Progress.Complete)),
)
images := []UserProcessingImage{}
err := getProcessingStmt.QueryContext(ctx, m.dbPool, &images)
return images, err
}
func (m ImageModel) IsUserAuthorized(ctx context.Context, imageId uuid.UUID, userId uuid.UUID) bool { func (m ImageModel) IsUserAuthorized(ctx context.Context, imageId uuid.UUID, userId uuid.UUID) bool {
getImageUserId := UserImages.SELECT(UserImages.UserID).WHERE(UserImages.ImageID.EQ(UUID(imageId))) getImageUserId := UserImages.SELECT(UserImages.UserID).WHERE(UserImages.ImageID.EQ(UUID(imageId)))