Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
小 白蛋
SigNoz
Commits
91f44ef2
Commit
91f44ef2
authored
3 years ago
by
Ankit Nayan
Browse files
Options
Download
Email Patches
Plain Diff
feat: CRUD APIs for rules working
parent
e120e44b
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
pkg/query-service/app/clickhouseReader/reader.go
+28
-23
pkg/query-service/app/clickhouseReader/reader.go
pkg/query-service/app/druidReader/reader.go
+3
-3
pkg/query-service/app/druidReader/reader.go
pkg/query-service/app/http_handler.go
+17
-24
pkg/query-service/app/http_handler.go
pkg/query-service/app/interface.go
+1
-1
pkg/query-service/app/interface.go
pkg/query-service/model/response.go
+1
-1
pkg/query-service/model/response.go
with
50 additions
and
52 deletions
+50
-52
pkg/query-service/app/clickhouseReader/reader.go
+
28
-
23
View file @
91f44ef2
...
...
@@ -466,6 +466,26 @@ type AlertingRuleWithGroup struct {
Id
int
}
func
(
r
*
ClickHouseReader
)
GetRule
(
localDB
*
sqlx
.
DB
,
id
string
)
(
*
model
.
RuleResponseItem
,
*
model
.
ApiError
)
{
idInt
,
_
:=
strconv
.
Atoi
(
id
)
rule
:=
&
model
.
RuleResponseItem
{}
query
:=
fmt
.
Sprintf
(
"SELECT id, updated_at, data FROM rules WHERE id=%d"
,
idInt
)
err
:=
localDB
.
Get
(
rule
,
query
)
zap
.
S
()
.
Info
(
query
)
if
err
!=
nil
{
zap
.
S
()
.
Debug
(
"Error in processing sql query: "
,
err
)
return
nil
,
&
model
.
ApiError
{
Typ
:
model
.
ErrorInternal
,
Err
:
err
}
}
return
rule
,
nil
}
func
(
r
*
ClickHouseReader
)
ListRulesFromProm
(
localDB
*
sqlx
.
DB
)
(
*
model
.
AlertDiscovery
,
*
model
.
ApiError
)
{
groups
:=
r
.
ruleManager
.
RuleGroups
()
...
...
@@ -520,24 +540,6 @@ func (r *ClickHouseReader) ListRulesFromProm(localDB *sqlx.DB) (*model.AlertDisc
return
res
,
nil
}
func
(
r
*
ClickHouseReader
)
GetRules
(
localDB
*
sqlx
.
DB
)
(
*
model
.
RuleGroups
,
*
model
.
ApiError
)
{
rules
:=
[]
*
model
.
RuleGroups
{}
err
:=
localDB
.
Select
(
&
rules
,
"SELECT id, updated_at, data FROM rules"
)
if
err
!=
nil
{
return
nil
,
&
model
.
ApiError
{
Typ
:
model
.
ErrorInternal
,
Err
:
err
}
}
if
len
(
rules
)
>
1
{
return
nil
,
&
model
.
ApiError
{
Typ
:
model
.
ErrorInternal
,
Err
:
fmt
.
Errorf
(
"multiple rule entry detected from db"
)}
}
if
len
(
rules
)
==
0
{
return
nil
,
nil
}
return
rules
[
0
],
nil
}
func
(
r
*
ClickHouseReader
)
CreateRule
(
localDB
*
sqlx
.
DB
,
rule
string
)
*
model
.
ApiError
{
dbQuery
:=
fmt
.
Sprintf
(
"INSERT into rules (updated_at, data) VALUES ('%s', '%s')"
,
time
.
Now
(),
rule
)
...
...
@@ -551,7 +553,9 @@ func (r *ClickHouseReader) CreateRule(localDB *sqlx.DB, rule string) *model.ApiE
id
,
_
:=
res
.
LastInsertId
()
groupName
:=
fmt
.
Sprintf
(
"%d-groupname"
,
id
)
err
=
r
.
ruleManager
.
UpdateGroupWithAction
(
time
.
Duration
(
r
.
promConfig
.
GlobalConfig
.
EvaluationInterval
),
rule
,
groupName
,
"add"
)
// err = r.ruleManager.UpdateGroupWithAction(time.Duration(r.promConfig.GlobalConfig.EvaluationInterval), rule, groupName, "add")
err
=
r
.
ruleManager
.
AddGroup
(
time
.
Duration
(
r
.
promConfig
.
GlobalConfig
.
EvaluationInterval
),
rule
,
groupName
)
if
err
!=
nil
{
return
&
model
.
ApiError
{
Typ
:
model
.
ErrorInternal
,
Err
:
err
}
...
...
@@ -563,7 +567,7 @@ func (r *ClickHouseReader) CreateRule(localDB *sqlx.DB, rule string) *model.ApiE
func
(
r
*
ClickHouseReader
)
EditRule
(
localDB
*
sqlx
.
DB
,
rule
string
,
id
string
)
*
model
.
ApiError
{
idInt
,
_
:=
strconv
.
Atoi
(
id
)
dbQuery
:=
fmt
.
Sprintf
(
"Update
into
rules updated_at='%s', data='%s' WHERE id=%d;"
,
time
.
Now
(),
rule
,
idInt
)
dbQuery
:=
fmt
.
Sprintf
(
"Update rules
SET
updated_at='%s', data='%s' WHERE id=%d;"
,
time
.
Now
(),
rule
,
idInt
)
_
,
err
:=
localDB
.
Exec
(
dbQuery
)
...
...
@@ -573,7 +577,7 @@ func (r *ClickHouseReader) EditRule(localDB *sqlx.DB, rule string, id string) *m
groupName
:=
fmt
.
Sprintf
(
"%d-groupname"
,
idInt
)
err
=
r
.
ruleManager
.
UpdateGroupWithAction
(
time
.
Duration
(
r
.
promConfig
.
GlobalConfig
.
EvaluationInterval
),
rule
,
groupName
,
"add"
)
err
=
r
.
ruleManager
.
EditGroup
(
time
.
Duration
(
r
.
promConfig
.
GlobalConfig
.
EvaluationInterval
),
rule
,
groupName
)
if
err
!=
nil
{
return
&
model
.
ApiError
{
Typ
:
model
.
ErrorInternal
,
Err
:
err
}
...
...
@@ -585,7 +589,7 @@ func (r *ClickHouseReader) EditRule(localDB *sqlx.DB, rule string, id string) *m
func
(
r
*
ClickHouseReader
)
DeleteRule
(
localDB
*
sqlx
.
DB
,
id
string
)
*
model
.
ApiError
{
idInt
,
_
:=
strconv
.
Atoi
(
id
)
dbQuery
:=
fmt
.
Sprintf
(
"
UPDATE INTO rules updated_at='%s', deleted=%d WHERE id=%d;"
,
time
.
Now
(),
1
,
idInt
)
dbQuery
:=
fmt
.
Sprintf
(
"
DELETE FROM rules WHERE id=%d;"
,
idInt
)
_
,
err
:=
localDB
.
Exec
(
dbQuery
)
...
...
@@ -596,7 +600,8 @@ func (r *ClickHouseReader) DeleteRule(localDB *sqlx.DB, id string) *model.ApiErr
groupName
:=
fmt
.
Sprintf
(
"%d-groupname"
,
idInt
)
rule
:=
""
// dummy rule to pass to function
err
=
r
.
ruleManager
.
UpdateGroupWithAction
(
time
.
Duration
(
r
.
promConfig
.
GlobalConfig
.
EvaluationInterval
),
rule
,
groupName
,
"delete"
)
// err = r.ruleManager.UpdateGroupWithAction(time.Duration(r.promConfig.GlobalConfig.EvaluationInterval), rule, groupName, "delete")
err
=
r
.
ruleManager
.
DeleteGroup
(
time
.
Duration
(
r
.
promConfig
.
GlobalConfig
.
EvaluationInterval
),
rule
,
groupName
)
if
err
!=
nil
{
return
&
model
.
ApiError
{
Typ
:
model
.
ErrorInternal
,
Err
:
err
}
...
...
This diff is collapsed.
Click to expand it.
pkg/query-service/app/druidReader/reader.go
+
3
-
3
View file @
91f44ef2
...
...
@@ -56,11 +56,11 @@ func (druid *DruidReader) GetInstantQueryMetricsResult(ctx context.Context, quer
func
(
druid
*
DruidReader
)
ListRulesFromProm
(
localDB
*
sqlx
.
DB
)
(
*
model
.
AlertDiscovery
,
*
model
.
ApiError
)
{
res
:=
model
.
AlertDiscovery
{}
return
&
res
,
nil
return
&
res
,
&
model
.
ApiError
{
model
.
ErrorNotImplemented
,
fmt
.
Errorf
(
"Druid does not support getting rules for alerting"
)}
}
func
(
druid
*
DruidReader
)
GetRule
s
(
localDB
*
sqlx
.
DB
)
(
*
model
.
Rule
Groups
,
*
model
.
ApiError
)
{
func
(
druid
*
DruidReader
)
GetRule
(
localDB
*
sqlx
.
DB
,
id
string
)
(
*
model
.
Rule
ResponseItem
,
*
model
.
ApiError
)
{
return
nil
,
&
model
.
ApiError
{
model
.
ErrorNotImplemented
,
fmt
.
Errorf
(
"Druid does not support
s
etting rules for alerting"
)}
return
nil
,
&
model
.
ApiError
{
model
.
ErrorNotImplemented
,
fmt
.
Errorf
(
"Druid does not support
g
etting rules for alerting"
)}
}
func
(
druid
*
DruidReader
)
CreateRule
(
localDB
*
sqlx
.
DB
,
alert
string
)
*
model
.
ApiError
{
...
...
This diff is collapsed.
Click to expand it.
pkg/query-service/app/http_handler.go
+
17
-
24
View file @
91f44ef2
...
...
@@ -173,11 +173,11 @@ func (aH *APIHandler) respond(w http.ResponseWriter, data interface{}) {
func
(
aH
*
APIHandler
)
RegisterRoutes
(
router
*
mux
.
Router
)
{
router
.
HandleFunc
(
"/api/v1/query_range"
,
aH
.
queryRangeMetrics
)
.
Methods
(
http
.
MethodGet
)
router
.
HandleFunc
(
"/api/v1/query"
,
aH
.
queryMetrics
)
.
Methods
(
http
.
MethodGet
)
router
.
HandleFunc
(
"/api/v1/rules"
,
aH
.
L
istRulesFromProm
)
.
Methods
(
http
.
MethodGet
)
//
router.HandleFunc("/api/v1/rules", aH.getRule
s
).Methods(http.MethodGet)
router
.
HandleFunc
(
"/api/v1/rules"
,
aH
.
l
istRulesFromProm
)
.
Methods
(
http
.
MethodGet
)
router
.
HandleFunc
(
"/api/v1/rules
/{id}
"
,
aH
.
getRule
)
.
Methods
(
http
.
MethodGet
)
router
.
HandleFunc
(
"/api/v1/rules"
,
aH
.
createRule
)
.
Methods
(
http
.
MethodPost
)
router
.
HandleFunc
(
"/api/v1/rules"
,
aH
.
editRule
)
.
Methods
(
http
.
MethodPut
)
router
.
HandleFunc
(
"/api/v1/rules"
,
aH
.
deleteRule
)
.
Methods
(
http
.
MethodDelete
)
router
.
HandleFunc
(
"/api/v1/rules
/{id}
"
,
aH
.
editRule
)
.
Methods
(
http
.
MethodPut
)
router
.
HandleFunc
(
"/api/v1/rules
/{id}
"
,
aH
.
deleteRule
)
.
Methods
(
http
.
MethodDelete
)
router
.
HandleFunc
(
"/api/v1/dashboards"
,
aH
.
getDashboards
)
.
Methods
(
http
.
MethodGet
)
router
.
HandleFunc
(
"/api/v1/dashboards"
,
aH
.
createDashboards
)
.
Methods
(
http
.
MethodPost
)
...
...
@@ -221,7 +221,17 @@ func Intersection(a, b []int) (c []int) {
return
}
func
(
aH
*
APIHandler
)
ListRulesFromProm
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
func
(
aH
*
APIHandler
)
getRule
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
id
:=
mux
.
Vars
(
r
)[
"id"
]
alertList
,
apiErrorObj
:=
(
*
aH
.
reader
)
.
GetRule
(
aH
.
localDB
,
id
)
if
apiErrorObj
!=
nil
{
aH
.
respondError
(
w
,
apiErrorObj
,
nil
)
return
}
aH
.
respond
(
w
,
alertList
)
}
func
(
aH
*
APIHandler
)
listRulesFromProm
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
alertList
,
apiErrorObj
:=
(
*
aH
.
reader
)
.
ListRulesFromProm
(
aH
.
localDB
)
if
apiErrorObj
!=
nil
{
aH
.
respondError
(
w
,
apiErrorObj
,
nil
)
...
...
@@ -366,23 +376,6 @@ func (aH *APIHandler) createDashboards(w http.ResponseWriter, r *http.Request) {
}
func
(
aH
*
APIHandler
)
getRules
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
rules
,
err
:=
(
*
aH
.
reader
)
.
GetRules
(
aH
.
localDB
)
if
err
!=
nil
{
aH
.
respondError
(
w
,
err
,
nil
)
return
}
if
rules
!=
nil
{
aH
.
respond
(
w
,
rules
.
Data
)
return
}
aH
.
respond
(
w
,
""
)
}
func
(
aH
*
APIHandler
)
deleteRule
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
id
:=
mux
.
Vars
(
r
)[
"id"
]
...
...
@@ -413,7 +406,7 @@ func (aH *APIHandler) editRule(w http.ResponseWriter, r *http.Request) {
return
}
aH
.
respond
(
w
,
"rule successfully
set
"
)
aH
.
respond
(
w
,
"rule successfully
edited
"
)
}
...
...
@@ -436,7 +429,7 @@ func (aH *APIHandler) createRule(w http.ResponseWriter, r *http.Request) {
return
}
aH
.
respond
(
w
,
"rule successfully
edit
ed"
)
aH
.
respond
(
w
,
"rule successfully
add
ed"
)
}
...
...
This diff is collapsed.
Click to expand it.
pkg/query-service/app/interface.go
+
1
-
1
View file @
91f44ef2
...
...
@@ -10,8 +10,8 @@ import (
)
type
Reader
interface
{
GetRule
(
localDB
*
sqlx
.
DB
,
id
string
)
(
*
model
.
RuleResponseItem
,
*
model
.
ApiError
)
ListRulesFromProm
(
localDB
*
sqlx
.
DB
)
(
*
model
.
AlertDiscovery
,
*
model
.
ApiError
)
GetRules
(
localDB
*
sqlx
.
DB
)
(
*
model
.
RuleGroups
,
*
model
.
ApiError
)
CreateRule
(
localDB
*
sqlx
.
DB
,
alert
string
)
*
model
.
ApiError
EditRule
(
localDB
*
sqlx
.
DB
,
alert
string
,
id
string
)
*
model
.
ApiError
DeleteRule
(
localDB
*
sqlx
.
DB
,
id
string
)
*
model
.
ApiError
...
...
This diff is collapsed.
Click to expand it.
pkg/query-service/model/response.go
+
1
-
1
View file @
91f44ef2
...
...
@@ -35,7 +35,7 @@ type QueryData struct {
Stats
*
stats
.
QueryStats
`json:"stats,omitempty"`
}
type
Rule
Groups
struct
{
type
Rule
ResponseItem
struct
{
Id
int
`json:"id" db:"id"`
UpdatedAt
time
.
Time
`json:"updated_at" db:"updated_at"`
Data
string
`json:"data" db:"data"`
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment