2015-03-21 07:53:16 -05:00
|
|
|
package sqlstore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2020-10-13 03:19:42 -05:00
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
2020-02-29 06:35:15 -06:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2016-09-23 09:56:12 -05:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2015-03-21 07:53:16 -05:00
|
|
|
)
|
|
|
|
|
2018-02-20 16:10:59 -06:00
|
|
|
// DeleteExpiredSnapshots removes snapshots with old expiry dates.
|
|
|
|
// SnapShotRemoveExpired is deprecated and should be removed in the future.
|
|
|
|
// Snapshot expiry is decided by the user when they share the snapshot.
|
2021-09-01 06:05:15 -05:00
|
|
|
func (ss *SQLStore) DeleteExpiredSnapshots(cmd *models.DeleteExpiredSnapshotsCommand) error {
|
2017-05-23 03:56:23 -05:00
|
|
|
return inTransaction(func(sess *DBSession) error {
|
2018-02-20 16:10:59 -06:00
|
|
|
if !setting.SnapShotRemoveExpired {
|
|
|
|
sqlog.Warn("[Deprecated] The snapshot_remove_expired setting is outdated. Please remove from your config.")
|
|
|
|
return nil
|
|
|
|
}
|
2016-09-23 09:56:12 -05:00
|
|
|
|
2020-11-10 23:21:08 -06:00
|
|
|
deleteExpiredSQL := "DELETE FROM dashboard_snapshot WHERE expires < ?"
|
|
|
|
expiredResponse, err := sess.Exec(deleteExpiredSQL, time.Now())
|
2018-02-20 16:10:59 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-09-23 09:56:12 -05:00
|
|
|
}
|
2018-02-20 16:10:59 -06:00
|
|
|
cmd.DeletedRows, _ = expiredResponse.RowsAffected()
|
2016-09-23 09:56:12 -05:00
|
|
|
|
2016-09-28 14:06:00 -05:00
|
|
|
return nil
|
2016-09-23 09:56:12 -05:00
|
|
|
})
|
2015-03-21 07:53:16 -05:00
|
|
|
}
|
|
|
|
|
2021-09-01 06:05:15 -05:00
|
|
|
func (ss *SQLStore) CreateDashboardSnapshot(cmd *models.CreateDashboardSnapshotCommand) error {
|
2017-05-23 03:56:23 -05:00
|
|
|
return inTransaction(func(sess *DBSession) error {
|
2015-03-26 06:00:52 -05:00
|
|
|
// never
|
|
|
|
var expires = time.Now().Add(time.Hour * 24 * 365 * 50)
|
|
|
|
if cmd.Expires > 0 {
|
|
|
|
expires = time.Now().Add(time.Second * time.Duration(cmd.Expires))
|
|
|
|
}
|
|
|
|
|
2020-10-13 03:19:42 -05:00
|
|
|
snapshot := &models.DashboardSnapshot{
|
|
|
|
Name: cmd.Name,
|
|
|
|
Key: cmd.Key,
|
|
|
|
DeleteKey: cmd.DeleteKey,
|
|
|
|
OrgId: cmd.OrgId,
|
|
|
|
UserId: cmd.UserId,
|
|
|
|
External: cmd.External,
|
|
|
|
ExternalUrl: cmd.ExternalUrl,
|
|
|
|
ExternalDeleteUrl: cmd.ExternalDeleteUrl,
|
|
|
|
Dashboard: simplejson.New(),
|
2021-09-01 06:05:15 -05:00
|
|
|
DashboardEncrypted: cmd.DashboardEncrypted,
|
2020-10-13 03:19:42 -05:00
|
|
|
Expires: expires,
|
|
|
|
Created: time.Now(),
|
|
|
|
Updated: time.Now(),
|
|
|
|
}
|
2021-09-01 06:05:15 -05:00
|
|
|
_, err := sess.Insert(snapshot)
|
2015-03-21 07:53:16 -05:00
|
|
|
cmd.Result = snapshot
|
|
|
|
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-09-01 06:05:15 -05:00
|
|
|
func (ss *SQLStore) DeleteDashboardSnapshot(cmd *models.DeleteDashboardSnapshotCommand) error {
|
2017-05-23 03:56:23 -05:00
|
|
|
return inTransaction(func(sess *DBSession) error {
|
2020-11-10 23:21:08 -06:00
|
|
|
var rawSQL = "DELETE FROM dashboard_snapshot WHERE delete_key=?"
|
|
|
|
_, err := sess.Exec(rawSQL, cmd.DeleteKey)
|
2015-03-26 14:34:58 -05:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-09-01 06:05:15 -05:00
|
|
|
func (ss *SQLStore) GetDashboardSnapshot(query *models.GetDashboardSnapshotQuery) error {
|
2020-02-29 06:35:15 -06:00
|
|
|
snapshot := models.DashboardSnapshot{Key: query.Key, DeleteKey: query.DeleteKey}
|
2015-03-26 06:00:52 -05:00
|
|
|
has, err := x.Get(&snapshot)
|
2015-03-21 07:53:16 -05:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
Simplify comparison to bool constant (gosimple)
This fixes:
build.go:553:6: should omit comparison to bool constant, can be simplified to !strings.Contains(path, ".sha256") (S1002)
pkg/cmd/grafana-cli/commands/ls_command.go:27:5: should omit comparison to bool constant, can be simplified to !pluginDirInfo.IsDir() (S1002)
pkg/components/dynmap/dynmap_test.go:24:5: should omit comparison to bool constant, can be simplified to !value (S1002)
pkg/components/dynmap/dynmap_test.go:122:14: should omit comparison to bool constant, can be simplified to b (S1002)
pkg/components/dynmap/dynmap_test.go:125:14: should omit comparison to bool constant, can be simplified to !b (S1002)
pkg/components/dynmap/dynmap_test.go:128:14: should omit comparison to bool constant, can be simplified to !b (S1002)
pkg/models/org_user.go:51:5: should omit comparison to bool constant, can be simplified to !(*r).IsValid() (S1002)
pkg/plugins/datasource/wrapper/datasource_plugin_wrapper_test.go:77:12: should omit comparison to bool constant, can be simplified to !haveBool (S1002)
pkg/services/alerting/conditions/evaluator.go:23:9: should omit comparison to bool constant, can be simplified to !reducedValue.Valid (S1002)
pkg/services/alerting/conditions/evaluator.go:48:5: should omit comparison to bool constant, can be simplified to !reducedValue.Valid (S1002)
pkg/services/alerting/conditions/evaluator.go:91:5: should omit comparison to bool constant, can be simplified to !reducedValue.Valid (S1002)
pkg/services/alerting/conditions/query.go:56:6: should omit comparison to bool constant, can be simplified to !reducedValue.Valid (S1002)
pkg/services/alerting/extractor.go:107:20: should omit comparison to bool constant, can be simplified to !enabled.MustBool() (S1002)
pkg/services/alerting/notifiers/telegram.go:222:41: should omit comparison to bool constant, can be simplified to this.UploadImage (S1002)
pkg/services/sqlstore/apikey.go:58:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/apikey.go:72:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/dashboard.go:66:33: should omit comparison to bool constant, can be simplified to !cmd.Overwrite (S1002)
pkg/services/sqlstore/dashboard.go:175:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/dashboard.go:311:13: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/dashboard.go:444:12: should omit comparison to bool constant, can be simplified to !exists (S1002)
pkg/services/sqlstore/dashboard.go:472:12: should omit comparison to bool constant, can be simplified to !exists (S1002)
pkg/services/sqlstore/dashboard.go:554:32: should omit comparison to bool constant, can be simplified to !cmd.Overwrite (S1002)
pkg/services/sqlstore/dashboard_snapshot.go:83:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/plugin_setting.go:39:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/quota.go:34:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/quota.go:111:6: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/quota.go:136:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/quota.go:213:6: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/temp_user.go:129:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/user.go:157:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/user.go:182:5: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/user.go:191:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/user.go:212:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/services/sqlstore/user.go:307:12: should omit comparison to bool constant, can be simplified to !has (S1002)
pkg/social/generic_oauth.go:185:5: should omit comparison to bool constant, can be simplified to !s.extractToken(&data, token) (S1002)
pkg/tsdb/mssql/mssql.go:148:39: should omit comparison to bool constant, can be simplified to ok (S1002)
pkg/tsdb/mssql/mssql.go:212:6: should omit comparison to bool constant, can be simplified to !query.Model.Get("fillNull").MustBool(false) (S1002)
pkg/tsdb/mssql/mssql.go:247:56: should omit comparison to bool constant, can be simplified to ok (S1002)
pkg/tsdb/mssql/mssql.go:274:7: should omit comparison to bool constant, can be simplified to !exist (S1002)
pkg/tsdb/mssql/mssql.go:282:8: should omit comparison to bool constant, can be simplified to !exist (S1002)
pkg/tsdb/mysql/mysql.go:221:6: should omit comparison to bool constant, can be simplified to !query.Model.Get("fillNull").MustBool(false) (S1002)
pkg/tsdb/mysql/mysql.go:256:56: should omit comparison to bool constant, can be simplified to ok (S1002)
pkg/tsdb/mysql/mysql.go:283:7: should omit comparison to bool constant, can be simplified to !exist (S1002)
pkg/tsdb/mysql/mysql.go:291:8: should omit comparison to bool constant, can be simplified to !exist (S1002)
pkg/tsdb/postgres/postgres.go:134:39: should omit comparison to bool constant, can be simplified to ok (S1002)
pkg/tsdb/postgres/postgres.go:201:6: should omit comparison to bool constant, can be simplified to !query.Model.Get("fillNull").MustBool(false) (S1002)
pkg/tsdb/postgres/postgres.go:236:56: should omit comparison to bool constant, can be simplified to ok (S1002)
pkg/tsdb/postgres/postgres.go:263:7: should omit comparison to bool constant, can be simplified to !exist (S1002)
pkg/tsdb/postgres/postgres.go:271:8: should omit comparison to bool constant, can be simplified to !exist (S1002)
2018-04-16 13:12:59 -05:00
|
|
|
} else if !has {
|
2020-02-29 06:35:15 -06:00
|
|
|
return models.ErrDashboardSnapshotNotFound
|
2015-03-21 07:53:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
query.Result = &snapshot
|
|
|
|
return nil
|
|
|
|
}
|
2016-01-19 03:37:36 -06:00
|
|
|
|
2018-02-20 16:26:08 -06:00
|
|
|
// SearchDashboardSnapshots returns a list of all snapshots for admins
|
|
|
|
// for other roles, it returns snapshots created by the user
|
2021-09-01 06:05:15 -05:00
|
|
|
func (ss *SQLStore) SearchDashboardSnapshots(query *models.GetDashboardSnapshotsQuery) error {
|
2020-02-29 06:35:15 -06:00
|
|
|
var snapshots = make(models.DashboardSnapshotsList, 0)
|
2016-01-19 03:37:36 -06:00
|
|
|
|
2021-02-15 02:59:39 -06:00
|
|
|
sess := x.NewSession()
|
|
|
|
if query.Limit > 0 {
|
|
|
|
sess.Limit(query.Limit)
|
|
|
|
}
|
2017-10-10 07:25:19 -05:00
|
|
|
sess.Table("dashboard_snapshot")
|
2016-01-19 03:37:36 -06:00
|
|
|
|
2016-01-19 07:05:24 -06:00
|
|
|
if query.Name != "" {
|
|
|
|
sess.Where("name LIKE ?", query.Name)
|
|
|
|
}
|
2016-01-19 03:37:36 -06:00
|
|
|
|
2018-02-20 16:26:08 -06:00
|
|
|
// admins can see all snapshots, everyone else can only see their own snapshots
|
2020-07-16 07:39:01 -05:00
|
|
|
switch {
|
|
|
|
case query.SignedInUser.OrgRole == models.ROLE_ADMIN:
|
2018-02-20 16:26:08 -06:00
|
|
|
sess.Where("org_id = ?", query.OrgId)
|
2020-07-16 07:39:01 -05:00
|
|
|
case !query.SignedInUser.IsAnonymous:
|
2018-02-20 16:26:08 -06:00
|
|
|
sess.Where("org_id = ? AND user_id = ?", query.OrgId, query.SignedInUser.UserId)
|
2020-07-16 07:39:01 -05:00
|
|
|
default:
|
2018-02-20 16:26:08 -06:00
|
|
|
query.Result = snapshots
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-19 07:05:24 -06:00
|
|
|
err := sess.Find(&snapshots)
|
|
|
|
query.Result = snapshots
|
|
|
|
return err
|
2016-01-19 03:37:36 -06:00
|
|
|
}
|