From 21ec2bb51d2794475106bb04772ba7583cc5deab Mon Sep 17 00:00:00 2001 From: Jan Eitzinger Date: Sun, 11 Jun 2023 09:00:57 +0200 Subject: [PATCH 1/4] Consider initial run case for compression --- cmd/cc-backend/main.go | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/cmd/cc-backend/main.go b/cmd/cc-backend/main.go index 2200670..02eb33b 100644 --- a/cmd/cc-backend/main.go +++ b/cmd/cc-backend/main.go @@ -482,12 +482,12 @@ func main() { if cfg.Retention.IncludeDB { cnt, err := jobRepo.DeleteJobsBefore(startTime) if err != nil { - log.Errorf("Error while deleting retention jobs from db: %s", err.Error()) + log.Errorf("Error while deleting retention jobs from db: %v", err) } else { log.Infof("Retention: Removed %d jobs from db", cnt) } if err = jobRepo.Optimize(); err != nil { - log.Errorf("Error occured in db optimization: %s", err.Error()) + log.Errorf("Error occured in db optimization: %v", err) } } }) @@ -497,12 +497,21 @@ func main() { log.Info("Register compression service") s.Every(1).Day().At("5:00").Do(func() { + var jobs []*schema.Job + ar := archive.GetHandle() startTime := time.Now().Unix() - int64(cfg.Compression*24*3600) lastTime := ar.CompressLast(startTime) - jobs, err := jobRepo.FindJobsBetween(lastTime, startTime) + if startTime == lastTime { + log.Info("Compression Service - Complete archive run") + jobs, err = jobRepo.FindJobsBetween(0, startTime) + + } else { + jobs, err = jobRepo.FindJobsBetween(lastTime, startTime) + } + if err != nil { - log.Warnf("Error while looking for retention jobs: %s", err.Error()) + log.Warnf("Error while looking for retention jobs: %v", err) } ar.Compress(jobs) }) From 0d2e20e9e4fa378be984f8deb9348ecdbfbaad97 Mon Sep 17 00:00:00 2001 From: Christoph Kluge Date: Mon, 12 Jun 2023 11:35:16 +0200 Subject: [PATCH 2/4] Handle users with no roles as "user role" -for backwards compatibility --- internal/repository/query.go | 14 +++++-- internal/repository/tags.go | 9 +++-- web/frontend/src/Header.svelte | 4 +- web/frontend/src/filters/UserOrProject.svelte | 39 +++++++++---------- 4 files changed, 36 insertions(+), 30 deletions(-) diff --git a/internal/repository/query.go b/internal/repository/query.go index 27378ac..32ecea0 100644 --- a/internal/repository/query.go +++ b/internal/repository/query.go @@ -204,7 +204,10 @@ func (r *JobRepository) CountJobs( func SecurityCheck(ctx context.Context, query sq.SelectBuilder) (queryOut sq.SelectBuilder, err error) { user := auth.GetUser(ctx) - if user == nil || user.HasAnyRole([]auth.Role{auth.RoleAdmin, auth.RoleSupport, auth.RoleApi}) { // Admin & Co. : All jobs + if user == nil { + var qnil sq.SelectBuilder + return qnil, fmt.Errorf("user context is nil!") + } else if user.HasAnyRole([]auth.Role{auth.RoleAdmin, auth.RoleSupport}) { // Admin & Co. : All jobs return query, nil } else if user.HasRole(auth.RoleManager) { // Manager : Add filter for managed projects' jobs only + personal jobs if len(user.Projects) != 0 { @@ -215,9 +218,12 @@ func SecurityCheck(ctx context.Context, query sq.SelectBuilder) (queryOut sq.Sel } } else if user.HasRole(auth.RoleUser) { // User : Only personal jobs return query.Where("job.user = ?", user.Username), nil - } else { // Unauthorized : Error - var qnil sq.SelectBuilder - return qnil, fmt.Errorf("user '%s' with unknown roles [%#v]", user.Username, user.Roles) + } else { + // Shortterm compatibility: Return User-Query if no roles: + return query.Where("job.user = ?", user.Username), nil + // // On the longterm: Return Error instead of fallback: + // var qnil sq.SelectBuilder + // return qnil, fmt.Errorf("user '%s' with unknown roles [%#v]", user.Username, user.Roles) } } diff --git a/internal/repository/tags.go b/internal/repository/tags.go index ce076ff..943d8b1 100644 --- a/internal/repository/tags.go +++ b/internal/repository/tags.go @@ -88,12 +88,15 @@ func (r *JobRepository) CountTags(user *auth.User) (tags []schema.Tag, counts ma LeftJoin("jobtag jt ON t.id = jt.tag_id"). GroupBy("t.tag_name") - if user != nil && user.HasRole(auth.RoleUser) { // USER: Only count own jobs - q = q.Where("jt.job_id IN (SELECT id FROM job WHERE job.user = ?)", user.Username) + if user != nil && user.HasAnyRole([]auth.Role{auth.RoleAdmin, auth.RoleSupport}) { // ADMIN || SUPPORT: Count all jobs + log.Info("CountTags: User Admin or Support -> Count all Jobs for Tags") + // Unchanged: Needs to be own case still, due to UserRole/NoRole compatibility handling in else case } else if user != nil && user.HasRole(auth.RoleManager) { // MANAGER: Count own jobs plus project's jobs // Build ("project1", "project2", ...) list of variable length directly in SQL string q = q.Where("jt.job_id IN (SELECT id FROM job WHERE job.user = ? OR job.project IN (\""+strings.Join(user.Projects, "\",\"")+"\"))", user.Username) - } // else: ADMIN || SUPPORT: Count all jobs + } else { // USER OR NO ROLE (Compatibility): Only count own jobs + q = q.Where("jt.job_id IN (SELECT id FROM job WHERE job.user = ?)", user.Username) + } rows, err := q.RunWith(r.stmtCache).Query() if err != nil { diff --git a/web/frontend/src/Header.svelte b/web/frontend/src/Header.svelte index 227a9d4..b291f22 100644 --- a/web/frontend/src/Header.svelte +++ b/web/frontend/src/Header.svelte @@ -65,12 +65,10 @@ {#each managerviews as item} {item.title} {/each} - {:else if authlevel == roles.user} + {:else} {#each userviews as item} {item.title} {/each} - {:else} -

API User or Unauthorized!

{/if} {#each viewsPerCluster.filter(item => item.requiredRole <= authlevel) as item} diff --git a/web/frontend/src/filters/UserOrProject.svelte b/web/frontend/src/filters/UserOrProject.svelte index f1186ea..8235863 100644 --- a/web/frontend/src/filters/UserOrProject.svelte +++ b/web/frontend/src/filters/UserOrProject.svelte @@ -23,19 +23,9 @@ } let timeoutId = null + // Compatibility: Handle "user role" and "no role" identically function termChanged(sleep = throttle) { - if (authlevel == roles.user) { - project = term - - if (timeoutId != null) - clearTimeout(timeoutId) - - timeoutId = setTimeout(() => { - dispatch('update', { - project - }) - }, sleep) - } else if (authlevel >= roles.manager) { + if (authlevel >= roles.manager) { if (mode == 'user') user = term else @@ -50,17 +40,21 @@ project }) }, sleep) + } else { + project = term + if (timeoutId != null) + clearTimeout(timeoutId) + + timeoutId = setTimeout(() => { + dispatch('update', { + project + }) + }, sleep) } } -{#if authlevel == roles.user} - - termChanged()} on:keyup={(event) => termChanged(event.key == 'Enter' ? 0 : throttle)} placeholder='filter project...' - /> - -{:else if authlevel >= roles.manager} +{#if authlevel >= roles.manager} termChanged()} on:keyup={(event) => termChanged(event.key == 'Enter' ? 0 : throttle)} placeholder='filter project...' + /> + {/if} From f31d8a3af05f2926712622c9cdb45de9baed220b Mon Sep 17 00:00:00 2001 From: Christoph Kluge Date: Mon, 12 Jun 2023 11:58:10 +0200 Subject: [PATCH 3/4] fix test --- internal/repository/tags.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/repository/tags.go b/internal/repository/tags.go index 943d8b1..2c98b60 100644 --- a/internal/repository/tags.go +++ b/internal/repository/tags.go @@ -94,7 +94,7 @@ func (r *JobRepository) CountTags(user *auth.User) (tags []schema.Tag, counts ma } else if user != nil && user.HasRole(auth.RoleManager) { // MANAGER: Count own jobs plus project's jobs // Build ("project1", "project2", ...) list of variable length directly in SQL string q = q.Where("jt.job_id IN (SELECT id FROM job WHERE job.user = ? OR job.project IN (\""+strings.Join(user.Projects, "\",\"")+"\"))", user.Username) - } else { // USER OR NO ROLE (Compatibility): Only count own jobs + } else if user != nil { // USER OR NO ROLE (Compatibility): Only count own jobs q = q.Where("jt.job_id IN (SELECT id FROM job WHERE job.user = ?)", user.Username) } From 02c31a24bddba6ed4533d6eaaf2562e47abeed38 Mon Sep 17 00:00:00 2001 From: Christoph Kluge Date: Mon, 12 Jun 2023 12:12:15 +0200 Subject: [PATCH 4/4] Add filter presets to status list links --- web/frontend/src/Status.root.svelte | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/web/frontend/src/Status.root.svelte b/web/frontend/src/Status.root.svelte index 519b95e..2b57bfa 100644 --- a/web/frontend/src/Status.root.svelte +++ b/web/frontend/src/Status.root.svelte @@ -161,7 +161,7 @@ User NameNumber of Nodes {#each $mainQuery.data.topUsers.sort((a, b) => b.count - a.count) as { name, count }} - {name} + {name} {count} {/each} @@ -182,7 +182,7 @@ Project CodeNumber of Nodes {#each $mainQuery.data.topProjects.sort((a, b) => b.count - a.count) as { name, count }} - {name} + {name} {count} {/each}