From e17c17d2863a9bb848b5494da11252a7c4875b43 Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Fri, 8 Dec 2023 01:17:54 +0000 Subject: [PATCH 1/7] fix --- models/actions/run.go | 23 +++++++++++++++++++---- models/actions/run_job.go | 3 ++- options/locale/locale_en-US.ini | 1 + routers/web/repo/actions/view.go | 8 ++++++++ templates/repo/actions/runs_list.tmpl | 2 +- 5 files changed, 31 insertions(+), 6 deletions(-) diff --git a/models/actions/run.go b/models/actions/run.go index 4656aa22a2933..b5b27bd76d75d 100644 --- a/models/actions/run.go +++ b/models/actions/run.go @@ -46,10 +46,13 @@ type ActionRun struct { TriggerEvent string // the trigger event defined in the `on` configuration of the triggered workflow Status Status `xorm:"index"` Version int `xorm:"version default 0"` // Status could be updated concomitantly, so an optimistic lock is needed - Started timeutil.TimeStamp - Stopped timeutil.TimeStamp - Created timeutil.TimeStamp `xorm:"created"` - Updated timeutil.TimeStamp `xorm:"updated"` + // Started and Stopped is used for recording last run time, if rerun happened, they will be reset to 0 + Started timeutil.TimeStamp + Stopped timeutil.TimeStamp + // TotalDuration is used for recording the total duration of the workflow + TotalDuration time.Duration + Created timeutil.TimeStamp `xorm:"created"` + Updated timeutil.TimeStamp `xorm:"updated"` } func init() { @@ -121,6 +124,18 @@ func (run *ActionRun) Duration() time.Duration { return calculateDuration(run.Started, run.Stopped, run.Status) } +func (run *ActionRun) DisplayDuration() time.Duration { + duration := calculateDuration(run.Started, run.Stopped, run.Status) + if run.Status.IsDone() { + // there was a bug when record stopped time, see https://github.com/go-gitea/gitea/issues/28323#issuecomment-1841867298 + // and duration will be negitive because of the incorrect stopped time in db + // there's no way to fix them now, so in order to avoid displaying incorrect duration time, + // we will display 0 instead of negitive time + return slices.Max([]time.Duration{run.TotalDuration, duration}) + } + return duration +} + func (run *ActionRun) GetPushEventPayload() (*api.PushPayload, error) { if run.Event == webhook_module.HookEventPush { var payload api.PushPayload diff --git a/models/actions/run_job.go b/models/actions/run_job.go index 4b8664077dca9..5190e0c65644d 100644 --- a/models/actions/run_job.go +++ b/models/actions/run_job.go @@ -143,8 +143,9 @@ func UpdateRunJob(ctx context.Context, job *ActionRunJob, cond builder.Cond, col } if run.Stopped.IsZero() && run.Status.IsDone() { run.Stopped = timeutil.TimeStampNow() + run.TotalDuration += run.Duration() } - if err := UpdateRun(ctx, run, "status", "started", "stopped"); err != nil { + if err := UpdateRun(ctx, run, "status", "started", "stopped", "total_duration"); err != nil { return 0, fmt.Errorf("update run %d: %w", run.ID, err) } } diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index fa7eee9bc5587..3565adffeea27 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -3534,6 +3534,7 @@ runs.no_workflows.quick_start = Don't know how to start with Gitea Action? See < runs.no_workflows.documentation = For more information on the Gitea Action, see the documentation. runs.no_runs = The workflow has no runs yet. runs.empty_commit_message = (empty commit message) +runs.last_run_time = Last run: %s workflow.disable = Disable Workflow workflow.disable_success = Workflow '%s' disabled successfully. diff --git a/routers/web/repo/actions/view.go b/routers/web/repo/actions/view.go index 1cdae32a32fef..f6a875f94d112 100644 --- a/routers/web/repo/actions/view.go +++ b/routers/web/repo/actions/view.go @@ -279,6 +279,14 @@ func Rerun(ctx *context_module.Context) { return } + // reset run's start and stop time + run.Started = 0 + run.Stopped = 0 + if err := actions_model.UpdateRun(ctx, run, "started", "stopped"); err != nil { + ctx.Error(http.StatusInternalServerError, err.Error()) + return + } + job, jobs := getRunJobs(ctx, runIndex, jobIndex) if ctx.Written() { return diff --git a/templates/repo/actions/runs_list.tmpl b/templates/repo/actions/runs_list.tmpl index 3b289fb68f2ea..50ee6ec694592 100644 --- a/templates/repo/actions/runs_list.tmpl +++ b/templates/repo/actions/runs_list.tmpl @@ -35,7 +35,7 @@
{{svg "octicon-calendar" 16}}{{TimeSinceUnix .Updated ctx.Locale}}
-
{{svg "octicon-stopwatch" 16}}{{.Duration}}
+
{{svg "octicon-stopwatch" 16}}{{.DisplayDuration}}
{{end}} From 4c1873122513655d780d5e55bf6607e85b43908f Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Fri, 8 Dec 2023 02:17:10 +0000 Subject: [PATCH 2/7] improve --- routers/web/repo/actions/view.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/routers/web/repo/actions/view.go b/routers/web/repo/actions/view.go index f6a875f94d112..5a8f72007c04a 100644 --- a/routers/web/repo/actions/view.go +++ b/routers/web/repo/actions/view.go @@ -279,12 +279,14 @@ func Rerun(ctx *context_module.Context) { return } - // reset run's start and stop time - run.Started = 0 - run.Stopped = 0 - if err := actions_model.UpdateRun(ctx, run, "started", "stopped"); err != nil { - ctx.Error(http.StatusInternalServerError, err.Error()) - return + // reset run's start and stop time when it is done + if run.Status.IsDone() { + run.Started = 0 + run.Stopped = 0 + if err := actions_model.UpdateRun(ctx, run, "started", "stopped"); err != nil { + ctx.Error(http.StatusInternalServerError, err.Error()) + return + } } job, jobs := getRunJobs(ctx, runIndex, jobIndex) From ef0064780bd8945b2cc0c3b36a6bddd8dcbbeead Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Fri, 8 Dec 2023 05:30:46 +0000 Subject: [PATCH 3/7] improve --- models/actions/run.go | 22 +++++++--------------- models/actions/run_job.go | 3 +-- options/locale/locale_en-US.ini | 2 +- routers/web/repo/actions/view.go | 3 ++- templates/repo/actions/runs_list.tmpl | 2 +- 5 files changed, 12 insertions(+), 20 deletions(-) diff --git a/models/actions/run.go b/models/actions/run.go index b5b27bd76d75d..24182a3310465 100644 --- a/models/actions/run.go +++ b/models/actions/run.go @@ -49,10 +49,10 @@ type ActionRun struct { // Started and Stopped is used for recording last run time, if rerun happened, they will be reset to 0 Started timeutil.TimeStamp Stopped timeutil.TimeStamp - // TotalDuration is used for recording the total duration of the workflow - TotalDuration time.Duration - Created timeutil.TimeStamp `xorm:"created"` - Updated timeutil.TimeStamp `xorm:"updated"` + // PreviousDuration is used for recording previous duration + PreviousDuration time.Duration + Created timeutil.TimeStamp `xorm:"created"` + Updated timeutil.TimeStamp `xorm:"updated"` } func init() { @@ -121,19 +121,11 @@ func (run *ActionRun) LoadAttributes(ctx context.Context) error { } func (run *ActionRun) Duration() time.Duration { - return calculateDuration(run.Started, run.Stopped, run.Status) + return run.CurrentDuration() + run.PreviousDuration } -func (run *ActionRun) DisplayDuration() time.Duration { - duration := calculateDuration(run.Started, run.Stopped, run.Status) - if run.Status.IsDone() { - // there was a bug when record stopped time, see https://github.com/go-gitea/gitea/issues/28323#issuecomment-1841867298 - // and duration will be negitive because of the incorrect stopped time in db - // there's no way to fix them now, so in order to avoid displaying incorrect duration time, - // we will display 0 instead of negitive time - return slices.Max([]time.Duration{run.TotalDuration, duration}) - } - return duration +func (run *ActionRun) CurrentDuration() time.Duration { + return calculateDuration(run.Started, run.Stopped, run.Status) } func (run *ActionRun) GetPushEventPayload() (*api.PushPayload, error) { diff --git a/models/actions/run_job.go b/models/actions/run_job.go index 5190e0c65644d..4b8664077dca9 100644 --- a/models/actions/run_job.go +++ b/models/actions/run_job.go @@ -143,9 +143,8 @@ func UpdateRunJob(ctx context.Context, job *ActionRunJob, cond builder.Cond, col } if run.Stopped.IsZero() && run.Status.IsDone() { run.Stopped = timeutil.TimeStampNow() - run.TotalDuration += run.Duration() } - if err := UpdateRun(ctx, run, "status", "started", "stopped", "total_duration"); err != nil { + if err := UpdateRun(ctx, run, "status", "started", "stopped"); err != nil { return 0, fmt.Errorf("update run %d: %w", run.ID, err) } } diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index 3565adffeea27..1dafceed5b8fb 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -3534,7 +3534,7 @@ runs.no_workflows.quick_start = Don't know how to start with Gitea Action? See < runs.no_workflows.documentation = For more information on the Gitea Action, see the documentation. runs.no_runs = The workflow has no runs yet. runs.empty_commit_message = (empty commit message) -runs.last_run_time = Last run: %s +runs.last_duration = Last run: %s workflow.disable = Disable Workflow workflow.disable_success = Workflow '%s' disabled successfully. diff --git a/routers/web/repo/actions/view.go b/routers/web/repo/actions/view.go index 5a8f72007c04a..9cda30d23dc70 100644 --- a/routers/web/repo/actions/view.go +++ b/routers/web/repo/actions/view.go @@ -281,9 +281,10 @@ func Rerun(ctx *context_module.Context) { // reset run's start and stop time when it is done if run.Status.IsDone() { + run.PreviousDuration = run.Duration() run.Started = 0 run.Stopped = 0 - if err := actions_model.UpdateRun(ctx, run, "started", "stopped"); err != nil { + if err := actions_model.UpdateRun(ctx, run, "started", "stopped", "previous_duration"); err != nil { ctx.Error(http.StatusInternalServerError, err.Error()) return } diff --git a/templates/repo/actions/runs_list.tmpl b/templates/repo/actions/runs_list.tmpl index 50ee6ec694592..9bc0226c04fe4 100644 --- a/templates/repo/actions/runs_list.tmpl +++ b/templates/repo/actions/runs_list.tmpl @@ -35,7 +35,7 @@
{{svg "octicon-calendar" 16}}{{TimeSinceUnix .Updated ctx.Locale}}
-
{{svg "octicon-stopwatch" 16}}{{.DisplayDuration}}
+
{{svg "octicon-stopwatch" 16}}{{.Duration}}
{{end}} From 44c2acf1c3b6de421c2f7d7ae355a733b5e1a807 Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Fri, 8 Dec 2023 05:36:32 +0000 Subject: [PATCH 4/7] add mmigration --- models/migrations/migrations.go | 2 ++ models/migrations/v1_22/v283.go | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 models/migrations/v1_22/v283.go diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go index 28e3be503b299..50d94fdac447a 100644 --- a/models/migrations/migrations.go +++ b/models/migrations/migrations.go @@ -550,6 +550,8 @@ var migrations = []Migration{ NewMigration("Add auth_token table", v1_22.CreateAuthTokenTable), // v282 -> v283 NewMigration("Add Index to pull_auto_merge.doer_id", v1_22.AddIndexToPullAutoMergeDoerID), + // v283 -> v284 + NewMigration("Add PreviousDuration to ActionRun", v1_22.AddPreviousDurationToActionRun), } // GetCurrentDBVersion returns the current db version diff --git a/models/migrations/v1_22/v283.go b/models/migrations/v1_22/v283.go new file mode 100644 index 0000000000000..c0dacd40bcdd4 --- /dev/null +++ b/models/migrations/v1_22/v283.go @@ -0,0 +1,18 @@ +// Copyright 2023 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package v1_22 //nolint + +import ( + "time" + + "xorm.io/xorm" +) + +func AddPreviousDurationToActionRun(x *xorm.Engine) error { + type ActionRun struct { + PreviousDuration time.Duration + } + + return x.Sync(&ActionRun{}) +} From 29aa2560f8f00185a8431401533dad03ec79a982 Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Mon, 15 Jan 2024 05:06:38 +0000 Subject: [PATCH 5/7] rename to v284 --- models/migrations/v1_22/{v283.go => v284.go} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename models/migrations/v1_22/{v283.go => v284.go} (100%) diff --git a/models/migrations/v1_22/v283.go b/models/migrations/v1_22/v284.go similarity index 100% rename from models/migrations/v1_22/v283.go rename to models/migrations/v1_22/v284.go From c471b0ab9274bd98e44c9c12197edd63d55e7467 Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Wed, 17 Jan 2024 08:33:16 +0000 Subject: [PATCH 6/7] only display one duration in ui --- models/actions/run.go | 6 +----- options/locale/locale_en-US.ini | 1 - templates/repo/actions/runs_list.tmpl | 2 +- 3 files changed, 2 insertions(+), 7 deletions(-) diff --git a/models/actions/run.go b/models/actions/run.go index 2be4286e52e87..fcac58d515c3b 100644 --- a/models/actions/run.go +++ b/models/actions/run.go @@ -121,11 +121,7 @@ func (run *ActionRun) LoadAttributes(ctx context.Context) error { } func (run *ActionRun) Duration() time.Duration { - return run.CurrentDuration() + run.PreviousDuration -} - -func (run *ActionRun) CurrentDuration() time.Duration { - return calculateDuration(run.Started, run.Stopped, run.Status) + return calculateDuration(run.Started, run.Stopped, run.Status) + run.PreviousDuration } func (run *ActionRun) GetPushEventPayload() (*api.PushPayload, error) { diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index 37ba8843bee9f..0e5d91bd57648 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -3544,7 +3544,6 @@ runs.no_workflows.quick_start = Don't know how to start with Gitea Actions? See runs.no_workflows.documentation = For more information on Gitea Actions, see the documentation. runs.no_runs = The workflow has no runs yet. runs.empty_commit_message = (empty commit message) -runs.last_duration = Last run: %s workflow.disable = Disable Workflow workflow.disable_success = Workflow '%s' disabled successfully. diff --git a/templates/repo/actions/runs_list.tmpl b/templates/repo/actions/runs_list.tmpl index a0edb5b1bdf68..580fb08a9ee2e 100644 --- a/templates/repo/actions/runs_list.tmpl +++ b/templates/repo/actions/runs_list.tmpl @@ -35,7 +35,7 @@
{{svg "octicon-calendar" 16}}{{TimeSinceUnix .Updated ctx.Locale}}
-
{{svg "octicon-stopwatch" 16}}{{.Duration}}
+
{{svg "octicon-stopwatch" 16}}{{.Duration}}
{{end}} From 6aa3443715b96473470b122b9e5f9180cd95d3c0 Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Wed, 17 Jan 2024 08:34:56 +0000 Subject: [PATCH 7/7] rename migration --- models/migrations/v1_22/{v284.go => v285.go} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename models/migrations/v1_22/{v284.go => v285.go} (100%) diff --git a/models/migrations/v1_22/v284.go b/models/migrations/v1_22/v285.go similarity index 100% rename from models/migrations/v1_22/v284.go rename to models/migrations/v1_22/v285.go