Skip to content

Commit 18103d7

Browse files
committed
chore: update docker-compose and improve data access logic
- Modified docker-compose.yml to adjust the build context and dockerfile path for the graph-deploy service. - Enhanced data access logic in data-read.ts to streamline transaction filtering and pagination. - Updated GraphQL queries in queries.ts to correctly retrieve transactions by topics and ensure proper ordering. - Refactored subgraph-client.ts to handle channel-based transaction retrieval, improving data organization and pagination handling. These changes improve the overall structure and functionality of the data access layer.
1 parent cfd2861 commit 18103d7

File tree

4 files changed

+45
-46
lines changed

4 files changed

+45
-46
lines changed

docker-compose.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,16 @@ services:
5252
restart: on-failure:20
5353
graph-deploy:
5454
build:
55-
context: https://github.com/RequestNetwork/docker-images.git#main
56-
dockerfile: request-subgraph-storage/Dockerfile
55+
context: https://github.com/RequestNetwork/docker-images.git#main:request-subgraph-storage
56+
dockerfile: ./Dockerfile
5757
depends_on:
5858
- ipfs
5959
- postgres
6060
- graph-node
6161
- ganache
6262
environment:
6363
GRAPH_NODE: 'http://graph-node:8020'
64-
IPFS_HOST: 'ipfs:5001'
64+
IPFS_HOST: 'http://ipfs:5001'
6565
KEEP_ALIVE: 0
6666
SUBGRAPH_FILE: 'subgraph-private.yaml'
6767
restart: on-failure:20

packages/data-access/src/data-read.ts

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ export class DataAccessRead implements DataAccessTypes.IDataRead {
7070
}
7171

7272
const pending = this.pendingStore?.findByTopics(topics) || [];
73+
7374
const pendingItems = pending.map((item) => ({
7475
hash: item.storageResult.id,
7576
channelId: item.channelId,
@@ -112,33 +113,25 @@ export class DataAccessRead implements DataAccessTypes.IDataRead {
112113

113114
const transactions = result.transactions.concat(...pendingItems);
114115

115-
// Apply timestamp filtering FIRST
116-
const filteredTransactions = updatedBetween
117-
? transactions.filter(
118-
(tx) =>
119-
tx.blockTimestamp >= (updatedBetween.from || 0) &&
120-
tx.blockTimestamp <= (updatedBetween.to || Number.MAX_SAFE_INTEGER),
121-
)
122-
: transactions;
123-
124-
// Then get unique channels from filtered transactions
125-
const channels = [...new Set(filteredTransactions.map((x) => x.channelId))];
116+
// list of channels having at least one tx updated during the updatedBetween boundaries
117+
const channels = (
118+
updatedBetween
119+
? transactions.filter(
120+
(tx) =>
121+
tx.blockTimestamp >= (updatedBetween.from || 0) &&
122+
tx.blockTimestamp <= (updatedBetween.to || Number.MAX_SAFE_INTEGER),
123+
)
124+
: transactions
125+
).map((x) => x.channelId);
126126

127-
// Get all transactions for these channels
128127
const filteredTxs = transactions.filter((tx) => channels.includes(tx.channelId));
129-
130-
// Apply pagination to the filtered results
131-
const start = ((page || 1) - 1) * (pageSize || filteredTxs.length);
132-
const end = start + (pageSize || filteredTxs.length);
133-
const paginatedTxs = filteredTxs.slice(start, end);
134-
135128
return {
136129
meta: {
137-
storageMeta: paginatedTxs.reduce((acc, tx) => {
130+
storageMeta: filteredTxs.reduce((acc, tx) => {
138131
acc[tx.channelId] = [this.toStorageMeta(tx, result.blockNumber, this.network)];
139132
return acc;
140133
}, {} as Record<string, StorageTypes.IEntryMetadata[]>),
141-
transactionsStorageLocation: paginatedTxs.reduce((prev, curr) => {
134+
transactionsStorageLocation: filteredTxs.reduce((prev, curr) => {
142135
if (!prev[curr.channelId]) {
143136
prev[curr.channelId] = [];
144137
}
@@ -148,15 +141,17 @@ export class DataAccessRead implements DataAccessTypes.IDataRead {
148141
pagination:
149142
page && pageSize
150143
? {
151-
total: filteredTxs.length,
144+
total: result.transactions.length + pendingItems.length,
152145
page,
153146
pageSize,
154-
hasMore: end < filteredTxs.length,
147+
hasMore:
148+
(page - 1) * pageSize + filteredTxs.length - pendingItemsOnCurrentPage <
149+
result.transactions.length,
155150
}
156151
: undefined,
157152
},
158153
result: {
159-
transactions: paginatedTxs.reduce((prev, curr) => {
154+
transactions: filteredTxs.reduce((prev, curr) => {
160155
if (!prev[curr.channelId]) {
161156
prev[curr.channelId] = [];
162157
}

packages/thegraph-data-access/src/queries.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,17 @@ ${TransactionsBodyFragment}
7979
8080
query GetTransactionsByTopics($topics: [String!]!, $first: Int!, $skip: Int!) {
8181
${metaQueryBody}
82-
transactions(
82+
channels(
8383
where: { topics_contains: $topics }
84-
first: $first
85-
skip: $skip
86-
orderBy: blockTimestamp
87-
orderDirection: asc
88-
) {
89-
...TransactionsBody
84+
){
85+
transactions(
86+
orderBy: blockTimestamp,
87+
orderDirection: asc
88+
first: $first
89+
skip: $skip
90+
) {
91+
...TransactionsBody
92+
}
9093
}
9194
}`;
9295

packages/thegraph-data-access/src/subgraph-client.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -71,27 +71,28 @@ export class SubgraphClient implements StorageTypes.IIndexer {
7171
const effectivePage = page ?? 1;
7272
const skip = (effectivePage - 1) * effectivePageSize;
7373

74-
const topicsArray = Array.isArray(topics) ? topics : [topics];
75-
76-
const response = await this.graphql.request<Meta & { transactions: Transaction[] }>(
77-
GetTransactionsByTopics,
78-
{
79-
topics: topicsArray,
80-
first: effectivePageSize,
81-
skip,
82-
},
83-
);
74+
const { _meta, channels } = await this.graphql.request<
75+
Meta & { channels: { transactions: Transaction[] }[] }
76+
>(GetTransactionsByTopics, {
77+
topics,
78+
first: effectivePageSize,
79+
skip,
80+
});
8481

85-
const indexedTransactions = response.transactions.map(this.toIndexedTransaction);
82+
const transactionsByChannel = channels
83+
.map(({ transactions }) => transactions)
84+
.flat()
85+
.sort((a, b) => a.blockTimestamp - b.blockTimestamp);
8686

87+
const indexedTransactions = transactionsByChannel.map(this.toIndexedTransaction);
8788
return {
8889
transactions: indexedTransactions,
89-
blockNumber: response._meta.block.number,
90+
blockNumber: _meta.block.number,
9091
pagination: {
9192
page: effectivePage,
9293
pageSize: effectivePageSize,
9394
total: indexedTransactions.length,
94-
hasMore: indexedTransactions.length === effectivePageSize,
95+
hasMore: skip + effectivePageSize < indexedTransactions.length,
9596
},
9697
};
9798
}

0 commit comments

Comments
 (0)