Skip to content

Commit

Permalink
Merge pull request #50 from rsksmart/feat/status-remaining
Browse files Browse the repository at this point in the history
Feat/status repository remaining methods
  • Loading branch information
IOVgomezdn authored Mar 20, 2023
2 parents 09887ed + 627f5ba commit 219a157
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 25 deletions.
12 changes: 5 additions & 7 deletions src/api/Status.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,12 @@ export class Status extends DataCollector {
getState () {
return this.formatData(this.state)
}
getBlocksServiceStatus () {
async getBlocksServiceStatus () {
const Status = this.getModule('Status')
return Status.find({}, { timestamp: -1 }, 1)
.then(res => {
res = res.data[0]
if (res) delete (res._id)
return res
})
const { data } = await Status.find({}, { timestamp: -1 }, 1)
const [latestStatus] = data

return latestStatus
}
async updateState () {
try {
Expand Down
16 changes: 15 additions & 1 deletion src/converters/status.converters.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,18 @@ function rawStatusToEntity ({
}
}

export { rawStatusToEntity }
function statusEntityToRaw ({
pendingBlocks,
requestingBlocks,
nodeDown,
timestamp
}) {
return {
pendingBlocks,
requestingBlocks,
nodeDown,
timestamp: Number(timestamp)
}
}

export { rawStatusToEntity, statusEntityToRaw }
35 changes: 18 additions & 17 deletions src/repositories/status.repository.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
import { rawStatusToEntity } from '../converters/status.converters'
import { rawStatusToEntity, statusEntityToRaw } from '../converters/status.converters'
import {prismaClient} from '../lib/Setup'
import { createPrismaOrderBy, mongoQueryToPrisma } from './utils'

const statsEntitySelect = {
pendingBlocks: true,
requestingBlocks: true,
nodeDown: true,
timestamp: true
}

export const statusRepository = {
findOne (query = {}, project = {}, collection) {
return collection.findOne(query, project)
},
find (query = {}, project = {}, collection, sort = {}, limit = 0, isArray = true) {
if (isArray) {
return collection
.find(query, project)
.sort(sort)
.limit(limit)
.toArray()
} else {
return collection
.find(query, project)
.sort(sort)
.limit(limit)
}
async find (query = {}, project = {}, collection, sort = {}, limit = 0, isArray = true) {
const statusArr = await prismaClient.status.findMany({
where: mongoQueryToPrisma(query),
select: statsEntitySelect,
orderBy: createPrismaOrderBy(sort),
take: limit
})

return statusArr.map(status => statusEntityToRaw(status))
},
async insertOne (data, collection) {
await prismaClient.status.create({ data: rawStatusToEntity(data) })
Expand Down

0 comments on commit 219a157

Please sign in to comment.