2 Commits

Author SHA1 Message Date
03b4b55927 feat(api): support optional systemInfo in submissions
All checks were successful
Build and Publish Docker Image / deploy (push) Successful in 1m17s
Extend the submission contract to accept a `systemInfo` object and persist it with each submission, including deep-copy support for `extra` metadata.

Also update client-facing docs and HTTP examples (JSON and multipart) and document that the schema is available at `GET /api/schema`, so clients can reliably implement the updated payload format.feat(api): support optional systemInfo in submissions

Extend the submission contract to accept a `systemInfo` object and persist it with each submission, including deep-copy support for `extra` metadata.

Also update client-facing docs and HTTP examples (JSON and multipart) and document that the schema is available at `GET /api/schema`, so clients can reliably implement the updated payload format.
2026-04-17 13:57:55 +03:00
d2be2276ec Better logging 2026-04-17 13:25:48 +03:00
9 changed files with 828 additions and 31 deletions

View File

@@ -163,6 +163,13 @@ Ready-to-run HTTP client examples are included in:
- `http/submit-multipart.http` - `http/submit-multipart.http`
- `http/search.http` - `http/search.http`
Client-facing submission contract docs are included in:
- `docs/submit-api.md`
- `docs/submit-schema.json`
The schema is also served by the app at `GET /api/schema`.
You can also submit one of the provided sample payloads directly: You can also submit one of the provided sample payloads directly:
```bash ```bash

117
docs/submit-api.md Normal file
View File

@@ -0,0 +1,117 @@
# Submission API
Use `POST /api/submit` to send benchmark results to the server.
## Endpoint
- Method: `POST`
- URL: `/api/submit`
- Content-Types:
- `application/json`
- `multipart/form-data`
## Schema
- Import URL: `/api/schema`
- Source file in repo: `docs/submit-schema.json`
## JSON format
Clients can send either:
1. An envelope with `submitter`, `platform`, optional `systemInfo`, and nested `benchmark`
2. A flat benchmark payload with optional `submitter`, `platform`, and optional `systemInfo`
Example envelope:
```json
{
"submitter": "AMD Bazzite",
"platform": "linux",
"systemInfo": {
"hostname": "bench-rig-01",
"osName": "Bazzite",
"osVersion": "41",
"distro": "Fedora Atomic",
"kernelVersion": "6.14.2-300.fc41.x86_64",
"kernelArch": "x86_64",
"architecture": "amd64",
"locale": "en-US",
"timezone": "Europe/Bucharest",
"country": "Romania",
"city": "Bucharest",
"clientVersion": "1.4.0",
"appVersion": "desktop-1.4.0",
"sessionID": "session-123",
"userID": "anonymous-user",
"extra": {
"gpuDriver": "Mesa 25.0.3",
"desktopEnvironment": "KDE Plasma"
}
},
"benchmark": {
"config": {
"durationSecs": 10,
"intensity": 1,
"coreFilter": 0,
"multiCore": true
},
"cpuInfo": {
"brandString": "AMD Ryzen 9 9950X3D 16-Core Processor",
"vendorID": "AuthenticAMD",
"physicalCores": 16,
"logicalCores": 32,
"baseClockMHz": 5756,
"boostClockMHz": 0,
"l1DataKB": 48,
"l2KB": 1024,
"l3MB": 32,
"isHybrid": false,
"has3DVCache": true,
"supportedFeatures": ["SSE4.2", "AVX", "AVX2"]
},
"startedAt": "2026-04-17T13:20:09.170770511+03:00",
"duration": 10003356676,
"totalOps": 543678267392,
"mOpsPerSec": 54349.58334499758,
"score": 5434958,
"coreResults": [
{
"logicalID": 0,
"coreType": "Standard",
"mOpsPerSec": 1642.986525056302,
"totalOps": 16435380224
}
]
}
}
```
## Multipart format
Multipart requests may include:
- `submitter`: text field
- `platform`: text field
- `systemInfo`: JSON text field
- `benchmark`, `file`, or `benchmarkFile`: benchmark JSON file field
Example `systemInfo` field value:
```json
{
"osName": "Windows",
"osVersion": "11 24H2",
"kernelVersion": "10.0.26100",
"architecture": "amd64",
"locale": "en-US",
"timezone": "America/New_York"
}
```
## Notes
- `systemInfo` is optional. Older clients do not need to send it.
- The server may enrich stored analytics with request metadata such as `ipAddress`, `forwardedFor`, `userAgent`, and `locale`.
- `baseClockMHz` and related CPU clock fields currently expect integer MHz values.
- Full JSON Schema is available in [submit-schema.json](./submit-schema.json) and is served by `GET /api/schema`.

