comparison pkg/imports/statsupdate.go @ 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
children ade07a3f2cfd
comparison
equal deleted inserted replaced
5386:fc3a5345b0fd 5387:8e30b926b94d
1 // This is Free Software under GNU Affero General Public License v >= 3.0
2 // without warranty, see README.md and license for details.
3 //
4 // SPDX-License-Identifier: AGPL-3.0-or-later
5 // License-Filename: LICENSES/AGPL-3.0.txt
6 //
7 // Copyright (C) 2021 by via donau
8 // – Österreichische Wasserstraßen-Gesellschaft mbH
9 // Software engineering by Intevation GmbH
10 //
11 // Author(s):
12 // * Sascha L. Teichmann <sascha.teichmann@intevation.de>
13
14 package imports
15
16 import (
17 "context"
18 "database/sql"
19 "errors"
20 "fmt"
21 "time"
22
23 "gemma.intevation.de/gemma/pkg/auth"
24 "gemma.intevation.de/gemma/pkg/common"
25 "gemma.intevation.de/gemma/pkg/models"
26 )
27
28 type StatsUpdate struct {
29 models.QueueConfigurationType
30 Name string `json:"name"`
31 }
32
33 const StatsUpdateJobKind JobKind = "statsupdate"
34
35 type statsUpdateJobCreator struct{}
36
37 func init() { RegisterJobCreator(StatsUpdateJobKind, statsUpdateJobCreator{}) }
38
39 func (statsUpdateJobCreator) Description() string { return "statsupdate" }
40
41 func (statsUpdateJobCreator) AutoAccept() bool { return true }
42
43 func (statsUpdateJobCreator) Create() Job { return new(StatsUpdate) }
44
45 func (statsUpdateJobCreator) Depends() [2][]string { return [2][]string{{}, {}} }
46
47 func (statsUpdateJobCreator) StageDone(context.Context, *sql.Tx, int64, Feedback) error {
48 return nil
49 }
50
51 // RequiresRoles enforces to be a sys_admin to run this .
52 func (*StatsUpdate) RequiresRoles() auth.Roles { return auth.Roles{"sys_admin"} }
53
54 func (su *StatsUpdate) Description() (string, error) { return su.Name, nil }
55
56 func (*StatsUpdate) CleanUp() error { return nil }
57
58 func (su *StatsUpdate) MarshalAttributes(attrs common.Attributes) error {
59 if err := su.QueueConfigurationType.MarshalAttributes(attrs); err != nil {
60 return err
61 }
62 attrs.Set("name", su.Name)
63 return nil
64 }
65
66 func (su *StatsUpdate) UnmarshalAttributes(attrs common.Attributes) error {
67 if err := su.QueueConfigurationType.UnmarshalAttributes(attrs); err != nil {
68 return err
69 }
70 name, found := attrs.Get("name")
71 if !found {
72 return errors.New("missing 'name' attribute")
73 }
74 su.Name = name
75 return nil
76 }
77
78 const loadUpdateStatsScriptSQL = `SELECT script FROM sys_admin.stats_updates WHERE name = $1`
79
80 func (su *StatsUpdate) Do(
81 ctx context.Context,
82 importID int64,
83 conn *sql.Conn,
84 feedback Feedback,
85 ) (interface{}, error) {
86
87 start := time.Now()
88
89 feedback.Info("Running stats update %s.", su.Name)
90
91 tx, err := conn.BeginTx(ctx, nil)
92 if err != nil {
93 return nil, err
94 }
95 defer tx.Rollback()
96
97 var script string
98 switch err := tx.QueryRowContext(ctx, loadUpdateStatsScriptSQL, su.Name).Scan(&script); {
99 case err == sql.ErrNoRows:
100 return nil, fmt.Errorf("no update script found for '%s'", su.Name)
101 case err != nil:
102 return nil, err
103 }
104
105 if _, err := tx.ExecContext(ctx, script); err != nil {
106 return nil, err
107 }
108
109 if err := tx.Commit(); err != nil {
110 return nil, err
111 }
112
113 feedback.Info("Running stats update took %v.", time.Since(start))
114
115 return nil, nil
116 }