-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(status-api): Overall Status endpoint (#1338)
* /overall/status controller Signed-off-by: Suraj Auwal <[email protected]> * Minor changes to README.md Signed-off-by: Suraj Auwal <[email protected]> * Updated Imports Signed-off-by: Suraj Auwal <[email protected]> * Changed endpoint to stay consistent. Signed-off-by: Suraj Auwal <[email protected]> * Updated tests Signed-off-by: Suraj Auwal <[email protected]> * Refactored AgregateStatusController Signed-off-by: Suraj Auwal <[email protected]> * Refactored AgregateStatusController Signed-off-by: Suraj Auwal <[email protected]> * Removed redundant code Signed-off-by: Suraj Auwal <[email protected]> * Test fixtures and documentation Signed-off-by: Suraj Auwal <[email protected]> * Renamed files Signed-off-by: Suraj Auwal <[email protected]> * Updated test fixtures Signed-off-by: Suraj Auwal <[email protected]> * Removed any in test files Signed-off-by: Suraj Auwal <[email protected]> * Controller should return outage when ocean is not ready. Signed-off-by: Suraj Auwal <[email protected]> * Added comments for better clarity Signed-off-by: Suraj Auwal <[email protected]> * Updated as per Eli's comment Co-authored-by: Joel-David Wong <[email protected]>
- Loading branch information
1 parent
54a37cb
commit 6d2148f
Showing
7 changed files
with
193 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
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
137 changes: 137 additions & 0 deletions
137
apps/status-api/__test__/controllers/OverallStatusController.test.ts
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 |
---|---|---|
@@ -0,0 +1,137 @@ | ||
import { StatusApiTesting } from '../../testing/StatusApiTesting' | ||
import { ApiPagedResponse, WhaleApiClient } from '@defichain/whale-api-client' | ||
import { WhaleApiProbeIndicator } from '../../src/modules/WhaleApiModule' | ||
import { HealthIndicatorResult, HealthIndicatorStatus } from '@nestjs/terminus' | ||
import { Block } from '@defichain/whale-api-client/dist/api/Blocks' | ||
|
||
describe('AggregateController - Status test', () => { | ||
const apiTesting = StatusApiTesting.create() | ||
beforeAll(async () => { | ||
await apiTesting.start() | ||
}) | ||
|
||
afterAll(async () => { | ||
await apiTesting.stop() | ||
}) | ||
|
||
it('/overall - should get operational when Ocean and blockchain are up', async () => { | ||
jest | ||
.spyOn(apiTesting.app.get(WhaleApiProbeIndicator), 'liveness') | ||
.mockReturnValueOnce(getWhaleStatus('up')) | ||
jest | ||
.spyOn(apiTesting.app.get(WhaleApiProbeIndicator), 'readiness') | ||
.mockReturnValueOnce(getWhaleStatus('up')) | ||
jest | ||
.spyOn(apiTesting.app.get(WhaleApiClient).blocks, 'list') | ||
.mockReturnValueOnce(getBlockResponseWithPresetTime(25)) | ||
|
||
const res = await apiTesting.app.inject({ | ||
method: 'GET', | ||
url: '/overall' | ||
}) | ||
|
||
expect(res.statusCode).toStrictEqual(200) | ||
expect(res.json()).toStrictEqual({ | ||
status: 'operational' | ||
}) | ||
}) | ||
|
||
it('/overall - should get outage when blockchain is down', async () => { | ||
jest | ||
.spyOn(apiTesting.app.get(WhaleApiClient).blocks, 'list') | ||
.mockReturnValueOnce(getBlockResponseWithPresetTime(46)) | ||
|
||
const res = await apiTesting.app.inject({ | ||
method: 'GET', | ||
url: '/overall' | ||
}) | ||
|
||
expect(res.statusCode).toStrictEqual(200) | ||
expect(res.json()).toStrictEqual({ | ||
status: 'outage' | ||
}) | ||
}) | ||
|
||
it('/overall - should get degraded when blockchain degraded', async () => { | ||
jest | ||
.spyOn(apiTesting.app.get(WhaleApiClient).blocks, 'list') | ||
.mockReturnValueOnce(getBlockResponseWithPresetTime(36)) | ||
|
||
const res = await apiTesting.app.inject({ | ||
method: 'GET', | ||
url: '/overall' | ||
}) | ||
|
||
expect(res.statusCode).toStrictEqual(200) | ||
expect(res.json()).toStrictEqual({ | ||
status: 'degraded' | ||
}) | ||
}) | ||
|
||
it('/overall - should get outage when Ocean down', async () => { | ||
jest | ||
.spyOn(apiTesting.app.get(WhaleApiProbeIndicator), 'liveness') | ||
.mockReturnValueOnce(getWhaleStatus('down')) | ||
|
||
const res = await apiTesting.app.inject({ | ||
method: 'GET', | ||
url: '/overall' | ||
}) | ||
|
||
expect(res.statusCode).toStrictEqual(200) | ||
expect(res.json()).toStrictEqual({ | ||
status: 'outage' | ||
}) | ||
}) | ||
|
||
it('/overall - should get outage when Ocean is not ready ', async () => { | ||
jest | ||
.spyOn(apiTesting.app.get(WhaleApiProbeIndicator), 'readiness') | ||
.mockReturnValueOnce(getWhaleStatus('down')) | ||
|
||
const res = await apiTesting.app.inject({ | ||
method: 'GET', | ||
url: '/overall' | ||
}) | ||
|
||
expect(res.statusCode).toStrictEqual(200) | ||
expect(res.json()).toStrictEqual({ | ||
status: 'outage' | ||
}) | ||
}) | ||
}) | ||
|
||
async function getWhaleStatus (status: HealthIndicatorStatus): Promise<HealthIndicatorResult> { | ||
return { | ||
whale: { | ||
status | ||
} | ||
} | ||
} | ||
|
||
async function getBlockResponseWithPresetTime (minutesDiff: number): Promise<ApiPagedResponse<Block>> { | ||
const blockTime = Date.now() / 1000 - (minutesDiff * 60) | ||
|
||
return new ApiPagedResponse({ | ||
data: [{ | ||
time: blockTime, | ||
id: '', | ||
hash: '', | ||
previousHash: '', | ||
height: 0, | ||
version: 0, | ||
medianTime: 0, | ||
transactionCount: 0, | ||
difficulty: 0, | ||
masternode: '', | ||
minter: '', | ||
minterBlockCount: 0, | ||
reward: '', | ||
stakeModifier: '', | ||
merkleroot: '', | ||
size: 0, | ||
sizeStripped: 0, | ||
weight: 0 | ||
}] | ||
}, 'GET', 'blocks') | ||
} |
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
40 changes: 40 additions & 0 deletions
40
apps/status-api/src/controllers/OverallStatusController.ts
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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { Controller, Get } from '@nestjs/common' | ||
import { WhaleApiClient } from '@defichain/whale-api-client' | ||
import { BlockchainStatus, BlockchainStatusController } from './BlockchainStatusController' | ||
import { WhaleApiProbeIndicator } from '../modules/WhaleApiModule' | ||
|
||
@Controller('overall') | ||
export class OverallStatusController { | ||
constructor ( | ||
private readonly client: WhaleApiClient, | ||
private readonly probe: WhaleApiProbeIndicator, | ||
private readonly blockchainStatusController: BlockchainStatusController | ||
) { | ||
} | ||
|
||
/** | ||
* To provide overall status for Ocean and blockchain services. Returns | ||
* 'operational' when both services are up, | ||
* 'outage' when either service is down and | ||
* 'degraded' when blockchain service is degraded. | ||
* | ||
* @return {Promise<{ status: BlockchainStatus }>} | ||
*/ | ||
|
||
@Get() | ||
async getOverallStatus (): Promise<{ status: BlockchainStatus }> { | ||
if ( | ||
(await this.probe.liveness()).whale.status === 'down' || | ||
(await this.probe.readiness()).whale.status === 'down' | ||
) { | ||
return { | ||
status: 'outage' | ||
} | ||
} | ||
|
||
const blockchain = await this.blockchainStatusController.getBlockChainStatus() | ||
return { | ||
status: blockchain.status | ||
} | ||
} | ||
} |
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