Skip to content

Commit

Permalink
feat(hapi): avoid requesting duplicate endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
Torresmorah committed Mar 7, 2023
1 parent 8462d17 commit 8550d14
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 12 deletions.
10 changes: 3 additions & 7 deletions hapi/src/services/node.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,11 @@ const updateEndpointInfo = async endpoint => {
}
`

const { nodeInfo, ...response } = await producerUtil.getNodeInfo(
endpoint.value
)

await hasuraUtil.request(updateMutation, {
id: endpoint.id,
response,
head_block_time: nodeInfo?.head_block_time || null,
updated_at: new Date()
response: endpoint?.response || {},
head_block_time: endpoint?.head_block_time || null,
updated_at: endpoint.updated_at
})
}

Expand Down
38 changes: 33 additions & 5 deletions hapi/src/services/producer.service.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { hasuraUtil, sequelizeUtil } = require('../utils')
const { hasuraUtil, sequelizeUtil, producerUtil } = require('../utils')
const { eosConfig } = require('../config')

const lacchainService = require('./lacchain.service')
Expand Down Expand Up @@ -71,7 +71,6 @@ const syncProducers = async () => {
if (producers?.length) {
producers = await updateProducers(producers)
await syncNodes(producers.slice(0, eosConfig.eosTopLimit))
await syncEndpoints()

if (!eosConfig.stateHistoryPluginEndpoint) {
await statsService.sync()
Expand Down Expand Up @@ -112,7 +111,7 @@ const syncNodes = async producers => {
const syncEndpoints = async () => {
const query = `
query {
endpoints: endpoint (where: {type: {_in: ["api","ssl"]}}) {
endpoints: endpoint (where: {type: {_in: ["api","ssl"]}}, order_by: {value: asc}) {
id,
type,
value
Expand All @@ -123,9 +122,38 @@ const syncEndpoints = async () => {

if (!endpoints?.length) return

endpoints.forEach(async endpoint => {
const checkedList = []
const today = new Date()

today.setHours(0,0,0,0)

for(index in endpoints){
const endpoint = {...endpoints[index]}
const previous = checkedList[checkedList.length - 1]
let startTime

if(previous?.value === endpoint.value){
endpoint.response = previous.response
endpoint.head_block_time = previous.head_block_time
endpoint.updated_at = previous.updated_at
}else{
startTime = new Date()
const {nodeInfo, ...reponse} = await producerUtil.getNodeInfo(
endpoint.value
)
endpoint.time = (new Date() - startTime) / 1000

endpoint.response = reponse
endpoint.head_block_time = nodeInfo?.head_block_time || null
endpoint.updated_at = new Date()
}

await nodeService.updateEndpointInfo(endpoint)
})

if(previous?.value !== endpoint.value){
checkedList.push(endpoint)
}
}
}

const requestProducers = async ({ where, whereEndpointList }) => {
Expand Down

0 comments on commit 8550d14

Please sign in to comment.