forked from witnet/data-feeds-explorer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): avoid store unnecessary feed information on db
- Loading branch information
Showing
10 changed files
with
800 additions
and
868 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,158 +1,75 @@ | ||
import { AggregationCursor } from 'mongodb' | ||
import { | ||
FeedDbObjectNormalized, | ||
FeedDbObject, | ||
Collection, | ||
Db, | ||
FeedInfo, | ||
WithoutId, | ||
PaginatedFeedsObject | ||
} from '../types' | ||
import { containFalsyValues } from './containFalsyValues' | ||
import { PaginatedFeedsObject, FeedInfo, Network } from '../types' | ||
|
||
export class FeedRepository { | ||
collection: Collection<FeedDbObject> | ||
// list of fullNames to include in the search queries using feedFullName as an id for each data feed | ||
dataFeedsFullNames: Array<string> | ||
sortedDataFeeds: Array<FeedInfo> | ||
// TODO: replace string with Network | ||
dataFeedsByNetwork: Record<string, Array<FeedInfo>> | ||
|
||
constructor (db: Db, dataFeeds: Array<FeedInfo>) { | ||
this.collection = db.collection('feed') | ||
this.dataFeedsFullNames = dataFeeds.map(dataFeed => dataFeed.feedFullName) | ||
this.collection.createIndex({ feedFullName: 1 }) | ||
} | ||
|
||
async getAll (): Promise<Array<FeedDbObjectNormalized>> { | ||
return ( | ||
await this.collection | ||
.find({ | ||
feedFullName: { $in: this.dataFeedsFullNames } | ||
}) | ||
.sort('network') | ||
.toArray() | ||
).map(this.normalizeId) | ||
} | ||
|
||
async insert ( | ||
feed: WithoutId<FeedDbObject> | ||
): Promise<FeedDbObjectNormalized | null> { | ||
if (this.isValidFeed(feed)) { | ||
const response = await this.collection.insertOne(feed) | ||
return this.normalizeId(response.ops[0]) | ||
} else { | ||
console.error('Error inserting feed: Validation Error', feed) | ||
constructor (dataFeeds: Array<FeedInfo>) { | ||
this.dataFeedsByNetwork = dataFeeds.reduce( | ||
(acc: Record<string, Array<FeedInfo>>, feedInfo: FeedInfo) => ({ | ||
...acc, | ||
[feedInfo.network]: acc[feedInfo.network] | ||
? [...acc[feedInfo.network], feedInfo] | ||
: [feedInfo] | ||
}), | ||
{} | ||
) | ||
|
||
return null | ||
} | ||
} | ||
const sortedFeedsWithoutEth = dataFeeds | ||
.filter(feed => !feed.network.includes(Network.EthereumMainnet)) | ||
.sort((a, b) => (b.network < a.network ? 1 : -1)) | ||
|
||
async get (feedFullName: string): Promise<FeedDbObjectNormalized> { | ||
const response = await this.collection.findOne({ feedFullName }) | ||
return this.normalizeId(response) | ||
this.sortedDataFeeds = [ | ||
...this.dataFeedsByNetwork[Network.EthereumMainnet], | ||
...this.dataFeedsByNetwork[Network.EthereumGoerli], | ||
...this.dataFeedsByNetwork[Network.EthereumRinkeby], | ||
...sortedFeedsWithoutEth | ||
] | ||
} | ||
|
||
async updateFeed ( | ||
feed: WithoutId<FeedDbObject> | ||
): Promise<FeedDbObjectNormalized> { | ||
const feedFound = await this.collection.findOne({ | ||
feedFullName: feed.feedFullName | ||
}) | ||
if (feedFound) { | ||
await this.collection.updateOne( | ||
{ feedFullName: feed.feedFullName }, | ||
{ $set: feed } | ||
) | ||
} | ||
return this.normalizeId(feedFound) | ||
get (feedFullName: string): FeedInfo { | ||
return this.sortedDataFeeds.find(feed => feed.feedFullName === feedFullName) | ||
} | ||
|
||
async getPaginatedFeeds ( | ||
// starts in 1 | ||
page: number, | ||
size: number, | ||
network: string | ||
): Promise<PaginatedFeedsObject> { | ||
const queryByNetwork = { | ||
feedFullName: { $in: this.dataFeedsFullNames }, | ||
network | ||
} | ||
const queryAll = { | ||
feedFullName: { $in: this.dataFeedsFullNames } | ||
} | ||
const match = network !== 'all' ? queryByNetwork : queryAll | ||
const aggregation = ( | ||
await (await this.aggregateCollection(match, page, size)).toArray() | ||
)[0] | ||
const filteredFeeds = | ||
network === 'all' | ||
? this.sortedDataFeeds | ||
: this.dataFeedsByNetwork[network] | ||
|
||
const paginatedFeeds = filteredFeeds.slice((page - 1) * size, page * size) | ||
|
||
return { | ||
feeds: aggregation.feeds.map(this.normalizeId), | ||
total: aggregation.total[0]?.count || 0 | ||
feeds: paginatedFeeds, | ||
total: filteredFeeds.length | ||
} | ||
} | ||
|
||
private async aggregateCollection ( | ||
match, | ||
page, | ||
size | ||
): Promise<AggregationCursor> { | ||
return await this.collection.aggregate([ | ||
{ | ||
$project: { | ||
_id: 1, | ||
address: 1, | ||
blockExplorer: 1, | ||
feedFullName: 1, | ||
label: 1, | ||
name: 1, | ||
network: 1, | ||
order: { | ||
$cond: { | ||
if: { $eq: ['$network', 'ethereum-mainnet'] }, | ||
then: 1, | ||
else: { | ||
$cond: { | ||
if: { $eq: [{ $substr: ['$network', 0, 8] }, 'ethereum'] }, | ||
then: 2, | ||
else: 3 | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
{ $match: match }, | ||
{ $sort: { order: 1, network: 1 } }, | ||
{ | ||
$project: { | ||
_id: 1, | ||
address: 1, | ||
blockExplorer: 1, | ||
feedFullName: 1, | ||
label: 1, | ||
name: 1, | ||
network: 1 | ||
} | ||
}, | ||
{ | ||
$facet: { | ||
feeds: [{ $skip: size * (page - 1) }, { $limit: size }], | ||
total: [ | ||
{ | ||
$count: 'count' | ||
} | ||
] | ||
} | ||
} | ||
]) | ||
} | ||
updateFeedAddress (feedFullName: string, address: string): FeedInfo { | ||
const hasSameFeedFullName = (feed: FeedInfo) => | ||
feed.feedFullName === feedFullName | ||
|
||
private normalizeId (feed: FeedDbObject): FeedDbObjectNormalized | null { | ||
if (feed && feed._id) { | ||
return { ...feed, id: feed._id.toString() } | ||
} else { | ||
// this code should be unreachable: value from db always contains _id | ||
return null | ||
} | ||
} | ||
// Update address in sortedDataFeeds | ||
const sortedDataFeedIndex = this.sortedDataFeeds.findIndex( | ||
hasSameFeedFullName | ||
) | ||
const feed = this.sortedDataFeeds[sortedDataFeedIndex] | ||
feed.address = address | ||
|
||
// Update address in dataFeedsByNetwork | ||
const dataFeedsByNetworkIndex = this.dataFeedsByNetwork[ | ||
feed.network | ||
].findIndex(hasSameFeedFullName) | ||
this.dataFeedsByNetwork[feed.network][ | ||
dataFeedsByNetworkIndex | ||
].address = address | ||
|
||
private isValidFeed (feed: Omit<FeedDbObject, '_id'>): boolean { | ||
return !containFalsyValues(feed) | ||
return feed | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.