From a1698bb12cb05f863a097d93fa3642a6b3c7609d Mon Sep 17 00:00:00 2001 From: nick Date: Tue, 20 Feb 2024 23:10:20 +0900 Subject: [PATCH] fix: move utility functions to utils package --- node/pkg/fetcher/fetcher.go | 4 +- node/pkg/fetcher/local_aggregator.go | 21 ------ node/pkg/fetcher/types.go | 8 +-- node/pkg/{fetcher => utils}/reducer.go | 10 ++- .../{fetcher => utils/tests}/reducer_test.go | 64 +++++++++---------- .../tests/utils_test.go} | 8 ++- node/pkg/utils/utils.go | 16 +++++ node/taskfiles/taskfile.local.yml | 3 + 8 files changed, 64 insertions(+), 70 deletions(-) delete mode 100644 node/pkg/fetcher/local_aggregator.go rename node/pkg/{fetcher => utils}/reducer.go (93%) rename node/pkg/{fetcher => utils/tests}/reducer_test.go (68%) rename node/pkg/{fetcher/local_aggregator_test.go => utils/tests/utils_test.go} (70%) diff --git a/node/pkg/fetcher/fetcher.go b/node/pkg/fetcher/fetcher.go index fe0929878..e0b849056 100644 --- a/node/pkg/fetcher/fetcher.go +++ b/node/pkg/fetcher/fetcher.go @@ -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 @@ -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 diff --git a/node/pkg/fetcher/local_aggregator.go b/node/pkg/fetcher/local_aggregator.go deleted file mode 100644 index c916d44db..000000000 --- a/node/pkg/fetcher/local_aggregator.go +++ /dev/null @@ -1,21 +0,0 @@ -package fetcher - -import ( - "sort" -) - -func getAvg(data []float64) float64 { - var sum float64 - for _, v := range data { - sum += v - } - return sum / float64(len(data)) -} - -func getMed(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] -} diff --git a/node/pkg/fetcher/types.go b/node/pkg/fetcher/types.go index b47007544..c8084149e 100644 --- a/node/pkg/fetcher/types.go +++ b/node/pkg/fetcher/types.go @@ -5,6 +5,7 @@ import ( "time" "bisonai.com/orakl/node/pkg/bus" + "bisonai.com/orakl/node/pkg/utils" ) const ( @@ -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 { diff --git a/node/pkg/fetcher/reducer.go b/node/pkg/utils/reducer.go similarity index 93% rename from node/pkg/fetcher/reducer.go rename to node/pkg/utils/reducer.go index abd9c20e5..82288e6df 100644 --- a/node/pkg/fetcher/reducer.go +++ b/node/pkg/utils/reducer.go @@ -1,4 +1,4 @@ -package fetcher +package utils import ( "fmt" @@ -6,7 +6,12 @@ import ( "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 @@ -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{}") diff --git a/node/pkg/fetcher/reducer_test.go b/node/pkg/utils/tests/reducer_test.go similarity index 68% rename from node/pkg/fetcher/reducer_test.go rename to node/pkg/utils/tests/reducer_test.go index 847d02d33..c12716f31 100644 --- a/node/pkg/fetcher/reducer_test.go +++ b/node/pkg/utils/tests/reducer_test.go @@ -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", @@ -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) } diff --git a/node/pkg/fetcher/local_aggregator_test.go b/node/pkg/utils/tests/utils_test.go similarity index 70% rename from node/pkg/fetcher/local_aggregator_test.go rename to node/pkg/utils/tests/utils_test.go index 91289efb3..34b9e0fb4 100644 --- a/node/pkg/fetcher/local_aggregator_test.go +++ b/node/pkg/utils/tests/utils_test.go @@ -1,12 +1,14 @@ -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) } @@ -14,7 +16,7 @@ func TestAvg(t *testing.T) { 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) } diff --git a/node/pkg/utils/utils.go b/node/pkg/utils/utils.go index aa66bd34b..2fcd531d0 100644 --- a/node/pkg/utils/utils.go +++ b/node/pkg/utils/utils.go @@ -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] +} diff --git a/node/taskfiles/taskfile.local.yml b/node/taskfiles/taskfile.local.yml index 252649b12..5c992bf5c 100644 --- a/node/taskfiles/taskfile.local.yml +++ b/node/taskfiles/taskfile.local.yml @@ -13,3 +13,6 @@ tasks: dotenv: [".env"] cmds: - go test ./pkg/fetcher -v + test-utils: + cmds: + - go test ./pkg/utils/tests -v