Skip to content
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

Add extra Weighted Averages fields #311

Merged
merged 4 commits into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion test/1-api.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1805,7 +1805,9 @@ describe('API', () => {
'sellingWeightedPrice',
'sellingAmount',
'cumulativeWeightedPrice',
'cumulativeAmount'
'cumulativeAmount',
'firstTradeMts',
'lastTradeMts'
])
}
})
Expand Down
5 changes: 4 additions & 1 deletion test/helpers/mock-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,10 @@ module.exports = new Map([
-33.16213227,
null,
21185.36542503,
17990.3814539
17990.3814539,
null,
1590969600000,
1654041600000
]
]
])
11 changes: 10 additions & 1 deletion workers/loc.api/errors/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,14 @@ class GrcSlackAvailabilityError extends BaseError {
}
}

class WeightedAveragesTimeframeError extends UnprocessableEntityError {
constructor (message = 'ERR_TIME_FRAME_MORE_THAN_TWO_YEARS') {
super(message)

this.statusMessage = 'For Weighted Averages please select a time frame smaller than a two years'
}
}

module.exports = {
BaseError,
BadRequestError,
Expand All @@ -185,5 +193,6 @@ module.exports = {
ArgsParamsFilterError,
LedgerPaymentFilteringParamsError,
GrcSlackAvailabilityError,
ImplementationError
ImplementationError,
WeightedAveragesTimeframeError
}
8 changes: 6 additions & 2 deletions workers/loc.api/generate-csv/csv.job.data.js
Original file line number Diff line number Diff line change
Expand Up @@ -1064,10 +1064,14 @@ class CsvJobData {
sellingWeightedPrice: 'WEIGHTED PRICE',
sellingAmount: 'AMOUNT',
cumulativeWeightedPrice: 'WEIGHTED PRICE',
cumulativeAmount: 'AMOUNT'
cumulativeAmount: 'AMOUNT',
firstTradeMts: 'First Trade',
lastTradeMts: 'Last Trade'
},
formatSettings: {
symbol: 'symbol'
symbol: 'symbol',
firstTradeMts: 'date',
lastTradeMts: 'date'
},
csvCustomWriter: this.weightedAveragesReportCsvWriter
}
Expand Down
31 changes: 28 additions & 3 deletions workers/loc.api/weighted.averages.report/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
'use strict'

const moment = require('moment')

const {
WeightedAveragesTimeframeError
} = require('../errors')

const { decorateInjectable } = require('../di/utils')

const depsTypes = (TYPES) => [
Expand Down Expand Up @@ -65,7 +71,22 @@ class WeightedAveragesReport {

async _getWeightedAveragesReportFromApi (args) {
const limit = 100_000
const symbols = args?.params?.symbol ?? []

const {
start,
end,
symbol: symbols = []
} = args?.params ?? {}

const startDate = moment(start)
const endDate = moment(end)
const isTimeframeMoreThan2Y1h = endDate.isAfter(
startDate.add(2, 'year').add(1, 'hour')
)

if (isTimeframeMoreThan2Y1h) {
throw new WeightedAveragesTimeframeError()
}

const weightedAverages = []

Expand Down Expand Up @@ -97,7 +118,9 @@ class WeightedAveragesReport {
sumSellingSpent = 0,
sumSellingAmount = 0,
buyingWeightedPrice = 0,
sellingWeightedPrice = 0
sellingWeightedPrice = 0,
firstTradeMts,
lastTradeMts
} = res ?? {}

if (tradeCount >= limit) {
Expand All @@ -116,7 +139,9 @@ class WeightedAveragesReport {
sellingWeightedPrice,
sellingAmount: sumSellingAmount,
cumulativeWeightedPrice,
cumulativeAmount
cumulativeAmount,
firstTradeMts,
lastTradeMts
})
}

Expand Down