Files
stack/components/ledger/internal/api/v2/controllers_info.go

94 lines
2.0 KiB
Go

package v2
import (
"fmt"
"net/http"
"github.com/formancehq/stack/libs/go-libs/bun/bunpaginate"
"github.com/go-chi/chi/v5"
"github.com/formancehq/ledger/internal/api/backend"
"github.com/formancehq/ledger/internal/storage/ledgerstore"
sharedapi "github.com/formancehq/stack/libs/go-libs/api"
"github.com/formancehq/stack/libs/go-libs/migrations"
)
type Info struct {
Name string `json:"name"`
Storage StorageInfo `json:"storage"`
}
type StorageInfo struct {
Migrations []migrations.Info `json:"migrations"`
}
func getLedgerInfo(w http.ResponseWriter, r *http.Request) {
ledger := backend.LedgerFromContext(r.Context())
var err error
res := Info{
Name: chi.URLParam(r, "ledger"),
Storage: StorageInfo{},
}
res.Storage.Migrations, err = ledger.GetMigrationsInfo(r.Context())
if err != nil {
sharedapi.InternalServerError(w, r, err)
return
}
sharedapi.Ok(w, res)
}
func getStats(w http.ResponseWriter, r *http.Request) {
l := backend.LedgerFromContext(r.Context())
stats, err := l.Stats(r.Context())
if err != nil {
sharedapi.InternalServerError(w, r, err)
return
}
sharedapi.Ok(w, stats)
}
func getLogs(w http.ResponseWriter, r *http.Request) {
l := backend.LedgerFromContext(r.Context())
query := ledgerstore.GetLogsQuery{}
if r.URL.Query().Get(QueryKeyCursor) != "" {
err := bunpaginate.UnmarshalCursor(r.URL.Query().Get(QueryKeyCursor), &query)
if err != nil {
sharedapi.BadRequest(w, ErrValidation, fmt.Errorf("invalid '%s' query param", QueryKeyCursor))
return
}
} else {
var err error
pageSize, err := bunpaginate.GetPageSize(r)
if err != nil {
sharedapi.BadRequest(w, ErrValidation, err)
return
}
qb, err := getQueryBuilder(r)
if err != nil {
sharedapi.BadRequest(w, ErrValidation, err)
return
}
query = ledgerstore.NewGetLogsQuery(ledgerstore.PaginatedQueryOptions[any]{
QueryBuilder: qb,
PageSize: pageSize,
})
}
cursor, err := l.GetLogs(r.Context(), query)
if err != nil {
sharedapi.InternalServerError(w, r, err)
return
}
sharedapi.RenderCursor(w, *cursor)
}