Skip to content

Commit

Permalink
Merge pull request #561 from eoscostarica/feat/daily-transactions
Browse files Browse the repository at this point in the history
feat: add unique locations and daily transactions
  • Loading branch information
xavier506 authored May 31, 2021
2 parents d6ab41e + a357111 commit 1a32c71
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 92 deletions.
125 changes: 70 additions & 55 deletions hapi/src/services/stats.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,49 +6,59 @@ const { hasuraUtil, sequelizeUtil, sleepFor, eosUtil } = require('../utils')

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

const getTransactionsInTimeRage = async payload => {
const query = `
query($start: timestamptz!, $end: timestamptz!) {
block: block_history_aggregate(
where: { timestamp: { _gte: $start, _lte: $end } }
) {
info: aggregate {
sum {
transactions_length
}
}
}
}
`
const data = await hasuraUtil.request(query, payload)
const getTransactionsInTimeRage = async (start, end) => {
const [rows] = await sequelizeUtil.query(`
SELECT
sum(transactions_length)::integer as transactions_count
FROM
block_history
WHERE
timestamp between '${start.toISOString()}' and '${end.toISOString()}'
`)

return data.block.info.sum.transactions_length || 0
return rows?.[0]?.transactions_count || 0
}

const getNodesSummary = async payload => {
const query = `
query {
producers: producer {
bp_json
}
}
`
const data = await hasuraUtil.request(query, payload)
const getNodesSummary = async () => {
let total = 0
const totalByType = {}

data.producers.forEach(producer => {
producer.bp_json.nodes.forEach(node => {
if (!totalByType[node.node_type]) {
totalByType[node.node_type] = 0
}
const payload = {}
const [rows] = await sequelizeUtil.query(`
SELECT
value->>'node_type' as node_type,
count(*)::integer as nodes_count
FROM
producer,
json_array_elements(to_json(bp_json->'nodes'))
GROUP BY
value->>'node_type'
`)

totalByType[node.node_type]++
total++
})
rows.forEach(row => {
payload[row.node_type || 'unknown'] = row.nodes_count
total += row.nodes_count
})

return { total, ...totalByType }
return { ...payload, total }
}

const getUniqueLocations = async () => {
const [rows] = await sequelizeUtil.query(`
SELECT
bp_json->'org'->'location'->>'country' as type,
count(*)::integer as producers_count,
STRING_AGG (owner, ',') as producers
FROM
producer
GROUP BY
bp_json->'org'->'location'->>'country'
ORDER BY
producers_count DESC
`)

return {
count: rows?.length || 0,
details: rows
}
}

const getBlockDistribution = async (range = '1 day') => {
Expand Down Expand Up @@ -101,6 +111,7 @@ const getStats = async () => {
transactions_in_last_hour
transactions_in_last_day
transactions_in_last_week
average_daily_transactions_in_last_week
last_round
last_block_at
tps_all_time_high
Expand Down Expand Up @@ -263,12 +274,6 @@ const syncTPSAllTimeHigh = async () => {
}

const newValue = rows[0]
const blocks = newValue.blocks.split(',')

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

if (parseInt(newValue.transactions_count) < lastValue.transactions_count) {
await udpateStats({
Expand All @@ -277,10 +282,18 @@ const syncTPSAllTimeHigh = async () => {
checked_at: end.toISOString()
}
})
syncTPSAllTimeHigh()

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,
Expand All @@ -293,24 +306,26 @@ const syncTPSAllTimeHigh = async () => {
}

const sync = async () => {
const transactionsInLastWeek = await getTransactionsInTimeRage({
start: moment().subtract(1, 'week'),
end: moment()
})
const transactionsInLastWeek = await getTransactionsInTimeRage(
moment().subtract(1, 'week'),
moment()
)
const payload = {
transactions_in_last_hour: await getTransactionsInTimeRage({
start: moment().subtract(1, 'hour'),
end: moment()
}),
transactions_in_last_day: await getTransactionsInTimeRage({
start: moment().subtract(1, 'day'),
end: moment()
}),
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,
nodes_summary: await getNodesSummary()
nodes_summary: await getNodesSummary(),
unique_locations: await getUniqueLocations()
}
const stats = await getStats()

if (stats) {
await udpateStats(payload)
return
Expand Down
1 change: 1 addition & 0 deletions hasura/metadata/tables.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,6 @@
- transactions_in_last_day
- transactions_in_last_hour
- transactions_in_last_week
- unique_locations
- updated_at
filter: {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- Could not auto-generate a down migration.
-- Please write an appropriate down migration for the SQL below:
-- alter table "public"."stat" add column "unique_locations" jsonb
null default jsonb_build_object();
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
alter table "public"."stat" add column "unique_locations" jsonb
null default jsonb_build_object();
32 changes: 30 additions & 2 deletions webapp/src/components/TransactionsHistory.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ const TransactionsHistory = ({ t, classes }) => {
>
{data?.stats?.[0]?.tps_all_time_high?.blocks?.map(
(block, index) => (
<>
<span key={index}>
<a
href={getBlockNumUrl(block.block_num)}
target="_blank"
Expand All @@ -87,7 +87,7 @@ const TransactionsHistory = ({ t, classes }) => {
data?.stats?.[0]?.tps_all_time_high?.blocks.length - 1
? ', '
: ''}
</>
</span>
)
)}
</Typography>
Expand Down Expand Up @@ -126,6 +126,22 @@ const TransactionsHistory = ({ t, classes }) => {
</Card>
</Grid>

<Grid item xs={12} sm={4} lg={3}>
<Card>
<CardContent className={classes.cards}>
<Typography>{`${t('transactions')} ${t(
'dailyAverage'
)}`}</Typography>
<BodyGraphValue
value={formatWithThousandSeparator(
data?.stats?.[0]?.average_daily_transactions_in_last_week || 0
)}
loading={loading}
/>
</CardContent>
</Card>
</Grid>

<Grid item xs={12} sm={4} lg={3}>
<Card>
<CardContent className={classes.cards}>
Expand All @@ -139,6 +155,18 @@ const TransactionsHistory = ({ t, classes }) => {
</CardContent>
</Card>
</Grid>

<Grid item xs={12} sm={4} lg={3}>
<Card>
<CardContent className={classes.cards}>
<Typography>{`${t('uniqueLocations')}`}</Typography>
<BodyGraphValue
value={data?.stats?.[0]?.unique_locations?.count || 0}
loading={loading}
/>
</CardContent>
</Card>
</Grid>
</>
)
}
Expand Down
1 change: 1 addition & 0 deletions webapp/src/gql/producer.gql.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export const BLOCK_TRANSACTIONS_HISTORY = gql`
transactions_in_last_week
average_daily_transactions_in_last_week
tps_all_time_high
unique_locations
updated_at
}
}
Expand Down
4 changes: 3 additions & 1 deletion webapp/src/language/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"lastHour": "Last Hour",
"lastDay": "Last Day",
"lastWeek": "Last Week",
"dailyAverage":"Daily Average",
"lastYear": "Last Year",
"itemsPerPage": "Items per page",
"api": "API",
Expand Down Expand Up @@ -115,7 +116,8 @@
"query": " Query Nodes",
"producer": "Producer Nodes",
"query,seed": "Nodos Query, Seed",
"undefined": "Undefined Nodes"
"undefined": "Undefined Nodes",
"uniqueLocations": "Unique Locations"
},
"blockProducersRoute": {},
"rewardsDistributionRoute": {
Expand Down
4 changes: 3 additions & 1 deletion webapp/src/language/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"lastDay": "Último día",
"lastWeek": "Última semana",
"lastYear": "Último año",
"dailyAverage":"Promedio diario",
"itemsPerPage": "Elementos por página",
"api": "API",
"ssl": "SSL",
Expand Down Expand Up @@ -123,7 +124,8 @@
"producer": "Nodos Tipo Producer",
"query,seed": "Nodos Query, Seed",
"undefined": "Nodos Indefinidos",
"transactions": "Transacciones"
"transactions": "Transacciones",
"uniqueLocations": "Ubicaciones únicas"
},
"blockProducersRoute": {},
"rewardsDistributionRoute": {
Expand Down
18 changes: 9 additions & 9 deletions webapp/src/models/eos.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ export default {
producers: []
},
info: {},
tps: [],
tpb: [],
tps: new Array(30).fill({ blocks: [], transactions: 0 }),
tpb: new Array(60).fill({ blocks: [], transactions: 0 }),
tpsWaitingBlock: null
},
reducers: {
Expand All @@ -31,16 +31,16 @@ export default {
updateTransactionsStats(state, item) {
let tpb = state.tpb

if (state.tpb.length >= 30) {
tpb = state.tpb.splice(1, state.tpb.length)
if (state.tpb.length >= 60) {
tpb.pop()
}

tpb = [
...tpb,
{
blocks: [item.block],
transactions: item.transactions
}
},
...tpb
]

if (!state.tpsWaitingBlock) {
Expand All @@ -54,15 +54,15 @@ export default {
let tps = state.tps

if (state.tps.length >= 30) {
tps = state.tps.splice(1, state.tps.length)
tps.pop()
}

tps = [
...tps,
{
blocks: [state.tpsWaitingBlock.block, item.block],
transactions: state.tpsWaitingBlock.transactions + item.transactions
}
},
...tps
]

return {
Expand Down
Loading

0 comments on commit 1a32c71

Please sign in to comment.