306
docs/submit-schema.json Normal file
View File

@@ -0,0 +1,306 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://cpu-benchmarker.local/schemas/submit-schema.json",
"title": "CPU Benchmark Submission",
"description": "Request body for POST /api/submit. Older clients may omit systemInfo.",
"type": "object",
"additionalProperties": false,
"properties": {
"submitter": {
"type": "string",
"description": "Human-readable device or user label."
},
"platform": {
"type": "string",
"enum": ["windows", "linux", "macos"],
"description": "Normalized platform value."
},
"systemInfo": {
"$ref": "#/$defs/systemInfo"
},
"benchmark": {
"$ref": "#/$defs/benchmarkResult"
},
"result": {
"$ref": "#/$defs/benchmarkResult"
},
"data": {
"$ref": "#/$defs/benchmarkResult"
},
"config": {
"$ref": "#/$defs/benchmarkConfig"
},
"cpuInfo": {
"$ref": "#/$defs/cpuInfo"
},
"startedAt": {
"type": "string",
"format": "date-time"
},
"duration": {
"type": "integer",
"description": "Benchmark duration in nanoseconds."
},
"totalOps": {
"type": "integer"
},
"mOpsPerSec": {
"type": "number"
},
"score": {
"type": "integer"
},
"coreResults": {
"type": "array",
"items": {
"$ref": "#/$defs/coreResult"
}
}
},
"anyOf": [
{
"required": ["benchmark"]
},
{
"required": ["result"]
},
{
"required": ["data"]
},
{
"required": ["config", "cpuInfo", "startedAt", "duration", "totalOps", "mOpsPerSec", "score", "coreResults"]
}
],
"$defs": {
"benchmarkConfig": {
"type": "object",
"additionalProperties": false,
"properties": {
"durationSecs": {
"type": "integer",
"minimum": 1
},
"intensity": {
"type": "integer"
},
"coreFilter": {
"type": "integer"
},
"multiCore": {
"type": "boolean"
}
},
"required": ["durationSecs", "intensity", "coreFilter", "multiCore"]
},
"cpuInfo": {
"type": "object",
"additionalProperties": false,
"properties": {
"brandString": {
"type": "string"
},
"vendorID": {
"type": "string"
},
"physicalCores": {
"type": "integer",
"minimum": 0
},
"logicalCores": {
"type": "integer",
"minimum": 0
},
"baseClockMHz": {
"type": "integer",
"description": "Current server schema expects an integer MHz value."
},
"boostClockMHz": {
"type": "integer"
},
"l1DataKB": {
"type": "integer"
},
"l2KB": {
"type": "integer"
},
"l3MB": {
"type": "integer"
},
"isHybrid": {
"type": "boolean"
},
"has3DVCache": {
"type": "boolean"
},
"pCoreCount": {
"type": "integer"
},
"eCoreCount": {
"type": "integer"
},
"cores": {
"type": "array",
"items": {
"$ref": "#/$defs/cpuCoreDescriptor"
}
},
"supportedFeatures": {
"type": "array",
"items": {
"type": "string"
}
}
},
"required": ["brandString", "physicalCores", "logicalCores"]
},
"cpuCoreDescriptor": {
"type": "object",
"additionalProperties": false,
"properties": {
"LogicalID": {
"type": "integer"
},
"PhysicalID": {
"type": "integer"
},
"CoreID": {
"type": "integer"
},
"Type": {
"type": "integer"
}
},
"required": ["LogicalID", "PhysicalID", "CoreID", "Type"]
},
"coreResult": {
"type": "object",
"additionalProperties": false,
"properties": {
"logicalID": {
"type": "integer",
"minimum": 0
},
"coreType": {
"type": "string"
},
"mOpsPerSec": {
"type": "number",
"minimum": 0
},
"totalOps": {
"type": "integer",
"minimum": 0
}
},
"required": ["logicalID", "coreType", "mOpsPerSec", "totalOps"]
},
"benchmarkResult": {
"type": "object",
"additionalProperties": false,
"properties": {
"config": {
"$ref": "#/$defs/benchmarkConfig"
},
"cpuInfo": {
"$ref": "#/$defs/cpuInfo"
},
"startedAt": {
"type": "string",
"format": "date-time"
},
"duration": {
"type": "integer"
},
"totalOps": {
"type": "integer"
},
"mOpsPerSec": {
"type": "number"
},
"score": {
"type": "integer"
},
"coreResults": {
"type": "array",
"items": {
"$ref": "#/$defs/coreResult"
}
}
},
"required": ["config", "cpuInfo", "startedAt", "duration", "totalOps", "mOpsPerSec", "score", "coreResults"]
},
"systemInfo": {
"type": "object",
"description": "Optional analytics and environment metadata. Clients may omit this entire object.",
"additionalProperties": false,
"properties": {
"hostname": {
"type": "string"
},
"osName": {
"type": "string"
},
"osVersion": {
"type": "string"
},
"distro": {
"type": "string"
},
"kernelVersion": {
"type": "string"
},
"kernelArch": {
"type": "string"
},
"architecture": {
"type": "string"
},
"locale": {
"type": "string"
},
"timezone": {
"type": "string"
},
"region": {
"type": "string"
},
"country": {
"type": "string"
},
"city": {
"type": "string"
},
"isp": {
"type": "string"
},
"sessionID": {
"type": "string"
},
"userID": {
"type": "string"
},
"clientVersion": {
"type": "string"
},
"appVersion": {
"type": "string"
},
"ipAddress": {
"type": "string",
"description": "May be supplied by the client, and may also be enriched by the server from the request."
},
"forwardedFor": {
"type": "string"
},
"userAgent": {
"type": "string"
},
"extra": {
"type": "object",
"description": "Free-form extension point for future analytics.",
"additionalProperties": true
}
}
}
}
}

