2022-09-19 02:54:37 -05:00
package annotationsimpl
2016-08-01 03:07:00 -05:00
import (
2016-08-30 02:32:56 -05:00
"bytes"
2022-03-22 06:20:57 -05:00
"context"
2017-10-07 03:31:39 -05:00
"errors"
2016-08-30 02:32:56 -05:00
"fmt"
2016-09-14 01:36:44 -05:00
"strings"
2018-03-21 20:22:58 -05:00
"time"
2016-08-30 02:32:56 -05:00
2022-10-19 08:02:15 -05:00
"github.com/grafana/grafana/pkg/infra/db"
2022-09-19 02:54:37 -05:00
"github.com/grafana/grafana/pkg/infra/log"
2017-10-07 03:31:39 -05:00
"github.com/grafana/grafana/pkg/models"
2022-04-11 07:18:38 -05:00
ac "github.com/grafana/grafana/pkg/services/accesscontrol"
2016-08-01 03:07:00 -05:00
"github.com/grafana/grafana/pkg/services/annotations"
2022-11-04 10:39:26 -05:00
"github.com/grafana/grafana/pkg/services/sqlstore"
2022-04-11 07:18:38 -05:00
"github.com/grafana/grafana/pkg/services/sqlstore/permissions"
"github.com/grafana/grafana/pkg/services/sqlstore/searchstore"
2022-09-21 07:04:01 -05:00
"github.com/grafana/grafana/pkg/services/tag"
2022-08-10 04:56:48 -05:00
"github.com/grafana/grafana/pkg/services/user"
2022-09-19 02:54:37 -05:00
"github.com/grafana/grafana/pkg/setting"
2016-08-01 03:07:00 -05:00
)
2022-09-19 02:54:37 -05:00
var timeNow = time . Now
2019-08-16 03:49:30 -05:00
// Update the item so that EpochEnd >= Epoch
func validateTimeRange ( item * annotations . Item ) error {
if item . EpochEnd == 0 {
if item . Epoch == 0 {
2021-03-29 08:47:16 -05:00
return annotations . ErrTimerangeMissing
2019-08-16 03:49:30 -05:00
}
item . EpochEnd = item . Epoch
}
if item . Epoch == 0 {
item . Epoch = item . EpochEnd
}
if item . EpochEnd < item . Epoch {
2020-09-22 09:22:19 -05:00
item . Epoch , item . EpochEnd = item . EpochEnd , item . Epoch
2019-08-16 03:49:30 -05:00
}
return nil
}
2022-09-22 07:27:48 -05:00
type xormRepositoryImpl struct {
2022-09-23 05:04:41 -05:00
cfg * setting . Cfg
db db . DB
log log . Logger
maximumTagsLength int64
tagService tag . Service
2022-04-11 07:18:38 -05:00
}
2022-09-22 07:27:48 -05:00
func ( r * xormRepositoryImpl ) Add ( ctx context . Context , item * annotations . Item ) error {
2022-09-21 07:04:01 -05:00
tags := tag . ParseTagPairs ( item . Tags )
item . Tags = tag . JoinTagPairs ( tags )
item . Created = timeNow ( ) . UnixNano ( ) / int64 ( time . Millisecond )
item . Updated = item . Created
if item . Epoch == 0 {
item . Epoch = item . Created
}
2022-09-23 05:04:41 -05:00
if err := r . validateItem ( item ) ; err != nil {
2022-09-21 07:04:01 -05:00
return err
}
2018-03-22 10:21:47 -05:00
2022-10-19 08:02:15 -05:00
return r . db . WithDbSession ( ctx , func ( sess * db . Session ) error {
2016-08-01 03:07:00 -05:00
if _ , err := sess . Table ( "annotation" ) . Insert ( item ) ; err != nil {
return err
}
2022-11-04 10:39:26 -05:00
return r . synchronizeTags ( ctx , item )
} )
}
// AddMany inserts large batches of annotations at once.
// It does not return IDs associated with created annotations, and it does not support annotations with tags. If you need this functionality, use the single-item Add instead.
// This is due to a limitation with some supported databases:
// We cannot correlate the IDs of batch-inserted records without acquiring a full table lock in MySQL.
// Annotations have no other uniquifier field, so we also cannot re-query for them after the fact.
// So, callers can only reliably use this endpoint if they don't care about returned IDs.
func ( r * xormRepositoryImpl ) AddMany ( ctx context . Context , items [ ] annotations . Item ) error {
hasTags := make ( [ ] annotations . Item , 0 )
hasNoTags := make ( [ ] annotations . Item , 0 )
for i , item := range items {
tags := tag . ParseTagPairs ( item . Tags )
item . Tags = tag . JoinTagPairs ( tags )
item . Created = timeNow ( ) . UnixNano ( ) / int64 ( time . Millisecond )
item . Updated = item . Created
if item . Epoch == 0 {
item . Epoch = item . Created
}
if err := r . validateItem ( & items [ i ] ) ; err != nil {
return err
}
if len ( item . Tags ) > 0 {
hasTags = append ( hasTags , item )
} else {
hasNoTags = append ( hasNoTags , item )
}
}
return r . db . WithDbSession ( ctx , func ( sess * sqlstore . DBSession ) error {
// We can batch-insert every annotation with no tags. If an annotation has tags, we need the ID.
opts := sqlstore . NativeSettingsForDialect ( r . db . GetDialect ( ) )
if _ , err := sess . BulkInsert ( "annotation" , hasNoTags , opts ) ; err != nil {
return err
}
for i , item := range hasTags {
if _ , err := sess . Table ( "annotation" ) . Insert ( item ) ; err != nil {
return err
}
if err := r . synchronizeTags ( ctx , & hasTags [ i ] ) ; err != nil {
return err
}
}
return nil
} )
}
2016-08-01 03:07:00 -05:00
2022-11-04 10:39:26 -05:00
func ( r * xormRepositoryImpl ) synchronizeTags ( ctx context . Context , item * annotations . Item ) error {
// Will re-use session if one has already been opened with the same ctx.
return r . db . WithDbSession ( ctx , func ( sess * sqlstore . DBSession ) error {
2017-10-07 03:31:39 -05:00
if item . Tags != nil {
2022-11-04 10:39:26 -05:00
tags , err := r . tagService . EnsureTagsExist ( ctx , tag . ParseTagPairs ( item . Tags ) )
Outdent code after if block that ends with return (golint)
This commit fixes the following golint warnings:
pkg/bus/bus.go:64:9: if block ends with a return statement, so drop this else and outdent its block
pkg/bus/bus.go:84:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:137:10: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:177:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:183:10: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:199:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:208:9: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/components/dynmap/dynmap.go:236:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:242:10: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:257:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:263:10: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:278:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:284:10: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:299:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:331:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:350:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:356:10: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:366:12: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:390:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:396:10: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:405:12: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:427:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:433:10: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:442:12: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:459:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:465:10: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:474:12: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:491:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:497:10: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:506:12: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:523:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:529:10: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:538:12: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:555:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:561:10: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:570:12: if block ends with a return statement, so drop this else and outdent its block
pkg/login/ldap.go:55:11: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/login/ldap_test.go:372:10: if block ends with a return statement, so drop this else and outdent its block
pkg/middleware/middleware_test.go:213:12: if block ends with a return statement, so drop this else and outdent its block
pkg/plugins/dashboard_importer.go:153:11: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/plugins/dashboards_updater.go:39:9: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/plugins/dashboards_updater.go:121:10: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/plugins/plugins.go:210:9: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/plugins/plugins.go:235:9: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/services/alerting/eval_context.go:111:9: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/services/alerting/notifier.go:92:9: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/services/alerting/notifier.go:98:9: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/services/alerting/notifier.go:122:10: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/services/alerting/rule.go:108:10: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/services/alerting/rule.go:118:10: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/services/alerting/rule.go:121:11: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/services/alerting/notifiers/telegram.go:94:10: if block ends with a return statement, so drop this else and outdent its block
pkg/services/sqlstore/annotation.go:34:11: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/services/sqlstore/annotation.go:99:11: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/services/sqlstore/dashboard_test.go:107:13: if block ends with a return statement, so drop this else and outdent its block
pkg/services/sqlstore/plugin_setting.go:78:10: if block ends with a return statement, so drop this else and outdent its block
pkg/services/sqlstore/preferences.go:91:10: if block ends with a return statement, so drop this else and outdent its block
pkg/services/sqlstore/user.go:50:10: if block ends with a return statement, so drop this else and outdent its block
pkg/services/sqlstore/migrator/migrator.go:106:11: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/services/sqlstore/migrator/postgres_dialect.go:48:10: if block ends with a return statement, so drop this else and outdent its block
pkg/tsdb/time_range.go:59:9: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/tsdb/time_range.go:67:9: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/tsdb/cloudwatch/metric_find_query.go:225:9: if block ends with a return statement, so drop this else and outdent its block
pkg/util/filepath.go:68:11: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
2018-04-27 15:42:49 -05:00
if err != nil {
2017-10-07 03:31:39 -05:00
return err
Outdent code after if block that ends with return (golint)
This commit fixes the following golint warnings:
pkg/bus/bus.go:64:9: if block ends with a return statement, so drop this else and outdent its block
pkg/bus/bus.go:84:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:137:10: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:177:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:183:10: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:199:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:208:9: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/components/dynmap/dynmap.go:236:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:242:10: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:257:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:263:10: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:278:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:284:10: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:299:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:331:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:350:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:356:10: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:366:12: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:390:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:396:10: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:405:12: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:427:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:433:10: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:442:12: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:459:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:465:10: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:474:12: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:491:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:497:10: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:506:12: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:523:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:529:10: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:538:12: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:555:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:561:10: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:570:12: if block ends with a return statement, so drop this else and outdent its block
pkg/login/ldap.go:55:11: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/login/ldap_test.go:372:10: if block ends with a return statement, so drop this else and outdent its block
pkg/middleware/middleware_test.go:213:12: if block ends with a return statement, so drop this else and outdent its block
pkg/plugins/dashboard_importer.go:153:11: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/plugins/dashboards_updater.go:39:9: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/plugins/dashboards_updater.go:121:10: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/plugins/plugins.go:210:9: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/plugins/plugins.go:235:9: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/services/alerting/eval_context.go:111:9: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/services/alerting/notifier.go:92:9: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/services/alerting/notifier.go:98:9: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/services/alerting/notifier.go:122:10: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/services/alerting/rule.go:108:10: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/services/alerting/rule.go:118:10: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/services/alerting/rule.go:121:11: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/services/alerting/notifiers/telegram.go:94:10: if block ends with a return statement, so drop this else and outdent its block
pkg/services/sqlstore/annotation.go:34:11: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/services/sqlstore/annotation.go:99:11: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/services/sqlstore/dashboard_test.go:107:13: if block ends with a return statement, so drop this else and outdent its block
pkg/services/sqlstore/plugin_setting.go:78:10: if block ends with a return statement, so drop this else and outdent its block
pkg/services/sqlstore/preferences.go:91:10: if block ends with a return statement, so drop this else and outdent its block
pkg/services/sqlstore/user.go:50:10: if block ends with a return statement, so drop this else and outdent its block
pkg/services/sqlstore/migrator/migrator.go:106:11: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/services/sqlstore/migrator/postgres_dialect.go:48:10: if block ends with a return statement, so drop this else and outdent its block
pkg/tsdb/time_range.go:59:9: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/tsdb/time_range.go:67:9: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/tsdb/cloudwatch/metric_find_query.go:225:9: if block ends with a return statement, so drop this else and outdent its block
pkg/util/filepath.go:68:11: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
2018-04-27 15:42:49 -05:00
}
for _ , tag := range tags {
if _ , err := sess . Exec ( "INSERT INTO annotation_tag (annotation_id, tag_id) VALUES(?,?)" , item . Id , tag . Id ) ; err != nil {
return err
2017-10-07 03:31:39 -05:00
}
}
}
2016-08-01 03:07:00 -05:00
return nil
} )
2016-08-30 02:32:56 -05:00
}
2022-09-22 07:27:48 -05:00
func ( r * xormRepositoryImpl ) Update ( ctx context . Context , item * annotations . Item ) error {
2022-10-19 08:02:15 -05:00
return r . db . WithTransactionalDbSession ( ctx , func ( sess * db . Session ) error {
2017-10-07 03:31:39 -05:00
var (
isExist bool
err error
)
existing := new ( annotations . Item )
2019-08-16 03:49:30 -05:00
isExist , err = sess . Table ( "annotation" ) . Where ( "id=? AND org_id=?" , item . Id , item . OrgId ) . Get ( existing )
2017-10-07 03:31:39 -05:00
if err != nil {
return err
}
if ! isExist {
2020-11-05 04:57:20 -06:00
return errors . New ( "annotation not found" )
2017-10-07 03:31:39 -05:00
}
2020-10-26 01:45:30 -05:00
existing . Updated = timeNow ( ) . UnixNano ( ) / int64 ( time . Millisecond )
2017-10-07 03:31:39 -05:00
existing . Text = item . Text
2019-08-16 03:49:30 -05:00
if item . Epoch != 0 {
existing . Epoch = item . Epoch
}
if item . EpochEnd != 0 {
existing . EpochEnd = item . EpochEnd
}
2022-12-26 08:53:52 -06:00
if item . Data != nil {
existing . Data = item . Data
}
2017-10-07 03:31:39 -05:00
if item . Tags != nil {
2022-09-21 07:04:01 -05:00
tags , err := r . tagService . EnsureTagsExist ( ctx , tag . ParseTagPairs ( item . Tags ) )
Outdent code after if block that ends with return (golint)
This commit fixes the following golint warnings:
pkg/bus/bus.go:64:9: if block ends with a return statement, so drop this else and outdent its block
pkg/bus/bus.go:84:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:137:10: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:177:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:183:10: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:199:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:208:9: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/components/dynmap/dynmap.go:236:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:242:10: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:257:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:263:10: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:278:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:284:10: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:299:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:331:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:350:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:356:10: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:366:12: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:390:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:396:10: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:405:12: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:427:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:433:10: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:442:12: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:459:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:465:10: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:474:12: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:491:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:497:10: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:506:12: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:523:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:529:10: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:538:12: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:555:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:561:10: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:570:12: if block ends with a return statement, so drop this else and outdent its block
pkg/login/ldap.go:55:11: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/login/ldap_test.go:372:10: if block ends with a return statement, so drop this else and outdent its block
pkg/middleware/middleware_test.go:213:12: if block ends with a return statement, so drop this else and outdent its block
pkg/plugins/dashboard_importer.go:153:11: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/plugins/dashboards_updater.go:39:9: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/plugins/dashboards_updater.go:121:10: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/plugins/plugins.go:210:9: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/plugins/plugins.go:235:9: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/services/alerting/eval_context.go:111:9: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/services/alerting/notifier.go:92:9: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/services/alerting/notifier.go:98:9: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/services/alerting/notifier.go:122:10: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/services/alerting/rule.go:108:10: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/services/alerting/rule.go:118:10: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/services/alerting/rule.go:121:11: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/services/alerting/notifiers/telegram.go:94:10: if block ends with a return statement, so drop this else and outdent its block
pkg/services/sqlstore/annotation.go:34:11: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/services/sqlstore/annotation.go:99:11: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/services/sqlstore/dashboard_test.go:107:13: if block ends with a return statement, so drop this else and outdent its block
pkg/services/sqlstore/plugin_setting.go:78:10: if block ends with a return statement, so drop this else and outdent its block
pkg/services/sqlstore/preferences.go:91:10: if block ends with a return statement, so drop this else and outdent its block
pkg/services/sqlstore/user.go:50:10: if block ends with a return statement, so drop this else and outdent its block
pkg/services/sqlstore/migrator/migrator.go:106:11: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/services/sqlstore/migrator/postgres_dialect.go:48:10: if block ends with a return statement, so drop this else and outdent its block
pkg/tsdb/time_range.go:59:9: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/tsdb/time_range.go:67:9: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/tsdb/cloudwatch/metric_find_query.go:225:9: if block ends with a return statement, so drop this else and outdent its block
pkg/util/filepath.go:68:11: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
2018-04-27 15:42:49 -05:00
if err != nil {
return err
}
if _ , err := sess . Exec ( "DELETE FROM annotation_tag WHERE annotation_id = ?" , existing . Id ) ; err != nil {
2017-10-07 03:31:39 -05:00
return err
Outdent code after if block that ends with return (golint)
This commit fixes the following golint warnings:
pkg/bus/bus.go:64:9: if block ends with a return statement, so drop this else and outdent its block
pkg/bus/bus.go:84:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:137:10: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:177:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:183:10: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:199:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:208:9: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/components/dynmap/dynmap.go:236:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:242:10: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:257:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:263:10: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:278:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:284:10: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:299:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:331:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:350:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:356:10: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:366:12: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:390:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:396:10: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:405:12: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:427:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:433:10: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:442:12: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:459:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:465:10: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:474:12: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:491:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:497:10: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:506:12: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:523:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:529:10: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:538:12: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:555:9: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:561:10: if block ends with a return statement, so drop this else and outdent its block
pkg/components/dynmap/dynmap.go:570:12: if block ends with a return statement, so drop this else and outdent its block
pkg/login/ldap.go:55:11: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/login/ldap_test.go:372:10: if block ends with a return statement, so drop this else and outdent its block
pkg/middleware/middleware_test.go:213:12: if block ends with a return statement, so drop this else and outdent its block
pkg/plugins/dashboard_importer.go:153:11: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/plugins/dashboards_updater.go:39:9: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/plugins/dashboards_updater.go:121:10: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/plugins/plugins.go:210:9: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/plugins/plugins.go:235:9: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/services/alerting/eval_context.go:111:9: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/services/alerting/notifier.go:92:9: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/services/alerting/notifier.go:98:9: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/services/alerting/notifier.go:122:10: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/services/alerting/rule.go:108:10: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/services/alerting/rule.go:118:10: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/services/alerting/rule.go:121:11: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/services/alerting/notifiers/telegram.go:94:10: if block ends with a return statement, so drop this else and outdent its block
pkg/services/sqlstore/annotation.go:34:11: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/services/sqlstore/annotation.go:99:11: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/services/sqlstore/dashboard_test.go:107:13: if block ends with a return statement, so drop this else and outdent its block
pkg/services/sqlstore/plugin_setting.go:78:10: if block ends with a return statement, so drop this else and outdent its block
pkg/services/sqlstore/preferences.go:91:10: if block ends with a return statement, so drop this else and outdent its block
pkg/services/sqlstore/user.go:50:10: if block ends with a return statement, so drop this else and outdent its block
pkg/services/sqlstore/migrator/migrator.go:106:11: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/services/sqlstore/migrator/postgres_dialect.go:48:10: if block ends with a return statement, so drop this else and outdent its block
pkg/tsdb/time_range.go:59:9: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/tsdb/time_range.go:67:9: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
pkg/tsdb/cloudwatch/metric_find_query.go:225:9: if block ends with a return statement, so drop this else and outdent its block
pkg/util/filepath.go:68:11: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
2018-04-27 15:42:49 -05:00
}
for _ , tag := range tags {
if _ , err := sess . Exec ( "INSERT INTO annotation_tag (annotation_id, tag_id) VALUES(?,?)" , existing . Id , tag . Id ) ; err != nil {
2017-10-07 03:31:39 -05:00
return err
}
}
}
existing . Tags = item . Tags
2017-04-12 09:26:34 -05:00
2022-09-23 05:04:41 -05:00
if err := r . validateItem ( existing ) ; err != nil {
return err
}
2022-12-26 08:53:52 -06:00
_ , err = sess . Table ( "annotation" ) . ID ( existing . Id ) . Cols ( "epoch" , "text" , "epoch_end" , "updated" , "tags" , "data" ) . Update ( existing )
2018-04-16 12:54:23 -05:00
return err
2017-04-12 09:26:34 -05:00
} )
}
2022-09-22 07:27:48 -05:00
func ( r * xormRepositoryImpl ) Get ( ctx context . Context , query * annotations . ItemQuery ) ( [ ] * annotations . ItemDTO , error ) {
2016-08-30 02:32:56 -05:00
var sql bytes . Buffer
params := make ( [ ] interface { } , 0 )
2022-03-22 06:20:57 -05:00
items := make ( [ ] * annotations . ItemDTO , 0 )
2022-10-19 08:02:15 -05:00
err := r . db . WithDbSession ( ctx , func ( sess * db . Session ) error {
2022-03-22 06:20:57 -05:00
sql . WriteString ( `
SELECT
annotation . id ,
annotation . epoch as time ,
annotation . epoch_end as time_end ,
annotation . dashboard_id ,
annotation . panel_id ,
annotation . new_state ,
annotation . prev_state ,
annotation . alert_id ,
annotation . text ,
annotation . tags ,
annotation . data ,
annotation . created ,
annotation . updated ,
usr . email ,
usr . login ,
alert . name as alert_name
FROM annotation
2022-09-19 02:54:37 -05:00
LEFT OUTER JOIN ` + r.db.GetDialect().Quote("user") + ` as usr on usr . id = annotation . user_id
2022-03-22 06:20:57 -05:00
LEFT OUTER JOIN alert on alert . id = annotation . alert_id
INNER JOIN (
SELECT a . id from annotation a
` )
sql . WriteString ( ` WHERE a.org_id = ? ` )
params = append ( params , query . OrgId )
if query . AnnotationId != 0 {
// fmt.Print("annotation query")
sql . WriteString ( ` AND a.id = ? ` )
params = append ( params , query . AnnotationId )
}
2016-08-30 02:32:56 -05:00
2022-03-22 06:20:57 -05:00
if query . AlertId != 0 {
sql . WriteString ( ` AND a.alert_id = ? ` )
params = append ( params , query . AlertId )
}
2017-12-20 17:52:21 -06:00
2022-03-22 06:20:57 -05:00
if query . DashboardId != 0 {
sql . WriteString ( ` AND a.dashboard_id = ? ` )
params = append ( params , query . DashboardId )
}
2016-09-09 04:30:55 -05:00
2022-03-22 06:20:57 -05:00
if query . PanelId != 0 {
sql . WriteString ( ` AND a.panel_id = ? ` )
params = append ( params , query . PanelId )
}
2016-09-09 04:30:55 -05:00
2022-03-22 06:20:57 -05:00
if query . UserId != 0 {
sql . WriteString ( ` AND a.user_id = ? ` )
params = append ( params , query . UserId )
}
2016-09-09 04:30:55 -05:00
2022-03-22 06:20:57 -05:00
if query . From > 0 && query . To > 0 {
sql . WriteString ( ` AND a.epoch <= ? AND a.epoch_end >= ? ` )
params = append ( params , query . To , query . From )
}
2018-03-22 09:52:09 -05:00
2022-03-22 06:20:57 -05:00
if query . Type == "alert" {
sql . WriteString ( ` AND a.alert_id > 0 ` )
} else if query . Type == "annotation" {
sql . WriteString ( ` AND a.alert_id = 0 ` )
}
2016-09-08 04:25:45 -05:00
2022-03-22 06:20:57 -05:00
if len ( query . Tags ) > 0 {
keyValueFilters := [ ] string { }
2017-11-21 04:27:53 -06:00
2022-09-21 07:04:01 -05:00
tags := tag . ParseTagPairs ( query . Tags )
2022-03-22 06:20:57 -05:00
for _ , tag := range tags {
if tag . Value == "" {
2022-09-19 02:54:37 -05:00
keyValueFilters = append ( keyValueFilters , "(tag." + r . db . GetDialect ( ) . Quote ( "key" ) + " = ?)" )
2022-03-22 06:20:57 -05:00
params = append ( params , tag . Key )
} else {
2022-09-19 02:54:37 -05:00
keyValueFilters = append ( keyValueFilters , "(tag." + r . db . GetDialect ( ) . Quote ( "key" ) + " = ? AND tag." + r . db . GetDialect ( ) . Quote ( "value" ) + " = ?)" )
2022-03-22 06:20:57 -05:00
params = append ( params , tag . Key , tag . Value )
}
2017-10-07 03:31:39 -05:00
}
2016-08-30 02:32:56 -05:00
2022-03-22 06:20:57 -05:00
if len ( tags ) > 0 {
tagsSubQuery := fmt . Sprintf ( `
SELECT SUM ( 1 ) FROM annotation_tag at
INNER JOIN tag on tag . id = at . tag_id
WHERE at . annotation_id = a . id
AND (
% s
)
` , strings . Join ( keyValueFilters , " OR " ) )
if query . MatchAny {
sql . WriteString ( fmt . Sprintf ( " AND (%s) > 0 " , tagsSubQuery ) )
} else {
sql . WriteString ( fmt . Sprintf ( " AND (%s) = %d " , tagsSubQuery , len ( tags ) ) )
}
2018-09-11 08:50:04 -05:00
}
2016-09-14 01:36:44 -05:00
}
2016-08-30 02:32:56 -05:00
2022-09-19 02:54:37 -05:00
if ! ac . IsDisabled ( r . cfg ) {
2022-04-11 07:18:38 -05:00
acFilter , acArgs , err := getAccessControlFilter ( query . SignedInUser )
if err != nil {
return err
}
sql . WriteString ( fmt . Sprintf ( " AND (%s)" , acFilter ) )
params = append ( params , acArgs ... )
}
2022-03-22 06:20:57 -05:00
if query . Limit == 0 {
query . Limit = 100
}
2016-08-30 02:32:56 -05:00
2022-03-22 06:20:57 -05:00
// order of ORDER BY arguments match the order of a sql index for performance
2022-09-19 02:54:37 -05:00
sql . WriteString ( " ORDER BY a.org_id, a.epoch_end DESC, a.epoch DESC" + r . db . GetDialect ( ) . Limit ( query . Limit ) + " ) dt on dt.id = annotation.id" )
2022-03-22 06:20:57 -05:00
if err := sess . SQL ( sql . String ( ) , params ... ) . Find ( & items ) ; err != nil {
items = nil
return err
}
return nil
} ,
)
2016-08-01 03:07:00 -05:00
2022-03-22 06:20:57 -05:00
return items , err
2016-08-01 03:07:00 -05:00
}
2016-10-14 02:33:16 -05:00
2022-08-10 04:56:48 -05:00
func getAccessControlFilter ( user * user . SignedInUser ) ( string , [ ] interface { } , error ) {
2022-08-11 06:28:55 -05:00
if user == nil || user . Permissions [ user . OrgID ] == nil {
2022-04-11 07:18:38 -05:00
return "" , nil , errors . New ( "missing permissions" )
}
2022-08-11 06:28:55 -05:00
scopes , has := user . Permissions [ user . OrgID ] [ ac . ActionAnnotationsRead ]
2022-04-11 07:18:38 -05:00
if ! has {
return "" , nil , errors . New ( "missing permissions" )
}
types , hasWildcardScope := ac . ParseScopes ( ac . ScopeAnnotationsProvider . GetResourceScopeType ( "" ) , scopes )
if hasWildcardScope {
types = map [ interface { } ] struct { } { annotations . Dashboard . String ( ) : { } , annotations . Organization . String ( ) : { } }
}
var filters [ ] string
var params [ ] interface { }
for t := range types {
// annotation read permission with scope annotations:type:organization allows listing annotations that are not associated with a dashboard
if t == annotations . Organization . String ( ) {
filters = append ( filters , "a.dashboard_id = 0" )
}
// annotation read permission with scope annotations:type:dashboard allows listing annotations from dashboards which the user can view
if t == annotations . Dashboard . String ( ) {
dashboardFilter , dashboardParams := permissions . NewAccessControlDashboardPermissionFilter ( user , models . PERMISSION_VIEW , searchstore . TypeDashboard ) . Where ( )
filter := fmt . Sprintf ( "a.dashboard_id IN(SELECT id FROM dashboard WHERE %s)" , dashboardFilter )
filters = append ( filters , filter )
params = dashboardParams
}
}
return strings . Join ( filters , " OR " ) , params , nil
}
2022-09-22 07:27:48 -05:00
func ( r * xormRepositoryImpl ) Delete ( ctx context . Context , params * annotations . DeleteParams ) error {
2022-10-19 08:02:15 -05:00
return r . db . WithTransactionalDbSession ( ctx , func ( sess * db . Session ) error {
2017-10-07 03:31:39 -05:00
var (
2021-12-21 12:04:56 -06:00
sql string
annoTagSQL string
2017-10-07 03:31:39 -05:00
)
2022-09-19 02:54:37 -05:00
r . log . Info ( "delete" , "orgId" , params . OrgId )
2019-08-16 03:49:30 -05:00
if params . Id != 0 {
2020-11-10 23:21:08 -06:00
annoTagSQL = "DELETE FROM annotation_tag WHERE annotation_id IN (SELECT id FROM annotation WHERE id = ? AND org_id = ?)"
2018-06-25 09:02:34 -05:00
sql = "DELETE FROM annotation WHERE id = ? AND org_id = ?"
2021-12-21 12:04:56 -06:00
if _ , err := sess . Exec ( annoTagSQL , params . Id , params . OrgId ) ; err != nil {
return err
}
if _ , err := sess . Exec ( sql , params . Id , params . OrgId ) ; err != nil {
return err
}
2017-10-07 03:31:39 -05:00
} else {
2020-11-10 23:21:08 -06:00
annoTagSQL = "DELETE FROM annotation_tag WHERE annotation_id IN (SELECT id FROM annotation WHERE dashboard_id = ? AND panel_id = ? AND org_id = ?)"
2018-06-25 09:02:34 -05:00
sql = "DELETE FROM annotation WHERE dashboard_id = ? AND panel_id = ? AND org_id = ?"
2019-03-04 09:57:29 -06:00
2021-12-21 12:04:56 -06:00
if _ , err := sess . Exec ( annoTagSQL , params . DashboardId , params . PanelId , params . OrgId ) ; err != nil {
return err
}
2019-03-04 09:57:29 -06:00
2021-12-21 12:04:56 -06:00
if _ , err := sess . Exec ( sql , params . DashboardId , params . PanelId , params . OrgId ) ; err != nil {
return err
}
2016-10-14 02:33:16 -05:00
}
return nil
} )
}
2021-06-30 06:42:54 -05:00
2022-09-22 07:27:48 -05:00
func ( r * xormRepositoryImpl ) GetTags ( ctx context . Context , query * annotations . TagsQuery ) ( annotations . FindTagsResult , error ) {
2022-03-25 12:23:09 -05:00
var items [ ] * annotations . Tag
2022-10-19 08:02:15 -05:00
err := r . db . WithDbSession ( ctx , func ( dbSession * db . Session ) error {
2022-03-25 12:23:09 -05:00
if query . Limit == 0 {
query . Limit = 100
}
2021-06-30 06:42:54 -05:00
2022-03-25 12:23:09 -05:00
var sql bytes . Buffer
params := make ( [ ] interface { } , 0 )
2022-09-19 02:54:37 -05:00
tagKey := ` tag. ` + r . db . GetDialect ( ) . Quote ( "key" )
tagValue := ` tag. ` + r . db . GetDialect ( ) . Quote ( "value" )
2021-06-30 06:42:54 -05:00
2022-03-25 12:23:09 -05:00
sql . WriteString ( `
2021-06-30 06:42:54 -05:00
SELECT
` + tagKey + ` ,
` + tagValue + ` ,
count ( * ) as count
FROM tag
INNER JOIN annotation_tag ON tag . id = annotation_tag . tag_id
` )
2022-03-25 12:23:09 -05:00
sql . WriteString ( ` WHERE EXISTS(SELECT 1 FROM annotation WHERE annotation.id = annotation_tag.annotation_id AND annotation.org_id = ?) ` )
params = append ( params , query . OrgID )
2021-06-30 06:42:54 -05:00
2022-09-19 02:54:37 -05:00
sql . WriteString ( ` AND ( ` + tagKey + ` ` + r . db . GetDialect ( ) . LikeStr ( ) + ` ? OR ` + tagValue + ` ` + r . db . GetDialect ( ) . LikeStr ( ) + ` ?) ` )
2022-03-25 12:23:09 -05:00
params = append ( params , ` % ` + query . Tag + ` % ` , ` % ` + query . Tag + ` % ` )
2021-06-30 06:42:54 -05:00
2022-03-25 12:23:09 -05:00
sql . WriteString ( ` GROUP BY ` + tagKey + ` , ` + tagValue )
sql . WriteString ( ` ORDER BY ` + tagKey + ` , ` + tagValue )
2022-09-19 02:54:37 -05:00
sql . WriteString ( ` ` + r . db . GetDialect ( ) . Limit ( query . Limit ) )
2021-06-30 06:42:54 -05:00
2022-03-25 12:23:09 -05:00
err := dbSession . SQL ( sql . String ( ) , params ... ) . Find ( & items )
return err
} )
if err != nil {
2021-06-30 06:42:54 -05:00
return annotations . FindTagsResult { Tags : [ ] * annotations . TagsDTO { } } , err
}
tags := make ( [ ] * annotations . TagsDTO , 0 )
for _ , item := range items {
tag := item . Key
if len ( item . Value ) > 0 {
tag = item . Key + ":" + item . Value
}
tags = append ( tags , & annotations . TagsDTO {
Tag : tag ,
Count : item . Count ,
} )
}
return annotations . FindTagsResult { Tags : tags } , nil
}
2022-09-22 07:27:48 -05:00
2022-09-23 05:04:41 -05:00
func ( r * xormRepositoryImpl ) validateItem ( item * annotations . Item ) error {
if err := validateTimeRange ( item ) ; err != nil {
return err
}
if err := r . validateTagsLength ( item ) ; err != nil {
return err
}
return nil
}
func ( r * xormRepositoryImpl ) validateTagsLength ( item * annotations . Item ) error {
estimatedTagsLength := 1 // leading: [
for i , t := range item . Tags {
if i == 0 {
estimatedTagsLength += len ( t ) + 2 // quotes
} else {
estimatedTagsLength += len ( t ) + 3 // leading comma and quotes
}
}
estimatedTagsLength += 1 // trailing: ]
if estimatedTagsLength > int ( r . maximumTagsLength ) {
return annotations . ErrBaseTagLimitExceeded . Errorf ( "tags length (%d) exceeds the maximum allowed (%d): modify the configuration to increase it" , estimatedTagsLength , r . maximumTagsLength )
}
return nil
}
2022-09-22 07:27:48 -05:00
func ( r * xormRepositoryImpl ) CleanAnnotations ( ctx context . Context , cfg setting . AnnotationCleanupSettings , annotationType string ) ( int64 , error ) {
var totalAffected int64
if cfg . MaxAge > 0 {
cutoffDate := time . Now ( ) . Add ( - cfg . MaxAge ) . UnixNano ( ) / int64 ( time . Millisecond )
deleteQuery := ` DELETE FROM annotation WHERE id IN (SELECT id FROM (SELECT id FROM annotation WHERE %s AND created < %v ORDER BY id DESC %s) a) `
sql := fmt . Sprintf ( deleteQuery , annotationType , cutoffDate , r . db . GetDialect ( ) . Limit ( r . cfg . AnnotationCleanupJobBatchSize ) )
affected , err := r . executeUntilDoneOrCancelled ( ctx , sql )
totalAffected += affected
if err != nil {
return totalAffected , err
}
}
if cfg . MaxCount > 0 {
deleteQuery := ` DELETE FROM annotation WHERE id IN (SELECT id FROM (SELECT id FROM annotation WHERE %s ORDER BY id DESC %s) a) `
sql := fmt . Sprintf ( deleteQuery , annotationType , r . db . GetDialect ( ) . LimitOffset ( r . cfg . AnnotationCleanupJobBatchSize , cfg . MaxCount ) )
affected , err := r . executeUntilDoneOrCancelled ( ctx , sql )
totalAffected += affected
return totalAffected , err
}
return totalAffected , nil
}
func ( r * xormRepositoryImpl ) CleanOrphanedAnnotationTags ( ctx context . Context ) ( int64 , error ) {
deleteQuery := ` DELETE FROM annotation_tag WHERE id IN ( SELECT id FROM (SELECT id FROM annotation_tag WHERE NOT EXISTS (SELECT 1 FROM annotation a WHERE annotation_id = a.id) %s) a) `
sql := fmt . Sprintf ( deleteQuery , r . db . GetDialect ( ) . Limit ( r . cfg . AnnotationCleanupJobBatchSize ) )
return r . executeUntilDoneOrCancelled ( ctx , sql )
}
func ( r * xormRepositoryImpl ) executeUntilDoneOrCancelled ( ctx context . Context , sql string ) ( int64 , error ) {
var totalAffected int64
for {
select {
case <- ctx . Done ( ) :
return totalAffected , ctx . Err ( )
default :
var affected int64
2022-10-19 08:02:15 -05:00
err := r . db . WithDbSession ( ctx , func ( session * db . Session ) error {
2022-09-22 07:27:48 -05:00
res , err := session . Exec ( sql )
if err != nil {
return err
}
affected , err = res . RowsAffected ( )
totalAffected += affected
return err
} )
if err != nil {
return totalAffected , err
}
if affected == 0 {
return totalAffected , nil
}
}
}
}