optimize block audit scores db query
This commit is contained in:
parent
5fc3b8b70c
commit
de04914851
@ -133,45 +133,6 @@ class Audit {
|
|||||||
score
|
score
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public async $getBlockAuditScores(fromHeight?: number, limit: number = 15): Promise<AuditScore[]> {
|
|
||||||
let currentHeight = fromHeight !== undefined ? fromHeight : await blocksRepository.$mostRecentBlockHeight();
|
|
||||||
const returnScores: AuditScore[] = [];
|
|
||||||
|
|
||||||
if (currentHeight < 0) {
|
|
||||||
return returnScores;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (let i = 0; i < limit && currentHeight >= 0; i++) {
|
|
||||||
const block = blocks.getBlocks().find((b) => b.height === currentHeight);
|
|
||||||
if (block?.extras?.matchRate != null) {
|
|
||||||
returnScores.push({
|
|
||||||
hash: block.id,
|
|
||||||
matchRate: block.extras.matchRate
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
let currentHash;
|
|
||||||
if (!currentHash && Common.indexingEnabled()) {
|
|
||||||
const dbBlock = await blocksRepository.$getBlockByHeight(currentHeight);
|
|
||||||
if (dbBlock && dbBlock['id']) {
|
|
||||||
currentHash = dbBlock['id'];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!currentHash) {
|
|
||||||
currentHash = await bitcoinApi.$getBlockHash(currentHeight);
|
|
||||||
}
|
|
||||||
if (currentHash) {
|
|
||||||
const auditScore = await blocksAuditsRepository.$getBlockAuditScore(currentHash);
|
|
||||||
returnScores.push({
|
|
||||||
hash: currentHash,
|
|
||||||
matchRate: auditScore?.matchRate
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
currentHeight--;
|
|
||||||
}
|
|
||||||
return returnScores;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default new Audit();
|
export default new Audit();
|
@ -283,9 +283,12 @@ class MiningRoutes {
|
|||||||
|
|
||||||
private async $getBlockAuditScores(req: Request, res: Response) {
|
private async $getBlockAuditScores(req: Request, res: Response) {
|
||||||
try {
|
try {
|
||||||
const height = req.params.height === undefined ? undefined : parseInt(req.params.height, 10);
|
let height = req.params.height === undefined ? undefined : parseInt(req.params.height, 10);
|
||||||
|
if (height == null) {
|
||||||
|
height = await BlocksRepository.$mostRecentBlockHeight();
|
||||||
|
}
|
||||||
res.setHeader('Expires', new Date(Date.now() + 1000 * 60).toUTCString());
|
res.setHeader('Expires', new Date(Date.now() + 1000 * 60).toUTCString());
|
||||||
res.json(await audits.$getBlockAuditScores(height, 15));
|
res.json(await BlocksAuditsRepository.$getBlockAuditScores(height, height - 15));
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
res.status(500).send(e instanceof Error ? e.message : e);
|
res.status(500).send(e instanceof Error ? e.message : e);
|
||||||
}
|
}
|
||||||
|
@ -93,6 +93,20 @@ class BlocksAuditRepositories {
|
|||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async $getBlockAuditScores(maxHeight: number, minHeight: number): Promise<AuditScore[]> {
|
||||||
|
try {
|
||||||
|
const [rows]: any[] = await DB.query(
|
||||||
|
`SELECT hash, match_rate as matchRate
|
||||||
|
FROM blocks_audits
|
||||||
|
WHERE blocks_audits.height BETWEEN ? AND ?
|
||||||
|
`, [minHeight, maxHeight]);
|
||||||
|
return rows;
|
||||||
|
} catch (e: any) {
|
||||||
|
logger.err(`Cannot fetch block audit from db. Reason: ` + (e instanceof Error ? e.message : e));
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default new BlocksAuditRepositories();
|
export default new BlocksAuditRepositories();
|
||||||
|
@ -52,8 +52,7 @@
|
|||||||
[ngStyle]="{'width': (100 - (auditScores[block.id] || 0)) + '%' }"></div>
|
[ngStyle]="{'width': (100 - (auditScores[block.id] || 0)) + '%' }"></div>
|
||||||
<div class="progress-text">
|
<div class="progress-text">
|
||||||
<span *ngIf="auditScores[block.id] != null;">{{ auditScores[block.id] }}%</span>
|
<span *ngIf="auditScores[block.id] != null;">{{ auditScores[block.id] }}%</span>
|
||||||
<span *ngIf="auditScores[block.id] === undefined" class="skeleton-loader"></span>
|
<span *ngIf="auditScores[block.id] == null">~</span>
|
||||||
<span *ngIf="auditScores[block.id] === null">~</span>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</a>
|
</a>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user