From bdec2050317d8e3ef21dbb5b68fefc5389663789 Mon Sep 17 00:00:00 2001 From: viktorstrate Date: Thu, 12 Mar 2020 12:55:53 +0100 Subject: [PATCH] Cleanup + debounce scanner notifications --- api/go.mod | 1 + api/go.sum | 2 ++ api/scanner/album_scanner.go | 24 ++++++++++++------- api/scanner/process_photo.go | 2 -- ui/src/components/photoGallery/Photo.js | 2 +- .../components/photoGallery/PhotoGallery.js | 2 -- 6 files changed, 19 insertions(+), 14 deletions(-) diff --git a/api/go.mod b/api/go.mod index f8e5032e..93e4aea7 100644 --- a/api/go.mod +++ b/api/go.mod @@ -4,6 +4,7 @@ go 1.13 require ( github.com/99designs/gqlgen v0.10.2 + github.com/bep/debounce v1.2.0 github.com/disintegration/imaging v1.6.2 github.com/fatih/color v1.9.0 github.com/go-sql-driver/mysql v1.5.0 diff --git a/api/go.sum b/api/go.sum index 81cfb4b3..4dd758e0 100644 --- a/api/go.sum +++ b/api/go.sum @@ -3,6 +3,8 @@ github.com/99designs/gqlgen v0.10.2/go.mod h1:aDB7oabSAyZ4kUHLEySsLxnWrBy3lA0A2g github.com/agnivade/levenshtein v1.0.1 h1:3oJU7J3FGFmyhn8KHjmVaZCN5hxTr7GxgRue+sxIXdQ= github.com/agnivade/levenshtein v1.0.1/go.mod h1:CURSv5d9Uaml+FovSIICkLbAUZ9S4RqaHDIsdSBg7lM= github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883/go.mod h1:rCTlJbsFo29Kk6CurOXKm700vrz8f0KW0JNfpkRJY/8= +github.com/bep/debounce v1.2.0 h1:wXds8Kq8qRfwAOpAxHrJDbCXgC5aHSzgQb/0gKsHQqo= +github.com/bep/debounce v1.2.0/go.mod h1:H8yggRPQKLUhUoqrJC1bO2xNya7vanpDl7xR3ISbCJ0= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= diff --git a/api/scanner/album_scanner.go b/api/scanner/album_scanner.go index 2ca14a08..ca480725 100644 --- a/api/scanner/album_scanner.go +++ b/api/scanner/album_scanner.go @@ -10,7 +10,9 @@ import ( "path" "strconv" "strings" + "time" + "github.com/bep/debounce" "github.com/h2non/filetype" "github.com/viktorstrate/photoview/api/graphql/models" "github.com/viktorstrate/photoview/api/graphql/notification" @@ -80,6 +82,7 @@ func scan(database *sql.DB, user *models.User) { notifyKey := utils.GenerateToken() processKey := utils.GenerateToken() + scanNotifyDebounce := debounce.New(1 * time.Second) timeout := 3000 notification.BroadcastNotification(&models.Notification{ @@ -178,14 +181,14 @@ func scan(database *sql.DB, user *models.User) { if isNewPhoto { newPhotos.PushBack(photo) - if newPhotos.Len()%25 == 0 { + scanNotifyDebounce(func() { notification.BroadcastNotification(&models.Notification{ Key: processKey, Type: models.NotificationTypeMessage, Header: "Scanning photo", Content: fmt.Sprintf("Scanning image at %s", photoPath), }) - } + }) } tx.Commit() @@ -329,6 +332,7 @@ func isPathImage(path string, cache *scanner_cache) bool { func processUnprocessedPhotos(database *sql.DB, user *models.User, notifyKey string) error { processKey := utils.GenerateToken() + processNotifyDebounce := debounce.New(500 * time.Microsecond) rows, err := database.Query(` SELECT photo.* FROM photo JOIN album ON photo.album_id = album.album_id @@ -362,14 +366,16 @@ func processUnprocessedPhotos(database *sql.DB, user *models.User, notifyKey str continue } - var progress float64 = float64(count) / float64(len(photosToProcess)) * 100.0 + processNotifyDebounce(func() { + var progress float64 = float64(count) / float64(len(photosToProcess)) * 100.0 - notification.BroadcastNotification(&models.Notification{ - Key: processKey, - Type: models.NotificationTypeProgress, - Header: fmt.Sprintf("Processing photos (%d of %d)", count, len(photosToProcess)), - Content: fmt.Sprintf("Processing photo at %s", photo.Path), - Progress: &progress, + notification.BroadcastNotification(&models.Notification{ + Key: processKey, + Type: models.NotificationTypeProgress, + Header: fmt.Sprintf("Processing photos (%d of %d)", count, len(photosToProcess)), + Content: fmt.Sprintf("Processing photo at %s", photo.Path), + Progress: &progress, + }) }) err = ProcessPhoto(tx, photo) diff --git a/api/scanner/process_photo.go b/api/scanner/process_photo.go index b59f814d..431c8dbc 100644 --- a/api/scanner/process_photo.go +++ b/api/scanner/process_photo.go @@ -342,8 +342,6 @@ func (img *ProcessImageData) PhotoImage(tx *sql.Tx) (image.Image, error) { } } - log.Printf("ORIENTATION: %d\n", orientation) - switch orientation { case 2: photoImg = imaging.FlipH(photoImg) diff --git a/ui/src/components/photoGallery/Photo.js b/ui/src/components/photoGallery/Photo.js index 14e14e7c..01620922 100644 --- a/ui/src/components/photoGallery/Photo.js +++ b/ui/src/components/photoGallery/Photo.js @@ -7,7 +7,7 @@ import ProtectedImage from './ProtectedImage' const PhotoContainer = styled.div` flex-grow: 1; - /* flex-basis: 1; */ + flex-basis: 0; height: 200px; margin: 4px; background-color: #eee; diff --git a/ui/src/components/photoGallery/PhotoGallery.js b/ui/src/components/photoGallery/PhotoGallery.js index add7dca8..b583081f 100644 --- a/ui/src/components/photoGallery/PhotoGallery.js +++ b/ui/src/components/photoGallery/PhotoGallery.js @@ -78,8 +78,6 @@ const PhotoGallery = ({ ) } - console.log(minWidth) - return (