Skip to content

Commit

Permalink
Merge pull request #113 from cybertec-postgresql/93-add-support-for-m…
Browse files Browse the repository at this point in the history
…etric-endpoint

[+] add `/metric` endpoint, closes #93
  • Loading branch information
pashagolub authored Feb 16, 2023
2 parents 0a410b5 + 2414a5b commit 7540a66
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,54 @@ type uiapihandler struct{}

var uiapi uiapihandler

// GetMetrics returns the list of monitored databases
func (uiapi uiapihandler) GetMetrics() (res string, err error) {
sql := `select coalesce(jsonb_agg(to_jsonb(m)), '[]') from metric m`
err = configDb.Get(&res, sql)
return
}

// DeleteMetric removes the database from the list of monitored databases
func (uiapi uiapihandler) DeleteMetric(id int) error {
_, err := configDb.Exec("DELETE FROM pgwatch3.metric WHERE m_id = $1", id)
return err
}

// AddMetric adds the metric to the list of stored metrics
func (uiapi uiapihandler) AddMetric(params []byte) error {
sql := `INSERT INTO pgwatch3.metric(
m_name, m_pg_version_from, m_sql, m_comment, m_is_active, m_is_helper,
m_master_only, m_standby_only, m_column_attrs, m_sql_su)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)`
var m map[string]any
err := json.Unmarshal(params, &m)
if err == nil {
_, err = configDb.Exec(sql, m["m_name"], m["m_pg_version_from"],
m["m_sql"], m["m_comment"], m["m_is_active"],
m["m_is_helper"], m["m_master_only"], m["m_standby_only"],
m["m_column_attrs"], m["m_sql_su"])
}
return err
}

// UpdateMetric updates the stored metric information
func (uiapi uiapihandler) UpdateMetric(id int, params []byte) error {
sql := `UPDATE pgwatch3.metric(
m_name, m_pg_version_from, m_sql, m_comment, m_is_active, m_is_helper,
m_master_only, m_standby_only, m_column_attrs, m_sql_su)
= ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
WHERE m_id = $11`
var m map[string]any
err := json.Unmarshal(params, &m)
if err == nil {
_, err = configDb.Exec(sql, m["m_name"], m["m_pg_version_from"],
m["m_sql"], m["m_comment"], m["m_is_active"],
m["m_is_helper"], m["m_master_only"], m["m_standby_only"],
m["m_column_attrs"], m["m_sql_su"], id)
}
return err
}

// GetDatabases returns the list of monitored databases
func (uiapi uiapihandler) GetDatabases() (res string, err error) {
sql := `select coalesce(jsonb_agg(to_jsonb(db)), '[]') from monitored_db db`
Expand Down
59 changes: 59 additions & 0 deletions src/webserver/restapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,65 @@ package webserver
import (
"io"
"net/http"
"strconv"
)

func (Server *WebUIServer) handleMetrics(w http.ResponseWriter, r *http.Request) {
var (
err error
params []byte
res string
id int
)

defer func() {
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}()

switch r.Method {
case http.MethodGet:
// return stored metrics
if res, err = Server.api.GetMetrics(); err != nil {
return
}
_, err = w.Write([]byte(res))

case http.MethodPost:
// add new stored metric
if params, err = io.ReadAll(r.Body); err != nil {
return
}
err = Server.api.AddMetric(params)

case http.MethodPatch:
// update monitored database
if params, err = io.ReadAll(r.Body); err != nil {
return
}
if id, err = strconv.Atoi(r.URL.Query().Get("id")); err != nil {
return
}
err = Server.api.UpdateMetric(id, params)

case http.MethodDelete:
// delete stored metric
if id, err = strconv.Atoi(r.URL.Query().Get("id")); err != nil {
return
}
err = Server.api.DeleteMetric(id)

case http.MethodOptions:
w.Header().Set("Allow", "GET, POST, PATCH, DELETE, OPTIONS")
w.WriteHeader(http.StatusNoContent)

default:
w.Header().Set("Allow", "GET, POST, PATCH, DELETE, OPTIONS")
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
}
}

func (Server *WebUIServer) handleDBs(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case http.MethodGet:
Expand All @@ -21,6 +78,7 @@ func (Server *WebUIServer) handleDBs(w http.ResponseWriter, r *http.Request) {
p, err := io.ReadAll(r.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if err := Server.api.AddDatabase(p); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
Expand All @@ -30,6 +88,7 @@ func (Server *WebUIServer) handleDBs(w http.ResponseWriter, r *http.Request) {
p, err := io.ReadAll(r.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if err := Server.api.UpdateDatabase(r.URL.Query().Get("id"), p); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
Expand Down
5 changes: 5 additions & 0 deletions src/webserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ type apiHandler interface {
AddDatabase(params []byte) error
DeleteDatabase(id string) error
UpdateDatabase(id string, params []byte) error
GetMetrics() (res string, err error)
AddMetric(params []byte) error
DeleteMetric(id int) error
UpdateMetric(id int, params []byte) error
}

type WebUIServer struct {
Expand Down Expand Up @@ -50,6 +54,7 @@ func Init(addr string, webuifs fs.FS, api apiHandler) *WebUIServer {

mux.HandleFunc("/health", s.handleHealth)
mux.HandleFunc("/db", s.handleDBs)
mux.HandleFunc("/metric", s.handleMetrics)
mux.HandleFunc("/", s.handleStatic)

if 8080 != 0 {
Expand Down

0 comments on commit 7540a66

Please sign in to comment.