From 296d1f5df2e4f39ee9674df3833b6d23a195de20 Mon Sep 17 00:00:00 2001 From: Omar Sy Date: Sun, 21 Apr 2024 18:27:58 +0200 Subject: [PATCH] feat: add aggregator --- .../gno.land/p/demo/teritori/worx/worxs.gno | 41 ++++++++- .../teritori/worx_aggregator/aggregator.gno | 17 ++++ .../r/demo/teritori/worx_aggregator/gno.mod | 5 ++ .../r/demo/teritori/worx_aggregator/worx.gno | 84 ++++++++++--------- 4 files changed, 105 insertions(+), 42 deletions(-) create mode 100644 examples/gno.land/r/demo/teritori/worx_aggregator/aggregator.gno create mode 100644 examples/gno.land/r/demo/teritori/worx_aggregator/gno.mod diff --git a/examples/gno.land/p/demo/teritori/worx/worxs.gno b/examples/gno.land/p/demo/teritori/worx/worxs.gno index 359f3bba311..eaa2835133d 100644 --- a/examples/gno.land/p/demo/teritori/worx/worxs.gno +++ b/examples/gno.land/p/demo/teritori/worx/worxs.gno @@ -1,13 +1,50 @@ package worx +import ( + "gno.land/p/demo/avl" + "gno.land/p/demo/ufmt" +) + type WorxKeeper struct { worxs []*Worx } +const dayToSeconds = 1 + +func NewWorxKeeper() *WorxKeeper { + return &WorxKeeper{ + worxs: avl.NewTree(), + } +} + func (keeper *WorxKeeper) Store(worx *Worx) { keeper.worxs = append(keeper.worxs, worx) } func (keeper *WorxKeeper) Get() []*Worx { - return keeper.worxs -} \ No newline at end of file + totalWorx := make([]*Worx, 0) + keeper.worxs.Iterate("", "", func(key string, value interface{}) bool { + worxByDay := value.([]*Worx) + totalWorx = append(totalWorx, worxByDay...) + return false + }) + + return totalWorx +} + +func (keeper *WorxKeeper) GetFromDate(date int64) []*Worx { + start := ufmt.Sprintf("%d", date) + totalWorx := make([]*Worx, 0) + keeper.worxs.Iterate(start, "", func(key string, value interface{}) bool { + // here we account for string comparation "95" > "105" because comparing first character 9 > 1 (Comparation of lenght) + // and simply comparing same lenght string 25 > 12 this in order to stop tree for iterating over lower dates leaves + if len(start) > len(key) || start > key { + return true + } + worxByDay := value.([]*Worx) + totalWorx = append(totalWorx, worxByDay...) + return false + }) + + return totalWorx +} diff --git a/examples/gno.land/r/demo/teritori/worx_aggregator/aggregator.gno b/examples/gno.land/r/demo/teritori/worx_aggregator/aggregator.gno new file mode 100644 index 00000000000..07c5727f76a --- /dev/null +++ b/examples/gno.land/r/demo/teritori/worx_aggregator/aggregator.gno @@ -0,0 +1,17 @@ +package worx_aggregator + +import ( + "std" + + "gno.land/p/demo/teritori/worx" +) + +func GetWorx(addr std.Address) []*worx.Worx { + keeper := getKeeper(addr) + return keeper.Get() +} + +func GetWorxFromDate(addr std.Address, date int64) []*worx.Worx { + keeper := getKeeper(addr) + return keeper.GetFromDate(date) +} diff --git a/examples/gno.land/r/demo/teritori/worx_aggregator/gno.mod b/examples/gno.land/r/demo/teritori/worx_aggregator/gno.mod new file mode 100644 index 00000000000..701b2f6b6cd --- /dev/null +++ b/examples/gno.land/r/demo/teritori/worx_aggregator/gno.mod @@ -0,0 +1,5 @@ +module gno.land/r/demo/teritori/worx_aggregator + +require ( + gno.land/p/demo/teritori/worx v0.0.0-latest +) diff --git a/examples/gno.land/r/demo/teritori/worx_aggregator/worx.gno b/examples/gno.land/r/demo/teritori/worx_aggregator/worx.gno index 3b853bc6d11..99432f3c1bd 100644 --- a/examples/gno.land/r/demo/teritori/worx_aggregator/worx.gno +++ b/examples/gno.land/r/demo/teritori/worx_aggregator/worx.gno @@ -1,54 +1,58 @@ package worx_aggregator -import( +import ( + "std" + "gno.land/p/demo/avl" - "gno.land/p/demo/teritori/worx" - "std" + "gno.land/p/demo/teritori/worx" +) + +var ( + admin std.Address + registeredProviders avl.Tree + worxByAddress avl.Tree ) -var admin std.Address -var registeredProviders avl.Tree -var worxByAddress avl.Tree func Push(hours int, metadata string, addr std.Address, points int, timestamp int64) { - prevRealm := std.PrevRealm().Addr() - dataProviders, ok := registeredProviders.Get(string(prevRealm)) - if !ok { - panic("caller realm is not registered as provider") - } - keeper := getKeeper(addr) - keeper.Store(worx.NewWorx(hours, metadata, addr, points, timestamp)) - worxByAddress.Set(string(addr), keeper) - - std.Emit("worx_added", - "addr",string(addr), - "metadata", metadata, - ) -} + assertRegistered(addr) -func getKeeper(addr std.Address) *worx.WorxKeeper{ - data, ok := worxByAddress.Get(string(addr)) - if ok { - return data.(*worx.WorxKeeper) - } - return &worx.WorxKeeper{} -} + keeper := getKeeper(addr) + keeper.Store(worx.NewWorx(hours, metadata, addr, points, timestamp)) + worxByAddress.Set(string(addr), keeper) -func Get(dataType string, addr std.Address) []*worx.Worx { - return getKeeper(addr).Get() + std.Emit("worx_added", + "addr", string(addr), + "metadata", metadata, + ) } +func getKeeper(addr std.Address) *worx.WorxKeeper { + data, ok := worxByAddress.Get(string(addr)) + if ok { + return data.(*worx.WorxKeeper) + } + return &worx.WorxKeeper{} +} func RegisterDataProvider(addr std.Address) { - assertAdmin() - _, ok :=registeredProviders.Get(string(addr)) - if !ok { - panic("Data provider already registered") - } - registeredProviders.Set(string(addr), 0) + assertAdmin() + _, ok := registeredProviders.Get(string(addr)) + if !ok { + panic("Data provider already registered") + } + registeredProviders.Set(string(addr), 0) +} + +func assertRegistered(addr std.Address) { + prevRealm := std.PrevRealm().Addr() + dataProviders, ok := registeredProviders.Get(string(prevRealm)) + if !ok { + panic("caller realm is not registered as provider") + } } -func assertAdmin(){ - if (std.PrevRealm().Addr() != admin) { - panic("unathorized") - } -} \ No newline at end of file +func assertAdmin() { + if std.PrevRealm().Addr() != admin { + panic("unathorized") + } +}