@@ -57,6 +57,32 @@ type explorerAPI struct {
57
57
service * Service
58
58
}
59
59
60
+ type paginationOps struct {
61
+ page int
62
+ size int
63
+ queryType types.QueryType
64
+ }
65
+
66
+ func newPaginationFromReq (r * http.Request ) (op * paginationOps ) {
67
+ op = & paginationOps {}
68
+ op .page , _ = strconv .Atoi (r .URL .Query ().Get ("page" ))
69
+ op .size , _ = strconv .Atoi (r .URL .Query ().Get ("size" ))
70
+ if r .URL .Query ().Get ("type" ) == types .ReadQuery .String () {
71
+ op .queryType = types .ReadQuery
72
+ } else if r .URL .Query ().Get ("type" ) == types .WriteQuery .String () {
73
+ op .queryType = types .WriteQuery
74
+ } else {
75
+ op .queryType = types .NumberOfQueryType
76
+ }
77
+ if op .page <= 0 {
78
+ op .page = 1
79
+ }
80
+ if op .size <= 0 {
81
+ op .size = 10
82
+ }
83
+ return
84
+ }
85
+
60
86
func (a * explorerAPI ) GetAck (rw http.ResponseWriter , r * http.Request ) {
61
87
vars := mux .Vars (r )
62
88
@@ -175,7 +201,9 @@ func (a *explorerAPI) GetBlockV3(rw http.ResponseWriter, r *http.Request) {
175
201
return
176
202
}
177
203
178
- sendResponse (200 , true , "" , a .formatBlockV3 (count , height , block ), rw )
204
+ op := newPaginationFromReq (r )
205
+
206
+ sendResponse (200 , true , "" , a .formatBlockV3 (count , height , block , op ), rw )
179
207
}
180
208
181
209
func (a * explorerAPI ) GetBlockByCount (rw http.ResponseWriter , r * http.Request ) {
@@ -239,7 +267,9 @@ func (a *explorerAPI) GetBlockByCountV3(rw http.ResponseWriter, r *http.Request)
239
267
return
240
268
}
241
269
242
- sendResponse (200 , true , "" , a .formatBlockV3 (count , height , block ), rw )
270
+ op := newPaginationFromReq (r )
271
+
272
+ sendResponse (200 , true , "" , a .formatBlockV3 (count , height , block , op ), rw )
243
273
}
244
274
245
275
func (a * explorerAPI ) GetBlockByHeight (rw http.ResponseWriter , r * http.Request ) {
@@ -303,7 +333,9 @@ func (a *explorerAPI) GetBlockByHeightV3(rw http.ResponseWriter, r *http.Request
303
333
return
304
334
}
305
335
306
- sendResponse (200 , true , "" , a .formatBlockV3 (count , height , block ), rw )
336
+ op := newPaginationFromReq (r )
337
+
338
+ sendResponse (200 , true , "" , a .formatBlockV3 (count , height , block , op ), rw )
307
339
}
308
340
309
341
func (a * explorerAPI ) GetHighestBlock (rw http.ResponseWriter , r * http.Request ) {
@@ -396,7 +428,9 @@ func (a *explorerAPI) GetHighestBlockV3(rw http.ResponseWriter, r *http.Request)
396
428
return
397
429
}
398
430
399
- sendResponse (200 , true , "" , a .formatBlockV3 (count , height , block ), rw )
431
+ op := newPaginationFromReq (r )
432
+
433
+ sendResponse (200 , true , "" , a .formatBlockV3 (count , height , block , op ), rw )
400
434
}
401
435
402
436
func (a * explorerAPI ) formatBlock (height int32 , b * types.Block ) (res map [string ]interface {}) {
@@ -425,30 +459,96 @@ func (a *explorerAPI) formatBlockV2(count, height int32, b *types.Block) (res ma
425
459
return
426
460
}
427
461
428
- func (a * explorerAPI ) formatBlockV3 (count , height int32 , b * types.Block ) (res map [string ]interface {}) {
462
+ func (a * explorerAPI ) formatBlockV3 (count , height int32 , b * types.Block ,
463
+ pagination * paginationOps ) (res map [string ]interface {}) {
429
464
res = a .formatBlockV2 (count , height , b )
430
465
blockRes := res ["block" ].(map [string ]interface {})
431
- blockRes ["acks" ] = func () (acks []interface {}) {
432
- acks = make ([]interface {}, 0 , len (b .Acks ))
433
-
434
- for _ , ack := range b .Acks {
435
- acks = append (acks , a .formatAck (ack )["ack" ])
436
- }
437
-
438
- return
439
- }()
440
466
blockRes ["queries" ] = func () (tracks []interface {}) {
441
- tracks = make ([]interface {}, 0 , len (b .QueryTxs ))
467
+ tracks = make ([]interface {}, 0 , len (b .QueryTxs )+ len (b .FailedReqs ))
468
+
469
+ var (
470
+ offset = (pagination .page - 1 ) * pagination .size
471
+ end = pagination .page * pagination .size
472
+ pos = 0
473
+ )
442
474
443
475
for _ , tx := range b .QueryTxs {
476
+ if (pagination .queryType == types .ReadQuery || pagination .queryType == types .WriteQuery ) &&
477
+ tx .Request .Header .QueryType != pagination .queryType {
478
+ // count all
479
+ continue
480
+ }
481
+
482
+ if pos >= end {
483
+ return
484
+ }
485
+
444
486
t := a .formatRequest (tx .Request )
445
487
t ["response" ] = a .formatResponseHeader (tx .Response )["response" ]
446
- tracks = append (tracks , t )
488
+ t ["failed" ] = false
489
+
490
+ if pos >= offset {
491
+ tracks = append (tracks , t )
492
+ }
493
+
494
+ pos ++
495
+ }
496
+
497
+ for _ , req := range b .FailedReqs {
498
+ if (pagination .queryType == types .ReadQuery || pagination .queryType == types .WriteQuery ) &&
499
+ req .Header .QueryType != pagination .queryType {
500
+ // count all
501
+ continue
502
+ }
503
+
504
+ if pos >= end {
505
+ return
506
+ }
507
+
508
+ t := a .formatRequest (req )
509
+ t ["failed" ] = true
510
+
511
+ if pos >= offset {
512
+ tracks = append (tracks , t )
513
+ }
514
+
515
+ pos ++
447
516
}
448
517
449
518
return
450
519
}()
451
520
521
+ if pagination != nil {
522
+ blockRes ["pagination" ] = func () (res map [string ]interface {}) {
523
+ // pagination features
524
+ res = map [string ]interface {}{}
525
+ res ["page" ] = pagination .page
526
+ res ["size" ] = pagination .size
527
+
528
+ if pagination .queryType != types .ReadQuery && pagination .queryType != types .WriteQuery {
529
+ res ["total" ] = len (b .QueryTxs ) + len (b .FailedReqs )
530
+ } else {
531
+ var total int
532
+
533
+ for _ , tx := range b .QueryTxs {
534
+ if tx .Request .Header .QueryType == pagination .queryType {
535
+ total ++
536
+ }
537
+ }
538
+
539
+ for _ , req := range b .FailedReqs {
540
+ if req .Header .QueryType == pagination .queryType {
541
+ total ++
542
+ }
543
+ }
544
+
545
+ res ["total" ] = total
546
+ }
547
+
548
+ return
549
+ }()
550
+ }
551
+
452
552
return
453
553
}
454
554
0 commit comments