Skip to content

Commit 987d0b7

Browse files
authored
Feature/address sorting (#36)
* Add sortable to address list * Address sorting
1 parent 0e902cf commit 987d0b7

File tree

6 files changed

+60
-26
lines changed

6 files changed

+60
-26
lines changed

Dockerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ FROM alpine:latest
1919
RUN apk update && apk add --no-cache zeromq
2020

2121
WORKDIR /app
22+
RUN mkdir /app/logs
2223

2324
COPY --from=builder /etc/passwd /etc/passwd
2425
COPY --from=builder /go/bin/api /app/api

internal/framework/sort.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ var ErrInvalidSortDirection = errors.New("The sort direction is invalid format")
1414
type Sort interface {
1515
Options() []SortOption
1616
IsEmpty() bool
17+
HasOption(field string) bool
1718
}
1819

1920
type sort struct {
@@ -63,6 +64,16 @@ func (s *sort) Options() []SortOption {
6364
return s.options
6465
}
6566

67+
func (s *sort) HasOption(field string) bool {
68+
for _, option := range s.options {
69+
if option.field == field {
70+
return true
71+
}
72+
}
73+
74+
return false
75+
}
76+
6677
func (s *sort) IsEmpty() bool {
6778
return len(s.options) == 0
6879
}

internal/repository/address_history_repository.go

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ func NewAddressHistoryRepository(elastic *elastic_cache.Index) AddressHistoryRep
4141
}
4242

4343
func (r *addressHistoryRepository) GetLatestByHash(n network.Network, hash string) (*explorer.AddressHistory, error) {
44-
query := elastic.NewBoolQuery()
45-
query = query.Must(elastic.NewTermQuery("hash.keyword", hash))
44+
query := elastic.NewBoolQuery().Filter(elastic.NewMatchPhraseQuery("hash", hash))
4645

4746
results, err := r.elastic.Client.Search(elastic_cache.AddressHistoryIndex.Get(n)).
4847
Query(query).
@@ -54,8 +53,7 @@ func (r *addressHistoryRepository) GetLatestByHash(n network.Network, hash strin
5453
}
5554

5655
func (r *addressHistoryRepository) GetFirstByHash(n network.Network, hash string) (*explorer.AddressHistory, error) {
57-
query := elastic.NewBoolQuery()
58-
query = query.Must(elastic.NewTermQuery("hash.keyword", hash))
56+
query := elastic.NewBoolQuery().Filter(elastic.NewMatchPhraseQuery("hash", hash))
5957

6058
results, err := r.elastic.Client.Search(elastic_cache.AddressHistoryIndex.Get(n)).
6159
Query(query).
@@ -67,8 +65,7 @@ func (r *addressHistoryRepository) GetFirstByHash(n network.Network, hash string
6765
}
6866

6967
func (r *addressHistoryRepository) GetCountByHash(n network.Network, hash string) (int64, error) {
70-
query := elastic.NewBoolQuery()
71-
query = query.Must(elastic.NewTermQuery("hash.keyword", hash))
68+
query := elastic.NewBoolQuery().Filter(elastic.NewMatchPhraseQuery("hash", hash))
7269

7370
results, err := r.elastic.Client.Search(elastic_cache.AddressHistoryIndex.Get(n)).
7471
Query(query).
@@ -84,8 +81,7 @@ func (r *addressHistoryRepository) GetCountByHash(n network.Network, hash string
8481
}
8582

8683
func (r *addressHistoryRepository) GetStakingSummary(n network.Network, hash string) (count, stakable, spendable, votingWeight int64, err error) {
87-
query := elastic.NewBoolQuery()
88-
query = query.Must(elastic.NewTermQuery("hash.keyword", hash))
84+
query := elastic.NewBoolQuery().Filter(elastic.NewMatchPhraseQuery("hash", hash))
8985

9086
changeAgg := elastic.NewNestedAggregation().Path("changes")
9187
changeAgg.SubAggregation("stakable", elastic.NewSumAggregation().Field("changes.stakable"))
@@ -123,9 +119,9 @@ func (r *addressHistoryRepository) GetStakingSummary(n network.Network, hash str
123119
}
124120

125121
func (r *addressHistoryRepository) GetSpendSummary(n network.Network, hash string) (spendableReceive, spendableSent, stakableReceive, stakableSent, votingWeightReceive, votingWeightSent int64, err error) {
126-
query := elastic.NewBoolQuery()
127-
query = query.Must(elastic.NewTermQuery("hash.keyword", hash))
128-
query = query.Must(elastic.NewTermQuery("is_stake", false))
122+
query := elastic.NewBoolQuery().
123+
Filter(elastic.NewMatchPhraseQuery("hash", hash)).
124+
Must(elastic.NewTermQuery("is_stake", false))
129125

130126
spendableReceiveAgg := elastic.NewRangeAggregation().Field("changes.spendable").Gt(0)
131127
spendableReceiveAgg.SubAggregation("sum", elastic.NewSumAggregation().Field("changes.spendable"))
@@ -205,8 +201,7 @@ func (r *addressHistoryRepository) GetSpendSummary(n network.Network, hash strin
205201
}
206202

207203
func (r *addressHistoryRepository) GetHistoryByHash(n network.Network, hash string, p framework.Pagination, s framework.Sort, f framework.Filters) ([]*explorer.AddressHistory, int64, error) {
208-
query := elastic.NewBoolQuery()
209-
query = query.Must(elastic.NewTermQuery("hash.keyword", hash))
204+
query := elastic.NewBoolQuery().Filter(elastic.NewMatchPhraseQuery("hash", hash))
210205

211206
options := f.OnlySupportedOptions([]string{"type"})
212207
if option, err := options.Get("type"); err == nil {
@@ -305,9 +300,9 @@ func (r *addressHistoryRepository) GetStakingChart(n network.Network, period str
305300
count := 12
306301
now := time.Now().UTC().Truncate(time.Second)
307302

308-
query := elastic.NewBoolQuery()
309-
query = query.Must(elastic.NewTermQuery("hash.keyword", hash))
310-
query = query.Must(elastic.NewMatchQuery("is_stake", true))
303+
query := elastic.NewBoolQuery().
304+
Filter(elastic.NewMatchPhraseQuery("hash", hash)).
305+
Must(elastic.NewMatchQuery("is_stake", true))
311306

312307
agg := elastic.NewFilterAggregation().Filter(query)
313308

internal/repository/address_repository.go

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@ import (
55
"encoding/json"
66
"errors"
77
"github.com/NavExplorer/navexplorer-api-go/v2/internal/elastic_cache"
8+
"github.com/NavExplorer/navexplorer-api-go/v2/internal/framework"
89
"github.com/NavExplorer/navexplorer-api-go/v2/internal/service/address/entity"
910
"github.com/NavExplorer/navexplorer-api-go/v2/internal/service/network"
1011
"github.com/NavExplorer/navexplorer-indexer-go/v2/pkg/explorer"
1112
"github.com/olivere/elastic/v7"
1213
log "github.com/sirupsen/logrus"
14+
"go.uber.org/zap"
1315
)
1416

1517
type AddressRepository interface {
16-
GetAddresses(n network.Network, size, page int) ([]*explorer.Address, int64, error)
18+
GetAddresses(n network.Network, size, page int, f framework.Filters, s framework.Sort) ([]*explorer.Address, int64, error)
1719
GetAddressByHash(n network.Network, hash string) (*explorer.Address, error)
1820
GetBalancesForAddresses(n network.Network, addresses []string) ([]*explorer.Address, error)
1921
GetWealthDistribution(n network.Network, groups []int, totalSupply uint64) ([]*entity.Wealth, error)
@@ -33,12 +35,33 @@ func NewAddressRepository(elastic *elastic_cache.Index) AddressRepository {
3335
return &addressRepository{elastic: elastic}
3436
}
3537

36-
func (r *addressRepository) GetAddresses(n network.Network, size, page int) ([]*explorer.Address, int64, error) {
37-
results, err := r.elastic.Client.Search(elastic_cache.AddressIndex.Get(n)).
38-
Sort("spendable", false).
38+
func (r *addressRepository) GetAddresses(n network.Network, size, page int, f framework.Filters, s framework.Sort) ([]*explorer.Address, int64, error) {
39+
service := r.elastic.Client.Search(elastic_cache.AddressIndex.Get(n)).
3940
From((page * size) - size).
4041
Size(size).
41-
Do(context.Background())
42+
TrackTotalHits(true)
43+
44+
options := f.OnlySupportedOptions([]string{"exclude"})
45+
if option, err := options.Get("exclude"); err == nil {
46+
zap.L().Info("Has the exclude filter")
47+
switch option.Values()[0] {
48+
case "empty":
49+
if s.HasOption("spendable") {
50+
service.Query(elastic.NewRangeQuery("spendable").Gt(0))
51+
}
52+
if s.HasOption("stakable") {
53+
service.Query(elastic.NewRangeQuery("stakable").Gt(0))
54+
}
55+
if s.HasOption("voting_weight") {
56+
service.Query(elastic.NewRangeQuery("voting_weight").Gt(0))
57+
}
58+
break
59+
}
60+
}
61+
62+
sort(service, s, &defaultSort{"spendable", false})
63+
64+
results, err := service.Do(context.Background())
4265

4366
addresses, total, err := r.findMany(results, err)
4467
for idx := range addresses {
@@ -51,8 +74,10 @@ func (r *addressRepository) GetAddresses(n network.Network, size, page int) ([]*
5174
}
5275

5376
func (r *addressRepository) GetAddressByHash(n network.Network, hash string) (*explorer.Address, error) {
77+
query := elastic.NewBoolQuery().Filter(elastic.NewMatchPhraseQuery("hash", hash))
78+
5479
results, err := r.elastic.Client.Search(elastic_cache.AddressIndex.Get(n)).
55-
Query(elastic.NewTermQuery("hash.keyword", hash)).
80+
Query(query).
5681
Size(1).
5782
Do(context.Background())
5883

internal/resource/address_resource.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ func (r *AddressResource) GetAddress(c *gin.Context) {
3737
}
3838

3939
func (r *AddressResource) GetAddresses(c *gin.Context) {
40-
addresses, total, err := r.addressService.GetAddresses(network(c), pagination(c))
40+
req := rest(c)
41+
42+
addresses, total, err := r.addressService.GetAddresses(network(c), pagination(c), req.Filters(), req.Sort())
4143
if err != nil {
4244
errorInternalServerError(c, err.Error())
4345
return

internal/service/address/service.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212

1313
type Service interface {
1414
GetAddress(n network.Network, hash string) (*explorer.Address, error)
15-
GetAddresses(n network.Network, pagination framework.Pagination) ([]*explorer.Address, int64, error)
15+
GetAddresses(n network.Network, pagination framework.Pagination, filters framework.Filters, sort framework.Sort) ([]*explorer.Address, int64, error)
1616
GetAddressSummary(n network.Network, hash string) (*entity.AddressSummary, error)
1717
GetStakingChart(n network.Network, period string, address string) ([]*entity.StakingGroup, error)
1818
GetAddressGroups(n network.Network, period *group.Period, count int) ([]entity.AddressGroup, error)
@@ -55,8 +55,8 @@ func (s *service) GetAddress(n network.Network, hash string) (*explorer.Address,
5555
return address, err
5656
}
5757

58-
func (s *service) GetAddresses(n network.Network, pagination framework.Pagination) ([]*explorer.Address, int64, error) {
59-
return s.addressRepository.GetAddresses(n, pagination.Size(), pagination.Page())
58+
func (s *service) GetAddresses(n network.Network, pagination framework.Pagination, filters framework.Filters, sort framework.Sort) ([]*explorer.Address, int64, error) {
59+
return s.addressRepository.GetAddresses(n, pagination.Size(), pagination.Page(), filters, sort)
6060
}
6161

6262
func (s *service) GetHistory(n network.Network, hash string, request framework.RestRequest) ([]*explorer.AddressHistory, int64, error) {

0 commit comments

Comments
 (0)