Skip to content

Commit

Permalink
feat(governance): get all intervals from recent
Browse files Browse the repository at this point in the history
  • Loading branch information
liorfrenkel committed Feb 13, 2020
1 parent 9bbc0e1 commit 57ce191
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 45 deletions.
58 changes: 22 additions & 36 deletions server/components/api/voteIntervals/voteIntervalsDAL.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ voteIntervalsDAL.findAllWithoutSnapshot = async function(height) {
where: {
hasSnapshot: false,
beginHeight: {
[Op.lte]: height,
},
},
[Op.lte]: height
}
}
});
};

voteIntervalsDAL.findByIntervalAndPhase = async function(interval, phase) {
return this.findOne({
where: {
interval,
phase,
},
phase
}
});
};

Expand All @@ -36,36 +36,36 @@ voteIntervalsDAL.findCurrent = async function(currentBlock) {
where: {
[Op.and]: {
beginHeight: {
[Op.lte]: currentBlock,
[Op.lte]: currentBlock
},
endHeight: {
[Op.gt]: currentBlock,
},
},
[Op.gt]: currentBlock
}
}
},
order: [['beginHeight', 'ASC']],
order: [['beginHeight', 'ASC']]
});
};

voteIntervalsDAL.findPrev = async function(currentBlock) {
return this.findOne({
where: {
endHeight: {
[Op.lte]: currentBlock,
},
[Op.lte]: currentBlock
}
},
order: [['endHeight', 'DESC']],
order: [['endHeight', 'DESC']]
});
};

voteIntervalsDAL.findNext = async function(currentBlock) {
return this.findOne({
where: {
beginHeight: {
[Op.gt]: currentBlock,
},
[Op.gt]: currentBlock
}
},
order: [['beginHeight', 'ASC']],
order: [['beginHeight', 'ASC']]
});
};

Expand All @@ -74,26 +74,12 @@ voteIntervalsDAL.findNext = async function(currentBlock) {
*
* @param {number} currentBlock
*/
voteIntervalsDAL.findAllRecent = async function(currentBlock, limit = 5) {
const [prevs, current, next] = await Promise.all([
this.findAll({
where: {
endHeight: {
[Op.lte]: currentBlock,
},
},
order: [['endHeight', 'DESC']],
limit,
}),
this.findCurrent(currentBlock),
this.findNext(currentBlock),
]);

const intervals = [];
next && intervals.push(next);
current && intervals.push(current);
intervals.push.apply(intervals, prevs);
return intervals;
voteIntervalsDAL.findAllRecent = async function({ limit, offset = 0 }) {
return await this.findAll({
limit,
offset,
order: [['beginHeight', 'ASC']]
});
};

/**
Expand Down
33 changes: 25 additions & 8 deletions server/components/api/votes/votesBLL.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ const AFTER_TALLY_BLOCKS = configAfterTallyBlocks
module.exports = {
findIntervalAndTally: async function({ interval, phase } = {}) {
const currentBlock = await blocksBLL.getCurrentBlockNumber();
const currentInterval = await getCurrentInterval({interval, phase, currentBlock});
const currentInterval = await getCurrentInterval({
interval,
phase,
currentBlock
});

if (!currentInterval) {
return null;
Expand Down Expand Up @@ -53,7 +57,11 @@ module.exports = {
sorted
} = {}) {
const currentBlock = await blocksBLL.getCurrentBlockNumber();
const currentInterval = await getCurrentInterval({interval, phase, currentBlock});
const currentInterval = await getCurrentInterval({
interval,
phase,
currentBlock
});
if (!currentInterval) {
return null;
}
Expand Down Expand Up @@ -87,7 +95,11 @@ module.exports = {
pageSize = 10
} = {}) {
const currentBlock = await blocksBLL.getCurrentBlockNumber();
const currentInterval = await getCurrentInterval({interval, phase, currentBlock});
const currentInterval = await getCurrentInterval({
interval,
phase,
currentBlock
});
if (!currentInterval) {
return null;
}
Expand All @@ -109,13 +121,18 @@ module.exports = {
)
]).then(votesDAL.getItemsAndCountResult);
},
findRecentIntervals: async function() {
const currentBlock = await blocksBLL.getCurrentBlockNumber();
return voteIntervalsDAL.findAllRecent(currentBlock);
findRecentIntervals: async function({ page, pageSize } = {}) {
return voteIntervalsDAL.findAllRecent(
createQueryObject({ page, pageSize })
);
},
findContestantWinners: async function({ interval } = {}) {
const currentBlock = await blocksBLL.getCurrentBlockNumber();
const currentInterval = await getCurrentInterval({interval, phase: 'Contestant', currentBlock});
const currentInterval = await getCurrentInterval({
interval,
phase: 'Contestant',
currentBlock
});
if (!currentInterval) {
return null;
}
Expand All @@ -134,7 +151,7 @@ module.exports = {
* 4. previous interval if next does not exist
* 4. next block
*/
async function getCurrentInterval({interval, phase, currentBlock} = {}) {
async function getCurrentInterval({ interval, phase, currentBlock } = {}) {
if (interval && phase) {
return voteIntervalsDAL.findByIntervalAndPhase(interval, phase);
}
Expand Down
3 changes: 2 additions & 1 deletion server/components/api/votes/votesController.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ module.exports = {
.json(jsonResponse.create(httpStatus.OK, result || []));
},
recentIntervals: async function(req, res) {
const result = await votesBLL.findRecentIntervals();
const { page, pageSize } = req.query;
const result = await votesBLL.findRecentIntervals({ page, pageSize });
res.status(httpStatus.OK).json(jsonResponse.create(httpStatus.OK, result));
},
getCandidates: async function(req, res) {
Expand Down

0 comments on commit 57ce191

Please sign in to comment.