-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from noislabs/dev
Add customer usage data
- Loading branch information
Showing
3 changed files
with
110 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ import { HttpBatchClient, Tendermint34Client } from "npm:@cosmjs/tendermint-rpc" | |
import * as promclient from "npm:prom-client"; | ||
import express from "npm:[email protected]"; | ||
import settings from "./settings.ts"; | ||
import { communityPoolFunds, totalSupply } from "./queries.ts"; | ||
import { communityPoolFunds, getContractUsage, getIbcChannels, totalSupply } from "./queries.ts"; | ||
|
||
function printableCoin(coin: Coin): string { | ||
if (coin.denom?.startsWith("u")) { | ||
|
@@ -17,6 +17,12 @@ function printableCoin(coin: Coin): string { | |
} | ||
} | ||
|
||
|
||
function mapChannelToDescription(chainId: string, channelId: string): string { | ||
// Default to a unknown if the channel is not found | ||
return settings[chainId].mappingChannels[channelId] || "unknown-proxy"; | ||
} | ||
|
||
export interface Account { | ||
readonly name: string; | ||
readonly address: string; | ||
|
@@ -32,7 +38,6 @@ function errorLog(msg: string) { | |
} | ||
|
||
if (import.meta.main) { | ||
|
||
const app = express(); | ||
|
||
const balances = new Map<string, string>(); | ||
|
@@ -43,6 +48,18 @@ if (import.meta.main) { | |
labelNames: ["account", "rpcEndpoint", "chainId"] as const, | ||
}); | ||
|
||
const customerUsageGauge = new promclient.Gauge({ | ||
name: "customer_usage", | ||
help: "Usage data per customer chain", | ||
labelNames: [ | ||
"channel", | ||
"rpcEndpoint", | ||
"chainId", | ||
"description", | ||
"address", | ||
] as const, | ||
}); | ||
|
||
// Updates all gauges with the current balances | ||
const gaugify = () => { | ||
for (const [key, val] of balances.entries()) { | ||
|
@@ -53,6 +70,7 @@ if (import.meta.main) { | |
// deno-lint-ignore no-explicit-any | ||
app.get("/metrics", (_req: any, res: any) => { | ||
gaugify(); | ||
updateContractUsage(); | ||
|
||
res.set("Content-Type", promclient.register.contentType); | ||
promclient.register.metrics().then((metrics) => res.end(metrics)); | ||
|
@@ -91,21 +109,57 @@ if (import.meta.main) { | |
}; | ||
|
||
const updateCommunityPool = () => { | ||
communityPoolFunds(tmClient, "unois").then((balance) => { | ||
const exportAccountName = "community pool"; | ||
debugLog(`${exportAccountName}: ${printableCoin(balance)}`); | ||
balances.set(exportAccountName, balance.amount); | ||
}, (err) => errorLog(err.toString())); | ||
communityPoolFunds(tmClient, "unois").then( | ||
(balance) => { | ||
const exportAccountName = "community pool"; | ||
debugLog(`${exportAccountName}: ${printableCoin(balance)}`); | ||
balances.set(exportAccountName, balance.amount); | ||
}, | ||
(err) => errorLog(err.toString()) | ||
); | ||
}; | ||
|
||
const updateContractUsage = async () => { | ||
try { | ||
const contractState = await getContractUsage( | ||
tmClient, | ||
settings[chainId].gatewayAddr | ||
); | ||
|
||
// get an array of channels infos | ||
const channelsInfo = await getIbcChannels(tmClient); | ||
|
||
// array of customer data | ||
for (const customer of contractState.customers) { | ||
const description = mapChannelToDescription(chainId, customer.channel_id); | ||
const connection = channelsInfo.filter(channel => channel.channelId === customer.channel_id); | ||
|
||
customerUsageGauge.set( | ||
{ | ||
channel: customer.channel_id, | ||
rpcEndpoint: rpcEndpoint, | ||
chainId: chainId, | ||
description: description, | ||
address: connection[0].counterparty?.portId?.slice(5) | ||
}, | ||
customer.requested_beacons | ||
); | ||
} | ||
} catch (err) { | ||
errorLog(err.toString()); | ||
} | ||
}; | ||
|
||
// Initial call | ||
updateAccounts(); | ||
updateTotalSupply(); | ||
updateCommunityPool(); | ||
updateContractUsage(); | ||
|
||
setInterval(updateAccounts, 10_000); | ||
setInterval(updateTotalSupply, 45_000); | ||
setInterval(updateCommunityPool, 100_000); | ||
setInterval(updateContractUsage, 60_000); | ||
|
||
const port = 3000; | ||
app.listen(port, function () { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters