Skip to content

Commit

Permalink
refactor(sync): get eden scope data from cronjob query (#697)
Browse files Browse the repository at this point in the history
* refactor(sync): get eden scope data from cronjob query

* refactor(hapi): change default reference to general

* refactor(hapi): change default reference to general

Co-authored-by: Leister Alvarez Campos <[email protected]>
  • Loading branch information
leisterfrancisco and Leister Alvarez Campos authored Aug 26, 2021
1 parent edd719f commit 4c0854a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
2 changes: 1 addition & 1 deletion hapi/src/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ const massiveDB = (async () => await massive(massiveConfig))()
module.exports = {
massiveConfig,
massiveDB,
defaultContractScope: process.env.HAPI_RATING_CONTRACT || 'rateproducer',
generalContractScope: process.env.HAPI_RATING_CONTRACT || 'rateproducer',
edenContractScope: 'eden'
}
14 changes: 7 additions & 7 deletions hapi/src/libs/sync-bp-stats.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const {
massiveDB,
defaultContractScope,
generalContractScope,
edenContractScope
} = require('../config')
const eosjs = require('eosjs')
Expand Down Expand Up @@ -33,7 +33,7 @@ const getBpStats = async (bp, scope) => {
const updateBpStats = async (bpName) => {
try {
const edenResult = await getEdenBpStats(bpName)
await getDefaultBpStats(bpName)
await getGeneralBpStats(bpName)

return edenResult
} catch (err) {
Expand All @@ -57,29 +57,29 @@ const getEdenBpStats = async (bpName) => {
}
}

const getDefaultBpStats = async (bpName) => {
const getGeneralBpStats = async (bpName) => {
try {
const bpStat = await getBpStats(bpName, defaultContractScope)
const bpStat = await getBpStats(bpName, generalContractScope)

if (!bpStat.rows.length || !bpStat.rows[0].bp === bpName)
return 'Did not find ratings for BP: ' + bpName

const stat = bpStat.rows[0]

return await updateBpStatsDefault(bpName, stat)
return await updateBpStatsGeneral(bpName, stat)
} catch (err) {
console.error(`sync-bp-stats: ${err}`)
}
}

const updateBpStatsDefault = async (bpName, stat) => {
const updateBpStatsGeneral = async (bpName, stat) => {
const db = await massiveDB
const resultRatingsSave = await db.ratings_stats.save(stat)
const dbResult = resultRatingsSave
? resultRatingsSave
: await db.ratings_stats.insert(stat)
console.log(
`Default save or insert of ${bpName} was ${
`General save or insert of ${bpName} was ${
dbResult ? 'SUCCESSFULL' : 'UNSUCCESSFULL'
}`
)
Expand Down
25 changes: 19 additions & 6 deletions hapi/src/libs/sync-stats.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
#!/usr/bin/env node
const { JsonRpc } = require('eosjs')
const fetch = require('node-fetch')
const { massiveDB } = require('../config')
const {
generalContractScope,
edenContractScope,
massiveDB
} = require('../config')

const HAPI_EOS_API_ENDPOINT = process.env.HAPI_EOS_API_ENDPOINT || 'https://jungle.eosio.cr'
const HAPI_RATING_CONTRACT = process.env.HAPI_RATING_CONTRACT || 'rateproducer'

const getRatingsStats = async () => {
const getRatingsStats = async (scope) => {
const eos = new JsonRpc(HAPI_EOS_API_ENDPOINT, { fetch })

try {
const ratings = await eos.get_table_rows({
json: true,
code: HAPI_RATING_CONTRACT,
scope: HAPI_RATING_CONTRACT,
scope: scope,
table: 'stats',
limit: 1000,
reverse: false,
Expand All @@ -29,13 +33,22 @@ const getRatingsStats = async () => {

const updateRatingsStats = async () => {
console.log('==== Updating ratings stats ====')
const ratingsStats = await getRatingsStats()
const generalRatingsStats = await getRatingsStats(generalContractScope)
const edenRatingsStats = await getRatingsStats(edenContractScope)

ratingsStats.rows.forEach(async (rating) => {
generalRatingsStats.rows.forEach(async (rating) => {
try {
const resultRatingStatsSave = await (await massiveDB).ratings_stats.save(rating)
const dbResult = resultRatingStatsSave ? resultRatingStatsSave : await (await massiveDB).ratings_stats.insert(rating)
console.log(`Save or insert of ${rating.bp} was ${dbResult ? 'SUCCESSFULL' : 'UNSUCCESSFULL'}`)
console.log(`General save or insert of ${rating.bp} was ${dbResult ? 'SUCCESSFULL' : 'UNSUCCESSFULL'}`)
} catch (err) { console.log(`Error: ${err}`) }
})

edenRatingsStats.rows.forEach(async (rating) => {
try {
const resultRatingStatsSave = await (await massiveDB).eden_ratings_stats.save(rating)
const dbResult = resultRatingStatsSave ? resultRatingStatsSave : await (await massiveDB).eden_ratings_stats.insert(rating)
console.log(`Eden save or insert of ${rating.bp} was ${dbResult ? 'SUCCESSFULL' : 'UNSUCCESSFULL'}`)
} catch (err) { console.log(`Error: ${err}`) }
})
}
Expand Down

0 comments on commit 4c0854a

Please sign in to comment.