2022-01-29 00:55:09 +08:00
|
|
|
package queryhistory
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-04-17 18:02:47 +08:00
|
|
|
"time"
|
2022-01-29 00:55:09 +08:00
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/api/routing"
|
2022-10-19 21:02:15 +08:00
|
|
|
"github.com/grafana/grafana/pkg/infra/db"
|
2022-01-29 00:55:09 +08:00
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
2024-08-01 02:10:52 +08:00
|
|
|
ac "github.com/grafana/grafana/pkg/services/accesscontrol"
|
2025-10-08 04:28:34 +08:00
|
|
|
"github.com/grafana/grafana/pkg/services/apiserver"
|
|
|
|
"github.com/grafana/grafana/pkg/services/apiserver/endpoints/request"
|
|
|
|
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
2022-08-10 17:56:48 +08:00
|
|
|
"github.com/grafana/grafana/pkg/services/user"
|
2022-01-29 00:55:09 +08:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
|
|
|
)
|
|
|
|
|
2025-10-08 04:28:34 +08:00
|
|
|
func ProvideService(cfg *setting.Cfg,
|
|
|
|
sqlStore db.DB,
|
|
|
|
routeRegister routing.RouteRegister,
|
|
|
|
accessControl ac.AccessControl,
|
|
|
|
features featuremgmt.FeatureToggles,
|
|
|
|
configProvider apiserver.DirectRestConfigProvider,
|
|
|
|
) *QueryHistoryService {
|
2022-01-29 00:55:09 +08:00
|
|
|
s := &QueryHistoryService{
|
2022-10-19 21:02:15 +08:00
|
|
|
store: sqlStore,
|
2022-01-29 00:55:09 +08:00
|
|
|
Cfg: cfg,
|
|
|
|
RouteRegister: routeRegister,
|
|
|
|
log: log.New("query-history"),
|
2023-04-17 18:02:47 +08:00
|
|
|
now: time.Now,
|
2024-08-01 02:10:52 +08:00
|
|
|
accessControl: accessControl,
|
2022-01-29 00:55:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Register routes only when query history is enabled
|
|
|
|
if s.Cfg.QueryHistoryEnabled {
|
2025-10-08 04:28:34 +08:00
|
|
|
if features.IsEnabledGlobally(featuremgmt.FlagKubernetesStars) {
|
|
|
|
s.k8sClients = &k8sClients{
|
|
|
|
namespacer: request.GetNamespaceMapper(s.Cfg),
|
|
|
|
configProvider: configProvider,
|
|
|
|
}
|
|
|
|
}
|
2022-01-29 00:55:09 +08:00
|
|
|
s.registerAPIEndpoints()
|
|
|
|
}
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
type Service interface {
|
2022-08-10 17:56:48 +08: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)
|
2022-05-03 20:49:58 +08:00
|
|
|
DeleteStaleQueriesInQueryHistory(ctx context.Context, olderThan int64) (int, error)
|
|
|
|
EnforceRowLimitInQueryHistory(ctx context.Context, limit int, starredQueries bool) (int, error)
|
2022-01-29 00:55:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type QueryHistoryService struct {
|
2022-10-19 21:02:15 +08:00
|
|
|
store db.DB
|
2022-01-29 00:55:09 +08:00
|
|
|
Cfg *setting.Cfg
|
|
|
|
RouteRegister routing.RouteRegister
|
|
|
|
log log.Logger
|
2023-04-17 18:02:47 +08:00
|
|
|
now func() time.Time
|
2024-08-01 02:10:52 +08:00
|
|
|
accessControl ac.AccessControl
|
2025-10-08 04:28:34 +08:00
|
|
|
k8sClients *k8sClients
|
2022-01-29 00:55:09 +08:00
|
|
|
}
|
|
|
|
|
2022-08-10 17:56:48 +08:00
|
|
|
func (s QueryHistoryService) CreateQueryInQueryHistory(ctx context.Context, user *user.SignedInUser, cmd CreateQueryInQueryHistoryCommand) (QueryHistoryDTO, error) {
|
2022-01-29 00:55:09 +08:00
|
|
|
return s.createQuery(ctx, user, cmd)
|
|
|
|
}
|
2022-02-04 23:14:36 +08:00
|
|
|
|
2022-08-10 17:56:48 +08:00
|
|
|
func (s QueryHistoryService) SearchInQueryHistory(ctx context.Context, user *user.SignedInUser, query SearchInQueryHistoryQuery) (QueryHistorySearchResult, error) {
|
2022-03-07 19:28:04 +08:00
|
|
|
return s.searchQueries(ctx, user, query)
|
|
|
|
}
|
|
|
|
|
2022-08-10 17:56:48 +08:00
|
|
|
func (s QueryHistoryService) DeleteQueryFromQueryHistory(ctx context.Context, user *user.SignedInUser, UID string) (int64, error) {
|
2022-02-04 23:14:36 +08:00
|
|
|
return s.deleteQuery(ctx, user, UID)
|
|
|
|
}
|
2022-02-15 22:43:17 +08:00
|
|
|
|
2022-08-10 17:56:48 +08:00
|
|
|
func (s QueryHistoryService) PatchQueryCommentInQueryHistory(ctx context.Context, user *user.SignedInUser, UID string, cmd PatchQueryCommentInQueryHistoryCommand) (QueryHistoryDTO, error) {
|
2022-02-15 22:43:17 +08:00
|
|
|
return s.patchQueryComment(ctx, user, UID, cmd)
|
|
|
|
}
|
2022-02-24 00:03:04 +08:00
|
|
|
|
2022-08-10 17:56:48 +08:00
|
|
|
func (s QueryHistoryService) StarQueryInQueryHistory(ctx context.Context, user *user.SignedInUser, UID string) (QueryHistoryDTO, error) {
|
2022-02-24 00:03:04 +08:00
|
|
|
return s.starQuery(ctx, user, UID)
|
|
|
|
}
|
|
|
|
|
2022-08-10 17:56:48 +08:00
|
|
|
func (s QueryHistoryService) UnstarQueryInQueryHistory(ctx context.Context, user *user.SignedInUser, UID string) (QueryHistoryDTO, error) {
|
2022-02-24 00:03:04 +08:00
|
|
|
return s.unstarQuery(ctx, user, UID)
|
|
|
|
}
|
2022-04-14 15:33:41 +08:00
|
|
|
|
2022-05-03 20:49:58 +08: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)
|
|
|
|
}
|