mirror of
https://github.com/hanzoai/base.git
synced 2026-10-21 03:41:50 +00:00
BASE_DB_TIER=sql opened PostgreSQL and then spoke to it in SQLite: strftime, sqlite_master, json_extract, json_each, an INTEGER column holding an int64, an outer join with no ON, backticked identifiers, and a rowid that only one engine has. The tier picked a backend the queries could not reach. Every one of those now comes from hanzoai/orm/dialect, resolved once from the driver the data DB was opened with and reachable as app.Dialect(). Base holds no switch on the engine — it asks for the JSON accessors, the schema catalog, the generated instant and identifier, and the column types, and it asks whether a feature exists at all where one engine has nothing to offer: an insertion-order column, a write-ahead log to fold, a function that renders an instant. Empty means the caller falls back or refuses rather than emitting something the engine will reject. Some differences turned out not to need a dialect at all. An instant is stored as text in a fixed layout, so bucketing the log rollup to the hour is a substring rather than a calendar function, which every engine spells the same way and an index can be built on. Null-safe equality is IS NOT DISTINCT FROM. An int64 column is BIGINT. A join with nothing of its own to match still states a condition. Case-insensitive comparison stays COLLATE NOCASE, because the schema now asks the engine for the collation that name refers to. The proof is a Postgres 18 running the data plane: migrations apply, a collection created over /v1 lands as a table with text, numeric and jsonb columns and its declared index, and records insert, page, sort, filter across scalars, multi-value elements and JSON paths, update and delete. A unique index declared COLLATE NOCASE rejects the second spelling of a handle. Comparing a JSON sub-path to a number is refused there: the extraction yields text, the literal binds as a number, and the filter language does not say which the comparison meant. Compared as text it works.
74 lines
1.8 KiB
Go
74 lines
1.8 KiB
Go
package apis
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/hanzoai/base/core"
|
|
"github.com/hanzoai/base/tools/router"
|
|
"github.com/hanzoai/base/tools/search"
|
|
"github.com/hanzoai/dbx"
|
|
)
|
|
|
|
// bindLogsApi registers the request logs api endpoints.
|
|
func bindLogsApi(app core.App, rg *router.RouterGroup[*core.RequestEvent]) {
|
|
sub := rg.Group("/logs").Bind(RequireSuperuserAuth(), SkipSuccessActivityLog())
|
|
sub.GET("", logsList)
|
|
sub.GET("/stats", logsStats)
|
|
sub.GET("/{id}", logsView)
|
|
}
|
|
|
|
var logFilterFields = []string{
|
|
"id", "created", "level", "message", "data",
|
|
`^data\.[\w\.\:]*\w+$`,
|
|
}
|
|
|
|
func logsList(e *core.RequestEvent) error {
|
|
fieldResolver := search.NewSimpleFieldResolver(e.App.Dialect(), logFilterFields...)
|
|
|
|
result, err := search.NewProvider(fieldResolver).
|
|
Query(e.App.AuxModelQuery(&core.Log{})).
|
|
ParseAndExec(e.Request.URL.Query().Encode(), &[]*core.Log{})
|
|
|
|
if err != nil {
|
|
return e.BadRequestError("", err)
|
|
}
|
|
|
|
return e.JSON(http.StatusOK, result)
|
|
}
|
|
|
|
func logsStats(e *core.RequestEvent) error {
|
|
fieldResolver := search.NewSimpleFieldResolver(e.App.Dialect(), logFilterFields...)
|
|
|
|
filter := e.Request.URL.Query().Get(search.FilterQueryParam)
|
|
|
|
var expr dbx.Expression
|
|
if filter != "" {
|
|
var err error
|
|
expr, err = search.FilterData(filter).BuildExpr(fieldResolver)
|
|
if err != nil {
|
|
return e.BadRequestError("Invalid filter format.", err)
|
|
}
|
|
}
|
|
|
|
stats, err := e.App.LogsStats(expr)
|
|
if err != nil {
|
|
return e.BadRequestError("Failed to generate logs stats.", err)
|
|
}
|
|
|
|
return e.JSON(http.StatusOK, stats)
|
|
}
|
|
|
|
func logsView(e *core.RequestEvent) error {
|
|
id := e.Request.PathValue("id")
|
|
if id == "" {
|
|
return e.NotFoundError("", nil)
|
|
}
|
|
|
|
log, err := e.App.FindLogById(id)
|
|
if err != nil || log == nil {
|
|
return e.NotFoundError("", err)
|
|
}
|
|
|
|
return e.JSON(http.StatusOK, log)
|
|
}
|