mirror of https://github.com/grafana/grafana
Query history: Create API to delete query from query history (#44653)
* Query history: Add delete and refactor * Update docs/sources/http_api/query_history.md Co-authored-by: Piotr Jamróz <pm.jamroz@gmail.com> Co-authored-by: Piotr Jamróz <pm.jamroz@gmail.com>pull/44921/head
parent
1680e284e5
commit
0f362f8dfc
@ -1,21 +1,48 @@ |
|||||||
package queryhistory |
package queryhistory |
||||||
|
|
||||||
import ( |
import ( |
||||||
|
"errors" |
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/components/simplejson" |
"github.com/grafana/grafana/pkg/components/simplejson" |
||||||
) |
) |
||||||
|
|
||||||
|
var ( |
||||||
|
ErrQueryNotFound = errors.New("query in query history not found") |
||||||
|
) |
||||||
|
|
||||||
type QueryHistory struct { |
type QueryHistory struct { |
||||||
Id int64 `json:"id"` |
ID int64 `xorm:"pk autoincr 'id'"` |
||||||
Uid string `json:"uid"` |
UID string `xorm:"uid"` |
||||||
DatasourceUid string `json:"datasourceUid"` |
DatasourceUID string `xorm:"datasource_uid"` |
||||||
OrgId int64 `json:"orgId"` |
OrgID int64 `xorm:"org_id"` |
||||||
|
CreatedBy int64 |
||||||
|
CreatedAt int64 |
||||||
|
Comment string |
||||||
|
Queries *simplejson.Json |
||||||
|
} |
||||||
|
|
||||||
|
type CreateQueryInQueryHistoryCommand struct { |
||||||
|
DatasourceUID string `json:"datasourceUid"` |
||||||
|
Queries *simplejson.Json `json:"queries"` |
||||||
|
} |
||||||
|
|
||||||
|
type QueryHistoryDTO struct { |
||||||
|
UID string `json:"uid"` |
||||||
|
DatasourceUID string `json:"datasourceUid"` |
||||||
CreatedBy int64 `json:"createdBy"` |
CreatedBy int64 `json:"createdBy"` |
||||||
CreatedAt int64 `json:"createdAt"` |
CreatedAt int64 `json:"createdAt"` |
||||||
Comment string `json:"comment"` |
Comment string `json:"comment"` |
||||||
Queries *simplejson.Json `json:"queries"` |
Queries *simplejson.Json `json:"queries"` |
||||||
|
Starred bool `json:"starred"` |
||||||
} |
} |
||||||
|
|
||||||
type CreateQueryInQueryHistoryCommand struct { |
// QueryHistoryResponse is a response struct for QueryHistoryDTO
|
||||||
DatasourceUid string `json:"datasourceUid"` |
type QueryHistoryResponse struct { |
||||||
Queries *simplejson.Json `json:"queries"` |
Result QueryHistoryDTO `json:"result"` |
||||||
|
} |
||||||
|
|
||||||
|
// DeleteQueryFromQueryHistoryResponse is the response struct for deleting a query from query history
|
||||||
|
type DeleteQueryFromQueryHistoryResponse struct { |
||||||
|
ID int64 `json:"id"` |
||||||
|
Message string `json:"message"` |
||||||
} |
} |
||||||
|
@ -0,0 +1,23 @@ |
|||||||
|
package queryhistory |
||||||
|
|
||||||
|
import ( |
||||||
|
"testing" |
||||||
|
|
||||||
|
"github.com/grafana/grafana/pkg/web" |
||||||
|
"github.com/stretchr/testify/require" |
||||||
|
) |
||||||
|
|
||||||
|
func TestDeleteQueryFromQueryHistory(t *testing.T) { |
||||||
|
testScenarioWithQueryInQueryHistory(t, "When users tries to delete query in query history that does not exist, it should fail", |
||||||
|
func(t *testing.T, sc scenarioContext) { |
||||||
|
resp := sc.service.deleteHandler(sc.reqContext) |
||||||
|
require.Equal(t, 404, resp.Status()) |
||||||
|
}) |
||||||
|
|
||||||
|
testScenarioWithQueryInQueryHistory(t, "When users tries to delete query in query history that exists, it should succeed", |
||||||
|
func(t *testing.T, sc scenarioContext) { |
||||||
|
sc.ctx.Req = web.SetURLParams(sc.ctx.Req, map[string]string{":uid": sc.initialResult.Result.UID}) |
||||||
|
resp := sc.service.deleteHandler(sc.reqContext) |
||||||
|
require.Equal(t, 200, resp.Status()) |
||||||
|
}) |
||||||
|
} |
Loading…
Reference in new issue