Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add database stats #70

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion api/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ tags:
- name: Admin
description: Manage PURLs
- name: System
description: Endpoints for interacting / managing the overal system
description: Endpoints for interacting / managing the overall system

security: []

Expand All @@ -31,6 +31,24 @@ paths:
500:
description: Service is not healthy

/s/stats/public:
get:
operationId: getPublicStats
summary: Get public stats about this system
tags: [System]
responses:
200:
description: Returns stats
content:
application/json:
schema:
type: object
properties:
domains_total:
type: integer
purls_total:
type: integer

/r/{domain}/{name}:
get:
operationId: resolvePURL
Expand Down
39 changes: 0 additions & 39 deletions api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ package api

import (
"errors"
"fmt"
"net/http"

"github.com/fabiante/persurl/api/res"
"github.com/fabiante/persurl/app"
"github.com/gin-gonic/gin"
)
Expand Down Expand Up @@ -34,40 +32,3 @@ func (s *Server) Resolve(ctx *gin.Context) {
respondWithError(ctx, http.StatusInternalServerError, err)
}
}

func (s *Server) SavePURL(ctx *gin.Context) {
domain := ctx.Param("domain")
name := ctx.Param("name")

var req res.SavePURL
if err := ctx.BindJSON(&req); err != nil {
ctx.Abort()
return
}

err := s.service.SavePURL(domain, name, req.Target)
switch true {
case err == nil:
break
case errors.Is(err, app.ErrBadRequest):
respondWithError(ctx, http.StatusBadRequest, err)
default:
respondWithError(ctx, http.StatusInternalServerError, err)
}

ctx.JSON(http.StatusOK, res.NewSavePURLResponse(fmt.Sprintf("/r/%s/%s", domain, name)))
}

func (s *Server) CreateDomain(ctx *gin.Context) {
domain := ctx.Param("domain")

err := s.service.CreateDomain(domain)
switch true {
case err == nil:
ctx.Status(http.StatusNoContent)
case errors.Is(err, app.ErrBadRequest):
respondWithError(ctx, http.StatusBadRequest, err)
default:
respondWithError(ctx, http.StatusInternalServerError, err)
}
}
48 changes: 48 additions & 0 deletions api/server_admin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package api

import (
"errors"
"fmt"
"net/http"

"github.com/fabiante/persurl/api/res"
"github.com/fabiante/persurl/app"
"github.com/gin-gonic/gin"
)

func (s *Server) SavePURL(ctx *gin.Context) {
domain := ctx.Param("domain")
name := ctx.Param("name")

var req res.SavePURL
if err := ctx.BindJSON(&req); err != nil {
ctx.Abort()
return
}

err := s.service.SavePURL(domain, name, req.Target)
switch true {
case err == nil:
break
case errors.Is(err, app.ErrBadRequest):
respondWithError(ctx, http.StatusBadRequest, err)
default:
respondWithError(ctx, http.StatusInternalServerError, err)
}

ctx.JSON(http.StatusOK, res.NewSavePURLResponse(fmt.Sprintf("/r/%s/%s", domain, name)))
}

func (s *Server) CreateDomain(ctx *gin.Context) {
domain := ctx.Param("domain")

err := s.service.CreateDomain(domain)
switch true {
case err == nil:
ctx.Status(http.StatusNoContent)
case errors.Is(err, app.ErrBadRequest):
respondWithError(ctx, http.StatusBadRequest, err)
default:
respondWithError(ctx, http.StatusInternalServerError, err)
}
}
2 changes: 2 additions & 0 deletions api/server_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,7 @@ func SetupRouting(r gin.IRouter, s *Server) {

ctx.JSON(http.StatusOK, "service is ready to receive requests")
})

sys.GET("/stats/public", s.GetPublicStats)
}
}
18 changes: 18 additions & 0 deletions api/server_stats.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package api

import (
"net/http"

"github.com/gin-gonic/gin"
)

func (s *Server) GetPublicStats(ctx *gin.Context) {
stats, err := s.service.DetermineServiceStats()

switch true {
case err == nil:
ctx.JSON(http.StatusOK, stats)
default:
respondWithError(ctx, http.StatusInternalServerError, err)
}
}
11 changes: 11 additions & 0 deletions app/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,15 @@ type ServiceInterface interface {
//
// ErrBadRequest is returned if the domain already exists.
CreateDomain(domain string) error

// DetermineServiceStats calculates statistics about the service.
//
// This is potentially an expensive operation and should not be called
// frequently.
DetermineServiceStats() (*Stats, error)
}

type Stats struct {
DomainsTotal int `json:"domains_total"`
PurlsTotal int `json:"purls_total"`
}
28 changes: 28 additions & 0 deletions db/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,34 @@ func (db *Database) CreateDomain(domain string) error {
}
}

func (db *Database) DetermineServiceStats() (*app.Stats, error) {
stat := &app.Stats{}
var errs []error
var err error

stat.DomainsTotal, err = db.countRows("domains", "id")
errs = append(errs, err)

stat.PurlsTotal, err = db.countRows("purls", "id")
errs = append(errs, err)

return stat, errors.Join(errs...)
}

func (db *Database) countRows(table string, column string) (int, error) {
var count int

found, err := db.db.From("domains").Select(goqu.COUNT(goqu.C("id"))).ScanVal(&count)
switch {
case err != nil:
return 0, fmt.Errorf("failed to count rows of table %s: %w", table, err)
case !found:
return 0, fmt.Errorf("failed to count rows of table %s, no rows found: %w", table, err)
}

return count, nil
}

const (
pgErrUniqueKeyViolation = "23505"
)
Expand Down