View File

@@ -4,6 +4,16 @@ Content-Type: application/json
{ {
"submitter": "Workstation-Lab-A", "submitter": "Workstation-Lab-A",
"platform": "windows", "platform": "windows",
"systemInfo": {
"hostname": "workstation-lab-a",
"osName": "Windows",
"osVersion": "11 24H2",
"kernelVersion": "10.0.26100",
"architecture": "amd64",
"locale": "en-US",
"timezone": "Europe/Bucharest",
"clientVersion": "1.0.0"
},
"benchmark": { "benchmark": {
"config": { "config": {
"durationSecs": 20, "durationSecs": 20,

View File

@@ -10,6 +10,10 @@ Content-Disposition: form-data; name="platform"
linux linux
--BenchBoundary --BenchBoundary
Content-Disposition: form-data; name="systemInfo"
{"hostname":"intel-test-rig","osName":"Ubuntu","osVersion":"24.04","kernelVersion":"6.8.0-58-generic","architecture":"amd64","locale":"en-US","timezone":"Europe/Bucharest","clientVersion":"1.0.0"}
--BenchBoundary
Content-Disposition: form-data; name="benchmark"; filename="cpu-bench-result.json" Content-Disposition: form-data; name="benchmark"; filename="cpu-bench-result.json"
Content-Type: application/json Content-Type: application/json

View File

@@ -39,6 +39,30 @@ type CPUCoreDescriptor struct {
Type int `json:"Type"` Type int `json:"Type"`
} }
type SystemInfo struct {
Hostname string `json:"hostname,omitempty"`
OSName string `json:"osName,omitempty"`
OSVersion string `json:"osVersion,omitempty"`
Distro string `json:"distro,omitempty"`
KernelVersion string `json:"kernelVersion,omitempty"`
KernelArch string `json:"kernelArch,omitempty"`
Architecture string `json:"architecture,omitempty"`
Locale string `json:"locale,omitempty"`
Timezone string `json:"timezone,omitempty"`
Region string `json:"region,omitempty"`
Country string `json:"country,omitempty"`
City string `json:"city,omitempty"`
ISP string `json:"isp,omitempty"`
SessionID string `json:"sessionID,omitempty"`
UserID string `json:"userID,omitempty"`
ClientVersion string `json:"clientVersion,omitempty"`
AppVersion string `json:"appVersion,omitempty"`
IPAddress string `json:"ipAddress,omitempty"`
ForwardedFor string `json:"forwardedFor,omitempty"`
UserAgent string `json:"userAgent,omitempty"`
Extra map[string]any `json:"extra,omitempty"`
}
type CoreResult struct { type CoreResult struct {
LogicalID int `json:"logicalID"` LogicalID int `json:"logicalID"`
CoreType string `json:"coreType"` CoreType string `json:"coreType"`
@@ -58,10 +82,11 @@ type BenchmarkResult struct {
} }
type Submission struct { type Submission struct {
SubmissionID string `json:"submissionID"` SubmissionID string `json:"submissionID"`
Submitter string `json:"submitter"` Submitter string `json:"submitter"`
Platform string `json:"platform"` Platform string `json:"platform"`
SubmittedAt time.Time `json:"submittedAt"` SubmittedAt time.Time `json:"submittedAt"`
SystemInfo *SystemInfo `json:"systemInfo,omitempty"`
BenchmarkResult BenchmarkResult
} }
@@ -158,6 +183,16 @@ func CloneSubmission(submission *Submission) *Submission {
if len(submission.CPUInfo.SupportedFeatures) > 0 { if len(submission.CPUInfo.SupportedFeatures) > 0 {
copySubmission.CPUInfo.SupportedFeatures = append([]string(nil), submission.CPUInfo.SupportedFeatures...) copySubmission.CPUInfo.SupportedFeatures = append([]string(nil), submission.CPUInfo.SupportedFeatures...)
} }
if submission.SystemInfo != nil {
copySystemInfo := *submission.SystemInfo
if len(submission.SystemInfo.Extra) > 0 {
copySystemInfo.Extra = make(map[string]any, len(submission.SystemInfo.Extra))
for key, value := range submission.SystemInfo.Extra {
copySystemInfo.Extra[key] = value
}
}
copySubmission.SystemInfo = &copySystemInfo
}
return &copySubmission return &copySubmission
} }

View File

@@ -66,12 +66,13 @@ func (s *Store) Count() int {
return len(s.orderedIDs) return len(s.orderedIDs)
} }
func (s *Store) SaveSubmission(result model.BenchmarkResult, submitter, platform string) (*model.Submission, error) { func (s *Store) SaveSubmission(result model.BenchmarkResult, submitter, platform string, systemInfo *model.SystemInfo) (*model.Submission, error) {
submission := &model.Submission{ submission := &model.Submission{
SubmissionID: uuid.NewString(), SubmissionID: uuid.NewString(),
Submitter: model.NormalizeSubmitter(submitter), Submitter: model.NormalizeSubmitter(submitter),
Platform: model.NormalizePlatform(platform), Platform: model.NormalizePlatform(platform),
SubmittedAt: time.Now().UTC(), SubmittedAt: time.Now().UTC(),
SystemInfo: cloneSystemInfo(systemInfo),
BenchmarkResult: result, BenchmarkResult: result,
} }
@@ -97,6 +98,22 @@ func (s *Store) SaveSubmission(result model.BenchmarkResult, submitter, platform
return model.CloneSubmission(submission), nil return model.CloneSubmission(submission), nil
} }
func cloneSystemInfo(info *model.SystemInfo) *model.SystemInfo {
if info == nil {
return nil
}
copyInfo := *info
if len(info.Extra) > 0 {
copyInfo.Extra = make(map[string]any, len(info.Extra))
for key, value := range info.Extra {
copyInfo.Extra[key] = value
}
}
return &copyInfo
}
func (s *Store) ListSubmissions(page, pageSize int) ([]model.Submission, int) { func (s *Store) ListSubmissions(page, pageSize int) ([]model.Submission, int) {
s.mu.RLock() s.mu.RLock()
defer s.mu.RUnlock() defer s.mu.RUnlock()

View File

@@ -5,11 +5,16 @@ import (
"fmt" "fmt"
"html/template" "html/template"
"io" "io"
"log"
"mime" "mime"
"net"
"net/http" "net/http"
"net/url" "net/url"
"os"
"path/filepath"
"strconv" "strconv"
"strings" "strings"
"sync"
"time" "time"
"cpu-benchmark-server/lib/model" "cpu-benchmark-server/lib/model"
@@ -25,6 +30,7 @@ type App struct {
store *store.Store store *store.Store
templates *template.Template templates *template.Template
pageSize int pageSize int
errorLog *dailyErrorLogger
} }
type indexPageData struct { type indexPageData struct {
@@ -46,16 +52,17 @@ type indexPageData struct {
} }
type jsonSubmissionEnvelope struct { type jsonSubmissionEnvelope struct {
Submitter string `json:"submitter"` Submitter string `json:"submitter"`
Platform string `json:"platform"` Platform string `json:"platform"`
Benchmark *model.BenchmarkResult `json:"benchmark"` Benchmark json.RawMessage `json:"benchmark"`
Result *model.BenchmarkResult `json:"result"` Result json.RawMessage `json:"result"`
Data *model.BenchmarkResult `json:"data"` Data json.RawMessage `json:"data"`
} }
type flatSubmissionEnvelope struct { type flatSubmissionEnvelope struct {
Submitter string `json:"submitter"` Submitter string `json:"submitter"`
Platform string `json:"platform"` Platform string `json:"platform"`
SystemInfo *model.SystemInfo `json:"systemInfo,omitempty"`
model.BenchmarkResult model.BenchmarkResult
} }
@@ -63,7 +70,7 @@ type errorResponse struct {
Error string `json:"error"` Error string `json:"error"`
} }
func New(store *store.Store, pageSize int) (*App, error) { func New(store *store.Store, pageSize int, errorLogDir string) (*App, error) {
funcs := template.FuncMap{ funcs := template.FuncMap{
"formatInt64": formatInt64, "formatInt64": formatInt64,
"formatFloat": formatFloat, "formatFloat": formatFloat,
@@ -76,10 +83,16 @@ func New(store *store.Store, pageSize int) (*App, error) {
return nil, err return nil, err
} }
errorLog, err := newDailyErrorLogger(errorLogDir)
if err != nil {
return nil, err
}
return &App{ return &App{
store: store, store: store,
templates: templates, templates: templates,
pageSize: pageSize, pageSize: pageSize,
errorLog: errorLog,
}, nil }, nil
} }
@@ -87,18 +100,176 @@ func (a *App) Routes() http.Handler {
router := chi.NewRouter() router := chi.NewRouter()
router.Use(middleware.RequestID) router.Use(middleware.RequestID)
router.Use(middleware.RealIP) router.Use(middleware.RealIP)
router.Use(a.errorLoggingMiddleware)
router.Use(middleware.Logger) router.Use(middleware.Logger)
router.Use(middleware.Recoverer) router.Use(middleware.Recoverer)
router.Use(middleware.Timeout(30 * time.Second)) router.Use(middleware.Timeout(30 * time.Second))
router.Get("/", a.handleIndex) router.Get("/", a.handleIndex)
router.Get("/healthz", a.handleHealth) router.Get("/healthz", a.handleHealth)
router.Get("/api/schema", a.handleSubmitSchema)
router.Get("/api/search", a.handleSearch) router.Get("/api/search", a.handleSearch)
router.Post("/api/submit", a.handleSubmit) router.Post("/api/submit", a.handleSubmit)
return router return router
} }
func (a *App) Close() error {
if a == nil || a.errorLog == nil {
return nil
}
return a.errorLog.Close()
}
type dailyErrorLogger struct {
dir string
mu sync.Mutex
date string
file *os.File
logger *log.Logger
}
const maxLoggedResponseBodyBytes = 2048
func newDailyErrorLogger(dir string) (*dailyErrorLogger, error) {
l := &dailyErrorLogger{dir: dir}
if err := l.rotateIfNeeded(time.Now()); err != nil {
return nil, err
}
return l, nil
}
func (l *dailyErrorLogger) rotateIfNeeded(now time.Time) error {
date := now.Format("2006-01-02")
if l.file != nil && l.date == date {
return nil
}
if err := os.MkdirAll(l.dir, 0o755); err != nil {
return err
}
path := filepath.Join(l.dir, date+".error.log")
file, err := os.OpenFile(path, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0o644)
if err != nil {
return err
}
if l.file != nil {
_ = l.file.Close()
}
l.date = date
l.file = file
l.logger = log.New(file, "", log.LstdFlags)
return nil
}
func (l *dailyErrorLogger) LogRequest(r *http.Request, status int, duration time.Duration, responseBody string) {
l.mu.Lock()
defer l.mu.Unlock()
now := time.Now()
if err := l.rotateIfNeeded(now); err != nil {
log.Printf("error log rotation failed: %v", err)
return
}
l.logger.Printf(
`status=%d method=%s path=%q query=%q ip=%q request_id=%q duration=%s content_type=%q content_length=%d user_agent=%q response_body=%q`,
status,
r.Method,
r.URL.Path,
r.URL.RawQuery,
r.RemoteAddr,
middleware.GetReqID(r.Context()),
duration,
r.Header.Get("Content-Type"),
r.ContentLength,
r.UserAgent(),
responseBody,
)
}
func (l *dailyErrorLogger) Close() error {
l.mu.Lock()
defer l.mu.Unlock()
if l.file == nil {
return nil
}
err := l.file.Close()
l.file = nil
l.logger = nil
l.date = ""
return err
}
type statusCapturingResponseWriter struct {
http.ResponseWriter
status int
body strings.Builder
bodyTruncated bool
}
func (w *statusCapturingResponseWriter) WriteHeader(status int) {
w.status = status
w.ResponseWriter.WriteHeader(status)
}
func (w *statusCapturingResponseWriter) Write(b []byte) (int, error) {
if w.status == 0 {
w.status = http.StatusOK
}
if w.body.Len() < maxLoggedResponseBodyBytes {
remaining := maxLoggedResponseBodyBytes - w.body.Len()
if len(b) > remaining {
_, _ = w.body.Write(b[:remaining])
w.bodyTruncated = true
} else {
_, _ = w.body.Write(b)
}
} else {
w.bodyTruncated = true
}
return w.ResponseWriter.Write(b)
}
func (w *statusCapturingResponseWriter) LoggedBody() string {
body := strings.TrimSpace(w.body.String())
if body == "" {
return ""
}
if w.bodyTruncated {
return body + "... [truncated]"
}
return body
}
func (a *App) errorLoggingMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
startedAt := time.Now()
ww := &statusCapturingResponseWriter{ResponseWriter: w}
next.ServeHTTP(ww, r)
status := ww.status
if status == 0 {
status = http.StatusOK
}
if status >= http.StatusBadRequest {
a.errorLog.LogRequest(r, status, time.Since(startedAt), ww.LoggedBody())
}
})
}
func (a *App) handleIndex(w http.ResponseWriter, r *http.Request) { func (a *App) handleIndex(w http.ResponseWriter, r *http.Request) {
page := parsePositiveInt(r.URL.Query().Get("page"), 1) page := parsePositiveInt(r.URL.Query().Get("page"), 1)
text := strings.TrimSpace(r.URL.Query().Get("text")) text := strings.TrimSpace(r.URL.Query().Get("text"))
@@ -160,6 +331,10 @@ func (a *App) handleHealth(w http.ResponseWriter, _ *http.Request) {
}) })
} }
func (a *App) handleSubmitSchema(w http.ResponseWriter, r *http.Request) {
http.ServeFileFS(w, r, os.DirFS("."), "docs/submit-schema.json")
}
func (a *App) handleSearch(w http.ResponseWriter, r *http.Request) { func (a *App) handleSearch(w http.ResponseWriter, r *http.Request) {
results := a.store.SearchSubmissions( results := a.store.SearchSubmissions(
r.URL.Query().Get("text"), r.URL.Query().Get("text"),
@@ -176,7 +351,7 @@ func (a *App) handleSearch(w http.ResponseWriter, r *http.Request) {
func (a *App) handleSubmit(w http.ResponseWriter, r *http.Request) { func (a *App) handleSubmit(w http.ResponseWriter, r *http.Request) {
r.Body = http.MaxBytesReader(w, r.Body, maxSubmissionBytes) r.Body = http.MaxBytesReader(w, r.Body, maxSubmissionBytes)
result, submitter, platform, err := parseSubmissionRequest(r) result, submitter, platform, systemInfo, err := parseSubmissionRequest(r)
if err != nil { if err != nil {
writeJSON(w, http.StatusBadRequest, errorResponse{Error: err.Error()}) writeJSON(w, http.StatusBadRequest, errorResponse{Error: err.Error()})
return return
@@ -193,7 +368,9 @@ func (a *App) handleSubmit(w http.ResponseWriter, r *http.Request) {
return return
} }
submission, err := a.store.SaveSubmission(result, submitter, platform) systemInfo = enrichSystemInfo(systemInfo, r)
submission, err := a.store.SaveSubmission(result, submitter, platform, systemInfo)
if err != nil { if err != nil {
writeJSON(w, http.StatusInternalServerError, errorResponse{Error: fmt.Sprintf("store submission: %v", err)}) writeJSON(w, http.StatusInternalServerError, errorResponse{Error: fmt.Sprintf("store submission: %v", err)})
return return
@@ -208,11 +385,11 @@ func (a *App) handleSubmit(w http.ResponseWriter, r *http.Request) {
}) })
} }
func parseSubmissionRequest(r *http.Request) (model.BenchmarkResult, string, string, error) { func parseSubmissionRequest(r *http.Request) (model.BenchmarkResult, string, string, *model.SystemInfo, error) {
contentType := r.Header.Get("Content-Type") contentType := r.Header.Get("Content-Type")
mediaType, _, err := mime.ParseMediaType(contentType) mediaType, _, err := mime.ParseMediaType(contentType)
if err != nil && contentType != "" { if err != nil && contentType != "" {
return model.BenchmarkResult{}, "", "", fmt.Errorf("parse content type: %w", err) return model.BenchmarkResult{}, "", "", nil, fmt.Errorf("parse content type: %w", err)
} }
switch mediaType { switch mediaType {
@@ -221,14 +398,14 @@ func parseSubmissionRequest(r *http.Request) (model.BenchmarkResult, string, str
case "multipart/form-data": case "multipart/form-data":
return parseMultipartSubmission(r) return parseMultipartSubmission(r)
default: default:
return model.BenchmarkResult{}, "", "", fmt.Errorf("unsupported content type %q", mediaType) return model.BenchmarkResult{}, "", "", nil, fmt.Errorf("unsupported content type %q", mediaType)
} }
} }
func parseJSONSubmission(r *http.Request) (model.BenchmarkResult, string, string, error) { func parseJSONSubmission(r *http.Request) (model.BenchmarkResult, string, string, *model.SystemInfo, error) {
body, err := io.ReadAll(r.Body) body, err := io.ReadAll(r.Body)
if err != nil { if err != nil {
return model.BenchmarkResult{}, "", "", fmt.Errorf("read request body: %w", err) return model.BenchmarkResult{}, "", "", nil, fmt.Errorf("read request body: %w", err)
} }
submitter := firstNonEmpty( submitter := firstNonEmpty(
@@ -244,37 +421,60 @@ func parseJSONSubmission(r *http.Request) (model.BenchmarkResult, string, string
if err := json.Unmarshal(body, &nested); err == nil { if err := json.Unmarshal(body, &nested); err == nil {
submitter = firstNonEmpty(nested.Submitter, submitter) submitter = firstNonEmpty(nested.Submitter, submitter)
platform = firstNonEmpty(nested.Platform, platform) platform = firstNonEmpty(nested.Platform, platform)
for _, candidate := range []*model.BenchmarkResult{nested.Benchmark, nested.Result, nested.Data} { systemInfo, err := extractSystemInfo(body)
if candidate != nil { if err != nil {
return *candidate, submitter, platform, nil return model.BenchmarkResult{}, "", "", nil, err
}
for _, candidate := range []struct {
name string
payload json.RawMessage
}{
{name: "benchmark", payload: nested.Benchmark},
{name: "result", payload: nested.Result},
{name: "data", payload: nested.Data},
} {
if len(candidate.payload) == 0 || string(candidate.payload) == "null" {
continue
} }
var result model.BenchmarkResult
if err := json.Unmarshal(candidate.payload, &result); err != nil {
return model.BenchmarkResult{}, "", "", nil, fmt.Errorf("decode %s JSON: %w", candidate.name, err)
}
return result, submitter, platform, systemInfo, nil
} }
} }
var flat flatSubmissionEnvelope var flat flatSubmissionEnvelope
if err := json.Unmarshal(body, &flat); err != nil { if err := json.Unmarshal(body, &flat); err != nil {
return model.BenchmarkResult{}, "", "", fmt.Errorf("decode benchmark JSON: %w", err) return model.BenchmarkResult{}, "", "", nil, fmt.Errorf("decode benchmark JSON: %w", err)
} }
return flat.BenchmarkResult, firstNonEmpty(flat.Submitter, submitter), firstNonEmpty(flat.Platform, platform, "windows"), nil return flat.BenchmarkResult, firstNonEmpty(flat.Submitter, submitter), firstNonEmpty(flat.Platform, platform, "windows"), cloneSystemInfo(flat.SystemInfo), nil
} }
func parseMultipartSubmission(r *http.Request) (model.BenchmarkResult, string, string, error) { func parseMultipartSubmission(r *http.Request) (model.BenchmarkResult, string, string, *model.SystemInfo, error) {
if err := r.ParseMultipartForm(maxSubmissionBytes); err != nil { if err := r.ParseMultipartForm(maxSubmissionBytes); err != nil {
return model.BenchmarkResult{}, "", "", fmt.Errorf("parse multipart form: %w", err) return model.BenchmarkResult{}, "", "", nil, fmt.Errorf("parse multipart form: %w", err)
} }
payload, err := readMultipartPayload(r) payload, err := readMultipartPayload(r)
if err != nil { if err != nil {
return model.BenchmarkResult{}, "", "", err return model.BenchmarkResult{}, "", "", nil, err
} }
var result model.BenchmarkResult var result model.BenchmarkResult
if err := json.Unmarshal(payload, &result); err != nil { if err := json.Unmarshal(payload, &result); err != nil {
return model.BenchmarkResult{}, "", "", fmt.Errorf("decode benchmark JSON: %w", err) return model.BenchmarkResult{}, "", "", nil, fmt.Errorf("decode benchmark JSON: %w", err)
} }
return result, r.FormValue("submitter"), firstNonEmpty(r.FormValue("platform"), "windows"), nil systemInfo, err := parseMultipartSystemInfo(r)
if err != nil {
return model.BenchmarkResult{}, "", "", nil, err
}
return result, r.FormValue("submitter"), firstNonEmpty(r.FormValue("platform"), "windows"), systemInfo, nil
} }
func readMultipartPayload(r *http.Request) ([]byte, error) { func readMultipartPayload(r *http.Request) ([]byte, error) {
@@ -304,6 +504,99 @@ func readMultipartPayload(r *http.Request) ([]byte, error) {
return nil, fmt.Errorf("multipart request must include benchmark JSON in a file field or text field named benchmark") return nil, fmt.Errorf("multipart request must include benchmark JSON in a file field or text field named benchmark")
} }
func extractSystemInfo(body []byte) (*model.SystemInfo, error) {
var envelope struct {
SystemInfo *model.SystemInfo `json:"systemInfo"`
}
if err := json.Unmarshal(body, &envelope); err != nil {
return nil, fmt.Errorf("decode systemInfo JSON: %w", err)
}
return cloneSystemInfo(envelope.SystemInfo), nil
}
func parseMultipartSystemInfo(r *http.Request) (*model.SystemInfo, error) {
raw := strings.TrimSpace(r.FormValue("systemInfo"))
if raw == "" {
return nil, nil
}
var info model.SystemInfo
if err := json.Unmarshal([]byte(raw), &info); err != nil {
return nil, fmt.Errorf("decode systemInfo JSON: %w", err)
}
return &info, nil
}
func enrichSystemInfo(info *model.SystemInfo, r *http.Request) *model.SystemInfo {
if info == nil {
info = &model.SystemInfo{}
} else {
info = cloneSystemInfo(info)
}
if host, _, err := net.SplitHostPort(r.RemoteAddr); err == nil {
info.IPAddress = firstNonEmpty(info.IPAddress, host)
} else {
info.IPAddress = firstNonEmpty(info.IPAddress, r.RemoteAddr)
}
info.ForwardedFor = firstNonEmpty(info.ForwardedFor, r.Header.Get("X-Forwarded-For"))
info.UserAgent = firstNonEmpty(info.UserAgent, r.UserAgent())
info.Locale = firstNonEmpty(info.Locale, r.Header.Get("Accept-Language"))
if isEmptySystemInfo(info) {
return nil
}
return info
}
func isEmptySystemInfo(info *model.SystemInfo) bool {
if info == nil {
return true
}
return strings.TrimSpace(info.Hostname) == "" &&
strings.TrimSpace(info.OSName) == "" &&
strings.TrimSpace(info.OSVersion) == "" &&
strings.TrimSpace(info.Distro) == "" &&
strings.TrimSpace(info.KernelVersion) == "" &&
strings.TrimSpace(info.KernelArch) == "" &&
strings.TrimSpace(info.Architecture) == "" &&
strings.TrimSpace(info.Locale) == "" &&
strings.TrimSpace(info.Timezone) == "" &&
strings.TrimSpace(info.Region) == "" &&
strings.TrimSpace(info.Country) == "" &&
strings.TrimSpace(info.City) == "" &&
strings.TrimSpace(info.ISP) == "" &&
strings.TrimSpace(info.SessionID) == "" &&
strings.TrimSpace(info.UserID) == "" &&
strings.TrimSpace(info.ClientVersion) == "" &&
strings.TrimSpace(info.AppVersion) == "" &&
strings.TrimSpace(info.IPAddress) == "" &&
strings.TrimSpace(info.ForwardedFor) == "" &&
strings.TrimSpace(info.UserAgent) == "" &&
len(info.Extra) == 0
}
func cloneSystemInfo(info *model.SystemInfo) *model.SystemInfo {
if info == nil {
return nil
}
copyInfo := *info
if len(info.Extra) > 0 {
copyInfo.Extra = make(map[string]any, len(info.Extra))
for key, value := range info.Extra {
copyInfo.Extra[key] = value
}
}
return &copyInfo
}
func parsePositiveInt(raw string, fallback int) int { func parsePositiveInt(raw string, fallback int) int {
if raw == "" { if raw == "" {
return fallback return fallback

10
main.go
View File

@@ -29,6 +29,9 @@ func run(logger *log.Logger) error {
if err := os.MkdirAll(cfg.BadgerDir, 0o755); err != nil { if err := os.MkdirAll(cfg.BadgerDir, 0o755); err != nil {
return err return err
} }
if err := os.MkdirAll("data/logs", 0o755); err != nil {
return err
}
benchmarkStore, err := store.Open(cfg.BadgerDir) benchmarkStore, err := store.Open(cfg.BadgerDir)
if err != nil { if err != nil {
@@ -43,10 +46,15 @@ func run(logger *log.Logger) error {
} }
defer closeOnce.Do(closeStore) defer closeOnce.Do(closeStore)
app, err := web.New(benchmarkStore, cfg.PageSize) app, err := web.New(benchmarkStore, cfg.PageSize, "data/logs")
if err != nil { if err != nil {
return err return err
} }
defer func() {
if err := app.Close(); err != nil {
logger.Printf("close error log: %v", err)
}
}()
server := &http.Server{ server := &http.Server{
Addr: cfg.Addr, Addr: cfg.Addr,