Skip to content

Commit

Permalink
Merge pull request #75 from pshenmic/feat/statistics
Browse files Browse the repository at this point in the history
Extend home page statistics
  • Loading branch information
pshenmic authored Oct 29, 2023
2 parents 22279c4 + 9e202e8 commit 472c5a1
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 11 deletions.
27 changes: 20 additions & 7 deletions packages/api/src/controllers/MainController.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const BlocksDAO = require('../dao/BlocksDAO')
const DataContractsDAO = require('../dao/DataContractsDAO')
const TransactionsDAO = require('../dao/TransactionsDAO')
const DocumentsDAO = require('../dao/DocumentsDAO')
const TenderdashRPC = require("../tenderdashRpc");

class MainController {
constructor(knex) {
Expand All @@ -12,15 +13,27 @@ class MainController {
}

getStatus = async (request, response) => {
const max = await this.blocksDAO.getMaxHeight()
let stats
let tdStatus

try {
stats = await this.blocksDAO.getStats()
tdStatus = await TenderdashRPC.getStatus();
} catch (e) {

}

response.send({
network: "dash-testnet-33",
appVersion: "1",
p2pVersion: "8",
blockVersion: "13",
blocksCount: max,
tenderdashVersion: "0.13.3"
appVersion: stats?.appVersion,
blockVersion: stats?.blockVersion,
blocksCount: stats?.topHeight,
blockTimeAverage: stats?.blockTimeAverage,
txCount: stats?.txCount,
transfersCount: stats?.transfersCount,
dataContractsCount: stats?.dataContractsCount,
documentsCount: stats?.documentsCount,
network: tdStatus?.network,
tenderdashVersion: tdStatus?.tenderdashVersion
});
}

Expand Down
61 changes: 61 additions & 0 deletions packages/api/src/dao/BlocksDAO.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,67 @@ module.exports = class BlockDAO {
return max
}

getStats = async () => {
// total tx count
// total documents count
// total data contracts
// total transfers


const blocksQuery = this.knex('blocks')
.select('height', 'timestamp', 'block_version', 'app_version', 'l1_locked_height')
.select(this.knex.raw('LAG(timestamp, 1) over (order by blocks.height asc) prev_timestamp'))
.orderBy('height', 'desc')
.limit(10)

const diffQuery = this.knex.with('with_alias', blocksQuery)
.select('height', 'timestamp', 'prev_timestamp', 'block_version', 'app_version', 'l1_locked_height')
.select(this.knex.raw('timestamp - prev_timestamp as diff'))
.from('with_alias')
.as('blocks')

const averageQuery = this.knex(diffQuery)
.select('height', 'timestamp', 'prev_timestamp', 'diff', 'block_version', 'app_version', 'l1_locked_height')
.select(this.knex.raw('avg(diff) over () average'))
.limit(1)
.as('asdasd')

const final = await this.knex(averageQuery)
.select('height', 'timestamp', 'prev_timestamp', 'diff', 'block_version', 'app_version', 'l1_locked_height', 'average')
.select(this.knex.raw('extract (epoch from average) as average_seconds'))
.select(this.knex('state_transitions').count('*').as('tx_count'))
.select(this.knex('transfers').count('*').as('transfers_count'))
.select(this.knex('data_contracts').count('*').as('data_contracts_count'))
.select(this.knex('documents').count('*').as('documents_count'))
.limit(1)

const [result] = final

const {
height,
block_version,
app_version,
l1_locked_height,
average_seconds,
tx_count,
transfers_count,
data_contracts_count,
documents_count
} = result

return {
topHeight: height,
blockTimeAverage: average_seconds,
blockVersion: block_version,
appVersion: app_version,
l1LockedHeight: l1_locked_height,
txCount: tx_count,
transfersCount: transfers_count,
dataContractsCount: data_contracts_count,
documentsCount: documents_count
}
}

getBlockByHash = async (blockHash) => {
const results = await this.knex
.select('blocks.hash as hash', 'state_transitions.hash as st_hash', 'blocks.height as height', 'blocks.timestamp as timestamp', 'blocks.block_version as block_version', 'blocks.app_version as app_version', 'blocks.l1_locked_height as l1_locked_height')
Expand Down
4 changes: 0 additions & 4 deletions packages/frontend/src/routes/home/home.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ function HomeRoute() {
<span>App Version</span>
<span>{appVersion}</span>
</div>
<div className="status_item">
<span>P2P version</span>
<span>{p2pVersion}</span>
</div>
<div className="status_item">
<span>Block version</span>
<span>{blockVersion}</span>
Expand Down

0 comments on commit 472c5a1

Please sign in to comment.