Skip to content

Commit

Permalink
fix: move utility functions to utils package
Browse files Browse the repository at this point in the history
  • Loading branch information
nick-bisonai committed Feb 20, 2024
1 parent 5c1aa1e commit a1698bb
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 70 deletions.
4 changes: 2 additions & 2 deletions node/pkg/fetcher/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (f *Fetcher) runAdapter(ctx context.Context) error {
if err != nil {
return err
}
aggregated := getAvg(result)
aggregated := utils.GetFloatAvg(result)
err = f.insertPgsql(ctx, adapter.Name, aggregated)
if err != nil {
return err
Expand Down Expand Up @@ -85,7 +85,7 @@ func (f *Fetcher) fetch(adapter AdapterDetail) ([]float64, error) {
continue
}

result, err := ReduceAll(res, definition.Reducers)
result, err := utils.Reduce(res, definition.Reducers)
if err != nil {
fmt.Println(err)
continue
Expand Down
21 changes: 0 additions & 21 deletions node/pkg/fetcher/local_aggregator.go

This file was deleted.

8 changes: 2 additions & 6 deletions node/pkg/fetcher/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"time"

"bisonai.com/orakl/node/pkg/bus"
"bisonai.com/orakl/node/pkg/utils"
)

const (
Expand Down Expand Up @@ -40,12 +41,7 @@ type Definition struct {
Url string `json:"url"`
Headers map[string]string `json:"headers"`
Method string `json:"method"`
Reducers []Reducer `json:"reducers"`
}

type Reducer struct {
Function string `json:"function"`
Args interface{} `json:"args"`
Reducers []utils.Reducer `json:"reducers"`
}

type Aggregate struct {
Expand Down
10 changes: 7 additions & 3 deletions node/pkg/fetcher/reducer.go → node/pkg/utils/reducer.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package fetcher
package utils

import (
"fmt"
"math"
"strconv"
)

func ReduceAll(raw interface{}, reducers []Reducer) (float64, error) {
type Reducer struct {
Function string `json:"function"`
Args interface{} `json:"args"`
}

func Reduce(raw interface{}, reducers []Reducer) (float64, error) {
var result float64
for _, reducer := range reducers {
var err error
Expand Down Expand Up @@ -37,7 +42,6 @@ func reduce(raw interface{}, reducer Reducer) (interface{}, error) {

return castedRaw[int(index)], nil
case "PARSE", "PATH":

args, ok := reducer.Args.([]interface{})
if !ok {
return nil, fmt.Errorf("cannot cast reducer.Args to []interface{}")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,43 +1,37 @@
package fetcher
package tests

import (
"encoding/json"
"testing"

"bisonai.com/orakl/node/pkg/utils"
"github.com/stretchr/testify/assert"
)

var sampleDefintion2 = `{
"url": "https://quotation-api-cdn.dunamu.com/v1/forex/recent?codes=FRX.KRWUSD",
"headers": {
"Content-Type": "application/json"
var reducers = `[
{
"function": "INDEX",
"args": 0
},
{
"function": "PARSE",
"args": [
"basePrice"
]
},
{
"function": "DIVFROM",
"args": 1
},
"method": "GET",
"reducers": [
{
"function": "INDEX",
"args": 0
},
{
"function": "PARSE",
"args": [
"basePrice"
]
},
{
"function": "DIVFROM",
"args": 1
},
{
"function": "POW10",
"args": 8
},
{
"function": "ROUND"
}
]
}`
var sampleResult2 = `[
{
"function": "POW10",
"args": 8
},
{
"function": "ROUND"
}
]`
var sampleResult = `[
{
"code": "FRX.KRWUSD",
"currencyCode": "USD",
Expand Down Expand Up @@ -78,19 +72,19 @@ var sampleResult2 = `[
]`

func TestReduceAll(t *testing.T) {
var def Definition
err := json.Unmarshal([]byte(sampleDefintion2), &def)
var red []utils.Reducer
err := json.Unmarshal([]byte(reducers), &red)
if err != nil {
t.Fatalf("error unmarshalling sample def: %v", err)
}

var res interface{}
err = json.Unmarshal([]byte(sampleResult2), &res)
err = json.Unmarshal([]byte(sampleResult), &res)
if err != nil {
t.Fatalf("error unmarshalling sample result: %v", err)
}

result, err := ReduceAll(res, def.Reducers)
result, err := utils.Reduce(res, red)
if err != nil {
t.Fatalf("error reducing: %v", err)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
package fetcher
package tests

import (
"testing"

"bisonai.com/orakl/node/pkg/utils"
)

func TestAvg(t *testing.T) {
data := []float64{1, 2, 3, 4, 5}
avg := getAvg(data)
avg := utils.GetFloatAvg(data)
if avg != 3 {
t.Errorf("Expected 3 but got %v", avg)
}
}

func TestMed(t *testing.T) {
data := []float64{1, 2, 3, 4, 5}
med := getMed(data)
med := utils.GetFloatMed(data)
if med != 3 {
t.Errorf("Expected 3 but got %v", med)
}
Expand Down
16 changes: 16 additions & 0 deletions node/pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,19 @@ func FindMedian(nums []int) int {
return nums[n/2]
}
}

func GetFloatAvg(data []float64) float64 {
var sum float64
for _, v := range data {
sum += v
}
return sum / float64(len(data))
}

func GetFloatMed(data []float64) float64 {
sort.Float64s(data)
if len(data)%2 == 0 {
return (data[len(data)/2-1] + data[len(data)/2]) / 2
}
return data[len(data)/2]
}
3 changes: 3 additions & 0 deletions node/taskfiles/taskfile.local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ tasks:
dotenv: [".env"]
cmds:
- go test ./pkg/fetcher -v
test-utils:
cmds:
- go test ./pkg/utils/tests -v

0 comments on commit a1698bb

Please sign in to comment.