Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/npm_and_yarn/prettier-3.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
bajtos authored Nov 20, 2023
2 parents 2ad5f1d + dbc4f41 commit c042ec2
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ This command outputs metrics and activity events:
$ station
{
"totalJobsCompleted": 161,
"rewardsScheduledForAddress": "0"
"rewardsScheduledForAddress": "0.041033208757289921"
}
[4/19/2023, 9:26:54 PM] INFO Saturn Node will try to connect to the Saturn Orchestrator...
[4/19/2023, 9:26:54 PM] INFO Saturn Node was able to connect to the Orchestrator and will now start connecting to the Saturn network...
Expand Down
7 changes: 6 additions & 1 deletion commands/station.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { paths } from '../lib/paths.js'
import pRetry from 'p-retry'
import { fetch } from 'undici'
import { ethAddressFromDelegated } from '@glif/filecoin-address'
import { formatEther } from 'ethers'

const { FIL_WALLET_ADDRESS } = process.env

Expand Down Expand Up @@ -72,7 +73,11 @@ export const station = async ({ json, experimental }) => {
total: metrics.totalJobsCompleted
}))
} else {
console.log(JSON.stringify(metrics, null, 2))
console.log(JSON.stringify({
totalJobsCompleted: metrics.totalJobsCompleted,
rewardsScheduledForAddress:
formatEther(metrics.rewardsScheduledForAddress)
}, null, 2))
}
})

Expand Down
8 changes: 6 additions & 2 deletions lib/metrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export class MetricsEvent {
/**
* @param {Object} options
* @param {Number} options.totalJobsCompleted
* @param {String} options.rewardsScheduledForAddress
* @param {bigint} options.rewardsScheduledForAddress
*/
constructor ({ totalJobsCompleted, rewardsScheduledForAddress }) {
this.totalJobsCompleted = totalJobsCompleted
Expand Down Expand Up @@ -47,10 +47,14 @@ export class Metrics {
this.moduleMetrics.set(moduleName, metrics)
const mergedMetrics = {
totalJobsCompleted: 0,
rewardsScheduledForAddress: '0'
rewardsScheduledForAddress: 0n
}
for (const [, metrics] of this.moduleMetrics) {
mergedMetrics.totalJobsCompleted += metrics.totalJobsCompleted
// Merging rewards metrics should be revisited as more modules start
// paying rewards
mergedMetrics.rewardsScheduledForAddress +=
metrics.rewardsScheduledForAddress
}
const isChanged = this.mergedMetrics === null ||
Object
Expand Down
11 changes: 10 additions & 1 deletion lib/zinnia.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ async function handleEvents ({
onMetrics({
totalJobsCompleted: event.total,
rewardsScheduledForAddress:
(await contract.rewardsScheduledFor(ethAddress)).toString()
await getScheduledRewardsWithFallback(contract, ethAddress)
})
break

Expand All @@ -163,3 +163,12 @@ async function handleEvents ({
}
}
}

async function getScheduledRewardsWithFallback (contract, ethAddress) {
try {
return await contract.rewardsScheduledFor(ethAddress)
} catch (err) {
console.error('Failed to get scheduled rewards:', err.stack)
return 0n
}
}
16 changes: 8 additions & 8 deletions test/metrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ describe('Metrics', () => {
const metrics = new Metrics()
metrics.submit('module1', {
totalJobsCompleted: 1,
rewardsScheduledForAddress: '0'
rewardsScheduledForAddress: 1n
})
metrics.submit('module2', {
totalJobsCompleted: 2,
rewardsScheduledForAddress: '0'
rewardsScheduledForAddress: 2n
})
assert.deepStrictEqual(metrics.mergedMetrics, {
totalJobsCompleted: 3,
rewardsScheduledForAddress: '0'
rewardsScheduledForAddress: 3n
})
})
it('should filter duplicate entries', () => {
Expand All @@ -25,12 +25,12 @@ describe('Metrics', () => {
if (i === 0) {
assert.deepStrictEqual(metrics, {
totalJobsCompleted: 1,
rewardsScheduledForAddress: '0'
rewardsScheduledForAddress: 0n
})
} else if (i === 1) {
assert.deepStrictEqual(metrics, {
totalJobsCompleted: 2,
rewardsScheduledForAddress: '0'
rewardsScheduledForAddress: 0n
})
} else {
throw new Error('should not be called')
Expand All @@ -39,15 +39,15 @@ describe('Metrics', () => {
})
metrics.submit('module1', {
totalJobsCompleted: 1,
rewardsScheduledForAddress: '0'
rewardsScheduledForAddress: 0n
})
metrics.submit('module1', {
totalJobsCompleted: 1,
rewardsScheduledForAddress: '0'
rewardsScheduledForAddress: 0n
})
metrics.submit('module2', {
totalJobsCompleted: 1,
rewardsScheduledForAddress: '0'
rewardsScheduledForAddress: 0n
})
})
})
Expand Down

0 comments on commit c042ec2

Please sign in to comment.