Skip to content
This repository has been archived by the owner on May 19, 2023. It is now read-only.

Commit

Permalink
feat: sorts plans + removes rounding of planPrice
Browse files Browse the repository at this point in the history
  • Loading branch information
jurajpiar committed Sep 7, 2020
1 parent 02590d3 commit 1764a70
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 26 deletions.
6 changes: 4 additions & 2 deletions src/services/storage/handlers/offer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ function updatePrices (offer: Offer, period: BigNumber, price: BigNumber): Promi
}

export function weightedCumulativeAverage (newValue: number, currentCt: number, currentAvg: number): number {
if (!currentAvg) { return newValue }
if (!currentAvg) {
return newValue
}

const values = [currentAvg, newValue]
const newValueWeight = 1 / (currentCt + 1)
Expand Down Expand Up @@ -77,7 +79,7 @@ const handlers: { [key: string]: Function } = {
const plan = await updatePrices(offer, period, price)

const { plans, averagePrice } = offer
const planPrice = Math.round(plan.price.div(1024 ** 3).toNumber())
const planPrice = plan.price.div(1024 ** 3).toNumber()

const newAvgPrice = plans && weightedCumulativeAverage(planPrice, plans.length, averagePrice)
offer.averagePrice = newAvgPrice || planPrice
Expand Down
51 changes: 27 additions & 24 deletions src/services/storage/hooks/offers.hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,35 +26,38 @@ export default {
find: [
(context: HookContext) => {
if (context.params.query) {
const { averagePrice, totalCapacity, periods } = context.params.query
const { averagePrice, totalCapacity, periods, $limit } = context.params.query

context.params.sequelize = {
raw: false,
nest: true,
include: [
{
model: BillingPlan,
as: 'plans'
},
{
model: Agreement
}
]
}
if (!$limit) {
context.params.sequelize = {
raw: false,
nest: true,
include: [
{
model: BillingPlan,
as: 'plans'
},
{
model: Agreement
}
],
order: [['plans', 'period', 'ASC']]
}

if ((averagePrice || totalCapacity)) {
context.params.sequelize.where = {
averagePrice: averagePrice && {
[Op.between]: [averagePrice.min, averagePrice.max]
},
totalCapacity: totalCapacity && {
[Op.between]: [totalCapacity.min, totalCapacity.max]
if ((averagePrice || totalCapacity)) {
context.params.sequelize.where = {
averagePrice: averagePrice && {
[Op.between]: [averagePrice.min, averagePrice.max]
},
totalCapacity: totalCapacity && {
[Op.between]: [totalCapacity.min, totalCapacity.max]
}
}
}
}

if (periods?.length) {
context.params.sequelize.where['$plans.period$'] = { [Op.in]: periods }
if (periods?.length) {
context.params.sequelize.where['$plans.period$'] = { [Op.in]: periods }
}
}
}
}
Expand Down

0 comments on commit 1764a70

Please sign in to comment.