2022-01-28 10:55:09 -06:00
|
|
|
package queryhistory
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/api/routing"
|
2022-10-19 08:02:15 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/db"
|
2022-01-28 10:55:09 -06:00
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
2022-08-10 04:56:48 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/user"
|
2022-01-28 10:55:09 -06:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
|
|
|
)
|
|
|
|
|
2022-10-14 14:33:06 -05:00
|
|
|
func ProvideService(cfg *setting.Cfg, sqlStore db.DB, routeRegister routing.RouteRegister) *QueryHistoryService {
|
2022-01-28 10:55:09 -06:00
|
|
|
s := &QueryHistoryService{
|
2022-10-19 08:02:15 -05:00
|
|
|
store: sqlStore,
|
2022-01-28 10:55:09 -06:00
|
|
|
Cfg: cfg,
|
|
|
|
RouteRegister: routeRegister,
|
|
|
|
log: log.New("query-history"),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register routes only when query history is enabled
|
|
|
|
if s.Cfg.QueryHistoryEnabled {
|
|
|
|
s.registerAPIEndpoints()
|
|
|
|
}
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
type Service interface {
|
2022-08-10 04:56:48 -05:00
|
|
|
CreateQueryInQueryHistory(ctx context.Context, user *user.SignedInUser, cmd CreateQueryInQueryHistoryCommand) (QueryHistoryDTO, error)
|
|
|
|
SearchInQueryHistory(ctx context.Context, user *user.SignedInUser, query SearchInQueryHistoryQuery) (QueryHistorySearchResult, error)
|
|
|
|
DeleteQueryFromQueryHistory(ctx context.Context, user *user.SignedInUser, UID string) (int64, error)
|
|
|
|
PatchQueryCommentInQueryHistory(ctx context.Context, user *user.SignedInUser, UID string, cmd PatchQueryCommentInQueryHistoryCommand) (QueryHistoryDTO, error)
|
|
|
|
StarQueryInQueryHistory(ctx context.Context, user *user.SignedInUser, UID string) (QueryHistoryDTO, error)
|
|
|
|
UnstarQueryInQueryHistory(ctx context.Context, user *user.SignedInUser, UID string) (QueryHistoryDTO, error)
|
|
|
|
MigrateQueriesToQueryHistory(ctx context.Context, user *user.SignedInUser, cmd MigrateQueriesToQueryHistoryCommand) (int, int, error)
|
2022-05-03 07:49:58 -05:00
|
|
|
DeleteStaleQueriesInQueryHistory(ctx context.Context, olderThan int64) (int, error)
|
|
|
|
EnforceRowLimitInQueryHistory(ctx context.Context, limit int, starredQueries bool) (int, error)
|
2022-01-28 10:55:09 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type QueryHistoryService struct {
|
2022-10-19 08:02:15 -05:00
|
|
|
store db.DB
|
2022-01-28 10:55:09 -06:00
|
|
|
Cfg *setting.Cfg
|
|
|
|
RouteRegister routing.RouteRegister
|
|
|
|
log log.Logger
|
|
|
|
}
|
|
|
|
|
2022-08-10 04:56:48 -05:00
|
|
|
func (s QueryHistoryService) CreateQueryInQueryHistory(ctx context.Context, user *user.SignedInUser, cmd CreateQueryInQueryHistoryCommand) (QueryHistoryDTO, error) {
|
2022-01-28 10:55:09 -06:00
|
|
|
return s.createQuery(ctx, user, cmd)
|
|
|
|
}
|
2022-02-04 09:14:36 -06:00
|
|
|
|
2022-08-10 04:56:48 -05:00
|
|
|
func (s QueryHistoryService) SearchInQueryHistory(ctx context.Context, user *user.SignedInUser, query SearchInQueryHistoryQuery) (QueryHistorySearchResult, error) {
|
2022-03-07 05:28:04 -06:00
|
|
|
return s.searchQueries(ctx, user, query)
|
|
|
|
}
|
|
|
|
|
2022-08-10 04:56:48 -05:00
|
|
|
func (s QueryHistoryService) DeleteQueryFromQueryHistory(ctx context.Context, user *user.SignedInUser, UID string) (int64, error) {
|
2022-02-04 09:14:36 -06:00
|
|
|
return s.deleteQuery(ctx, user, UID)
|
|
|
|
}
|
2022-02-15 08:43:17 -06:00
|
|
|
|
2022-08-10 04:56:48 -05:00
|
|
|
func (s QueryHistoryService) PatchQueryCommentInQueryHistory(ctx context.Context, user *user.SignedInUser, UID string, cmd PatchQueryCommentInQueryHistoryCommand) (QueryHistoryDTO, error) {
|
2022-02-15 08:43:17 -06:00
|
|
|
return s.patchQueryComment(ctx, user, UID, cmd)
|
|
|
|
}
|
2022-02-23 10:03:04 -06:00
|
|
|
|
2022-08-10 04:56:48 -05:00
|
|
|
func (s QueryHistoryService) StarQueryInQueryHistory(ctx context.Context, user *user.SignedInUser, UID string) (QueryHistoryDTO, error) {
|
2022-02-23 10:03:04 -06:00
|
|
|
return s.starQuery(ctx, user, UID)
|
|
|
|
}
|
|
|
|
|
2022-08-10 04:56:48 -05:00
|
|
|
func (s QueryHistoryService) UnstarQueryInQueryHistory(ctx context.Context, user *user.SignedInUser, UID string) (QueryHistoryDTO, error) {
|
2022-02-23 10:03:04 -06:00
|
|
|
return s.unstarQuery(ctx, user, UID)
|
|
|
|
}
|
2022-04-14 02:33:41 -05:00
|
|
|
|
2022-08-10 04:56:48 -05:00
|
|
|
func (s QueryHistoryService) MigrateQueriesToQueryHistory(ctx context.Context, user *user.SignedInUser, cmd MigrateQueriesToQueryHistoryCommand) (int, int, error) {
|
2022-04-14 02:33:41 -05:00
|
|
|
return s.migrateQueries(ctx, user, cmd)
|
|
|
|
}
|
2022-05-03 07:49:58 -05:00
|
|
|
|
|
|
|
func (s QueryHistoryService) DeleteStaleQueriesInQueryHistory(ctx context.Context, olderThan int64) (int, error) {
|
|
|
|
return s.deleteStaleQueries(ctx, olderThan)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s QueryHistoryService) EnforceRowLimitInQueryHistory(ctx context.Context, limit int, starredQueries bool) (int, error) {
|
|
|
|
return s.enforceQueryHistoryRowLimit(ctx, limit, starredQueries)
|
|
|
|
}
|