2022-09-19 02:54:37 -05:00
|
|
|
package annotationsimpl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2023-11-14 17:11:01 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/annotations/accesscontrol"
|
2023-12-12 16:43:09 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/annotations/annotationsimpl/loki"
|
2023-11-14 17:11:01 -06: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"
|
|
|
|
"github.com/grafana/grafana/pkg/services/annotations"
|
2023-04-06 03:16:15 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
2022-09-21 07:04:01 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/tag"
|
2022-09-19 02:54:37 -05:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
|
|
|
)
|
|
|
|
|
|
|
|
type RepositoryImpl struct {
|
2023-11-14 17:11:01 -06:00
|
|
|
db db.DB
|
|
|
|
authZ *accesscontrol.AuthService
|
|
|
|
features featuremgmt.FeatureToggles
|
2023-12-12 16:43:09 -06:00
|
|
|
reader readStore
|
|
|
|
writer writeStore
|
2022-09-19 02:54:37 -05:00
|
|
|
}
|
|
|
|
|
2023-11-14 17:11:01 -06:00
|
|
|
func ProvideService(
|
|
|
|
db db.DB,
|
|
|
|
cfg *setting.Cfg,
|
|
|
|
features featuremgmt.FeatureToggles,
|
|
|
|
tagService tag.Service,
|
|
|
|
) *RepositoryImpl {
|
|
|
|
l := log.New("annotations")
|
2023-12-12 16:43:09 -06:00
|
|
|
l.Debug("Initializing annotations service")
|
|
|
|
|
|
|
|
xormStore := NewXormStore(cfg, log.New("annotations.sql"), db, tagService)
|
|
|
|
write := xormStore
|
|
|
|
|
|
|
|
var read readStore
|
|
|
|
historianStore := loki.NewLokiHistorianStore(cfg.UnifiedAlerting.StateHistory, features, db, log.New("annotations.loki"))
|
|
|
|
if historianStore != nil {
|
|
|
|
l.Debug("Using composite read store")
|
|
|
|
read = NewCompositeStore(xormStore, historianStore)
|
|
|
|
} else {
|
|
|
|
l.Debug("Using xorm read store")
|
|
|
|
read = write
|
|
|
|
}
|
2023-11-14 17:11:01 -06:00
|
|
|
|
2022-09-19 02:54:37 -05:00
|
|
|
return &RepositoryImpl{
|
2023-11-14 17:11:01 -06:00
|
|
|
db: db,
|
|
|
|
features: features,
|
|
|
|
authZ: accesscontrol.NewAuthService(db, features),
|
2023-12-12 16:43:09 -06:00
|
|
|
reader: read,
|
|
|
|
writer: write,
|
2022-09-19 02:54:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RepositoryImpl) Save(ctx context.Context, item *annotations.Item) error {
|
2023-12-12 16:43:09 -06:00
|
|
|
return r.writer.Add(ctx, item)
|
2022-09-19 02:54:37 -05:00
|
|
|
}
|
|
|
|
|
2022-11-04 10:39:26 -05:00
|
|
|
// SaveMany inserts multiple annotations at once.
|
|
|
|
// It does not return IDs associated with created annotations. If you need this functionality, use the single-item Save instead.
|
|
|
|
func (r *RepositoryImpl) SaveMany(ctx context.Context, items []annotations.Item) error {
|
2023-12-12 16:43:09 -06:00
|
|
|
return r.writer.AddMany(ctx, items)
|
2022-11-04 10:39:26 -05:00
|
|
|
}
|
|
|
|
|
2022-09-19 02:54:37 -05:00
|
|
|
func (r *RepositoryImpl) Update(ctx context.Context, item *annotations.Item) error {
|
2023-12-12 16:43:09 -06:00
|
|
|
return r.writer.Update(ctx, item)
|
2022-09-19 02:54:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RepositoryImpl) Find(ctx context.Context, query *annotations.ItemQuery) ([]*annotations.ItemDTO, error) {
|
2023-11-14 17:11:01 -06:00
|
|
|
resources, err := r.authZ.Authorize(ctx, query.OrgID, query.SignedInUser)
|
|
|
|
if err != nil {
|
|
|
|
return make([]*annotations.ItemDTO, 0), err
|
|
|
|
}
|
|
|
|
|
2023-12-12 16:43:09 -06:00
|
|
|
return r.reader.Get(ctx, query, resources)
|
2022-09-19 02:54:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RepositoryImpl) Delete(ctx context.Context, params *annotations.DeleteParams) error {
|
2023-12-12 16:43:09 -06:00
|
|
|
return r.writer.Delete(ctx, params)
|
2022-09-19 02:54:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RepositoryImpl) FindTags(ctx context.Context, query *annotations.TagsQuery) (annotations.FindTagsResult, error) {
|
2023-12-12 16:43:09 -06:00
|
|
|
return r.reader.GetTags(ctx, query)
|
2022-09-19 02:54:37 -05:00
|
|
|
}
|