Skip to content

Commit

Permalink
refactor: address pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
1emu committed Sep 14, 2023
1 parent 334b046 commit a3d38f8
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 23 deletions.
10 changes: 7 additions & 3 deletions src/clients/SnapshotApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,9 @@ export class SnapshotApi {
}

async getScores(addresses: string[]) {
const { formattedAddresses, spaceName, network, strategies, scoreApiUrl } = await this.prepareArgs(addresses)
const { formattedAddresses, spaceName, network, strategies, scoreApiUrl } = await this.prepareScoresQueryArgs(
addresses
)

try {
const scores = await snapshot.utils.getScores(
Expand Down Expand Up @@ -189,7 +191,9 @@ export class SnapshotApi {
async ping(addressesSample: string[]) {
const addresses = addressesSample.length === 0 ? DEBUG_ADDRESSES : addressesSample
try {
const { formattedAddresses, spaceName, network, strategies, scoreApiUrl } = await this.prepareArgs(addresses)
const { formattedAddresses, spaceName, network, strategies, scoreApiUrl } = await this.prepareScoresQueryArgs(
addresses
)
const now = new Date()
const startTime = now.getTime()
await snapshot.utils.getScores(spaceName, strategies, network, formattedAddresses, undefined, scoreApiUrl)
Expand All @@ -200,7 +204,7 @@ export class SnapshotApi {
}
}

private async prepareArgs(addresses: string[]) {
private async prepareScoresQueryArgs(addresses: string[]) {
const formattedAddresses = addresses.map((address) => getChecksumAddress(address))
const spaceName = SnapshotApi.getSpaceName()
const network = getEnvironmentChainId().toString()
Expand Down
10 changes: 5 additions & 5 deletions src/clients/SnapshotGraphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,8 @@ export class SnapshotGraphql extends API {
const now = new Date()
const startTime = now.getTime()
try {
const startDate = getAMonthAgo(now).getTime()
const endDate = now.getTime()
const response = await this.fetch<SnapshotVoteResponse>(
GRAPHQL_ENDPOINT,
this.options()
Expand All @@ -456,15 +458,13 @@ export class SnapshotGraphql extends API {
variables: {
space: SNAPSHOT_SPACE,
first: 10,
start: getQueryTimestamp(getAMonthAgo(now).getTime()),
end: getQueryTimestamp(now.getTime()),
start: getQueryTimestamp(startDate),
end: getQueryTimestamp(endDate),
},
})
)

const addressesSample = response?.data?.votes.map((vote: SnapshotVote) => vote.voter)

const endTime = new Date().getTime()
const addressesSample = response?.data?.votes.map((vote: SnapshotVote) => vote.voter)
return { responseTime: endTime - startTime, addressesSample }
} catch (error) {
return { responseTime: -1, addressesSample: [] } // Return -1 to indicate API failures
Expand Down
16 changes: 7 additions & 9 deletions src/components/Debug/SnapshotStatus.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default function SnapshotStatus() {
const status = await Governance.get().getSnapshotStatus()
logIfNotNormal(status)
const show =
true || status.scoresStatus.health === ServiceHealth.Slow || status.scoresStatus.health === ServiceHealth.Failing
status.scoresStatus.health === ServiceHealth.Slow || status.scoresStatus.health === ServiceHealth.Failing
setShowTopBar(show)
setStatus((prev) => ({ ...prev, snapshotStatusBarOpen: show }))
}
Expand All @@ -39,13 +39,11 @@ export default function SnapshotStatus() {
}, [])

return (
<>
<div className={classNames(`SnapshotStatus__TopBar`, showTopBar && 'SnapshotStatus__TopBar--visible')}>
<WarningTriangle size="18" />
<Markdown size="sm" componentsClassNames={{ p: 'SnapshotStatus__Text', strong: 'SnapshotStatus__Text' }}>
{t('page.debug.snapshot_status.label')}
</Markdown>
</div>
</>
<div className={classNames(`SnapshotStatus__TopBar`, showTopBar && 'SnapshotStatus__TopBar--visible')}>
<WarningTriangle size="18" />
<Markdown size="sm" componentsClassNames={{ p: 'SnapshotStatus__Text', strong: 'SnapshotStatus__Text' }}>
{t('page.debug.snapshot_status.label')}
</Markdown>
</div>
)
}
12 changes: 6 additions & 6 deletions src/services/SnapshotService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class SnapshotService {
return cachedStatus
}

const snapshotStatus = await this.sense()
const snapshotStatus = await this.ping()
CacheService.set('snapshotStatus', snapshotStatus, STATUS_CACHE_TIMEOUT_IN_SECONDS)

logger.log('Snapshot status:', snapshotStatus)
Expand All @@ -48,13 +48,13 @@ export class SnapshotService {
}
}

private static async sense(): Promise<SnapshotStatus> {
const { status: graphQlStatus, addressesSample } = await this.senseGraphQl()
const scoresStatus = await this.senseScores(addressesSample)
private static async ping(): Promise<SnapshotStatus> {
const { status: graphQlStatus, addressesSample } = await this.pingGraphQl()
const scoresStatus = await this.pingScores(addressesSample)
return { scoresStatus, graphQlStatus }
}

private static async senseScores(addressesSample: string[]): Promise<ServiceStatus> {
private static async pingScores(addressesSample: string[]): Promise<ServiceStatus> {
let scoresHealth = ServiceHealth.Normal
const responseTime = await SnapshotApi.get().ping(addressesSample)
if (responseTime === -1) {
Expand All @@ -65,7 +65,7 @@ export class SnapshotService {
return { health: scoresHealth, responseTime }
}

private static async senseGraphQl(): Promise<{ status: ServiceStatus; addressesSample: string[] }> {
private static async pingGraphQl(): Promise<{ status: ServiceStatus; addressesSample: string[] }> {
let health = ServiceHealth.Normal
const { responseTime, addressesSample } = await SnapshotGraphql.get().ping()
if (responseTime === -1) {
Expand Down

0 comments on commit a3d38f8

Please sign in to comment.