Skip to content

Commit

Permalink
feat: load configs from admin api
Browse files Browse the repository at this point in the history
  • Loading branch information
nick-bisonai committed Jul 11, 2024
1 parent 46be64c commit 00ad3b5
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 28 deletions.
3 changes: 3 additions & 0 deletions node/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ DAL_API_PORT=
# (required)
API_KEY=

# (required)
ORAKL_NODE_ADMIN_URL=

# (required)
# DATABASE_URL=
# REDIS_HOST=
Expand Down
10 changes: 4 additions & 6 deletions node/pkg/dal/api/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,17 @@ import (
"bisonai.com/orakl/node/pkg/dal/collector"
dalcommon "bisonai.com/orakl/node/pkg/dal/common"
"bisonai.com/orakl/node/pkg/db"
"bisonai.com/orakl/node/pkg/utils/request"
"github.com/gofiber/contrib/websocket"
"github.com/gofiber/fiber/v2"
"github.com/rs/zerolog/log"
)

var ApiController Controller

func Setup(ctx context.Context) error {
configs, err := db.QueryRows[types.Config](ctx, "SELECT * FROM configs", nil)
if err != nil {
log.Error().Err(err).Msg("failed to get configs")
return err
}
func Setup(ctx context.Context, adminEndpoint string) error {
configs, err := request.Request[[]types.Config](request.WithEndpoint(adminEndpoint + "/config"))

Check failure on line 23 in node/pkg/dal/api/controller.go

View workflow job for this annotation

GitHub Actions / core-build

ineffectual assignment to err (ineffassign)

configMap := make(map[string]types.Config)
for _, config := range configs {
configMap[config.Name] = config
Expand Down
9 changes: 8 additions & 1 deletion node/pkg/dal/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dal

import (
"context"
"errors"
"os"

"bisonai.com/orakl/node/pkg/dal/api"
Expand All @@ -18,8 +19,14 @@ func Run(ctx context.Context) error {
log.Error().Err(err).Msg("Failed to setup DAL API server")
return err
}
defer app.Shutdown()

Check failure on line 22 in node/pkg/dal/app.go

View workflow job for this annotation

GitHub Actions / core-build

Error return value of `app.Shutdown` is not checked (errcheck)

err = api.Setup(ctx)
adminEndpoint := os.Getenv("ORAKL_NODE_ADMIN_URL")
if adminEndpoint == "" {
return errors.New("ORAKL_NODE_ADMIN_URL is not set")
}

err = api.Setup(ctx, adminEndpoint)
if err != nil {
log.Error().Err(err).Msg("Failed to setup DAL API server")
return err
Expand Down
43 changes: 22 additions & 21 deletions node/pkg/dal/tests/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ package test

import (
"context"
"net/http"
"net/http/httptest"
"os"
"testing"
"time"
Expand All @@ -26,6 +28,7 @@ type TestItems struct {
Controller *api.Controller
Collector *collector.Collector
TmpConfig types.Config
MockAdmin *httptest.Server
}

func testPublishData(ctx context.Context, submissionData aggregator.SubmissionData) error {
Expand Down Expand Up @@ -66,33 +69,35 @@ func generateSampleSubmissionData(configId int32, value int64, timestamp time.Ti
func setup(ctx context.Context) (func() error, *TestItems, error) {
var testItems = new(TestItems)

err := db.QueryWithoutResult(ctx, "DELETE FROM configs", nil)
if err != nil {
log.Error().Err(err).Msg("error deleting config")
return nil, nil, err
mockAdminServer := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
rw.Write([]byte(`[{

Check failure on line 73 in node/pkg/dal/tests/main_test.go

View workflow job for this annotation

GitHub Actions / core-build

Error return value of `rw.Write` is not checked (errcheck)
"id": 0,
"name": "test-aggregate",
"fetchInterval": 15000,
"aggregateInterval": 15000,
"submitInterval": 15000}]`))
}))

testItems.TmpConfig = types.Config{
ID: 0,
Name: "test-aggregate",
FetchInterval: 15000,
AggregateInterval: 15000,
SubmitInterval: 15000,
}

tmpConfig, err := db.QueryRow[types.Config](
ctx,
`INSERT INTO configs (name, fetch_interval, aggregate_interval, submit_interval) VALUES (@name, @fetch_interval, @aggregate_interval, @submit_interval) RETURNING name, id, submit_interval, aggregate_interval, fetch_interval;`,
map[string]any{"name": "test-aggregate", "submit_interval": 15000, "fetch_interval": 15000, "aggregate_interval": 15000})
if err != nil {
log.Error().Err(err).Msg("error inserting config 0")
return nil, nil, err
}
testItems.TmpConfig = tmpConfig

app, err := utils.Setup(ctx)
if err != nil {
return nil, nil, err
}
testItems.App = app
err = api.Setup(ctx)
err = api.Setup(ctx, mockAdminServer.URL)
if err != nil {
return nil, nil, err
}
testItems.Controller = &api.ApiController
testItems.Collector = api.ApiController.Collector
testItems.MockAdmin = mockAdminServer

v1 := app.Group("/api/v1")
v1.Get("/", func(c *fiber.Ctx) error {
Expand All @@ -105,12 +110,7 @@ func setup(ctx context.Context) (func() error, *TestItems, error) {

func cleanup(ctx context.Context, testItems *TestItems) func() error {
return func() error {
err := db.QueryWithoutResult(ctx, "DELETE FROM configs", nil)
if err != nil {
log.Error().Err(err).Msg("error deleting config")
return err
}
err = testItems.App.Shutdown()
err := testItems.App.Shutdown()
if err != nil {
log.Error().Err(err).Msg("error shutting down app")
return err
Expand All @@ -120,6 +120,7 @@ func cleanup(ctx context.Context, testItems *TestItems) func() error {

testItems.Controller = nil
testItems.Collector = nil
testItems.MockAdmin.Close()
return nil
}
}
Expand Down

0 comments on commit 00ad3b5

Please sign in to comment.