Skip to content

Commit

Permalink
[+] add POST /metric endpoint, closes #95
Browse files Browse the repository at this point in the history
  • Loading branch information
pashagolub committed Feb 15, 2023
1 parent e0173fc commit 5917c06
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
17 changes: 17 additions & 0 deletions src/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,23 @@ func (uiapi uiapihandler) DeleteMetric(id int) error {
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
}

// 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
19 changes: 10 additions & 9 deletions src/webserver/restapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@ func (Server *WebUIServer) handleMetrics(w http.ResponseWriter, r *http.Request)
}
_, _ = w.Write([]byte(dbs))

// case http.MethodPost:
// // add new monitored database
// p, err := io.ReadAll(r.Body)
// if err != nil {
// http.Error(w, err.Error(), http.StatusBadRequest)
// }
// if err := Server.api.AddDatabase(p); err != nil {
// http.Error(w, err.Error(), http.StatusBadRequest)
// }
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)
}

// case http.MethodPatch:
// // update monitored database
// p, err := io.ReadAll(r.Body)
Expand Down
1 change: 1 addition & 0 deletions src/webserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type apiHandler interface {
DeleteDatabase(id string) error
UpdateDatabase(id string, params []byte) error
GetMetrics() (res string, err error)
AddMetric(params []byte) error
DeleteMetric(id int) error
}

Expand Down

0 comments on commit 5917c06

Please sign in to comment.