From 4c30aabb4361697c55190671236ac0889446e77e Mon Sep 17 00:00:00 2001 From: Frank Elsinga Date: Fri, 1 Sep 2023 13:19:54 +0200 Subject: [PATCH 01/12] fixed docker builds not being done on PRs (#207) --- .github/workflows/main.yml | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 1bf16dee..3a06b89a 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -1,7 +1,9 @@ name: CI on: push: - branches: main + branches: [main] + pull_request: + branches: [ main ] jobs: build: runs-on: ubuntu-latest @@ -35,7 +37,9 @@ jobs: with: context: server pull: true - push: true + push: ${{ github.event_name != 'pull_request' }} build-args: version=${{ github.sha }} tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} + cache-from: type=gha + cache-to: type=gha,mode=max From cc51e64a1a58a11b767c8b86fc33a8711b4bb726 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Fri, 1 Sep 2023 20:47:27 +0200 Subject: [PATCH 02/12] Bump github.com/microcosm-cc/bluemonday from 1.0.23 to 1.0.25 in /server (#209) Bumps [github.com/microcosm-cc/bluemonday](https://github.com/microcosm-cc/bluemonday) from 1.0.23 to 1.0.25. - [Release notes](https://github.com/microcosm-cc/bluemonday/releases) - [Commits](https://github.com/microcosm-cc/bluemonday/compare/v1.0.23...v1.0.25) --- updated-dependencies: - dependency-name: github.com/microcosm-cc/bluemonday dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- server/go.mod | 2 +- server/go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/server/go.mod b/server/go.mod index 9840a7ee..bd2f8dad 100644 --- a/server/go.mod +++ b/server/go.mod @@ -11,7 +11,7 @@ require ( github.com/grpc-ecosystem/grpc-gateway/v2 v2.17.1 github.com/guregu/null v4.0.0+incompatible github.com/influxdata/influxdb-client-go/v2 v2.12.3 - github.com/microcosm-cc/bluemonday v1.0.23 + github.com/microcosm-cc/bluemonday v1.0.25 github.com/mmcdole/gofeed v1.2.0 github.com/satori/go.uuid v1.2.0 github.com/sirupsen/logrus v1.9.3 diff --git a/server/go.sum b/server/go.sum index 78d7ab2d..32c8094b 100644 --- a/server/go.sum +++ b/server/go.sum @@ -80,8 +80,8 @@ github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hd github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ= github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y= -github.com/microcosm-cc/bluemonday v1.0.23 h1:SMZe2IGa0NuHvnVNAZ+6B38gsTbi5e4sViiWJyDDqFY= -github.com/microcosm-cc/bluemonday v1.0.23/go.mod h1:mN70sk7UkkF8TUr2IGBpNN0jAgStuPzlK76QuruE/z4= +github.com/microcosm-cc/bluemonday v1.0.25 h1:4NEwSfiJ+Wva0VxN5B8OwMicaJvD8r9tlJWm9rtloEg= +github.com/microcosm-cc/bluemonday v1.0.25/go.mod h1:ZIOjCQp1OrzBBPIJmfX4qDYFuhU02nx4bn030ixfHLE= github.com/microsoft/go-mssqldb v0.21.0 h1:p2rpHIL7TlSv1QrbXJUAcbyRKnIT0C9rRkH2E4OjLn8= github.com/mmcdole/gofeed v1.2.0 h1:kuq7tJnDf0pnsDzF820ukuySHxFimAcizpG15gYHIns= github.com/mmcdole/gofeed v1.2.0/go.mod h1:TEyTG4gw4Q5Co+Hgahx/Oi3E0JHLM8BXtWC+mkJtRsw= From 75685614f7fbb9e9cf2c390fb548996c425df293 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Fri, 1 Sep 2023 20:49:20 +0200 Subject: [PATCH 03/12] Bump github.com/getsentry/sentry-go from 0.20.0 to 0.23.0 in /server (#210) Bumps [github.com/getsentry/sentry-go](https://github.com/getsentry/sentry-go) from 0.20.0 to 0.23.0. - [Release notes](https://github.com/getsentry/sentry-go/releases) - [Changelog](https://github.com/getsentry/sentry-go/blob/master/CHANGELOG.md) - [Commits](https://github.com/getsentry/sentry-go/compare/v0.20.0...v0.23.0) --- updated-dependencies: - dependency-name: github.com/getsentry/sentry-go dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- server/go.mod | 2 +- server/go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/server/go.mod b/server/go.mod index bd2f8dad..6cc0cc65 100644 --- a/server/go.mod +++ b/server/go.mod @@ -5,7 +5,7 @@ go 1.18 require ( github.com/disintegration/imaging v1.6.2 github.com/gabriel-vasile/mimetype v1.4.2 - github.com/getsentry/sentry-go v0.20.0 + github.com/getsentry/sentry-go v0.23.0 github.com/go-gormigrate/gormigrate/v2 v2.1.0 github.com/golang-jwt/jwt v3.2.2+incompatible github.com/grpc-ecosystem/grpc-gateway/v2 v2.17.1 diff --git a/server/go.sum b/server/go.sum index 32c8094b..34d763c3 100644 --- a/server/go.sum +++ b/server/go.sum @@ -17,8 +17,8 @@ github.com/disintegration/imaging v1.6.2/go.mod h1:44/5580QXChDfwIclfc/PCwrr44am github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU= github.com/gabriel-vasile/mimetype v1.4.2/go.mod h1:zApsH/mKG4w07erKIaJPFiX0Tsq9BFQgN3qGY5GnNgA= github.com/getkin/kin-openapi v0.61.0/go.mod h1:7Yn5whZr5kJi6t+kShccXS8ae1APpYTW6yheSwk8Yi4= -github.com/getsentry/sentry-go v0.20.0 h1:bwXW98iMRIWxn+4FgPW7vMrjmbym6HblXALmhjHmQaQ= -github.com/getsentry/sentry-go v0.20.0/go.mod h1:lc76E2QywIyW8WuBnwl8Lc4bkmQH4+w1gwTf25trprY= +github.com/getsentry/sentry-go v0.23.0 h1:dn+QRCeJv4pPt9OjVXiMcGIBIefaTJPw/h0bZWO05nE= +github.com/getsentry/sentry-go v0.23.0/go.mod h1:lc76E2QywIyW8WuBnwl8Lc4bkmQH4+w1gwTf25trprY= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= github.com/go-chi/chi/v5 v5.0.0/go.mod h1:BBug9lr0cqtdAhsu6R4AAdvufI0/XBzAQSsUqJpoZOs= github.com/go-errors/errors v1.4.2 h1:J6MZopCL4uSllY1OfXM374weqZFFItUbrImctkmUxIA= From 923285745e14b365791441d5cd5adfdfdd985408 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Fri, 1 Sep 2023 18:50:26 +0000 Subject: [PATCH 04/12] Bump github.com/mmcdole/gofeed from 1.2.0 to 1.2.1 in /server (#208) --- server/go.mod | 4 ++-- server/go.sum | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/server/go.mod b/server/go.mod index 6cc0cc65..f8c7e795 100644 --- a/server/go.mod +++ b/server/go.mod @@ -12,7 +12,7 @@ require ( github.com/guregu/null v4.0.0+incompatible github.com/influxdata/influxdb-client-go/v2 v2.12.3 github.com/microcosm-cc/bluemonday v1.0.25 - github.com/mmcdole/gofeed v1.2.0 + github.com/mmcdole/gofeed v1.2.1 github.com/satori/go.uuid v1.2.0 github.com/sirupsen/logrus v1.9.3 github.com/soheilhy/cmux v0.1.5 @@ -42,7 +42,7 @@ require ( github.com/jinzhu/now v1.1.5 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/kr/text v0.2.0 // indirect - github.com/mmcdole/goxpp v0.0.0-20200921145534-2f3784f67354 // indirect + github.com/mmcdole/goxpp v1.1.0 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect github.com/pkg/errors v0.9.1 // indirect diff --git a/server/go.sum b/server/go.sum index 34d763c3..e8dd74ad 100644 --- a/server/go.sum +++ b/server/go.sum @@ -83,10 +83,10 @@ github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwp github.com/microcosm-cc/bluemonday v1.0.25 h1:4NEwSfiJ+Wva0VxN5B8OwMicaJvD8r9tlJWm9rtloEg= github.com/microcosm-cc/bluemonday v1.0.25/go.mod h1:ZIOjCQp1OrzBBPIJmfX4qDYFuhU02nx4bn030ixfHLE= github.com/microsoft/go-mssqldb v0.21.0 h1:p2rpHIL7TlSv1QrbXJUAcbyRKnIT0C9rRkH2E4OjLn8= -github.com/mmcdole/gofeed v1.2.0 h1:kuq7tJnDf0pnsDzF820ukuySHxFimAcizpG15gYHIns= -github.com/mmcdole/gofeed v1.2.0/go.mod h1:TEyTG4gw4Q5Co+Hgahx/Oi3E0JHLM8BXtWC+mkJtRsw= -github.com/mmcdole/goxpp v0.0.0-20200921145534-2f3784f67354 h1:Z6i7ND25ixRtXFBylIUggqpvLMV1I15yprcqMVB7WZA= -github.com/mmcdole/goxpp v0.0.0-20200921145534-2f3784f67354/go.mod h1:pasqhqstspkosTneA62Nc+2p9SOBBYAPbnmRRWPQ0V8= +github.com/mmcdole/gofeed v1.2.1 h1:tPbFN+mfOLcM1kDF1x2c/N68ChbdBatkppdzf/vDe1s= +github.com/mmcdole/gofeed v1.2.1/go.mod h1:2wVInNpgmC85q16QTTuwbuKxtKkHLCDDtf0dCmnrNr4= +github.com/mmcdole/goxpp v1.1.0 h1:WwslZNF7KNAXTFuzRtn/OKZxFLJAAyOA9w82mDz2ZGI= +github.com/mmcdole/goxpp v1.1.0/go.mod h1:v+25+lT2ViuQ7mVxcncQ8ch1URund48oH+jhjiwEgS8= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= From 1d38f81bbb1a79eb0b43d7ce0ad81d0d4b447312 Mon Sep 17 00:00:00 2001 From: Frank Elsinga Date: Mon, 4 Sep 2023 20:46:09 +0200 Subject: [PATCH 05/12] chore:more strict linting (#206) * bumped the staticcheck version we are tested against * migrated from `staticcheck` to `golangci-lint` (includes `staticcheck` and other linters) * renamed the workflow file from `staticcheck` to `ci` * migrated to go 1.21 * fixed linting not running * fixed defects the new logger turned up * fixed more issues turned up by the linter * fixed more issues turned up by the linter * fixed another bug * Apply suggestions from code review --- .github/workflows/ci.yml | 29 ++++++++++++++ .github/workflows/staticcheck.yml | 22 ----------- client/go.mod | 2 +- client/go.sum | 4 ++ server/Dockerfile | 2 +- .../backend/cafeteriaRatingDBInitializer.go | 38 ++++++++++--------- server/backend/cafeteriaService.go | 3 +- server/backend/cron/canteenHeadCount.go | 8 +++- server/backend/cron/cronjobs.go | 12 +++--- server/backend/cron/dishNameDownload.go | 9 +++-- server/backend/cron/news.go | 11 ++++-- .../ios_scheduling/iosSchedulingService.go | 11 +++--- server/backend/rpcserver.go | 2 +- server/go.mod | 2 +- server/go.sum | 18 +++++++++ 15 files changed, 108 insertions(+), 65 deletions(-) create mode 100644 .github/workflows/ci.yml delete mode 100644 .github/workflows/staticcheck.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 00000000..505e0da3 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,29 @@ +name: CI +on: + push: + branches: [ main ] + pull_request: + +permissions: + contents: read + +jobs: + ci: + name: lint + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-go@v4 + with: + go-version: '1.21' + cache: false + - name: golangci-lint-server + uses: golangci/golangci-lint-action@v3 + with: + version: v1.54 + working-directory: server + - name: golangci-lint-client + uses: golangci/golangci-lint-action@v3 + with: + version: v1.54 + working-directory: client diff --git a/.github/workflows/staticcheck.yml b/.github/workflows/staticcheck.yml deleted file mode 100644 index 921f886b..00000000 --- a/.github/workflows/staticcheck.yml +++ /dev/null @@ -1,22 +0,0 @@ -name: "staticcheck" - -on: - push: - branches: [ main ] - pull_request: - branches: [ main ] - -jobs: - ci: - name: "Run staticcheck" - runs-on: ubuntu-latest - strategy: - matrix: { dir: [ './server' ] } - steps: - - uses: actions/checkout@v3 - with: - fetch-depth: 1 - - uses: dominikh/staticcheck-action@v1.3.0 - with: - version: "2022.1" - working-directory: ${{ matrix.dir }} diff --git a/client/go.mod b/client/go.mod index 9d696d48..dac32653 100644 --- a/client/go.mod +++ b/client/go.mod @@ -1,6 +1,6 @@ module github.com/TUM-Dev/Campus-Backend/client -go 1.18 +go 1.21 require ( github.com/TUM-Dev/Campus-Backend/api v0.0.0-20221212204029-68b05b451617 diff --git a/client/go.sum b/client/go.sum index 2d5e3449..21378bde 100644 --- a/client/go.sum +++ b/client/go.sum @@ -12,10 +12,13 @@ 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/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/grpc-ecosystem/grpc-gateway/v2 v2.11.3 h1:lLT7ZLSzGLI08vc9cpd+tYmNWjdKDqyr/2L+f6U12Fk= github.com/grpc-ecosystem/grpc-gateway/v2 v2.11.3/go.mod h1:o//XUCC/F+yRGJoPO/VU0GSB0f8Nhgmxx0VIRUvaC0w= github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= 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/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= @@ -48,6 +51,7 @@ google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/server/Dockerfile b/server/Dockerfile index 204bf122..fb044b41 100644 --- a/server/Dockerfile +++ b/server/Dockerfile @@ -1,4 +1,4 @@ -FROM golang:1.19.3-alpine3.15 as builder +FROM golang:1.21-alpine3.18 as builder # Install git + SSL ca certificates. # Git is required for fetching the dependencies. diff --git a/server/backend/cafeteriaRatingDBInitializer.go b/server/backend/cafeteriaRatingDBInitializer.go index f51dd4db..b8fd3056 100644 --- a/server/backend/cafeteriaRatingDBInitializer.go +++ b/server/backend/cafeteriaRatingDBInitializer.go @@ -103,21 +103,22 @@ func updateNameTagOptions(db *gorm.DB) { func addNotIncluded(parentId int32, db *gorm.DB, v nameTag) { var count int64 - for _, u := range v.NotIncluded { - errorLoadingIncluded := db.Model(&model.DishNameTagOptionExcluded{}). - Where("expression LIKE ? AND NameTagID = ?", u, parentId). + for _, expression := range v.NotIncluded { + fields := log.Fields{"expression": expression, "parentId": parentId} + err := db.Model(&model.DishNameTagOptionExcluded{}). + Where("expression LIKE ? AND NameTagID = ?", expression, parentId). Select("DishNameTagOptionExcluded"). Count(&count).Error - if errorLoadingIncluded != nil { - log.WithError(errorLoadingIncluded).Errorf("Unable to load can be excluded tag with expression %s and parentId %s", u, parentId) + if err != nil { + log.WithError(err).WithFields(fields).Error("Unable to load can be excluded tag") } else { if count == 0 { - createError := db.Model(&model.DishNameTagOptionExcluded{}). + err := db.Model(&model.DishNameTagOptionExcluded{}). Create(&model.DishNameTagOptionExcluded{ - Expression: u, + Expression: expression, NameTagID: parentId}).Error - if createError != nil { - log.WithError(errorLoadingIncluded).Error("Unable to create new can be excluded tag with expression {} and parentId {} ", u, parentId) + if err != nil { + log.WithError(err).WithFields(fields).Error("Unable to create new can be excluded tag") } } } @@ -126,22 +127,23 @@ func addNotIncluded(parentId int32, db *gorm.DB, v nameTag) { func addCanBeIncluded(parentId int32, db *gorm.DB, v nameTag) { var count int64 - for _, u := range v.CanBeIncluded { - errorLoadingIncluded := db.Model(&model.DishNameTagOptionIncluded{}). - Where("expression LIKE ? AND NameTagID = ?", u, parentId). + for _, expression := range v.CanBeIncluded { + fields := log.Fields{"expression": expression, "parentId": parentId} + err := db.Model(&model.DishNameTagOptionIncluded{}). + Where("expression LIKE ? AND NameTagID = ?", expression, parentId). Select("DishNameTagOptionIncluded"). Count(&count).Error - if errorLoadingIncluded != nil { - log.WithError(errorLoadingIncluded).Errorf("Unable to load can be included tag with expression %s and parentId %s", u, parentId) + if err != nil { + log.WithError(err).WithFields(fields).Error("Unable to load can be included tag") } else { if count == 0 { - createError := db.Model(&model.DishNameTagOptionIncluded{}). + err := db.Model(&model.DishNameTagOptionIncluded{}). Create(&model.DishNameTagOptionIncluded{ - Expression: u, + Expression: expression, NameTagID: parentId, }).Error - if createError != nil { - log.WithError(errorLoadingIncluded).Errorf("Unable to create new can be excluded tag with expression %s and parentId %s", u, parentId) + if err != nil { + log.WithError(err).WithFields(fields).Error("Unable to create new can be excluded tag") } } } diff --git a/server/backend/cafeteriaService.go b/server/backend/cafeteriaService.go index 0c79a9be..55acd47a 100644 --- a/server/backend/cafeteriaService.go +++ b/server/backend/cafeteriaService.go @@ -155,7 +155,8 @@ func (s *CampusServer) GetDishRatings(_ context.Context, input *pb.DishRatingReq First(&result) if err.Error != nil { - log.WithError(err.Error).Errorf("Error while querying the average ratings for the dish %s in the cafeteria %s.", dishID, cafeteriaID) + fields := log.Fields{"dishID": dishID, "cafeteriaID": cafeteriaID} + log.WithError(err.Error).WithFields(fields).Error("Error while querying the average ratings") return nil, status.Errorf(codes.Internal, "This dish has not yet been rated.") } diff --git a/server/backend/cron/canteenHeadCount.go b/server/backend/cron/canteenHeadCount.go index 2bb486c5..168af083 100644 --- a/server/backend/cron/canteenHeadCount.go +++ b/server/backend/cron/canteenHeadCount.go @@ -143,8 +143,12 @@ func (c *CronService) canteenHeadCountCron() error { } count := sumApCounts(aps) - updateDb(&canteen, count, c.db) - log.Debug("Canteen head count stats (", count, ") updated for: ", canteen.CanteenId) + fields := log.Fields{"count": count, "CanteenId": canteen.CanteenId} + if err := updateDb(&canteen, count, c.db); err != nil { + log.WithFields(fields).WithError(err).Error("Failed to update Canteen head count stats") + } else { + log.WithFields(fields).Debug("Canteen head count stats updated") + } } log.Info("Canteen head count stats updated.") return nil diff --git a/server/backend/cron/cronjobs.go b/server/backend/cron/cronjobs.go index d37dc1b4..8bf15fad 100644 --- a/server/backend/cron/cronjobs.go +++ b/server/backend/cron/cronjobs.go @@ -84,10 +84,13 @@ func (c *CronService) Run() error { cronjob.LastRun = int32(time.Now().Unix()) + offset c.db.Save(&cronjob) - // Run each job in a separate goroutine so we can parallelize them + // Run each job in a separate goroutine, so we can parallelize them switch cronjob.Type.String { case NewsType: - g.Go(func() error { return c.newsCron(&cronjob) }) + // if this is not copied here, this may not be threads save due to go's guarantees + // loop variable cronjob captured by func literal (govet) + copyCronjob := cronjob + g.Go(func() error { return c.newsCron(©Cronjob) }) case FileDownloadType: g.Go(func() error { return c.fileDownloadCron() }) case DishNameDownload: @@ -122,9 +125,8 @@ func (c *CronService) Run() error { } } - err := g.Wait() - if err != nil { - log.Println("Couldn't run all cron jobs: %v", err) + if err := g.Wait(); err != nil { + log.WithError(err).Println("Couldn't run all cron jobs") } log.Trace("Cron: sleeping for 60 seconds") time.Sleep(60 * time.Second) diff --git a/server/backend/cron/dishNameDownload.go b/server/backend/cron/dishNameDownload.go index 46e2de86..1dab432e 100644 --- a/server/backend/cron/dishNameDownload.go +++ b/server/backend/cron/dishNameDownload.go @@ -202,14 +202,15 @@ func addDishTagsToMapping(dishID int32, dishName string, db *gorm.DB) { } } - for _, a := range includedTags { - if a != -1 { + for _, nametagID := range includedTags { + if nametagID != -1 { err := db.Model(&model.DishToDishNameTag{}).Create(&model.DishToDishNameTag{ DishID: dishID, - NameTagID: a, + NameTagID: nametagID, }).Error if err != nil { - log.WithError(err).Errorf("Error while creating a new entry with dish %s and nametag %s", dishID, a) + fields := log.Fields{"dishID": dishID, "nametagID": nametagID} + log.WithError(err).WithFields(fields).Error("creating a new entry") } } } diff --git a/server/backend/cron/news.go b/server/backend/cron/news.go index 95ef7be3..4a7f4723 100644 --- a/server/backend/cron/news.go +++ b/server/backend/cron/news.go @@ -3,7 +3,6 @@ package cron import ( "crypto/md5" "database/sql" - "encoding/json" "errors" "fmt" "github.com/TUM-Dev/Campus-Backend/server/model" @@ -31,8 +30,14 @@ var ImageContentTypeRegex, _ = regexp.Compile("image/[a-z.]+") func (c *CronService) newsCron(cronjob *model.Crontab) error { //check if source id provided for news job is not null if !cronjob.ID.Valid { - cronjobJson, _ := json.Marshal(cronjob) - log.Println("skipping news job, id of source is null, cronjob: %s", string(cronjobJson)) + fields := log.Fields{ + "Cron": cronjob.Cron, + "Interval": cronjob.Interval, + "LastRun": cronjob.LastRun, + "Type": cronjob.Type, + "ID": cronjob.ID, + } + log.WithFields(fields).Warn("skipping news job, id of source is null") return nil } // get news source for cronjob diff --git a/server/backend/ios_notifications/ios_scheduling/iosSchedulingService.go b/server/backend/ios_notifications/ios_scheduling/iosSchedulingService.go index c27709c1..a2e60dad 100644 --- a/server/backend/ios_notifications/ios_scheduling/iosSchedulingService.go +++ b/server/backend/ios_notifications/ios_scheduling/iosSchedulingService.go @@ -97,14 +97,13 @@ func (service *Service) handleDevices(devices []model.IOSDeviceLastUpdated) { func (service *Service) handleDevicesChunk(devices []model.IOSDeviceLastUpdated) { for _, device := range devices { - err := service.APNs.RequestGradeUpdateForDevice(device.DeviceID) - - if err != nil { - log.Errorf("Error while handling device: %s", err) + if err := service.APNs.RequestGradeUpdateForDevice(device.DeviceID); err != nil { + log.WithError(err).Error("could not RequestGradeUpdateForDevice") continue } - - service.LogScheduledUpdate(device.DeviceID) + if err := service.LogScheduledUpdate(device.DeviceID); err != nil { + log.WithError(err).WithField("deviceID", device.DeviceID).Error("could not log scheduled update") + } } } diff --git a/server/backend/rpcserver.go b/server/backend/rpcserver.go index 5c974785..0a4b6d00 100644 --- a/server/backend/rpcserver.go +++ b/server/backend/rpcserver.go @@ -153,7 +153,7 @@ func (s *CampusServer) GetTopNews(ctx context.Context, _ *emptypb.Empty) (*pb.Ge var res *model.NewsAlert err := s.db.Joins("Company").Where("NOW() between `from` and `to`").Limit(1).First(&res).Error if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { - log.Errorf("Failed to fetch top news: %w", err) + log.WithError(err).Errorf("Failed to fetch top news") } else if res != nil { return &pb.GetTopNewsReply{ //ImageUrl: res.Name, diff --git a/server/go.mod b/server/go.mod index f8c7e795..af09fc68 100644 --- a/server/go.mod +++ b/server/go.mod @@ -1,6 +1,6 @@ module github.com/TUM-Dev/Campus-Backend/server -go 1.18 +go 1.21 require ( github.com/disintegration/imaging v1.6.2 diff --git a/server/go.sum b/server/go.sum index e8dd74ad..9a66ee21 100644 --- a/server/go.sum +++ b/server/go.sum @@ -22,6 +22,7 @@ github.com/getsentry/sentry-go v0.23.0/go.mod h1:lc76E2QywIyW8WuBnwl8Lc4bkmQH4+w github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= github.com/go-chi/chi/v5 v5.0.0/go.mod h1:BBug9lr0cqtdAhsu6R4AAdvufI0/XBzAQSsUqJpoZOs= github.com/go-errors/errors v1.4.2 h1:J6MZopCL4uSllY1OfXM374weqZFFItUbrImctkmUxIA= +github.com/go-errors/errors v1.4.2/go.mod h1:sIVyrIiJhuEF+Pj9Ebtd6P/rEYROXFi3BopGUQ5a5Og= github.com/go-gormigrate/gormigrate/v2 v2.1.0 h1:4/1xr9CjOox714EJWbxkF00lrNmbWJToSZzhykKKcKY= github.com/go-gormigrate/gormigrate/v2 v2.1.0/go.mod h1:gpA97koYGyjqaiLDTmLE5W7nyYTmI26AYIf2a/earuo= github.com/go-openapi/jsonpointer v0.19.5/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg= @@ -31,7 +32,9 @@ github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9 github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY= github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I= github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 h1:au07oEsX2xN0ktxqI+Sida1w446QrXBRJ0nee3SNZlA= +github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9/go.mod h1:8vg3r2VgvsThLBIFL93Qb5yWzgyZWhEmBwUJWevAkK0= github.com/golang-sql/sqlexp v0.1.0 h1:ZCD6MBpcuOVfGVqsEmY5/4FtYiKz6tSyUv9LPEDei6A= +github.com/golang-sql/sqlexp v0.1.0/go.mod h1:J4ad9Vo8ZCWQ2GMrC4UCQy1JpCbwU9m3EOqtpKwwwHI= github.com/golang/glog v1.1.0 h1:/d3pCKDPWNnvIWe0vVUpNP32qc8U3PDVxySP/y360qE= github.com/golang/glog v1.1.0/go.mod h1:pfYeQZ3JWZoXTV5sFc986z3HTpwQs9At6P4ImfuP3NQ= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= @@ -40,6 +43,7 @@ github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiu github.com/golangci/lint-1 v0.0.0-20181222135242-d2cdd8c08219/go.mod h1:/X8TswGSh1pIozq4ZwCfxS0WA5JGXguxk94ar/4c87Y= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/gorilla/css v1.0.0 h1:BQqNyPTi50JCFMTw/b67hByjMVXZRwGha6wxVGkeihY= github.com/gorilla/css v1.0.0/go.mod h1:Dn721qIggHpt4+EFCcTLTU/vk5ySda2ReITrtgBl60c= @@ -53,17 +57,22 @@ github.com/influxdata/influxdb-client-go/v2 v2.12.3/go.mod h1:IrrLUbCjjfkmRuaCiG github.com/influxdata/line-protocol v0.0.0-20200327222509-2487e7298839 h1:W9WBk7wlPfJLvMCdtV4zPulc4uCPrlywQOmbFOhgQNU= github.com/influxdata/line-protocol v0.0.0-20200327222509-2487e7298839/go.mod h1:xaLFMmpvUxqXtVkUJfg9QmT88cDaCJ3ZKgdZ78oO8Qo= github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= +github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk= +github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= github.com/jackc/pgx/v5 v5.3.1 h1:Fcr8QJ1ZeLi5zsPZqQeUZhNhxfkkKBOgJuYkJHoBOtU= +github.com/jackc/pgx/v5 v5.3.1/go.mod h1:t3JDKnCBlYIc0ewLF0Q7B8MXmoIaBOZj/ic7iHozM/8= github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= +github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= @@ -80,9 +89,11 @@ github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hd github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ= github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y= +github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= github.com/microcosm-cc/bluemonday v1.0.25 h1:4NEwSfiJ+Wva0VxN5B8OwMicaJvD8r9tlJWm9rtloEg= github.com/microcosm-cc/bluemonday v1.0.25/go.mod h1:ZIOjCQp1OrzBBPIJmfX4qDYFuhU02nx4bn030ixfHLE= github.com/microsoft/go-mssqldb v0.21.0 h1:p2rpHIL7TlSv1QrbXJUAcbyRKnIT0C9rRkH2E4OjLn8= +github.com/microsoft/go-mssqldb v0.21.0/go.mod h1:+4wZTUnz/SV6nffv+RRRB/ss8jPng5Sho2SmM1l2ts4= github.com/mmcdole/gofeed v1.2.1 h1:tPbFN+mfOLcM1kDF1x2c/N68ChbdBatkppdzf/vDe1s= github.com/mmcdole/gofeed v1.2.1/go.mod h1:2wVInNpgmC85q16QTTuwbuKxtKkHLCDDtf0dCmnrNr4= github.com/mmcdole/goxpp v1.1.0 h1:WwslZNF7KNAXTFuzRtn/OKZxFLJAAyOA9w82mDz2ZGI= @@ -93,12 +104,14 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJ github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= github.com/pingcap/errors v0.11.4 h1:lFuQV/oaUMGcD2tqt+01ROSmJs75VG1ToEOkZIZ4nE4= +github.com/pingcap/errors v0.11.4/go.mod h1:Oi8TUi2kEtXXLMJk9l1cGmz20kV3TaQ0usTwv5KuLY8= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= 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/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ= +github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog= github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww= github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0= github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= @@ -122,6 +135,7 @@ golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a/go.mod h1:LzIPMQfyMNhhGPh golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.12.0 h1:tFM/ta59kqch6LlvYnPa0yx5a83cL2nHflFhYKvv9Yk= +golang.org/x/crypto v0.12.0/go.mod h1:NF0Gs7EO5K4qLn+Ylc+fih8BSTeIjAP05siRnAh98yw= golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= golang.org/x/image v0.5.0 h1:5JMiNunQeQw++mMOz48/ISeNu3Iweh/JaZU8ZLqHRrI= golang.org/x/image v0.5.0/go.mod h1:FVC7BI/5Ym8R25iw5OLsgshdUBbT1h5jZTpA+mvAdZ4= @@ -195,6 +209,7 @@ google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqw gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= @@ -203,8 +218,11 @@ gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gorm.io/driver/mysql v1.5.1 h1:WUEH5VF9obL/lTtzjmML/5e6VfFR/788coz2uaVCAZw= gorm.io/driver/mysql v1.5.1/go.mod h1:Jo3Xu7mMhCyj8dlrb3WoCaRd1FhsVh+yMXb1jUInf5o= gorm.io/driver/postgres v1.5.2 h1:ytTDxxEv+MplXOfFe3Lzm7SjG09fcdb3Z/c056DTBx0= +gorm.io/driver/postgres v1.5.2/go.mod h1:fmpX0m2I1PKuR7mKZiEluwrP3hbs+ps7JIGMUBpCgl8= gorm.io/driver/sqlite v1.5.1 h1:hYyrLkAWE71bcarJDPdZNTLWtr8XrSjOWyjUYI6xdL4= +gorm.io/driver/sqlite v1.5.1/go.mod h1:7MZZ2Z8bqyfSQA1gYEV6MagQWj3cpUkJj9Z+d1HEMEQ= gorm.io/driver/sqlserver v1.5.0 h1:zol7ePfY1XiPfjEvjjBh4VatIF3kycNcPE0EMCXznHY= +gorm.io/driver/sqlserver v1.5.0/go.mod h1:tBAqioK34BHl0Iiez+BFfG5/K9nDAlhLxRkgc2qy3+4= gorm.io/gorm v1.25.1/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k= gorm.io/gorm v1.25.4 h1:iyNd8fNAe8W9dvtlgeRI5zSVZPsq3OpcTu37cYcpCmw= gorm.io/gorm v1.25.4/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k= From f368ce6fe842f831661af7f978e1a75ca343aa09 Mon Sep 17 00:00:00 2001 From: Frank Elsinga Date: Thu, 7 Sep 2023 03:22:37 +0200 Subject: [PATCH 06/12] chore:fixed the StorageDir being intermingled with the cron job names --- server/backend/cron/cronjobs.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/server/backend/cron/cronjobs.go b/server/backend/cron/cronjobs.go index 8bf15fad..fce0a34f 100644 --- a/server/backend/cron/cronjobs.go +++ b/server/backend/cron/cronjobs.go @@ -18,6 +18,8 @@ type CronService struct { APNs *ios_apns.Service } +const StorageDir = "/Storage/" // target location of files + // names for cron jobs as specified in database const ( NewsType = "news" @@ -25,7 +27,6 @@ const ( DishNameDownload = "dishNameDownload" AverageRatingComputation = "averageRatingComputation" CanteenHeadcount = "canteenHeadCount" - StorageDir = "/Storage/" // target location of files IOSNotifications = "iosNotifications" IOSActivityReset = "iosActivityReset" From 5a2aa976117d8e8aa7ed73f4ee36e0ed9a75dee8 Mon Sep 17 00:00:00 2001 From: Frank Elsinga Date: Thu, 7 Sep 2023 03:23:35 +0200 Subject: [PATCH 07/12] chore:sped up local builds by using a separate go mod download step in the Dockerfile --- server/Dockerfile | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/server/Dockerfile b/server/Dockerfile index fb044b41..5345de5c 100644 --- a/server/Dockerfile +++ b/server/Dockerfile @@ -9,6 +9,10 @@ RUN apk update && apk add --no-cache git ca-certificates tzdata alpine-sdk bash RUN adduser -D -g '' appuser WORKDIR /app +# Copy go mod and sum files and download dependencies +COPY go.mod go.sum ./ +RUN go mod download + # Copy source code COPY . . From eaee6b038828d917f51332403e4801043669f1b7 Mon Sep 17 00:00:00 2001 From: Frank Elsinga Date: Thu, 7 Sep 2023 03:27:22 +0200 Subject: [PATCH 08/12] chore:better logging for cronjobs --- server/backend/cron/cronjobs.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/server/backend/cron/cronjobs.go b/server/backend/cron/cronjobs.go index fce0a34f..508a4a08 100644 --- a/server/backend/cron/cronjobs.go +++ b/server/backend/cron/cronjobs.go @@ -48,7 +48,7 @@ func New(db *gorm.DB, mensaCronActivated bool) *CronService { } func (c *CronService) Run() error { - log.Printf("running cron service. Mensa Crons Running: %t", c.useMensa) + log.WithField("useMensa", c.useMensa).Trace("running cron service") g := new(errgroup.Group) g.Go(func() error { return c.dishNameDownloadCron() }) @@ -81,6 +81,8 @@ func (c *CronService) Run() error { } } } + cronFields := log.Fields{"Cron (id)": cronjob.Cron, "type": cronjob.Type.String, "offset": offset, "LastRun": cronjob.LastRun, "interval": cronjob.Interval, "id (not real id)": cronjob.ID.Int64} + log.WithFields(cronFields).Trace("Running cronjob") cronjob.LastRun = int32(time.Now().Unix()) + offset c.db.Save(&cronjob) @@ -127,7 +129,7 @@ func (c *CronService) Run() error { } if err := g.Wait(); err != nil { - log.WithError(err).Println("Couldn't run all cron jobs") + log.WithError(err).Error("Couldn't run all cron jobs") } log.Trace("Cron: sleeping for 60 seconds") time.Sleep(60 * time.Second) From 2635c0efd6533293798768b124c3eba8f3a103dc Mon Sep 17 00:00:00 2001 From: Frank Elsinga Date: Thu, 7 Sep 2023 19:31:03 +0200 Subject: [PATCH 09/12] increased the timeout of the linting job --- .github/workflows/ci.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 505e0da3..92b0e5a9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -22,8 +22,10 @@ jobs: with: version: v1.54 working-directory: server + args: --timeout=30m - name: golangci-lint-client uses: golangci/golangci-lint-action@v3 with: version: v1.54 working-directory: client + args: --timeout=30m From fa6863d8a22c0daecd36dd64b9329811eb57e0fe Mon Sep 17 00:00:00 2001 From: Frank Elsinga Date: Mon, 11 Sep 2023 20:46:38 +0200 Subject: [PATCH 10/12] made sure that grpc content is routed using the h2c scheme --- deployment/charts/backend/templates/networking/ingress.yaml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/deployment/charts/backend/templates/networking/ingress.yaml b/deployment/charts/backend/templates/networking/ingress.yaml index 09d3be37..cf8ca47d 100644 --- a/deployment/charts/backend/templates/networking/ingress.yaml +++ b/deployment/charts/backend/templates/networking/ingress.yaml @@ -19,6 +19,12 @@ spec: services: - name: backend-v2-svc port: 50051 + - kind: Rule + match: (Host(`{{ join "`) || Host(`" $.Values.urls.v2 }}`)) && Headers(`Content-Type`, `application/grpc`) + services: + - name: backend-v2-svc + port: 50051 + scheme: h2c tls: secretName: app.tum.de --- From 9acfa52feae1fc8439f263acbeb0905527e79a66 Mon Sep 17 00:00:00 2001 From: Frank Elsinga Date: Tue, 12 Sep 2023 15:01:58 +0200 Subject: [PATCH 11/12] chore:metadata in dockerfile (#217) * added additional metadata to our dockerfile * simplified the healthcheck --- server/Dockerfile | 5 +++++ server/main.go | 3 +-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/server/Dockerfile b/server/Dockerfile index 5345de5c..dad8bf1f 100644 --- a/server/Dockerfile +++ b/server/Dockerfile @@ -32,5 +32,10 @@ COPY --from=builder /etc/passwd /etc/passwd # Use an unprivileged user USER appuser +# metadata +EXPOSE 50051 +HEALTHCHECK --interval=5m --timeout=3s \ + CMD wget --no-verbose --tries=1 --spider http://localhost:50051/health && exit 0 || exit 1 + # Run the main binary CMD ["/backend"] diff --git a/server/main.go b/server/main.go index 877d7575..6fb4aa60 100644 --- a/server/main.go +++ b/server/main.go @@ -116,8 +116,7 @@ func main() { }) mux.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "application/json") - _ = json.NewEncoder(w).Encode(map[string]string{"status": "healthy"}) + _, _ = w.Write([]byte("healthy")) }) static, _ := fs.Sub(swagfs, "swagger") From b80d19bb1d98b67bc0498ef1976876c63f9bfbb9 Mon Sep 17 00:00:00 2001 From: Frank Elsinga Date: Tue, 12 Sep 2023 17:32:49 +0200 Subject: [PATCH 12/12] migrated a lot of error statements to the new syntax (#200) --- client/localServer/localTestClient.go | 129 ++++++++++-------- client/publicServer/client.go | 23 ++-- .../backend/cafeteriaRatingDBInitializer.go | 22 ++- server/backend/cafeteriaService.go | 44 +++--- server/backend/campus_api/campusApi.go | 8 +- server/backend/canteenHeadCount.go | 2 +- .../backend/cron/averageRatingComputation.go | 20 +-- server/backend/cron/canteenHeadCount.go | 33 +++-- server/backend/cron/cronjobs.go | 2 +- server/backend/cron/dishNameDownload.go | 8 +- server/backend/cron/news.go | 20 +-- .../ios_apns/iosAPNsRepository.go | 2 +- .../ios_apns/iosAPNsService.go | 4 +- .../iosDevicesActivityResetRepository.go | 4 +- .../iosDevicesActivityResetService.go | 16 +-- .../iosRequestResponseService.go | 8 +- .../ios_scheduling/iosSchedulingService.go | 2 +- server/backend/migration/20221119131300.go | 18 +-- .../backend/migration/safeEnumMigrate_test.go | 2 +- server/backend/rpcserver.go | 6 +- 20 files changed, 209 insertions(+), 164 deletions(-) diff --git a/client/localServer/localTestClient.go b/client/localServer/localTestClient.go index 191e9050..d6c2a2f4 100644 --- a/client/localServer/localTestClient.go +++ b/client/localServer/localTestClient.go @@ -30,7 +30,7 @@ func main() { conn, err := grpc.Dial(localAddress, grpc.WithTransportCredentials(insecure.NewCredentials())) if err != nil { - log.Info(err) + log.WithError(err).Error("could not dial localAddress") } c := pb.NewCampusClient(conn) ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) @@ -50,8 +50,7 @@ func canteenHeadCount(c pb.CampusClient, ctx context.Context) { if err != nil { log.Error(err) } else { - log.Info("Canteen HeadCount data request successful.") - log.Info(res) + log.WithField("res", res).Info("Canteen HeadCount data request successful.") } } @@ -80,43 +79,52 @@ func queryDish(cafeteria string, dish string, c pb.CampusClient, ctx context.Con }) if err != nil { - log.Info(err) + log.WithError(err).Info("failed to query dish") } else { - log.Info("Result: ") - log.Info("\tavg : ", res.Avg) - log.Info("\tmin ", res.Min) - log.Info("\tmax ", res.Max) - log.Info("\tstd ", res.Std) - log.Info("Number of individual Ratings", len(res.Rating)) + fields := log.Fields{ + "avg": res.Avg, + "min": res.Min, + "max": res.Max, + "std": res.Std, + "individualRatings": len(res.Rating), + } + log.WithFields(fields).Info("succeeded to query dish") path := fmt.Sprintf("%s%d%s", "./testImages/dishes/", time.Now().Unix(), "/") for _, v := range res.Rating { - log.Info("\nRating: ", v.Points) - log.Info("Comment ", v.Comment) - log.Info("Number of Tag Ratings : ", len(v.RatingTags)) - log.Info("Timestamp: ", v.Visited) - log.Info("ImageLength:", len(v.Image)) + fields := log.Fields{ + "Rating": v.Points, + "Comment": v.Comment, + "Number of Tag Ratings": len(v.RatingTags), + "Timestamp": v.Visited, + "ImageLength": len(v.Image), + } + log.WithFields(fields).Info("storing image") if imageShouldBeStored { _, err := storeImage(path, v.Image) if err != nil { - log.Info("image was not saved successfully") + log.WithError(err).Error("image was not saved successfully") } } } log.Info("Rating Tags: ") for _, v := range res.RatingTags { - log.Info("TagId: ", v.TagId) - log.Info("\tavg: ", v.Avg) - log.Info("\tmin: ", v.Min) - log.Info("\tmax: ", v.Max) - log.Info("\tstd: ", v.Std) + fields := log.Fields{ + "avg": v.Avg, + "min": v.Min, + "max": v.Max, + "std": v.Std, + } + log.WithFields(fields).Info(v.TagId) } log.Info("nameTags: ") for _, v := range res.NameTags { - log.Info("TagId: ", v.TagId) - log.Info("\tavg: ", v.Avg) - log.Info("\tmin: ", v.Min) - log.Info("\tmax: ", v.Max) - log.Info("\tstd: ", v.Std) + fields := log.Fields{ + "avg": v.Avg, + "min": v.Min, + "max": v.Max, + "std": v.Std, + } + log.WithFields(fields).Info(v.TagId) } } } @@ -130,34 +138,42 @@ func queryCafeteria(s string, c pb.CampusClient, ctx context.Context, imageShoul }) if err != nil { - log.Info(err) + log.WithError(err).Error("failed to query cafeteria") } else { - log.Info("Result: ") - log.Info("avg: ", res.Avg) - log.Info("min", res.Min) - log.Info("max", res.Max) - log.Info("Number of individual Ratings", len(res.Rating)) + fields := log.Fields{ + "avg": res.Avg, + "min": res.Min, + "max": res.Max, + "std": res.Std, + "Number of individual Ratings": len(res.Rating), + } + log.WithFields(fields).Info("succeeded to query cafeteria") path := fmt.Sprintf("%s%d%s", "./testImages/cafeteria/", time.Now().Unix(), "/") - for _, v := range res.Rating { - log.Info("\nRating: ", v.Points) - log.Info("Comment ", v.Comment) - log.Info("Number of Tag Ratings: ", len(v.RatingTags)) - log.Info("Timestamp: ", v.Visited) - log.Info("ImageLength:", len(v.Image)) + for i, v := range res.Rating { + fields := log.Fields{ + "Rating": v.Points, + "Comment": v.Comment, + "Number of Tag Ratings": len(v.RatingTags), + "Timestamp": v.Visited, + "ImageLength": len(v.Image), + } + log.WithFields(fields).Infof("Rating %d", i) if imageShouldBeStored { _, err := storeImage(path, v.Image) if err != nil { - log.Info("image was not saved successfully") + log.WithError(err).Error("image was not saved successfully") } } } for _, v := range res.RatingTags { - log.Info("\nTagId: ", v.TagId) - log.Info("avg: ", v.Avg) - log.Info("min", v.Min) - log.Info("max", v.Max) - log.Info("std", v.Std) + fields := log.Fields{ + "avg": v.Avg, + "min": v.Min, + "max": v.Max, + "std": v.Std, + } + log.WithFields(fields).Info(v.TagId) } } } @@ -182,9 +198,9 @@ func generateCafeteriaRating(c pb.CampusClient, ctx context.Context, cafeteria s }) if err != nil { - log.Info(err) + log.WithError(err).Error("could not store new Cafeteria Rating") } else { - log.Info("Request successfully: Cafeteria Rating should be stored") + log.Info("Cafeteria Rating successfully be stored") } } @@ -213,9 +229,9 @@ func generateDishRating(c pb.CampusClient, ctx context.Context, cafeteria string }) if err != nil { - log.Info(err) + log.WithError(err).Error("failed to store dish rating") } else { - log.Info("Request successfully: Dish Rating should be stored") + log.Info("Dish Rating successfully stored") } } @@ -231,7 +247,7 @@ func getImageToBytes(path string) []byte { defer func(file *os.File) { err := file.Close() if err != nil { - log.Info("Request successfully: Dish Rating should be stored") + log.WithError(err).Error("could not close file") } }(file) @@ -239,12 +255,13 @@ func getImageToBytes(path string) []byte { var size = fileInfo.Size() byteArray := make([]byte, size) - buffer := bufio.NewReader(file) - _, err = buffer.Read(byteArray) + n, err := bufio.NewReader(file).Read(byteArray) + fields := log.Fields{"readBytes": n, "len(byteArray)": len(byteArray)} if err != nil { - log.Info("Unable to read the byteArray") + log.WithError(err).Error("Unable to read the byteArray") + } else { + log.WithFields(fields).Info("read image to byteArray") } - log.Info("Length of the image as bytes: ", len(byteArray)) return byteArray } @@ -252,7 +269,7 @@ func storeImage(path string, i []byte) (string, error) { if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) { err := os.MkdirAll(path, os.ModePerm) if err != nil { - log.Info(err) + log.WithError(err).Error("could not make dir") } } img, _, _ := image.Decode(bytes.NewReader(i)) @@ -271,12 +288,12 @@ func storeImage(path string, i []byte) (string, error) { out, errFile := os.Create(imgPath) if errFile != nil { - log.Info("Unable to create the new testfile") + log.WithError(errFile).Error("Unable to create the new testfile") } defer func(out *os.File) { err := out.Close() if err != nil { - log.Info("File was not closed successfully") + log.WithError(err).Error("File was not closed successfully") } }(out) var opts jpeg.Options diff --git a/client/publicServer/client.go b/client/publicServer/client.go index 8e53ed1f..7db84b6a 100644 --- a/client/publicServer/client.go +++ b/client/publicServer/client.go @@ -4,11 +4,11 @@ import ( "context" "crypto/x509" pb "github.com/TUM-Dev/Campus-Backend/api" + log "github.com/sirupsen/logrus" "google.golang.org/grpc" "google.golang.org/grpc/credentials" "google.golang.org/grpc/metadata" "google.golang.org/protobuf/types/known/emptypb" - "log" "time" ) @@ -18,16 +18,21 @@ const ( func main() { // Set up a connection to the server. - log.Println("Connecting...") + log.Info("Connecting...") pool, _ := x509.SystemCertPool() // error handling omitted creds := credentials.NewClientTLSFromCert(pool, "") conn, err := grpc.Dial(address, grpc.WithTransportCredentials(creds)) if err != nil { - log.Fatalf("did not connect: %v", err) + log.WithError(err).Fatalf("did not connect") } - defer conn.Close() + defer func(conn *grpc.ClientConn) { + err := conn.Close() + if err != nil { + log.WithError(err).Error("did not close connection") + } + }(conn) c := pb.NewCampusClient(conn) ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) @@ -37,10 +42,10 @@ func main() { md := metadata.New(map[string]string{"x-device-id": "grpc-tests"}) ctx = metadata.NewOutgoingContext(ctx, md) - log.Println("Trying to fetch top news") - r, err := c.GetTopNews(ctx, &emptypb.Empty{}) - if err != nil { - log.Fatalf("could not greet: %v", err) + log.Info("Trying to fetch top news") + if r, err := c.GetTopNews(ctx, &emptypb.Empty{}); err != nil { + log.WithError(err).Fatal("could not greet") + } else { + log.WithField("topNewsResponse", r.String()).Info("fetched top news successfully") } - log.Printf("Greeting: %s", r.String()) } diff --git a/server/backend/cafeteriaRatingDBInitializer.go b/server/backend/cafeteriaRatingDBInitializer.go index b8fd3056..1f80d270 100644 --- a/server/backend/cafeteriaRatingDBInitializer.go +++ b/server/backend/cafeteriaRatingDBInitializer.go @@ -50,7 +50,7 @@ func addEntriesForCronJob(db *gorm.DB, cronName string, interval int32) { Error if err != nil { - log.WithError(err).Errorf("Error while checking if cronjob with name %s already exists in database", cronName) + log.WithError(err).WithField("cronName", cronName).Error("Error while checking if cronjob already exists in database") } else if count == 0 { errCreate := db.Model(&model.Crontab{}). Create(&model.Crontab{ @@ -78,8 +78,9 @@ func updateNameTagOptions(db *gorm.DB) { Where("EN LIKE ? AND DE LIKE ?", v.TagNameEnglish, v.TagNameGerman). Select("DishNameTagOption"). Scan(&parentId) + fields := log.Fields{"en": v.TagNameEnglish, "de": v.TagNameGerman} if res.Error != nil { - log.WithError(res.Error).Errorf("Unable to load tag with En %s and De %s", v.TagNameEnglish, v.TagNameGerman) + log.WithError(res.Error).WithFields(fields).Error("Unable to load tag") } if res.RowsAffected == 0 || res.Error != nil { parent := model.DishRatingTagOption{ @@ -87,17 +88,14 @@ func updateNameTagOptions(db *gorm.DB) { EN: v.TagNameEnglish, } - errCreate := db.Model(&model.DishNameTagOption{}). - Create(&parent).Error - if errCreate != nil { - log.WithError(errCreate).Error("Error while creating tag {}, {}.", v.TagNameGerman, v.TagNameEnglish) + if err := db.Model(&model.DishNameTagOption{}).Create(&parent).Error; err != nil { + log.WithError(err).WithFields(fields).Error("Error while creating tag") } parentId = parent.DishRatingTagOption } addCanBeIncluded(parentId, db, v) addNotIncluded(parentId, db, v) - } } @@ -161,20 +159,20 @@ func updateTagTable(path string, db *gorm.DB, tagType modelType) { insertModel := getTagModel(tagType, db) for _, v := range tagsDish.MultiLanguageTags { var count int64 - + fields := log.Fields{"de": v.TagNameGerman, "en": v.TagNameEnglish} if tagType == CAFETERIA { countError := db.Model(&model.CafeteriaRatingTagOption{}). Where("EN LIKE ? AND DE LIKE ?", v.TagNameEnglish, v.TagNameGerman). Select("cafeteriaRatingTagOption").Count(&count).Error if countError != nil { - log.WithError(countError).Errorf("Unable to find cafeteria rating tag with En %s and De %s", v.TagNameGerman, v.TagNameEnglish) + log.WithError(countError).WithFields(fields).Error("Unable to find cafeteria rating tag") } } else { countError := db.Model(&model.DishRatingTagOption{}). Where("EN LIKE ? AND DE LIKE ?", v.TagNameEnglish, v.TagNameGerman). Select("dishRatingTagOption").Count(&count).Error if countError != nil { - log.WithError(countError).Errorf("Unable to find dish rating tag with En %s and De %s", v.TagNameGerman, v.TagNameEnglish) + log.WithError(countError).WithFields(fields).Error("Unable to find dish rating tag") } } @@ -185,9 +183,9 @@ func updateTagTable(path string, db *gorm.DB, tagType modelType) { } createError := insertModel.Create(&element).Error if createError != nil { - log.WithError(createError).Errorf("Unable to create new can be excluded tag with En %s and De %s", v.TagNameGerman, v.TagNameEnglish) + log.WithError(createError).WithFields(fields).Error("Unable to create new can be excluded tag") } else { - log.Infof("New Entry with En %s and De %s successfully created.", v.TagNameGerman, v.TagNameEnglish) + log.WithFields(fields).Info("New Entry successfully created") } } } diff --git a/server/backend/cafeteriaService.go b/server/backend/cafeteriaService.go index 55acd47a..c0573a27 100644 --- a/server/backend/cafeteriaService.go +++ b/server/backend/cafeteriaService.go @@ -115,7 +115,7 @@ func queryLastCafeteriaRatingsWithLimit(input *pb.CafeteriaRatingRequest, cafete } if err != nil { - log.WithError(err).Error("Error while querying last cafeteria ratings.") + log.WithError(err).Error("while querying last cafeteria ratings.") return make([]*pb.SingleRatingReply, 0) } ratingResults := make([]*pb.SingleRatingReply, len(ratings)) @@ -224,7 +224,7 @@ func queryLastDishRatingsWithLimit(input *pb.DishRatingRequest, cafeteriaID int3 } if err != nil { - log.WithError(err).Error("Error while querying last dish ratings from Database.") + log.WithError(err).Error("while querying last dish ratings from Database.") return make([]*pb.SingleRatingReply, 0) } ratingResults := make([]*pb.SingleRatingReply, len(ratings)) @@ -251,7 +251,7 @@ func getImageToBytes(path string) []byte { file, err := os.Open(path) if err != nil { - log.WithError(err).Error("Error while opening image file with path: ", path) + log.WithError(err).Error("while opening image file with path: ", path) return nil } @@ -269,7 +269,7 @@ func getImageToBytes(path string) []byte { buffer := bufio.NewReader(file) _, err = buffer.Read(imageAsBytes) if err != nil { - log.WithError(err).Error("Error while trying to read image as bytes") + log.WithError(err).Error("while trying to read image as bytes") return nil } return imageAsBytes @@ -315,7 +315,7 @@ func queryTags(db *gorm.DB, cafeteriaID int32, dishID int32, ratingType modelTyp } if err != nil { - log.WithError(err).Error("Error while querying the tags for the request.") + log.WithError(err).Error("while querying the tags for the request.") } //needed since the gRPC element does not specify column names - cannot be directly queried into the grpc message object. @@ -351,7 +351,7 @@ func queryTagRatingsOverviewForRating(s *CampusServer, dishID int32, ratingType } if err != nil { - log.WithError(err).Error("Error while querying the tag rating overview.") + log.WithError(err).Error("while querying the tag rating overview.") } return results } @@ -406,7 +406,7 @@ func storeImage(path string, i []byte) (string, error) { if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) { err := os.MkdirAll(path, os.ModePerm) if err != nil { - log.WithError(err).Errorf("Directory with path %s could not be created successfully", path) + log.WithError(err).WithField("path", path).Error("Directory could not be created successfully") return "", nil } } @@ -432,7 +432,7 @@ func storeImage(path string, i []byte) (string, error) { defer func(out *os.File) { err := out.Close() if err != nil { - log.WithError(err).Error("Error while closing the file.") + log.WithError(err).Error("while closing the file.") } }(out) @@ -475,7 +475,7 @@ func (s *CampusServer) NewDishRating(_ context.Context, input *pb.NewDishRatingR err := s.db.Model(&model.DishRating{}).Create(&rating).Error if err != nil { - log.WithError(err).Error("Error while creating a new dish rating.") + log.WithError(err).Error("while creating a new dish rating.") return nil, status.Errorf(codes.Internal, "Error while creating the new rating in the database. Rating has not been saved.") } @@ -492,7 +492,7 @@ func assignDishNameTag(s *CampusServer, rating model.DishRating, dishID int32) { Select("nameTagID"). Scan(&result).Error if err != nil { - log.WithError(err).Error("Error while loading the dishID for the given name.") + log.WithError(err).Error("while loading the dishID for the given name.") } else { for _, tagID := range result { err := s.db.Model(&model.DishNameTag{}).Create(&model.DishNameTag{ @@ -501,7 +501,7 @@ func assignDishNameTag(s *CampusServer, rating model.DishRating, dishID int32) { TagNameID: tagID, }).Error if err != nil { - log.WithError(err).Error("Error while creating a new dish name rating.") + log.WithError(err).Error("while creating a new dish name rating.") } } } @@ -558,7 +558,11 @@ func storeRatingTags(s *CampusServer, parentRatingID int32, tags []*pb.RatingTag } if err == gorm.ErrRecordNotFound || count == 0 { - log.Info("tag with tagid ", currentTag.TagId, "does not exist") + fields := log.Fields{ + "tagid": currentTag.TagId, + "count": count, + } + log.WithFields(fields).Info("tag does not exist") errorOccurred = fmt.Sprintf("%s, %d", errorOccurred, currentTag.TagId) } else { if usedTagIds[int(currentTag.TagId)] == 0 { @@ -569,7 +573,7 @@ func storeRatingTags(s *CampusServer, parentRatingID int32, tags []*pb.RatingTag TagID: int(currentTag.TagId), }).Error if err != nil { - log.WithError(err).Error("Error while Creating a currentTag rating for a new rating.") + log.WithError(err).Error("while Creating a currentTag rating for a new rating.") } usedTagIds[int(currentTag.TagId)] = 1 @@ -609,7 +613,7 @@ func getIDForCafeteriaName(name string, db *gorm.DB) int32 { Select("cafeteria"). Scan(&result).Error if err != nil { - log.WithError(err).Error("Error while querying the cafeteria name.") + log.WithError(err).Error("while querying the cafeteria name.") result = -1 } return result @@ -622,7 +626,7 @@ func getIDForDishName(name string, cafeteriaID int32, db *gorm.DB) int32 { Select("dish"). Scan(&result).Error if err != nil { - log.WithError(err).Error("Error while querying the dish name.") + log.WithError(err).Error("while querying the dish name.") result = -1 } @@ -636,7 +640,7 @@ func (s *CampusServer) GetAvailableDishTags(_ context.Context, _ *emptypb.Empty) var requestStatus error = nil err := s.db.Model(&model.DishRatingTagOption{}).Select("DE as de, EN as en, dishRatingTagOption as TagId").Find(&result).Error if err != nil { - log.WithError(err).Error("Error while loading Cafeterias from database.") + log.WithError(err).Error("while loading Cafeterias from database.") requestStatus = status.Errorf(codes.Internal, "Available dish tags could not be loaded from the database.") } @@ -652,7 +656,7 @@ func (s *CampusServer) GetNameTags(_ context.Context, _ *emptypb.Empty) (*pb.Get var requestStatus error = nil err := s.db.Model(&model.DishNameTagOption{}).Select("DE as de, EN as en, dishNameTagOption as TagId").Find(&result).Error if err != nil { - log.WithError(err).Error("Error while loading available Name Tags from database.") + log.WithError(err).Error("while loading available Name Tags from database.") requestStatus = status.Errorf(codes.Internal, "Available dish tags could not be loaded from the database.") } @@ -668,7 +672,7 @@ func (s *CampusServer) GetAvailableCafeteriaTags(_ context.Context, _ *emptypb.E var requestStatus error = nil err := s.db.Model(&model.CafeteriaRatingTagOption{}).Select("DE as de, EN as en, cafeteriaRatingsTagOption as TagId").Find(&result).Error if err != nil { - log.WithError(err).Error("Error while loading Cafeterias from database.") + log.WithError(err).Error("while loading Cafeterias from database.") requestStatus = status.Errorf(codes.Internal, "Available cafeteria tags could not be loaded from the database.") } @@ -684,7 +688,7 @@ func (s *CampusServer) GetCafeterias(_ context.Context, _ *emptypb.Empty) (*pb.G var requestStatus error = nil err := s.db.Model(&model.Cafeteria{}).Select("cafeteria as id,address,latitude,longitude").Scan(&result).Error if err != nil { - log.WithError(err).Error("Error while loading Cafeterias from database.") + log.WithError(err).Error("while loading Cafeterias from database.") requestStatus = status.Errorf(codes.Internal, "Cafeterias could not be loaded from the database.") } @@ -716,7 +720,7 @@ func (s *CampusServer) GetDishes(_ context.Context, request *pb.GetDishesRequest Find(&results).Error if err != nil { - log.WithError(err).Error("Error while loading Cafeterias from database.") + log.WithError(err).Error("while loading Cafeterias from database.") requestStatus = status.Errorf(codes.Internal, "Cafeterias could not be loaded from the database.") } diff --git a/server/backend/campus_api/campusApi.go b/server/backend/campus_api/campusApi.go index fc59c0da..1a3d192d 100644 --- a/server/backend/campus_api/campusApi.go +++ b/server/backend/campus_api/campusApi.go @@ -28,7 +28,7 @@ func FetchGrades(token string) (*model.IOSGrades, error) { req, err := http.NewRequest(http.MethodGet, requestUrl, nil) if err != nil { - log.Errorf("Error while creating request: %s", err) + log.WithError(err).Error("Failed to create api-request") return nil, ErrCannotCreateRequest } @@ -40,14 +40,14 @@ func FetchGrades(token string) (*model.IOSGrades, error) { resp, err := http.DefaultClient.Do(req) if err != nil { - log.Errorf("Error while fetching grades: %s", err) + log.WithError(err).Error("failed to fetch grades") return nil, ErrWhileFetchingGrades } defer func(Body io.ReadCloser) { err := Body.Close() if err != nil { - log.Errorf("Error while closing body: %s", err) + log.WithError(err).Error("Could not close body") } }(resp.Body) @@ -55,7 +55,7 @@ func FetchGrades(token string) (*model.IOSGrades, error) { err = xml.NewDecoder(resp.Body).Decode(&grades) if err != nil { - log.Errorf("Error while unmarshalling grades: %s", err) + log.WithError(err).Error("could not unmarshall grades") return nil, ErrorWhileUnmarshalling } diff --git a/server/backend/canteenHeadCount.go b/server/backend/canteenHeadCount.go index d3c0293f..58af641f 100644 --- a/server/backend/canteenHeadCount.go +++ b/server/backend/canteenHeadCount.go @@ -16,7 +16,7 @@ func (s *CampusServer) GetCanteenHeadCount(_ context.Context, input *pb.GetCante data := model.CanteenHeadCount{Count: 0, MaxCount: 0, Percent: -1} // Initialize with an empty (not found) value err := s.db.Model(&model.CanteenHeadCount{}).Where(model.CanteenHeadCount{CanteenId: input.CanteenId}).FirstOrInit(&data).Error if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { - log.WithError(err).Error("Error while querying the canteen head count for: ", input.CanteenId) + log.WithError(err).Error("while querying the canteen head count for: ", input.CanteenId) return nil, errors.New("failed to query head count") } diff --git a/server/backend/cron/averageRatingComputation.go b/server/backend/cron/averageRatingComputation.go index b80cff0c..24bd0a6c 100644 --- a/server/backend/cron/averageRatingComputation.go +++ b/server/backend/cron/averageRatingComputation.go @@ -26,7 +26,7 @@ func computeAverageNameTags(c *CronService) { " GROUP BY mr.cafeteriaID, mnt.tagnameID").Scan(&results).Error if err != nil { - log.WithError(err).Error("Error while precomputing average name tags.") + log.WithError(err).Error("while precomputing average name tags.") } else if len(results) > 0 { errDelete := c.db.Where("1=1").Delete(&model.DishNameTagAverage{}).Error // Does not work with "true" if errDelete != nil { @@ -34,7 +34,7 @@ func computeAverageNameTags(c *CronService) { } err := c.db.Model(&model.DishNameTagAverage{}).Create(&results).Error if err != nil { - log.WithError(err).Error("Error while creating a new average name tag rating in the database.") + log.WithError(err).Error("while creating a new average name tag rating in the database.") } } } @@ -47,7 +47,7 @@ func computeAverageForDishesInCafeteriasTags(c *CronService) { " GROUP BY mr.cafeteriaID, mrt.tagID, mr.dishID").Scan(&results).Error if err != nil { - log.WithError(err).Error("Error while precomputing average dish tags.") + log.WithError(err).Error("while precomputing average dish tags.") } else if len(results) > 0 { errDelete := c.db.Where("1=1").Delete(&model.DishRatingTagAverage{}).Error if errDelete != nil { @@ -56,7 +56,7 @@ func computeAverageForDishesInCafeteriasTags(c *CronService) { err := c.db.Model(&model.DishRatingTagAverage{}).Create(&results).Error if err != nil { - log.WithError(err).Error("Error while creating a new average dish tag rating in the database.") + log.WithError(err).Error("while creating a new average dish tag rating in the database.") } } @@ -70,7 +70,7 @@ func computeAverageCafeteriaTags(c *CronService) { " GROUP BY cr.cafeteriaID, crt.tagID").Scan(&results).Error if err != nil { - log.WithError(err).Error("Error while precomputing average cafeteria tags.") + log.WithError(err).Error("while precomputing average cafeteria tags.") } else if len(results) > 0 { errDelete := c.db.Where("1=1").Delete(&model.CafeteriaRatingTagsAverage{}).Error if errDelete != nil { @@ -79,7 +79,7 @@ func computeAverageCafeteriaTags(c *CronService) { err := c.db.Model(&model.CafeteriaRatingTagsAverage{}).Create(&results).Error if err != nil { - log.WithError(err).Error("Error while creating a new average cafeteria tag rating in the database.") + log.WithError(err).Error("while creating a new average cafeteria tag rating in the database.") } } } @@ -91,7 +91,7 @@ func computeAverageForDishesInCafeterias(c *CronService) { Group("cafeteriaID,dishID").Scan(&results).Error if err != nil { - log.WithError(err).Error("Error while precomputing average dish ratings.") + log.WithError(err).Error("while precomputing average dish ratings.") } else if len(results) > 0 { errDelete := c.db.Where("1=1").Delete(&model.DishRatingAverage{}).Error if errDelete != nil { @@ -99,7 +99,7 @@ func computeAverageForDishesInCafeterias(c *CronService) { } err := c.db.Model(&model.DishRatingAverage{}).Create(&results).Error if err != nil { - log.WithError(err).Error("Error while creating a new average dish rating in the database.") + log.WithError(err).Error("while creating a new average dish rating in the database.") } } } @@ -111,7 +111,7 @@ func computeAverageForCafeteria(c *CronService) { Group("cafeteriaID").Find(&results).Error if err != nil { - log.WithError(err).Error("Error while precomputing average cafeteria ratings.") + log.WithError(err).Error("while precomputing average cafeteria ratings.") } else if len(results) > 0 { errDelete := c.db.Where("1=1").Delete(&model.CafeteriaRatingAverage{}).Error if errDelete != nil { @@ -120,7 +120,7 @@ func computeAverageForCafeteria(c *CronService) { err := c.db.Model(&model.CafeteriaRatingAverage{}).Create(&results).Error if err != nil { - log.WithError(err).Error("Error while creating a new average cafeteria rating in the database.") + log.WithError(err).Error("while creating a new average cafeteria rating in the database.") } } } diff --git a/server/backend/cron/canteenHeadCount.go b/server/backend/cron/canteenHeadCount.go index 168af083..5f42ddfe 100644 --- a/server/backend/cron/canteenHeadCount.go +++ b/server/backend/cron/canteenHeadCount.go @@ -4,6 +4,7 @@ import ( "encoding/json" "errors" "fmt" + "io" "net/http" "strings" "time" @@ -128,17 +129,17 @@ CanteenApInformation when performing a request. const BaseUrl = "http://graphite-kom.srv.lrz.de/render/?from=-10min&target=%s&format=json" func (c *CronService) canteenHeadCountCron() error { - log.Info("Updating canteen head count stats...") + log.Debug("Updating canteen head count stats...") for _, canteen := range Canteens { if len(canteen.Target) <= 0 { - log.Debug("Skipping canteen head count stats for '", canteen.CanteenId, "', since there is no target.") + log.WithField("CanteenId", canteen.CanteenId).Debug("Skipping canteen head count stats, since there is no target.") continue } - log.Debug("Updating canteen head count stats for: ", canteen.CanteenId) + log.WithField("CanteenId", canteen.CanteenId).Debug("Updating canteen head count stats") aps := canteen.requestApData() if len(aps) <= 0 { - log.Debug("No canteen head count data points found for: ", canteen.CanteenId) + log.WithField("CanteenId", canteen.CanteenId).Debug("No canteen head count data points found") continue } @@ -150,7 +151,7 @@ func (c *CronService) canteenHeadCountCron() error { log.WithFields(fields).Debug("Canteen head count stats updated") } } - log.Info("Canteen head count stats updated.") + log.Debug("Canteen head count stats updated.") return nil } @@ -183,11 +184,22 @@ func updateDb(canteen *CanteenApInformation, count uint32, db *gorm.DB) error { res := db.Model(&model.CanteenHeadCount{}).Where(model.CanteenHeadCount{CanteenId: canteen.CanteenId}).Updates(&entry) if res.Error != nil { + log.WithError(res.Error).WithField("CanteenId", canteen.CanteenId).Error("could not update all instances of headcount") return res.Error } if res.RowsAffected == 0 { - return db.Create(&entry).Error + err := db.Create(&entry).Error + if err != nil { + fields := log.Fields{ + "CanteenId": entry.CanteenId, + "Count": entry.Count, + "MaxCount": entry.MaxCount, + "Percent": entry.Percent, + "Timestamp": entry.Timestamp} + log.WithError(res.Error).WithFields(fields).Error("could not create headcount entry") + } + return err } return nil } @@ -197,13 +209,18 @@ func (canteen CanteenApInformation) requestApData() []AccessPoint { url := fmt.Sprintf(BaseUrl, canteen.Target) resp, err := http.Get(url) if err != nil { - log.WithError(err).Error("Canteen HeadCount web request failed for: ", canteen.CanteenId) + log.WithError(err).WithField("CanteenId", canteen.CanteenId).Error("Canteen HeadCount web request failed") return []AccessPoint{} } // Ensure we close the body once we leave this function if resp.Body != nil { - defer resp.Body.Close() + defer func(Body io.ReadCloser) { + err := Body.Close() + if err != nil { + log.WithError(err).Error("Could not close body") + } + }(resp.Body) } // Parse as JSON diff --git a/server/backend/cron/cronjobs.go b/server/backend/cron/cronjobs.go index 508a4a08..ae5348f1 100644 --- a/server/backend/cron/cronjobs.go +++ b/server/backend/cron/cronjobs.go @@ -48,7 +48,7 @@ func New(db *gorm.DB, mensaCronActivated bool) *CronService { } func (c *CronService) Run() error { - log.WithField("useMensa", c.useMensa).Trace("running cron service") + log.WithField("MensaCronsRunning", c.useMensa).Trace("running cron service") g := new(errgroup.Group) g.Go(func() error { return c.dishNameDownloadCron() }) diff --git a/server/backend/cron/dishNameDownload.go b/server/backend/cron/dishNameDownload.go index 1dab432e..5db223e7 100644 --- a/server/backend/cron/dishNameDownload.go +++ b/server/backend/cron/dishNameDownload.go @@ -71,13 +71,17 @@ func downloadDailyDishes(c *CronService) { cafeteriaName := strings.Replace(strings.ToLower(v.Name), "_", "-", 10) req := fmt.Sprintf("https://tum-dev.github.io/eat-api/%s/%d/%d.json", cafeteriaName, year, week) - log.Info("Fetching menu from: ", req) + log.WithField("req", req).Debug("Fetching menu") var resp, err = http.Get(req) if err != nil { log.WithError(err).Error("Error fetching menu.") } if resp.StatusCode != 200 { - log.WithError(err).Errorf("Menu for %s does not exist error 404 returned.", v.Name) + fields := log.Fields{ + "Name": v.Name, + "StatusCode": resp.StatusCode, + } + log.WithError(err).WithFields(fields).Error("Menu does not exist") } else { var dishes days errJson := json.NewDecoder(resp.Body).Decode(&dishes) diff --git a/server/backend/cron/news.go b/server/backend/cron/news.go index 4a7f4723..2485993d 100644 --- a/server/backend/cron/news.go +++ b/server/backend/cron/news.go @@ -65,7 +65,7 @@ func (c *CronService) newsCron(cronjob *model.Crontab) error { // parseNewsFeed processes a single news feed, extracts titles, content etc and saves it to the database func (c *CronService) parseNewsFeed(source model.NewsSource) error { - log.Printf("processing source %s", source.URL.String) + log.WithField("url", source.URL.String).Trace("processing newsfeed") feed, err := c.gf.ParseURL(source.URL.String) if err != nil { log.Printf("error parsing rss: %v", err) @@ -129,22 +129,26 @@ func (c *CronService) parseNewsFeed(source model.NewsSource) error { newNews = append(newNews, newsItem) } } - if len(newNews) != 0 { - log.Printf("Inserting %v new news", len(newNews)) + if ammountOfNewNews := len(newNews); ammountOfNewNews != 0 { err = c.db.Save(&newNews).Error + if err != nil { + log.WithField("ammountOfNewNews", ammountOfNewNews).Error("Inserting new news failed") + } else { + log.WithField("ammountOfNewNews", ammountOfNewNews).Trace("Inserting new news") + } return err } return nil } -// saveImage Saves an image to the database so it can be downloaded by another cronjob and returns it's id +// saveImage Saves an image to the database so it can be downloaded by another cronjob and returns its id func (c *CronService) saveImage(url string) (null.Int, error) { targetFileName := fmt.Sprintf("%x.jpg", md5.Sum([]byte(url))) var fileId null.Int if err := c.db.Model(model.Files{}). Where("name = ?", targetFileName). - Select("file").Scan(&fileId).Error; err != nil && err != gorm.ErrRecordNotFound { - log.Printf("Couldn't query database for file: %v", err) + Select("file").Scan(&fileId).Error; err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { + log.WithError(err).WithField("targetFileName", targetFileName).Error("Couldn't query database for file") return null.Int{}, err } if fileId.Valid { // file already in database -> return for current news. @@ -186,9 +190,9 @@ func skipNews(existingLinks []string, link string) bool { } func (c *CronService) cleanOldNewsForSource(source int32) error { - log.Printf("Truncating old entries for source %d\n", source) + log.WithField("source", source).Trace("Truncating old entries") if res := c.db.Delete(&model.News{}, "`src` = ? AND `created` < ?", source, time.Now().Add(time.Hour*24*365*-1)); res.Error == nil { - log.Infof("cleaned up %v old news", res.RowsAffected) + log.WithField("RowsAffected", res.RowsAffected).Info("cleaned up old news") } else { log.WithError(res.Error).Error("failed to clean up old news") sentry.CaptureException(res.Error) diff --git a/server/backend/ios_notifications/ios_apns/iosAPNsRepository.go b/server/backend/ios_notifications/ios_apns/iosAPNsRepository.go index b184a4bb..1091b8f7 100644 --- a/server/backend/ios_notifications/ios_apns/iosAPNsRepository.go +++ b/server/backend/ios_notifications/ios_apns/iosAPNsRepository.go @@ -107,7 +107,7 @@ func (r *Repository) SendNotification(notification *model.IOSNotificationPayload defer func(Body io.ReadCloser) { err := Body.Close() if err != nil { - log.Errorf("Error while closing body: %s", err) + log.WithError(err).Error("Could not close body") } }(resp.Body) diff --git a/server/backend/ios_notifications/ios_apns/iosAPNsService.go b/server/backend/ios_notifications/ios_apns/iosAPNsService.go index d81fdff9..37427fca 100644 --- a/server/backend/ios_notifications/ios_apns/iosAPNsService.go +++ b/server/backend/ios_notifications/ios_apns/iosAPNsService.go @@ -28,7 +28,7 @@ func (s *Service) RequestGradeUpdateForDevice(deviceID string) error { campusRequestToken, err := s.Repository.CreateCampusTokenRequest(deviceID) if err != nil { - log.Errorf("Could not create campus token request: %s", err) + log.WithError(err).Error("Could not create campus token request") return ErrCouldNotCreateTokenRequest } @@ -37,7 +37,7 @@ func (s *Service) RequestGradeUpdateForDevice(deviceID string) error { res, err := s.Repository.SendBackgroundNotification(notification) if err != nil { - log.Errorf("Could not send background notification: %s", err) + log.WithError(err).Error("Could not send background notification") return ErrCouldNotSendNotification } diff --git a/server/backend/ios_notifications/ios_devices_activity_reset/iosDevicesActivityResetRepository.go b/server/backend/ios_notifications/ios_devices_activity_reset/iosDevicesActivityResetRepository.go index e827c93e..12f7207c 100644 --- a/server/backend/ios_notifications/ios_devices_activity_reset/iosDevicesActivityResetRepository.go +++ b/server/backend/ios_notifications/ios_devices_activity_reset/iosDevicesActivityResetRepository.go @@ -104,8 +104,8 @@ func (repo *Repository) CreateInitialRecords() { LastReset: now, } - if err := repo.DB.Create(&reset); err != nil { - log.Errorf("Failed to create initial %s type: %v", resetType, err) + if err := repo.DB.Create(&reset).Error; err != nil { + log.WithError(err).WithField("resetType", resetType).Error("Failed to create IOSDevicesActivityReset") continue } } diff --git a/server/backend/ios_notifications/ios_devices_activity_reset/iosDevicesActivityResetService.go b/server/backend/ios_notifications/ios_devices_activity_reset/iosDevicesActivityResetService.go index c3d29060..ddea9248 100644 --- a/server/backend/ios_notifications/ios_devices_activity_reset/iosDevicesActivityResetService.go +++ b/server/backend/ios_notifications/ios_devices_activity_reset/iosDevicesActivityResetService.go @@ -44,41 +44,41 @@ func (service *Service) HandleScheduledActivityReset() error { if now.Sub(daily.LastReset).Hours() > 24 { if err := service.Repository.ResettedDevicesDaily(); err != nil { - log.Errorf("Error while resetting devices daily: %s", err) + log.WithError(err).Error("while resetting devices daily") } if err := devicesRepo.ResetDevicesDailyActivity(); err != nil { - log.Errorf("Error while resetting devices daily activity: %s", err) + log.WithError(err).Error("while resetting devices daily activity") } } if now.Sub(weekly.LastReset).Hours() > 168 { if err := service.Repository.ResettedDevicesWeekly(); err != nil { - log.Errorf("Error while resetting devices weekly: %s", err) + log.WithError(err).Error("while resetting devices weekly") } if err := devicesRepo.ResetDevicesWeeklyActivity(); err != nil { - log.Errorf("Error while resetting devices weekly activity: %s", err) + log.WithError(err).Error("while resetting devices weekly activity") } } if now.Sub(monthly.LastReset).Hours() > 730 { if err := service.Repository.ResettedDevicesMonthly(); err != nil { - log.Errorf("Error while resetting devices monthly: %s", err) + log.WithError(err).Error("while resetting devices monthly") } if err := devicesRepo.ResetDevicesMonthlyActivity(); err != nil { - log.Errorf("Error while resetting devices monthly activity: %s", err) + log.WithError(err).Error("while resetting devices monthly activity") } } if now.Sub(yearly.LastReset).Hours() > 8760 { if err := service.Repository.ResettedDevicesYearly(); err != nil { - log.Errorf("Error while resetting devices yearly: %s", err) + log.WithError(err).Error("while resetting devices yearly") } if err := devicesRepo.ResetDevicesYearlyActivity(); err != nil { - log.Errorf("Error while resetting devices yearly activity: %s", err) + log.WithError(err).Error("while resetting devices yearly activity") } } diff --git a/server/backend/ios_notifications/ios_request_response/iosRequestResponseService.go b/server/backend/ios_notifications/ios_request_response/iosRequestResponseService.go index aab49934..32a3faa9 100644 --- a/server/backend/ios_notifications/ios_request_response/iosRequestResponseService.go +++ b/server/backend/ios_notifications/ios_request_response/iosRequestResponseService.go @@ -33,7 +33,7 @@ func (service *Service) HandleDeviceRequestResponse(request *pb.IOSDeviceRequest // requestId refers to the request id that was sent to the device and stored in the Database requestId := request.GetRequestId() - log.Infof("Handling request with id %s", requestId) + log.WithField("requestId", requestId).Info("Handling request") requestLog, err := service.Repository.GetIOSDeviceRequest(requestId) @@ -63,7 +63,7 @@ func (service *Service) HandleDeviceRequestResponse(request *pb.IOSDeviceRequest } func (service *Service) handleDeviceCampusTokenRequest(requestLog *model.IOSDeviceRequestLog, campusToken string) (*pb.IOSDeviceRequestResponseReply, error) { - log.Infof("Handling campus token request for device %s", requestLog.DeviceID) + log.WithField("DeviceID", requestLog.DeviceID).Info("Handling campus token request") userRepo := ios_device.NewRepository(service.Repository.DB) @@ -110,7 +110,7 @@ func (service *Service) handleDeviceCampusTokenRequest(requestLog *model.IOSDevi service.encryptGradesAndStoreInDatabase(apiGrades.Grades, requestLog.DeviceID, campusToken) - log.Infof("Found %d old grades and %d new grades", len(oldGrades), len(newGrades)) + log.WithFields(log.Fields{"old": len(oldGrades), "new": len(newGrades)}).Info("Found grades") if len(newGrades) > 0 && len(oldGrades) > 0 { apnsRepository := ios_apns.NewRepository(service.Repository.DB, service.Repository.Token) @@ -210,7 +210,7 @@ func sendGradesToDevice(device *model.IOSDevice, grades []model.IOSGrade, apns * Alert(alertTitle, "", alertBody). Encrypt(device.PublicKey) - log.Infof("Sending push notification to device %s", device.DeviceID) + log.WithField("DeviceID", device.DeviceID).Infof("Sending push notification") _, err := apns.SendAlertNotification(notificationPayload) diff --git a/server/backend/ios_notifications/ios_scheduling/iosSchedulingService.go b/server/backend/ios_notifications/ios_scheduling/iosSchedulingService.go index a2e60dad..1e1f9ee6 100644 --- a/server/backend/ios_notifications/ios_scheduling/iosSchedulingService.go +++ b/server/backend/ios_notifications/ios_scheduling/iosSchedulingService.go @@ -37,7 +37,7 @@ func (service *Service) HandleScheduledCron() error { devices, err := service.DevicesRepository.GetDevicesThatShouldUpdateGrades() if err != nil { - log.Errorf("Error while getting devices: %s", err) + log.WithError(err).Error("can't get devices") return err } diff --git a/server/backend/migration/20221119131300.go b/server/backend/migration/20221119131300.go index 5007c06f..2f4588dc 100644 --- a/server/backend/migration/20221119131300.go +++ b/server/backend/migration/20221119131300.go @@ -34,33 +34,29 @@ func (m TumDBMigrator) migrate20221119131300() *gormigrate.Migration { return err } - err := SafeEnumMigrate(tx, &model.Crontab{}, "type", "iosNotifications", "iosActivityReset") - - if err != nil { + if err := SafeEnumMigrate(tx, &model.Crontab{}, "type", "iosNotifications", "iosActivityReset"); err != nil { return err } var priorities []model.IOSSchedulingPriority - unmarshalErr := json.Unmarshal(iosInitialPrioritiesFile, &priorities) - - if unmarshalErr != nil { - log.Info(unmarshalErr.Error()) - return unmarshalErr + if err := json.Unmarshal(iosInitialPrioritiesFile, &priorities); err != nil { + log.WithError(err).Error("could not unmarshal json") + return err } if err := tx.Create(&priorities).Error; err != nil { - log.Info(err.Error()) + log.WithError(err).Error("could not save priority's") return err } - err = tx.Create(&model.Crontab{ + err := tx.Create(&model.Crontab{ Interval: 60, Type: null.String{NullString: sql.NullString{String: cron.IOSNotifications, Valid: true}}, }).Error if err != nil { - log.Error(err.Error()) + log.WithError(err).Error("could not create crontab") return err } diff --git a/server/backend/migration/safeEnumMigrate_test.go b/server/backend/migration/safeEnumMigrate_test.go index a8aada4c..920bcc2a 100644 --- a/server/backend/migration/safeEnumMigrate_test.go +++ b/server/backend/migration/safeEnumMigrate_test.go @@ -50,7 +50,7 @@ func isValidEnum(enum string) bool { match, err := regexp.MatchString("enum\\s*\\('\\w+'(,\\s*'\\w+')*\\);?", enum) if err != nil { - log.Errorf("error matching regex: %s", err) + log.WithError(err).Error("error matching regex") return false } diff --git a/server/backend/rpcserver.go b/server/backend/rpcserver.go index 0a4b6d00..af412a6f 100644 --- a/server/backend/rpcserver.go +++ b/server/backend/rpcserver.go @@ -40,7 +40,7 @@ type CampusServer struct { var _ pb.CampusServer = (*CampusServer)(nil) func New(db *gorm.DB) *CampusServer { - log.Println("Server starting up") + log.Trace("Server starting up") initTagRatingOptions(db) return &CampusServer{ @@ -92,7 +92,7 @@ func (s *CampusServer) GetNewsSources(ctx context.Context, _ *emptypb.Empty) (ne if err := s.db.Where("file = ?", source.Icon).First(&icon).Error; err != nil { icon = model.Files{File: 0} } - log.Info("sending news source", source.Title) + log.WithField("Title", source.Title).Info("sending news source") resp = append(resp, &pb.NewsSource{ Source: fmt.Sprintf("%d", source.Source), Title: source.Title, @@ -149,7 +149,7 @@ func (s *CampusServer) GetTopNews(ctx context.Context, _ *emptypb.Empty) (*pb.Ge if err := s.checkDevice(ctx); err != nil { return nil, err } - log.Printf("Received: get top news") + var res *model.NewsAlert err := s.db.Joins("Company").Where("NOW() between `from` and `to`").Limit(1).First(&res).Error if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {