Skip to content

Commit

Permalink
Merge pull request #1232 from edenia/fix/ATH-is-not-working-1229
Browse files Browse the repository at this point in the history
Fix/ath is not working 1229
  • Loading branch information
xavier506 authored Jul 11, 2023
2 parents 647d477 + d56f080 commit f88c99b
Show file tree
Hide file tree
Showing 12 changed files with 191 additions and 138 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/deploy-libre-testnet.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:
REACT_APP_TITLE: 'Libre Testnet Network Dashboard'
REACT_APP_VERSION: ${{github.ref}}
REACT_APP_DEFAULT_PRODUCER_LOGO: 'https://antelope.tools/images/libre.png'
REACT_APP_FOOTER_LINKS: '[{ "text": "Libre Website", "src": "https://libre.org/" },{ "text": "Libre Block Explorer", "src": "https://libre-testnet-explorer.edenia.cloud/" },{"text": "Documentation","src": "https://libre-chain.gitbook.io/"},{"text": "Libre Network Monitor","src": "https://libre-testnet.antelope.tools"}]'
REACT_APP_FOOTER_LINKS: '[{ "text": "Libre Website", "src": "https://libre.org/" },{ "text": "Libre Block Explorer", "src": "https://testnet.libre.org/v2/explore" },{"text": "Documentation","src": "https://libre-chain.gitbook.io/"},{"text": "Libre Network Monitor","src": "https://libre-testnet.antelope.tools"}]'
REACT_APP_EOS_RATE_LINK: ''
REACT_APP_USE_REWARDS: 'true'
REACT_APP_USE_VOTES: 'true'
Expand All @@ -57,7 +57,7 @@ jobs:
REACT_APP_TOKEN_SYMBOL: 'LIBRE'
REACT_APP_NETWORK_URL: '[{"label":"EOS","value":"https://eos.antelope.tools","mainnet":true,"pair":"eos","icon":"eos","order":1},{"label":"Proton","value":"https://proton.antelope.tools","mainnet":true,"pair":"proton","icon":"proton","order":2},{"label":"WAX","value":"https://wax.antelope.tools","mainnet":true,"pair":"wax","icon":"wax","order":3},{"label":"Telos","value":"https://telos.antelope.tools","mainnet":true,"pair":"telos","icon":"telos","order":4},{"label":"Libre","value":"https://libre.antelope.tools","mainnet":true,"pair":"libre","icon":"libre","order":5},{"label":"LACChain EOSIO","value":"https://lacchain.antelope.tools","mainnet":true,"pair":null,"icon":"lacchain","order":6},{"label":"Jungle4 Testnet","value":"https://jungle.antelope.tools","mainnet":false,"pair":"eos","icon":"jungle","order":1},{"label":"Proton Testnet","value":"https://proton-testnet.antelope.tools","mainnet":false,"pair":"proton","icon":"proton","order":2},{"label":"WAX Testnet","value":"https://wax-testnet.antelope.tools","mainnet":false,"pair":"wax","icon":"wax","order":3},{"label":"Telos Testnet","value":"https://telos-testnet.antelope.tools","mainnet":false,"pair":"telos","icon":"telos","order":4},{"label":"Libre Testnet","value":"https://libre-testnet.antelope.tools","mainnet":false,"pair":"libre","icon":"libre","order":5},{"label":"Ultra Testnet","value":"https://ultra-testnet.antelope.tools","mainnet":false,"pair":"ultra","icon":"ultra","order":6}]'
REACT_APP_DISABLED_MENU_ITEMS: '["/missed-blocks"]'
REACT_APP_BLOCK_EXPLORER_URL: 'https://libre-testnet-explorer.edenia.cloud/transaction/(transaction)'
REACT_APP_BLOCK_EXPLORER_URL: 'https://testnet.libre.org/v2/explore/transaction/(transaction)'
REACT_APP_STATE_HISTORY_ENABLED: 'true'
REACT_APP_GOOGLE_ANALITIC_PAGE_ID: 'G-E6Y0EC9FT8'
REACT_APP_PUBLIC_RE_CAPTCHA_KEY: ${{ secrets.REACT_APP_PUBLIC_RE_CAPTCHA_KEY }}
Expand Down Expand Up @@ -85,7 +85,7 @@ jobs:
HAPI_EOS_STATE_HISTORY_PLUGIN_ENDPOINT: 'ws://api-node.libre-testnet:8080'
HAPI_EOS_MISSED_BLOCKS_ENABLED: 'false'
HAPI_EOS_BLOCK_HISTORY_DAYS: 90
HAPI_EOS_MAX_CPU_BLOCK: 100000
HAPI_EOS_MAX_CPU_BLOCK: 250000
HAPI_EOS_MAX_NET_BLOCK: 1048576
HAPI_EOS_API_CHAIN_ID: b64646740308df2ee06c6b72f34c0f7fa066d940e831f752db2006fcc2b78dee
HAPI_EOS_BASE_ACCOUNT: ${{ secrets.HAPI_EOS_BASE_ACCOUNT }}
Expand Down
175 changes: 80 additions & 95 deletions hapi/src/services/stats.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const Boom = require('@hapi/boom')
const { StatusCodes } = require('http-status-codes')
const moment = require('moment')

const { hasuraUtil, sequelizeUtil, sleepFor, eosUtil } = require('../utils')
const { hasuraUtil, sequelizeUtil, sleepFor } = require('../utils')

const STAT_ID = 'bceb5b75-6cb9-45af-9735-5389e0664847'

Expand Down Expand Up @@ -44,14 +44,16 @@ const _getMissedBlock = async (start, end) => {
const getTransactionsInTimeRage = async (start, end) => {
const [rows] = await sequelizeUtil.query(`
SELECT
sum(transactions_length)::integer as transactions_count
sum(transactions_length)::integer as transactions_count,
avg(block_history.cpu_usage)::numeric(5,2) as cpu_usage,
avg(block_history.net_usage)::numeric(6,3) as net_usage
FROM
block_history
WHERE
timestamp between '${start.toISOString()}' and '${end.toISOString()}'
`)

return rows?.[0]?.transactions_count || 0
return rows?.[0]
}

const getNodesSummary = async () => {
Expand All @@ -67,7 +69,7 @@ const getNodesSummary = async () => {
type
`)

rows.forEach(row => {
rows.forEach((row) => {
payload[row.node_type || 'unknown'] = row.nodes_count
total += row.nodes_count
})
Expand Down Expand Up @@ -119,12 +121,12 @@ const getBlockDistribution = async (range = '1 day') => {
`
const data = await hasuraUtil.request(query, { startTime })

data.items.forEach(item => {
data.items.forEach((item) => {
totalBloks += item.blocks
})

return data.items
.map(item => ({
.map((item) => ({
account: item.producer || 'N/A',
blocks: item.blocks,
percent: item.blocks === 0 ? 0 : item.blocks / totalBloks
Expand Down Expand Up @@ -225,7 +227,7 @@ const getCurrentMissedBlock = async () => {

let newData = data

rows.forEach(element => {
rows.forEach((element) => {
if (newData[element.account]) {
newData = {
...newData,
Expand All @@ -248,7 +250,7 @@ const getCurrentMissedBlock = async () => {
getCurrentMissedBlock()
}

const udpateStats = async payload => {
const udpateStats = async (payload) => {
const mutation = `
mutation ($id: uuid!, $payload: stat_set_input!) {
update_stat_by_pk(pk_columns: {id: $id}, _set: $payload) {
Expand All @@ -259,7 +261,7 @@ const udpateStats = async payload => {
await hasuraUtil.request(mutation, { id: STAT_ID, payload })
}

const insertStats = async payload => {
const insertStats = async (payload) => {
const mutation = `
mutation ($payload: stat_insert_input!) {
insert_stat_one(object: $payload) {
Expand All @@ -275,65 +277,49 @@ const getLastTPSAllTimeHigh = async () => {

if (!stats || !stats.last_block_at) return null

if (stats.tps_all_time_high) {
return {
...stats.tps_all_time_high,
last_block_at: stats.last_block_at
}
}

const scheduleHistoryInfo = await _getScheduleHystory()

if (!scheduleHistoryInfo) return null

return {
transactions_count: 0,
last_block_at: stats.last_block_at,
checked_at: moment(scheduleHistoryInfo.first_block_at)
.subtract(1, 'second')
.toISOString()
}
return stats.tps_all_time_high
}

const getBlockUsage = async blockNum => {
const block = await eosUtil.getBlock(blockNum)
const info = await eosUtil.getInfo()
let cpuUsage = 0
let netUsage = 0
const getTimestampBlock = async (position) => {
const query = `
query {
blocks: block_history(limit: 1, order_by: {block_num: ${
position === 'first' ? 'asc' : 'desc'
}}, where: {producer: {_neq: "NULL"}}) {
timestamp
}
}
`

block.transactions.forEach(transaction => {
cpuUsage += transaction.cpu_usage_us
netUsage += transaction.net_usage_words
})
const data = await hasuraUtil.request(query)

return {
id: block.id,
block_num: block.block_num,
transactions_count: block.transactions.length,
cpu_usage_us: cpuUsage,
block_cpu_limit: info.block_cpu_limit,
cpu_usage_percent: cpuUsage / info.block_cpu_limit,
net_usage_words: netUsage,
block_net_limit: info.block_net_limit,
net_usage_percent: netUsage / info.block_net_limit
}
return data?.blocks[0]?.timestamp
}

const syncTPSAllTimeHigh = async () => {
const lastValue = await getLastTPSAllTimeHigh()

let start
let end

if (!lastValue) {
await sleepFor(60)
syncTPSAllTimeHigh()
const firstBlockInDB = new Date(await getTimestampBlock('first'))

return
start = moment(firstBlockInDB)
end = moment(start).add(
59 + (500 - firstBlockInDB.getMilliseconds()) / 1000,
'seconds'
)
} else {
start = moment(lastValue.checked_at).add(0.5, 'second')
end = moment(start).add(59.5, 'seconds')
}

const start = moment(lastValue.checked_at).add(1, 'second')
const end = moment(start).add(59, 'seconds')
const lastBlockInDB = await getTimestampBlock('last')
const diff = end.diff(moment(new Date(lastBlockInDB)), 'seconds')

if (_checkDateGap(lastValue.last_block_at, end)) {
await sleepFor(moment(lastValue.last_block_at).diff(end, 'seconds'))
if (diff >= 0) {
await sleepFor(diff)
syncTPSAllTimeHigh()

return
Expand All @@ -351,6 +337,8 @@ const syncTPSAllTimeHigh = async () => {
SELECT
interval.value as datetime,
sum(block_history.transactions_length) as transactions_count,
avg(block_history.cpu_usage) as cpu_usage,
avg(block_history.net_usage) as net_usage,
array_to_string(array_agg(block_history.block_num), ',') as blocks
FROM
interval
Expand All @@ -361,27 +349,20 @@ const syncTPSAllTimeHigh = async () => {
ORDER BY
2 DESC
)
SELECT datetime, transactions_count::integer, blocks FROM tps LIMIT 1
SELECT datetime, transactions_count::integer, cpu_usage::numeric(5,2), net_usage::numeric(6,3), blocks FROM tps LIMIT 1
`)

if (!rows.length) {
await udpateStats({
tps_all_time_high: {
...lastValue,
checked_at: end.toISOString()
}
})
syncTPSAllTimeHigh()

return
}

const newValue = rows[0]

if (parseInt(newValue.transactions_count) < lastValue.transactions_count) {
if (
newValue &&
(!lastValue ||
parseInt(newValue.transactions_count) >= lastValue.transactions_count)
) {
await udpateStats({
tps_all_time_high: {
...lastValue,
...newValue,
blocks: newValue.blocks.split(','),
checked_at: end.toISOString()
}
})
Expand All @@ -390,20 +371,10 @@ const syncTPSAllTimeHigh = async () => {
return
}

const blocks = newValue.blocks.split(',')

for (let index = 0; index < blocks.length; index++) {
const block = await getBlockUsage(blocks[index])

blocks[index] = block
}

await udpateStats({
tps_all_time_high: {
...newValue,
checked_at: end.toISOString(),
transactions_count: parseInt(newValue.transactions_count),
blocks
...lastValue,
checked_at: end.toISOString()
}
})
syncTPSAllTimeHigh()
Expand All @@ -423,27 +394,41 @@ const sync = async () => {
await insertStats(payload)
}

const syncTransactionsInfo = async () => {
const transactionsInLastWeek = await getTransactionsInTimeRage(
moment().subtract(1, 'week'),
const getTransactionsStats = async (range) => {
const transactionsStats = await getTransactionsInTimeRage(
moment().subtract(1, range),
moment()
)
const payload = {
transactions_in_last_hour: await getTransactionsInTimeRage(
moment().subtract(1, 'hour'),
moment()
),
transactions_in_last_day: await getTransactionsInTimeRage(
moment().subtract(1, 'day'),
moment()
),
transactions_in_last_week: transactionsInLastWeek,
average_daily_transactions_in_last_week: transactionsInLastWeek / 7

return {
[`transactions_in_last_${range}`]:
transactionsStats?.transactions_count || 0,
[`average_cpu_usage_in_last_${range}`]: transactionsStats?.cpu_usage || 0,
[`average_net_usage_in_last_${range}`]: transactionsStats?.net_usage || 0
}
}

const syncTransactionsInfo = async () => {
const ranges = ['day', 'hour', 'week']
let payload

for (const range of ranges) {
const stats = await getTransactionsStats(range)

payload = {
...payload,
...stats
}
}

payload.average_daily_transactions_in_last_week =
payload.transactions_in_last_day / 7 || 0

const stats = await getStats()

if (stats) {
await udpateStats(payload)

return
}
await insertStats(payload)
Expand Down
2 changes: 1 addition & 1 deletion hapi/src/workers/producers.worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ const start = async () => {
if (eosConfig.missedBlocksServiceEnabled) {
run('SYNC MISSED BLOCKS', missedBlocksService.syncMissedBlocks)
run('SYNC MISSED BLOCKS PER PRODUCER', statsService.getCurrentMissedBlock)
run('SYNC SCHEDULE HISTORY', demuxService.init)
}

run('SYNC SCHEDULE HISTORY', demuxService.init)
run('SYNC TPS', statsService.syncTPSAllTimeHigh)
run(
'SYNC TRANSACTIONS INFO',
Expand Down
8 changes: 7 additions & 1 deletion hasura/metadata/databases/default/tables/public_stat.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
table:
schema: public
name: stat
schema: public
select_permissions:
- role: guest
permission:
columns:
- average_cpu_usage_in_last_day
- average_cpu_usage_in_last_hour
- average_cpu_usage_in_last_week
- average_daily_transactions_in_last_week
- average_net_usage_in_last_day
- average_net_usage_in_last_hour
- average_net_usage_in_last_week
- created_at
- id
- missed_blocks
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- Could not auto-generate a down migration.
-- Please write an appropriate down migration for the SQL below:
-- alter table "public"."stat" add column "average_cpu_usage_in_last_hour" numeric
-- null default '0', add column "average_cpu_usage_in_last_day" numeric
-- null default '0', add column "average_cpu_usage_in_last_week" numeric
-- null default '0', add column "average_net_usage_in_last_hour" numeric
-- null default '0', add column "average_net_usage_in_last_day" numeric
-- null default '0', add column "average_net_usage_in_last_week" numeric
-- null default '0';
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
alter table "public"."stat" add column "average_cpu_usage_in_last_hour" numeric
null default '0', add column "average_cpu_usage_in_last_day" numeric
null default '0', add column "average_cpu_usage_in_last_week" numeric
null default '0', add column "average_net_usage_in_last_hour" numeric
null default '0', add column "average_net_usage_in_last_day" numeric
null default '0', add column "average_net_usage_in_last_week" numeric
null default '0';
Loading

0 comments on commit f88c99b

Please sign in to comment.