Skip to content

Commit

Permalink
feat(ui): first shitty content-type graph
Browse files Browse the repository at this point in the history
  • Loading branch information
philwinder committed Apr 26, 2023
1 parent 7f2cf63 commit 2f1f647
Show file tree
Hide file tree
Showing 17 changed files with 628 additions and 95 deletions.
92 changes: 92 additions & 0 deletions api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,39 @@ paths:
$ref: '#/components/schemas/failure'
description: "[Not found](https://jsonapi.org/format/#fetching-resources-responses-404)"
summary: Get a job by id
/v0/analytics/results/{result_metadata_key}:
get:
description: get result statistics by key
parameters:
- description: size of page for paginated results
in: query
name: "page[size]"
required: false
schema:
format: int32
type: integer
- in: path
name: result_metadata_key
required: true
schema:
type: string
responses:
"200":
content:
application/vnd.api+json:
schema:
$ref: '#/components/schemas/ResultCollection'
text/html:
schema:
example: <html><body>Body text</body></html>
type: string
description: "[OK](https://jsonapi.org/format/#fetching-resources-responses-200)"
"404":
content:
application/vnd.api+json:
schema:
$ref: '#/components/schemas/failure'
description: "[Not found](https://jsonapi.org/format/#fetching-resources-responses-404)"
components:
parameters:
sort:
Expand Down Expand Up @@ -978,6 +1011,65 @@ components:
properties:
data:
$ref: '#/components/schemas/JobSpec'
ResultCollection:
additionalProperties: false
properties:
data:
items:
$ref: '#/components/schemas/ResultDatum'
type: array
uniqueItems: true
meta:
additionalProperties: true
description: Non-standard meta-information that can not be represented as
an attribute or relationship.
type: object
links:
$ref: '#/components/schemas/PaginationLinks'
jsonapi:
$ref: '#/components/schemas/jsonapi'
required:
- data
type: object
ResultCollectionData:
items:
$ref: '#/components/schemas/ResultDatum'
type: array
uniqueItems: true
ResultDatum:
additionalProperties: false
properties:
type:
description: "[resource object type](https://jsonapi.org/format/#document-resource-object-identification)"
type: string
id:
description: "[resource object identifier](https://jsonapi.org/format/#document-resource-object-identification)"
type: string
attributes:
additionalProperties: false
description: Members of the attributes object (`attributes`) represent information
about the resource object in which it's defined.
type: object
relationships:
additionalProperties:
$ref: '#/components/schemas/relationship'
description: "Members of the relationships object represent references from\
\ the resource object in which it's defined to other resource objects.\
\ N.B. this is validation, not useful for inclusion."
type: object
links:
additionalProperties:
$ref: '#/components/schemas/link'
type: object
meta:
additionalProperties: true
description: Non-standard meta-information that can not be represented as
an attribute or relationship.
type: object
required:
- id
- type
type: object
PaginationLinks:
allOf:
- $ref: '#/components/schemas/links'
Expand Down
6 changes: 5 additions & 1 deletion cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"
"time"

"github.com/bacalhau-project/amplify/pkg/analytics"
"github.com/bacalhau-project/amplify/pkg/api"
"github.com/bacalhau-project/amplify/pkg/cli"
"github.com/bacalhau-project/amplify/pkg/dag"
Expand Down Expand Up @@ -118,8 +119,11 @@ func executeServeCommand(appContext cli.AppContext) runEFunc {
return err
}

// AnalyticsRepository manages amplify analytics
analyticsRepository := analytics.NewAnalyticsRepository(persistenceImpl.(db.Analytics))

// AmplifyAPI provides the REST API
amplifyAPI, err := api.NewAmplifyAPI(queueRepository, taskFactory)
amplifyAPI, err := api.NewAmplifyAPI(queueRepository, taskFactory, analyticsRepository)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ require (
github.com/multiformats/go-varint v0.0.7 // indirect
github.com/pelletier/go-toml/v2 v2.0.7 // indirect
github.com/perimeterx/marshmallow v1.1.4 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pkg/errors v0.9.1
github.com/polydawn/refmt v0.89.0 // indirect
github.com/ricochet2200/go-disk-usage/du v0.0.0-20210707232629-ac9918953285 // indirect
github.com/rubenv/sql-migrate v1.4.0
Expand Down
54 changes: 54 additions & 0 deletions pkg/analytics/analytics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package analytics

import (
"context"
"fmt"

"github.com/bacalhau-project/amplify/pkg/db"
"github.com/pkg/errors"
)

var (
ErrAnalyticsErr = fmt.Errorf("analytics error")
ErrInvalidPageSize = errors.Wrap(ErrAnalyticsErr, "invalid page size")
ErrInvalidKey = errors.Wrap(ErrAnalyticsErr, "invalid key")
)

type analyticsRepository struct {
database db.Analytics
}
type AnalyticsRepository interface {
QueryTopResultsByKey(ctx context.Context, params QueryTopResultsByKeyParams) (map[string]interface{}, error)
}

func NewAnalyticsRepository(d db.Analytics) AnalyticsRepository {
return &analyticsRepository{
database: d,
}
}

type QueryTopResultsByKeyParams struct {
Key string
PageSize int
}

func (r *analyticsRepository) QueryTopResultsByKey(ctx context.Context, params QueryTopResultsByKeyParams) (map[string]interface{}, error) {
if params.PageSize <= 0 {
return nil, ErrInvalidPageSize
}
if params.Key == "" {
return nil, ErrInvalidKey
}
rows, err := r.database.QueryTopResultsByKey(ctx, db.QueryTopResultsByKeyParams{
Key: params.Key,
PageSize: int32(params.PageSize),
})
if err != nil {
return nil, err
}
results := make(map[string]interface{}, params.PageSize)
for _, row := range rows {
results[row.Value] = row.Count
}
return results, nil
}
Loading

0 comments on commit 2f1f647

Please sign in to comment.