Skip to content

Commit

Permalink
[+] add PATCH /metric endpoint, closes #96
Browse files Browse the repository at this point in the history
  • Loading branch information
pashagolub committed Feb 15, 2023
1 parent 5917c06 commit 2414a5b
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 25 deletions.
18 changes: 18 additions & 0 deletions src/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,24 @@ VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)`
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
56 changes: 31 additions & 25 deletions src/webserver/restapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,50 @@ import (
)

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
dbs, err := Server.api.GetMetrics()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
if res, err = Server.api.GetMetrics(); err != nil {
return
}
_, _ = w.Write([]byte(dbs))
_, err = w.Write([]byte(res))

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

// case http.MethodPatch:
// // update monitored database
// p, err := io.ReadAll(r.Body)
// if err != nil {
// http.Error(w, err.Error(), http.StatusBadRequest)
// }
// if err := Server.api.UpdateDatabase(r.URL.Query().Get("id"), p); err != nil {
// http.Error(w, err.Error(), http.StatusBadRequest)
// }
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
id, err := strconv.Atoi(r.URL.Query().Get("id"))
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
if id, err = strconv.Atoi(r.URL.Query().Get("id")); err != nil {
return
}
if err := Server.api.DeleteMetric(id); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
}
err = Server.api.DeleteMetric(id)

case http.MethodOptions:
w.Header().Set("Allow", "GET, POST, PATCH, DELETE, OPTIONS")
Expand Down Expand Up @@ -74,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 @@ -83,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
1 change: 1 addition & 0 deletions src/webserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type apiHandler interface {
GetMetrics() (res string, err error)
AddMetric(params []byte) error
DeleteMetric(id int) error
UpdateMetric(id int, params []byte) error
}

type WebUIServer struct {
Expand Down

0 comments on commit 2414a5b

Please sign in to comment.