Files
zeekayandHanzo Dev 308c4b7372 dashboards, views, preferences: a path segment is read by name, not by router
The 28 reads across dashboards, saved views and preferences named
gorilla/mux to answer "which id is this request about?". Which router
matched a route is not a fact a handler should know; the VALUE it needs
is the segment. All 28 now go through coretypes.Param.

Query values are untouched: r.URL.Query().Get stays exactly as it was.
/rules/{id} is a PATH segment and ?limit=25 is a QUERY value, and the
seam exists precisely so those two cannot be confused.

Two shapes of read collapse to one. `nameString, ok := mux.Vars(r)["name"]`
becomes `nameString := coretypes.Param(r, "name")` with the ok-check
becoming an empty-check: a route with {name} cannot match an empty
segment, so present-but-empty was never reachable and absence still
answers "".

The two public-dashboard gates held byte-identical copies of the same
subject lookup. They are now one method, provider.publicDashboardSelectors.
That gate runs before any identity exists — it resolves WHO the request is
about from the path — so two copies is two places for the answer to drift.

Tests drive the REAL handlers through a REAL router at the REAL templates
and assert on what reached the module, not on a status code. Asserting a
reader against its own injector would prove only that the two halves of
coretypes agree with each other. Verified by mutation: with Param stubbed
to "" all four packages go red.

Registration is untouched — {id} literals and *mux.Router stay put for the
one atomic commit that moves them.

Co-authored-by: Hanzo Dev <[email protected]>
2026-08-01 18:56:25 -07:00

166 lines
4.2 KiB
Go

package implsavedview
import (
"context"
"encoding/json"
"net/http"
"time"
"github.com/hanzoai/o11y/pkg/errors"
"github.com/hanzoai/o11y/pkg/http/render"
"github.com/hanzoai/o11y/pkg/modules/savedview"
v3 "github.com/hanzoai/o11y/pkg/query-service/model/v3"
"github.com/hanzoai/o11y/pkg/types/authtypes"
"github.com/hanzoai/o11y/pkg/types/coretypes"
"github.com/hanzoai/o11y/pkg/valuer"
)
type handler struct {
module savedview.Module
}
func NewHandler(module savedview.Module) savedview.Handler {
return &handler{module: module}
}
func (handler *handler) Create(w http.ResponseWriter, r *http.Request) {
ctx, cancel := context.WithTimeout(r.Context(), 10*time.Second)
defer cancel()
claims, err := authtypes.ClaimsFromContext(ctx)
if err != nil {
render.Error(w, err)
return
}
var view v3.SavedView
if err := json.NewDecoder(r.Body).Decode(&view); err != nil {
render.Error(w, errors.Wrapf(err, errors.TypeInvalidInput, errors.CodeInvalidInput, "failed to decode request body"))
return
}
// validate the query
if err := view.Validate(); err != nil {
render.Error(w, errors.Wrapf(err, errors.TypeInvalidInput, errors.CodeInvalidInput, "failed to validate request body"))
return
}
uuid, err := handler.module.CreateView(ctx, claims.OrgID, view)
if err != nil {
render.Error(w, err)
return
}
render.Success(w, http.StatusOK, uuid)
}
func (handler *handler) Get(w http.ResponseWriter, r *http.Request) {
ctx, cancel := context.WithTimeout(r.Context(), 10*time.Second)
defer cancel()
claims, err := authtypes.ClaimsFromContext(ctx)
if err != nil {
render.Error(w, err)
return
}
viewID := coretypes.Param(r, "viewId")
viewUUID, err := valuer.NewUUID(viewID)
if err != nil {
render.Error(w, errors.Wrapf(err, errors.TypeInvalidInput, errors.CodeInvalidInput, "failed to parse view id"))
return
}
view, err := handler.module.GetView(ctx, claims.OrgID, viewUUID)
if err != nil {
render.Error(w, err)
return
}
render.Success(w, http.StatusOK, view)
}
func (handler *handler) Update(w http.ResponseWriter, r *http.Request) {
ctx, cancel := context.WithTimeout(r.Context(), 10*time.Second)
defer cancel()
claims, err := authtypes.ClaimsFromContext(ctx)
if err != nil {
render.Error(w, err)
return
}
viewID := coretypes.Param(r, "viewId")
viewUUID, err := valuer.NewUUID(viewID)
if err != nil {
render.Error(w, errors.Wrapf(err, errors.TypeInvalidInput, errors.CodeInvalidInput, "failed to parse view id"))
return
}
var view v3.SavedView
if err := json.NewDecoder(r.Body).Decode(&view); err != nil {
render.Error(w, errors.Wrapf(err, errors.TypeInvalidInput, errors.CodeInvalidInput, "failed to decode request body"))
return
}
// validate the query
if err := view.Validate(); err != nil {
render.Error(w, errors.Wrapf(err, errors.TypeInvalidInput, errors.CodeInvalidInput, "failed to validate request body"))
return
}
err = handler.module.UpdateView(ctx, claims.OrgID, viewUUID, view)
if err != nil {
render.Error(w, err)
return
}
render.Success(w, http.StatusOK, view)
}
func (handler *handler) Delete(w http.ResponseWriter, r *http.Request) {
ctx, cancel := context.WithTimeout(r.Context(), 10*time.Second)
defer cancel()
claims, err := authtypes.ClaimsFromContext(ctx)
if err != nil {
render.Error(w, err)
return
}
viewID := coretypes.Param(r, "viewId")
viewUUID, err := valuer.NewUUID(viewID)
if err != nil {
render.Error(w, errors.Wrapf(err, errors.TypeInvalidInput, errors.CodeInvalidInput, "failed to parse view id"))
return
}
err = handler.module.DeleteView(ctx, claims.OrgID, viewUUID)
if err != nil {
render.Error(w, err)
return
}
render.Success(w, http.StatusOK, nil)
}
func (handler *handler) List(w http.ResponseWriter, r *http.Request) {
ctx, cancel := context.WithTimeout(r.Context(), 10*time.Second)
defer cancel()
claims, err := authtypes.ClaimsFromContext(ctx)
if err != nil {
render.Error(w, err)
return
}
sourcePage := r.URL.Query().Get("sourcePage")
name := r.URL.Query().Get("name")
category := r.URL.Query().Get("category")
queries, err := handler.module.GetViewsForFilters(r.Context(), claims.OrgID, sourcePage, name, category)
if err != nil {
render.Error(w, err)
return
}
render.Success(w, http.StatusOK, queries)
}