Mercurial > gemma
changeset 5387:8e30b926b94d extented-report
Added an import to run update stats scripts.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Fri, 02 Jul 2021 00:42:05 +0200 |
parents | fc3a5345b0fd |
children | 60bba8e6322b |
files | pkg/controllers/routes.go pkg/imports/modelconvert.go pkg/imports/report.go pkg/imports/statsupdate.go |
diffstat | 4 files changed, 120 insertions(+), 3 deletions(-) [+] |
line wrap: on
line diff
--- a/pkg/controllers/routes.go Thu Jul 01 23:07:14 2021 +0200 +++ b/pkg/controllers/routes.go Fri Jul 02 00:42:05 2021 +0200 @@ -264,7 +264,7 @@ NoConn: true, })).Methods(http.MethodPost) - api.Handle("/imports/{kind:report}", sysAdmin(&mw.JSONHandler{ + api.Handle("/imports/{kind:report|statsupdate}", sysAdmin(&mw.JSONHandler{ Input: importModel, Handle: manualImport, NoConn: true,
--- a/pkg/imports/modelconvert.go Thu Jul 01 23:07:14 2021 +0200 +++ b/pkg/imports/modelconvert.go Fri Jul 02 00:42:05 2021 +0200 @@ -4,7 +4,7 @@ // SPDX-License-Identifier: AGPL-3.0-or-later // License-Filename: LICENSES/AGPL-3.0.txt // -// Copyright (C) 2018, 2020 by via donau +// Copyright (C) 2018, 2020, 2021 by via donau // – Österreichische Wasserstraßen-Gesellschaft mbH // Software engineering by Intevation GmbH // @@ -46,6 +46,7 @@ DSTJobKind: func() interface{} { return new(models.StretchDelete) }, DSRJobKind: func() interface{} { return new(models.SoundingResultDelete) }, ReportJobKind: func() interface{} { return FindJobCreator(ReportJobKind).Create() }, + StatsUpdateJobKind: func() interface{} { return FindJobCreator(StatsUpdateJobKind).Create() }, } // ImportModelForJobKind returns the constructor function to
--- a/pkg/imports/report.go Thu Jul 01 23:07:14 2021 +0200 +++ b/pkg/imports/report.go Fri Jul 02 00:42:05 2021 +0200 @@ -4,7 +4,7 @@ // SPDX-License-Identifier: AGPL-3.0-or-later // License-Filename: LICENSES/AGPL-3.0.txt // -// Copyright (C) 2018 by via donau +// Copyright (C) 2021 by via donau // – Österreichische Wasserstraßen-Gesellschaft mbH // Software engineering by Intevation GmbH //
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pkg/imports/statsupdate.go Fri Jul 02 00:42:05 2021 +0200 @@ -0,0 +1,116 @@ +// This is Free Software under GNU Affero General Public License v >= 3.0 +// without warranty, see README.md and license for details. +// +// SPDX-License-Identifier: AGPL-3.0-or-later +// License-Filename: LICENSES/AGPL-3.0.txt +// +// Copyright (C) 2021 by via donau +// – Österreichische Wasserstraßen-Gesellschaft mbH +// Software engineering by Intevation GmbH +// +// Author(s): +// * Sascha L. Teichmann <sascha.teichmann@intevation.de> + +package imports + +import ( + "context" + "database/sql" + "errors" + "fmt" + "time" + + "gemma.intevation.de/gemma/pkg/auth" + "gemma.intevation.de/gemma/pkg/common" + "gemma.intevation.de/gemma/pkg/models" +) + +type StatsUpdate struct { + models.QueueConfigurationType + Name string `json:"name"` +} + +const StatsUpdateJobKind JobKind = "statsupdate" + +type statsUpdateJobCreator struct{} + +func init() { RegisterJobCreator(StatsUpdateJobKind, statsUpdateJobCreator{}) } + +func (statsUpdateJobCreator) Description() string { return "statsupdate" } + +func (statsUpdateJobCreator) AutoAccept() bool { return true } + +func (statsUpdateJobCreator) Create() Job { return new(StatsUpdate) } + +func (statsUpdateJobCreator) Depends() [2][]string { return [2][]string{{}, {}} } + +func (statsUpdateJobCreator) StageDone(context.Context, *sql.Tx, int64, Feedback) error { + return nil +} + +// RequiresRoles enforces to be a sys_admin to run this . +func (*StatsUpdate) RequiresRoles() auth.Roles { return auth.Roles{"sys_admin"} } + +func (su *StatsUpdate) Description() (string, error) { return su.Name, nil } + +func (*StatsUpdate) CleanUp() error { return nil } + +func (su *StatsUpdate) MarshalAttributes(attrs common.Attributes) error { + if err := su.QueueConfigurationType.MarshalAttributes(attrs); err != nil { + return err + } + attrs.Set("name", su.Name) + return nil +} + +func (su *StatsUpdate) UnmarshalAttributes(attrs common.Attributes) error { + if err := su.QueueConfigurationType.UnmarshalAttributes(attrs); err != nil { + return err + } + name, found := attrs.Get("name") + if !found { + return errors.New("missing 'name' attribute") + } + su.Name = name + return nil +} + +const loadUpdateStatsScriptSQL = `SELECT script FROM sys_admin.stats_updates WHERE name = $1` + +func (su *StatsUpdate) Do( + ctx context.Context, + importID int64, + conn *sql.Conn, + feedback Feedback, +) (interface{}, error) { + + start := time.Now() + + feedback.Info("Running stats update %s.", su.Name) + + tx, err := conn.BeginTx(ctx, nil) + if err != nil { + return nil, err + } + defer tx.Rollback() + + var script string + switch err := tx.QueryRowContext(ctx, loadUpdateStatsScriptSQL, su.Name).Scan(&script); { + case err == sql.ErrNoRows: + return nil, fmt.Errorf("no update script found for '%s'", su.Name) + case err != nil: + return nil, err + } + + if _, err := tx.ExecContext(ctx, script); err != nil { + return nil, err + } + + if err := tx.Commit(); err != nil { + return nil, err + } + + feedback.Info("Running stats update took %v.", time.Since(start)) + + return nil, nil +}