Skip to content

Commit 477295d

Browse files
authored
Merge pull request #1077 from oasisprotocol/ptrus/feature/rofl-app-created-at
api/rofl_apps: Support ordering by created at
2 parents edd45df + 717df00 commit 477295d

18 files changed

+1284
-24
lines changed

.changelog/1077.feature.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
api/rofl_apps: Add support for sorting by creation time
2+
3+
Supports the `sort_by` query parameter:
4+
5+
- `sort_by=created_at`
6+
7+
- `sort_by=created_at_desc`
8+
9+
Example:
10+
`GET v1/{runtime}/rofl_apps?sort_by=created_at`

analyzer/queries/queries.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -594,9 +594,11 @@ var (
594594
INSERT INTO chain.rofl_related_transactions (runtime, app_id, tx_round, tx_index, method, likely_native_transfer)
595595
VALUES ($1, $2, $3, $4, $5, $6)`
596596

597-
RuntimeRoflNumTransactionsIncrement = `
597+
RuntimeRoflUpdateStatsOnTransaction = `
598598
UPDATE chain.rofl_apps
599-
SET num_transactions = num_transactions + 1
599+
SET
600+
num_transactions = num_transactions + 1,
601+
created_at_round = LEAST(created_at_round, $3::bigint)
600602
WHERE runtime = $1::runtime AND id = $2`
601603

602604
RuntimeAccountNumTxsUpsert = `
@@ -1377,10 +1379,11 @@ var (
13771379
last_processed_round = COALESCE(chain.rofl_instances.last_processed_round, excluded.last_processed_round)`
13781380

13791381
RuntimeRoflAppQueueRefresh = `
1380-
INSERT INTO chain.rofl_apps (runtime, id, last_queued_round)
1381-
VALUES ($1, $2, $3)
1382+
INSERT INTO chain.rofl_apps (runtime, id, created_at_round, last_queued_round)
1383+
VALUES ($1, $2, $3, $3)
13821384
ON CONFLICT (runtime, id) DO UPDATE
13831385
SET
1386+
created_at_round = LEAST(excluded.created_at_round, chain.rofl_apps.created_at_round),
13841387
last_queued_round = GREATEST(excluded.last_queued_round, chain.rofl_apps.last_queued_round)`
13851388

13861389
RuntimeRoflmarketStaleProviders = `

analyzer/rofl/instance_transactions/instance_transactions.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,10 @@ func (p *processor) ProcessItem(ctx context.Context, batch *storage.QueryBatch,
175175
isNativeTransfer,
176176
)
177177
batch.Queue(
178-
queries.RuntimeRoflNumTransactionsIncrement,
178+
queries.RuntimeRoflUpdateStatsOnTransaction,
179179
p.runtime,
180180
item.AppID,
181+
round,
181182
)
182183
}
183184

analyzer/runtime/runtime.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ func (m *processor) queueDbUpdates(batch *storage.QueryBatch, data *BlockData) {
431431
m.queueTransactionInsert(batch, data.Header.Round, data.Header.Timestamp, transactionData)
432432
for appID := range transactionData.RelatedRoflAddresses {
433433
batch.Queue(queries.RuntimeRoflRelatedTransactionInsert, m.runtime, appID, data.Header.Round, transactionData.Index, transactionData.Method, transactionData.IsLikelyTokenTransfer)
434-
batch.Queue(queries.RuntimeRoflNumTransactionsIncrement, m.runtime, appID)
434+
batch.Queue(queries.RuntimeRoflUpdateStatsOnTransaction, m.runtime, appID, data.Header.Round)
435435
}
436436

437437
if transactionData.ContractCandidate != nil {

api/spec/v1.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,6 +1284,15 @@ paths:
12841284
type: string
12851285
maxItems: 6
12861286
description: A filter on the name of the ROFL app. If multiple names are provided, the ROFL App must match all of them.
1287+
- in: query
1288+
name: sort_by
1289+
schema:
1290+
type: string
1291+
enum: [created_at, created_at_desc]
1292+
description: |
1293+
The field to sort the ROFL apps by.
1294+
If unset, the ROFL apps will be sorted by activity (num_active_instances, num_transactions) in descending order.
1295+
To sort by creation time, set this to `created_at` (or `created_at_desc`).
12871296
responses:
12881297
'200':
12891298
description: A JSON object containing a list of ROFL apps.

storage/client/client.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2495,9 +2495,20 @@ func (c *StorageClient) RuntimeRoflApps(ctx context.Context, runtime common.Runt
24952495
if params.Name != nil && len(*params.Name) > maxFilterNameFragments {
24962496
return nil, fmt.Errorf("too many names in the name filter: %w", apiCommon.ErrBadRequest)
24972497
}
2498+
var orderBy *string
2499+
if params.SortBy != nil {
2500+
switch *params.SortBy {
2501+
case "created_at":
2502+
orderBy = common.Ptr("created_at")
2503+
case "created_at_desc":
2504+
orderBy = common.Ptr("created_at_desc")
2505+
default:
2506+
return nil, fmt.Errorf("invalid sort_by value: %s", *params.SortBy)
2507+
}
2508+
}
24982509

24992510
args := []interface{}{runtime, id, ocAddrAdmin}
2500-
query := queries.RuntimeRoflApps(params.Name, &args)
2511+
query := queries.RuntimeRoflApps(params.Name, orderBy, &args)
25012512
args = append(args, params.Limit, params.Offset)
25022513

25032514
res, err := c.withDefaultTotalCount(

storage/client/queries/queries.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1311,7 +1311,19 @@ func EVMTokens(rawNames *[]string, args *[]interface{}) string {
13111311
// Dynamic query generation is necessary here to allow PostgreSQL to utilize the
13121312
// pg_trgm GIN index on `metadata_name` — index usage is only possible when each
13131313
// ILIKE condition is written explicitly.
1314-
func RuntimeRoflApps(rawNames *[]string, args *[]interface{}) string {
1314+
func RuntimeRoflApps(rawNames *[]string, orderBy *string, args *[]interface{}) string {
1315+
orderByClause := "num_active_instances DESC, ra.removed ASC, ra.num_transactions DESC, ra.id DESC"
1316+
if orderBy != nil {
1317+
switch *orderBy {
1318+
case "created_at":
1319+
orderByClause = "ra.created_at_round ASC, ra.id DESC"
1320+
case "created_at_desc":
1321+
orderByClause = "ra.created_at_round DESC, ra.id DESC"
1322+
default:
1323+
// Shouldn't happen.
1324+
}
1325+
}
1326+
13151327
var clauses []string
13161328
argOffset := len(*args) + 1
13171329

@@ -1396,9 +1408,9 @@ func RuntimeRoflApps(rawNames *[]string, args *[]interface{}) string {
13961408
-- Exclude not yet processed apps.
13971409
ra.last_processed_round IS NOT NULL
13981410
1399-
ORDER BY num_active_instances DESC, ra.removed ASC, ra.num_transactions DESC, ra.id DESC
1411+
ORDER BY %s
14001412
LIMIT $%d::bigint
1401-
OFFSET $%d::bigint`, nameCondition, argOffset+len(clauses), argOffset+len(clauses)+1)
1413+
OFFSET $%d::bigint`, nameCondition, orderByClause, argOffset+len(clauses), argOffset+len(clauses)+1)
14021414

14031415
return query
14041416
}

storage/migrations/19_runtime_rofl.up.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ CREATE TABLE chain.rofl_apps
1818
secrets JSONB, -- arbitrary key/value pairs.
1919

2020
-- num_transactions UINT63 NOT NULL, -- Added in 27_runtime_rofl_num_transactions.up.sql.
21+
-- created_at_round UINT63 NOT NULL, -- Added in 43_runtime_rofl_app_created_at.up.sql.
2122

2223
removed BOOLEAN NOT NULL DEFAULT FALSE,
2324

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
BEGIN;
2+
3+
ALTER TABLE chain.rofl_apps ADD COLUMN created_at_round UINT63 NOT NULL DEFAULT 0;
4+
5+
-- Populate created_at_round from rofl related and instance transactions.
6+
UPDATE chain.rofl_apps apps
7+
SET created_at_round = first_tx.first_round
8+
FROM (
9+
SELECT
10+
txs.runtime,
11+
txs.app_id,
12+
MIN(txs.tx_round) AS first_round
13+
FROM (
14+
SELECT runtime, app_id, tx_round FROM chain.rofl_related_transactions
15+
UNION ALL
16+
SELECT runtime, app_id, tx_round FROM chain.rofl_instance_transactions
17+
) txs
18+
GROUP BY txs.runtime, txs.app_id
19+
) first_tx
20+
WHERE
21+
apps.runtime = first_tx.runtime AND apps.id = first_tx.app_id;
22+
23+
COMMIT;

tests/e2e_regression/common_test_cases.sh

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -94,18 +94,20 @@ commonEmeraldTestCases=(
9494
)
9595

9696
commonSapphireTestCases=(
97-
'sapphire_blocks /v1/sapphire/blocks'
98-
'sapphire_txs /v1/sapphire/transactions'
99-
'sapphire_txs_by_method /v1/sapphire/transactions?method=consensus.Withdraw'
100-
'sapphire_txs_native_transfers /v1/sapphire/transactions?method=native_transfers'
101-
'sapphire_txs_evm_call /v1/sapphire/transactions?method=evm.Call'
102-
'sapphire_events /v1/sapphire/events'
103-
'sapphire_events_by_type /v1/sapphire/events?type=accounts.transfer'
104-
'sapphire_tokens /v1/sapphire/evm_tokens'
105-
'sapphire_tokens_sort_market_cap /v1/sapphire/evm_tokens?sort_by=market_cap'
106-
'sapphire_status /v1/sapphire/status'
107-
'sapphire_tx_volume /v1/sapphire/stats/tx_volume'
108-
'sapphire_rofl_apps /v1/sapphire/rofl_apps'
109-
'sapphire_roflmarket_providers /v1/sapphire/roflmarket_providers'
110-
'sapphire_roflmarket_instances /v1/sapphire/roflmarket_instances'
97+
'sapphire_blocks /v1/sapphire/blocks'
98+
'sapphire_txs /v1/sapphire/transactions'
99+
'sapphire_txs_by_method /v1/sapphire/transactions?method=consensus.Withdraw'
100+
'sapphire_txs_native_transfers /v1/sapphire/transactions?method=native_transfers'
101+
'sapphire_txs_evm_call /v1/sapphire/transactions?method=evm.Call'
102+
'sapphire_events /v1/sapphire/events'
103+
'sapphire_events_by_type /v1/sapphire/events?type=accounts.transfer'
104+
'sapphire_tokens /v1/sapphire/evm_tokens'
105+
'sapphire_tokens_sort_market_cap /v1/sapphire/evm_tokens?sort_by=market_cap'
106+
'sapphire_status /v1/sapphire/status'
107+
'sapphire_tx_volume /v1/sapphire/stats/tx_volume'
108+
'sapphire_rofl_apps /v1/sapphire/rofl_apps'
109+
'sapphire_rofl_apps_sort_created_at /v1/sapphire/rofl_apps?sort_by=created_at'
110+
'sapphire_rofl_apps_sort_created_at_desc /v1/sapphire/rofl_apps?sort_by=created_at_desc'
111+
'sapphire_roflmarket_providers /v1/sapphire/roflmarket_providers'
112+
'sapphire_roflmarket_instances /v1/sapphire/roflmarket_instances'
111113
)

0 commit comments

Comments
 (0)