-
Notifications
You must be signed in to change notification settings - Fork 3
Feat: add GET /auth/finance/cost-summary endpoint #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mukama
wants to merge
2
commits into
tetherto:develop
Choose a base branch
from
mukama:feat/finance-cost-summary
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+712
−4
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,149 @@ | ||
| 'use strict' | ||
|
|
||
| const test = require('brittle') | ||
| const { | ||
| getCostSummary, | ||
| processConsumptionData, | ||
| processPriceData, | ||
| processCostsData, | ||
| calculateCostSummary | ||
| } = require('../../../workers/lib/server/handlers/finance.handlers') | ||
|
|
||
| test('getCostSummary - happy path', async (t) => { | ||
| const mockCtx = { | ||
| conf: { | ||
| orks: [{ rpcPublicKey: 'key1' }] | ||
| }, | ||
| net_r0: { | ||
| jRequest: async (key, method, payload) => { | ||
| if (method === 'tailLogCustomRangeAggr') { | ||
| return [{ data: { 1700006400000: { site_power_w: 5000 } } }] | ||
| } | ||
| if (method === 'getWrkExtData') { | ||
| return { data: [{ prices: [{ ts: 1700006400000, price: 40000 }] }] } | ||
| } | ||
| return {} | ||
| } | ||
| }, | ||
| globalDataLib: { | ||
| getGlobalData: async () => [] | ||
| } | ||
| } | ||
|
|
||
| const mockReq = { | ||
| query: { start: 1700000000000, end: 1700100000000, period: 'daily' } | ||
| } | ||
|
|
||
| const result = await getCostSummary(mockCtx, mockReq, {}) | ||
| t.ok(result.log, 'should return log array') | ||
| t.ok(result.summary, 'should return summary') | ||
| t.ok(Array.isArray(result.log), 'log should be array') | ||
| t.pass() | ||
| }) | ||
|
|
||
| test('getCostSummary - missing start throws', async (t) => { | ||
| const mockCtx = { | ||
| conf: { orks: [] }, | ||
| net_r0: { jRequest: async () => ({}) }, | ||
| globalDataLib: { getGlobalData: async () => [] } | ||
| } | ||
|
|
||
| try { | ||
| await getCostSummary(mockCtx, { query: { end: 1700100000000 } }, {}) | ||
| t.fail('should have thrown') | ||
| } catch (err) { | ||
| t.is(err.message, 'ERR_MISSING_START_END', 'should throw missing start/end error') | ||
| } | ||
| t.pass() | ||
| }) | ||
|
|
||
| test('getCostSummary - invalid range throws', async (t) => { | ||
| const mockCtx = { | ||
| conf: { orks: [] }, | ||
| net_r0: { jRequest: async () => ({}) }, | ||
| globalDataLib: { getGlobalData: async () => [] } | ||
| } | ||
|
|
||
| try { | ||
| await getCostSummary(mockCtx, { query: { start: 1700100000000, end: 1700000000000 } }, {}) | ||
| t.fail('should have thrown') | ||
| } catch (err) { | ||
| t.is(err.message, 'ERR_INVALID_DATE_RANGE', 'should throw invalid range error') | ||
| } | ||
| t.pass() | ||
| }) | ||
|
|
||
| test('getCostSummary - empty ork results', async (t) => { | ||
| const mockCtx = { | ||
| conf: { orks: [{ rpcPublicKey: 'key1' }] }, | ||
| net_r0: { jRequest: async () => ({}) }, | ||
| globalDataLib: { getGlobalData: async () => [] } | ||
| } | ||
|
|
||
| const result = await getCostSummary(mockCtx, { query: { start: 1700000000000, end: 1700100000000 } }, {}) | ||
| t.ok(result.log, 'should return log array') | ||
| t.is(result.log.length, 0, 'log should be empty') | ||
| t.pass() | ||
| }) | ||
|
|
||
| test('processConsumptionData - processes valid data', (t) => { | ||
| const results = [[{ data: { 1700006400000: { site_power_w: 5000 } } }]] | ||
| const daily = processConsumptionData(results) | ||
| t.ok(typeof daily === 'object', 'should return object') | ||
| t.pass() | ||
| }) | ||
|
|
||
| test('processConsumptionData - handles error results', (t) => { | ||
| const results = [{ error: 'timeout' }] | ||
| const daily = processConsumptionData(results) | ||
| t.is(Object.keys(daily).length, 0, 'should be empty') | ||
| t.pass() | ||
| }) | ||
|
|
||
| test('processPriceData - processes valid data', (t) => { | ||
| const results = [[{ prices: [{ ts: 1700006400000, price: 40000 }] }]] | ||
| const daily = processPriceData(results) | ||
| t.ok(typeof daily === 'object', 'should return object') | ||
| t.pass() | ||
| }) | ||
|
|
||
| test('processCostsData - processes app-node format (energyCost)', (t) => { | ||
| const costs = [ | ||
| { site: 'site1', year: 2023, month: 11, energyCost: 30000, operationalCost: 6000 } | ||
| ] | ||
|
|
||
| const result = processCostsData(costs) | ||
| t.ok(result['2023-11'], 'should have month key') | ||
| t.is(result['2023-11'].energyCostPerDay, 1000, 'should have daily energy cost (30000/30)') | ||
| t.is(result['2023-11'].operationalCostPerDay, 200, 'should have daily operational cost (6000/30)') | ||
| t.pass() | ||
| }) | ||
|
|
||
| test('processCostsData - handles non-array input', (t) => { | ||
| const result = processCostsData(null) | ||
| t.is(Object.keys(result).length, 0, 'should be empty') | ||
| t.pass() | ||
| }) | ||
|
|
||
| test('calculateCostSummary - calculates from log entries', (t) => { | ||
| const log = [ | ||
| { energyCostsUSD: 5000, operationalCostsUSD: 1000, totalCostsUSD: 6000, consumptionMWh: 100, btcPrice: 40000 }, | ||
| { energyCostsUSD: 3000, operationalCostsUSD: 600, totalCostsUSD: 3600, consumptionMWh: 60, btcPrice: 42000 } | ||
| ] | ||
|
|
||
| const summary = calculateCostSummary(log) | ||
| t.is(summary.totalEnergyCostsUSD, 8000, 'should sum energy costs') | ||
| t.is(summary.totalOperationalCostsUSD, 1600, 'should sum operational costs') | ||
| t.is(summary.totalCostsUSD, 9600, 'should sum total costs') | ||
| t.is(summary.totalConsumptionMWh, 160, 'should sum consumption') | ||
| t.ok(summary.avgAllInCostPerMWh !== null, 'should calculate avg all-in cost') | ||
| t.ok(summary.avgBtcPrice !== null, 'should calculate avg BTC price') | ||
| t.pass() | ||
| }) | ||
|
|
||
| test('calculateCostSummary - handles empty log', (t) => { | ||
| const summary = calculateCostSummary([]) | ||
| t.is(summary.totalCostsUSD, 0, 'should be zero') | ||
| t.is(summary.avgAllInCostPerMWh, null, 'should be null') | ||
| t.pass() | ||
| }) |
This file contains hidden or 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,39 @@ | ||
| 'use strict' | ||
|
|
||
| const test = require('brittle') | ||
| const { testModuleStructure, testHandlerFunctions, testOnRequestFunctions } = require('../helpers/routeTestHelpers') | ||
| const { createRoutesForTest } = require('../helpers/mockHelpers') | ||
|
|
||
| const ROUTES_PATH = '../../../workers/lib/server/routes/finance.routes.js' | ||
|
|
||
| test('finance routes - module structure', (t) => { | ||
| testModuleStructure(t, ROUTES_PATH, 'finance') | ||
| t.pass() | ||
| }) | ||
|
|
||
| test('finance routes - route definitions', (t) => { | ||
| const routes = createRoutesForTest(ROUTES_PATH) | ||
| const routeUrls = routes.map(route => route.url) | ||
| t.ok(routeUrls.includes('/auth/finance/cost-summary'), 'should have cost-summary route') | ||
| t.pass() | ||
| }) | ||
|
|
||
| test('finance routes - HTTP methods', (t) => { | ||
| const routes = createRoutesForTest(ROUTES_PATH) | ||
| routes.forEach(route => { | ||
| t.is(route.method, 'GET', `route ${route.url} should be GET`) | ||
| }) | ||
| t.pass() | ||
| }) | ||
|
|
||
| test('finance routes - handler functions', (t) => { | ||
| const routes = createRoutesForTest(ROUTES_PATH) | ||
| testHandlerFunctions(t, routes, 'finance') | ||
| t.pass() | ||
| }) | ||
|
|
||
| test('finance routes - onRequest functions', (t) => { | ||
| const routes = createRoutesForTest(ROUTES_PATH) | ||
| testOnRequestFunctions(t, routes, 'finance') | ||
| t.pass() | ||
| }) | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
include in integration test as well