Skip to content

Commit

Permalink
Merge pull request #24 from Cairry/master
Browse files Browse the repository at this point in the history
[feat]: datasource check
  • Loading branch information
Cairry authored Apr 22, 2024
2 parents f2ea2cd + 0f1b231 commit 61876f4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 22 deletions.
24 changes: 2 additions & 22 deletions controllers/services/alert_datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"watchAlert/models"
"watchAlert/public/globals"
"watchAlert/public/utils/cmd"
"watchAlert/public/utils/http"
)

type AlertDataSourceService struct {
Expand All @@ -30,8 +29,8 @@ func NewInterAlertDataSourceService() InterAlertDataSourceService {

func (adss *AlertDataSourceService) Create(dataSource models.AlertDataSource) error {

err := adss.Check(dataSource)
if err != nil {
ok, err := dataSource.CheckHealth()
if !ok {
return err
}

Expand Down Expand Up @@ -166,22 +165,3 @@ func (adss *AlertDataSourceService) Search(req interface{}) (interface{}, interf

return newData, nil
}

func (adss *AlertDataSourceService) Check(dataSource models.AlertDataSource) error {

switch dataSource.Type {
case "Prometheus":
path := "/api/v1/format_query?query=foo/bar"
fullPath := dataSource.HTTP.URL + path
res, err := http.Get(fullPath)
if err != nil {
return err
}
if res.StatusCode != 200 {
return fmt.Errorf("StatusCode 非预期 -> %d", res.StatusCode)
}
}

return nil

}
30 changes: 30 additions & 0 deletions models/alert_datasource.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package models

import (
"fmt"
"net/http"
utilsHttp "watchAlert/public/utils/http"
)

type AlertDataSource struct {
TenantId string `json:"tenantId"`
Id string `json:"id"`
Expand All @@ -25,3 +31,27 @@ type DatasourceQuery struct {
Type string `json:"type" form:"type"`
Query string `json:"query" form:"query"`
}

func (ds AlertDataSource) CheckHealth() (bool, error) {
url := ds.HTTP.URL
var (
res = &http.Response{}
err error
)
switch ds.Type {
case "Prometheus":
path := "/-/healthy"
fullPath := url + path
res, err = utilsHttp.Get(fullPath)
}

if err != nil {
return false, fmt.Errorf("request url: %s failed", url)
}

if res.StatusCode != 200 {
return false, fmt.Errorf("request url: %s failed , StatusCode: %d", url, res.StatusCode)
}

return true, nil
}

0 comments on commit 61876f4

Please sign in to comment.