grafana/pkg/services/queryhistory/writers.go
Ivana Huckova b92fe0e0f5
Query history: Add from and to parameters (#48212)
* Query history: Add from and to parameters

* Update docs/sources/http_api/query_history.md

Co-authored-by: achatterjee-grafana <70489351+achatterjee-grafana@users.noreply.github.com>

* Update docs/sources/http_api/query_history.md

Co-authored-by: achatterjee-grafana <70489351+achatterjee-grafana@users.noreply.github.com>

* Update docs/sources/http_api/query_history.md

Co-authored-by: achatterjee-grafana <70489351+achatterjee-grafana@users.noreply.github.com>

* Implement Grafana relative time range

Co-authored-by: achatterjee-grafana <70489351+achatterjee-grafana@users.noreply.github.com>
2022-04-29 09:55:33 +02:00

54 lines
2.2 KiB
Go

package queryhistory
import (
"bytes"
"strings"
"github.com/grafana/grafana/pkg/models"
"github.com/grafana/grafana/pkg/services/sqlstore"
)
func writeStarredSQL(query SearchInQueryHistoryQuery, sqlStore *sqlstore.SQLStore, builder *sqlstore.SQLBuilder) {
if query.OnlyStarred {
builder.Write(sqlStore.Dialect.BooleanStr(true) + ` AS starred
FROM query_history
INNER JOIN query_history_star ON query_history_star.query_uid = query_history.uid
`)
} else {
builder.Write(` CASE WHEN query_history_star.query_uid IS NULL THEN ` + sqlStore.Dialect.BooleanStr(false) + ` ELSE ` + sqlStore.Dialect.BooleanStr(true) + ` END AS starred
FROM query_history
LEFT JOIN query_history_star ON query_history_star.query_uid = query_history.uid
`)
}
}
func writeFiltersSQL(query SearchInQueryHistoryQuery, user *models.SignedInUser, sqlStore *sqlstore.SQLStore, builder *sqlstore.SQLBuilder) {
params := []interface{}{user.OrgId, user.UserId, query.From, query.To, "%" + query.SearchString + "%", "%" + query.SearchString + "%"}
var sql bytes.Buffer
sql.WriteString(" WHERE query_history.org_id = ? AND query_history.created_by = ? AND query_history.created_at >= ? AND query_history.created_at <= ? AND (query_history.queries " + sqlStore.Dialect.LikeStr() + " ? OR query_history.comment " + sqlStore.Dialect.LikeStr() + " ?) ")
if len(query.DatasourceUIDs) > 0 {
for _, uid := range query.DatasourceUIDs {
params = append(params, uid)
}
sql.WriteString(" AND query_history.datasource_uid IN (? " + strings.Repeat(",?", len(query.DatasourceUIDs)-1) + ") ")
}
builder.Write(sql.String(), params...)
}
func writeSortSQL(query SearchInQueryHistoryQuery, sqlStore *sqlstore.SQLStore, builder *sqlstore.SQLBuilder) {
if query.Sort == "time-asc" {
builder.Write(" ORDER BY created_at ASC ")
} else {
builder.Write(" ORDER BY created_at DESC ")
}
}
func writeLimitSQL(query SearchInQueryHistoryQuery, sqlStore *sqlstore.SQLStore, builder *sqlstore.SQLBuilder) {
builder.Write(" LIMIT ? ", query.Limit)
}
func writeOffsetSQL(query SearchInQueryHistoryQuery, sqlStore *sqlstore.SQLStore, builder *sqlstore.SQLBuilder) {
builder.Write(" OFFSET ? ", query.Limit*(query.Page-1))
}