Skip to content

Commit

Permalink
Merge pull request #522 from sipcapture/add-grafana-path
Browse files Browse the repository at this point in the history
Add grafana path
  • Loading branch information
adubovikov authored Jun 13, 2023
2 parents 3084e4a + c4bec0b commit 31e9e57
Show file tree
Hide file tree
Showing 6 changed files with 332 additions and 241 deletions.
4 changes: 2 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type HomerSettingServer struct {
User string `default:""`
Password string `default:""`
ProxyControl bool `default:"false"`
Path string `default:""`
Path string `default:"grafana"`
ProxyCheck string `default:"simple"`
Enable bool `default:"false"`
}
Expand Down Expand Up @@ -74,7 +74,7 @@ type HomerSettingServer struct {
ServiceProviderImage string `default:""`
StateValue string `default:"jkwh027yasj"`
UrlToServiceRedirect string `default:"/api/v3/oauth2/redirect"`
UrlToService string `default:"/"`
UrlToService string `default:"/"`
Scope []string `default:"[email,openid,profile]"`
EnableGravatar bool `default:"false"`
EnableAutoRedirect bool `default:"false"`
Expand Down
160 changes: 107 additions & 53 deletions controller/v1/grafana.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,36 @@ type GrafanaController struct {
GrafanaService *service.GrafanaService
}

// swagger:route GET /proxy/grafana/path proxy grafanaGrafanaPath
//
// Returns Grafana Proxy Path
// ---
// produces:
// - application/json
// Security:
// - bearer: []
//
// SecurityDefinitions:
// bearer:
//
// type: apiKey
// name: Authorization
// in: header
//
// responses:
//
// 200: body:GrafanaUrl
// 400: body:FailureResponse
func (pc *GrafanaController) GrafanaPath(c echo.Context) error {

responseData, err := pc.GrafanaService.GrafanaPath()
if err != nil {
logger.Debug(responseData)
return httpresponse.CreateBadResponse(&c, http.StatusBadRequest, "Grafana service is not configured")
}
return httpresponse.CreateSuccessResponse(&c, http.StatusCreated, responseData)
}

// swagger:route GET /proxy/grafana/url proxy grafanaGrafanaURL
//
// Returns Grafana Proxy Url
Expand All @@ -25,13 +55,15 @@ type GrafanaController struct {
//
// SecurityDefinitions:
// bearer:
// type: apiKey
// name: Authorization
// in: header
//
// type: apiKey
// name: Authorization
// in: header
//
// responses:
// 200: body:GrafanaUrl
// 400: body:FailureResponse
//
// 200: body:GrafanaUrl
// 400: body:FailureResponse
func (pc *GrafanaController) GrafanaURL(c echo.Context) error {

responseData, err := pc.GrafanaService.GrafanaURL()
Expand All @@ -53,17 +85,21 @@ func (pc *GrafanaController) GrafanaURL(c echo.Context) error {
//
// SecurityDefinitions:
// JWT:
// type: apiKey
// name: Authorization
// in: header
//
// type: apiKey
// name: Authorization
// in: header
//
// ApiKeyAuth:
// type: apiKey
// in: header
// name: Auth-Token
//
// type: apiKey
// in: header
// name: Auth-Token
//
// Responses:
// 201: body:ListUsers
// 400: body:FailureResponse
//
// 201: body:ListUsers
// 400: body:FailureResponse
func (pc *GrafanaController) GrafanaStatus(c echo.Context) error {

responseData, err := pc.GrafanaService.GrafanaStatus()
Expand All @@ -85,12 +121,15 @@ func (pc *GrafanaController) GrafanaStatus(c echo.Context) error {
//
// SecurityDefinitions:
// bearer:
// type: apiKey
// name: Authorization
// in: header
//
// type: apiKey
// name: Authorization
// in: header
//
// responses:
// 200: body:GrafanaOrg
// 400: body:UserLoginFailureResponse
//
// 200: body:GrafanaOrg
// 400: body:UserLoginFailureResponse
func (pc *GrafanaController) GrafanaORG(c echo.Context) error {

err := pc.GrafanaService.SetGrafanaObject()
Expand All @@ -117,12 +156,15 @@ func (pc *GrafanaController) GrafanaORG(c echo.Context) error {
//
// SecurityDefinitions:
// bearer:
// type: apiKey
// name: Authorization
// in: header
//
// type: apiKey
// name: Authorization
// in: header
//
// responses:
// 200: body:GrafanaFolders
// 400: body:FailureResponse
//
// 200: body:GrafanaFolders
// 400: body:FailureResponse
func (pc *GrafanaController) GrafanaFolders(c echo.Context) error {

err := pc.GrafanaService.SetGrafanaObject()
Expand All @@ -145,23 +187,27 @@ func (pc *GrafanaController) GrafanaFolders(c echo.Context) error {
// produces:
// - application/json
// parameters:
// + name: uid
// in: path
// example: 9Aklz9aGz
// description: uid
// required: true
// type: string
// - name: uid
// in: path
// example: 9Aklz9aGz
// description: uid
// required: true
// type: string
//
// Security:
// - bearer: []
//
// SecurityDefinitions:
// bearer:
// type: apiKey
// name: Authorization
// in: header
//
// type: apiKey
// name: Authorization
// in: header
//
// responses:
// 200: body:GrafanaResponseValue
// 400: body:FailureResponse
//
// 200: body:GrafanaResponseValue
// 400: body:FailureResponse
func (pc *GrafanaController) GrafanaGetDashboardAgainstUUID(c echo.Context) error {

uuidDashboard := c.Param("uid")
Expand Down Expand Up @@ -189,19 +235,23 @@ func (pc *GrafanaController) GrafanaGetDashboardAgainstUUID(c echo.Context) erro
// - bearer
// SecurityDefinitions:
// bearer:
// type: apiKey
// name: Authorization
// in: header
//
// type: apiKey
// name: Authorization
// in: header
//
// parameters:
// + name: uid
// in: path
// example: 9Aklz9aGz
// description: uid
// required: true
// type: string
// - name: uid
// in: path
// example: 9Aklz9aGz
// description: uid
// required: true
// type: string
//
// responses:
// 200: body:ListUsers
// 400: body:FailureResponse
//
// 200: body:ListUsers
// 400: body:FailureResponse
func (pc *GrafanaController) GrafanaGetFoldersAgainstUUID(c echo.Context) error {

uuidDashboard := c.Param("uid")
Expand Down Expand Up @@ -230,17 +280,21 @@ func (pc *GrafanaController) GrafanaGetFoldersAgainstUUID(c echo.Context) error
//
// SecurityDefinitions:
// JWT:
// type: apiKey
// name: Authorization
// in: header
//
// type: apiKey
// name: Authorization
// in: header
//
// ApiKeyAuth:
// type: apiKey
// in: header
// name: Auth-Token
//
// type: apiKey
// in: header
// name: Auth-Token
//
// Responses:
// 201: body:ListUsers
// 400: body:FailureResponse
//
// 201: body:ListUsers
// 400: body:FailureResponse
func (pc *GrafanaController) GrafanaGetDashboardRequest(c echo.Context) error {

requestDashboard := c.Param("uid")
Expand Down
34 changes: 26 additions & 8 deletions data/service/grafana.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,23 @@ type GrafanaService struct {
ServiceGrafana
}

// LabelsData: this method get all Grafana labels from database
// This method will return sub-path from config to UI
func (ps *GrafanaService) GrafanaPath() (string, error) {

urlGrafana := ""
replyData := gabs.New()

if !config.Setting.GRAFANA_SETTINGS.Enable {
urlGrafana = "grafana"
} else {
urlGrafana = config.Setting.GRAFANA_SETTINGS.Path
}

replyData.Set(urlGrafana, "data")
return replyData.String(), nil
}

// This method returns Grafana Host from config
func (ps *GrafanaService) GrafanaURL() (string, error) {

urlGrafana := ""
Expand Down Expand Up @@ -48,7 +64,7 @@ func (ps *GrafanaService) GrafanaURL() (string, error) {
return replyData.String(), nil
}

// LabelsData: if grafana has been activated
// This method returns if Grafana is enabled in the system
func (ps *GrafanaService) GrafanaStatus() (string, error) {

replyData := gabs.New()
Expand All @@ -60,6 +76,7 @@ func (ps *GrafanaService) GrafanaStatus() (string, error) {
return replyData.String(), nil
}

// This method sets values in service based on Config
func (ps *GrafanaService) SetGrafanaObject() error {

// assign only need
Expand Down Expand Up @@ -99,7 +116,7 @@ func (ps *GrafanaService) SetGrafanaObject() error {
return nil
}

// LabelsData: this method get all Grafana labels from database
// This method returns Grafana Organisation
func (ps *GrafanaService) GrafanaORG() (string, error) {

grafanaQuery := fmt.Sprintf("%s/api/org", ps.Host)
Expand All @@ -120,9 +137,10 @@ func (ps *GrafanaService) GrafanaORG() (string, error) {

data, err := ps.HttpClient.Do(req)
defer ps.HttpClient.CloseIdleConnections()

println("token" + ps.Token)
if err != nil {
logger.Error("Couldn't make http query:", grafanaQuery)
println("ERROR IN HTTP REQUEST" + grafanaQuery)
return "", err
}

Expand Down Expand Up @@ -154,7 +172,7 @@ func (ps *GrafanaService) GrafanaORG() (string, error) {

}

// LabelsData: this method get all Grafana labels from database
// This method returns all Grafana Folders
func (ps *GrafanaService) GrafanaFolders() (string, error) {

grafanaQuery := fmt.Sprintf("%s/api/search?folderIds=0", ps.Host)
Expand Down Expand Up @@ -209,7 +227,7 @@ func (ps *GrafanaService) GrafanaFolders() (string, error) {

}

//LabelsData: this method get all Grafana labels from database
// This method perfroms search in Grafana for Dashboard by UUID
func (ps *GrafanaService) GrafanaGetDashboardByUUUID(uuid string) (string, error) {

grafanaQuery := fmt.Sprintf("%s/api/dashboards/uid/%s", ps.Host, uuid)
Expand Down Expand Up @@ -264,7 +282,7 @@ func (ps *GrafanaService) GrafanaGetDashboardByUUUID(uuid string) (string, error

}

//LabelsData: this method get all Grafana labels from database
// This method perfroms search in Grafana for Dashboard Folder by UUID
func (ps *GrafanaService) GrafanaGetFoldersdByUUUID(uuid string) (string, error) {

grafanaQuery := fmt.Sprintf("%s/api/search?folderIds=%s", ps.Host, uuid)
Expand Down Expand Up @@ -319,7 +337,7 @@ func (ps *GrafanaService) GrafanaGetFoldersdByUUUID(uuid string) (string, error)

}

//LabelsData: this method get all Grafana labels from database
// This method returns Grafana dashboard by UUID
func (ps *GrafanaService) GrafanaGetDashboardRequest(dashboard string, uuid string, query string) (string, error) {

grafanaQuery := fmt.Sprintf("%s/d/%s/%s?%s", ps.Host, dashboard, uuid, query)
Expand Down
Loading

0 comments on commit 31e9e57

Please sign in to comment.