Skip to content

Commit

Permalink
Merge pull request #23 from AbdulAhadArain/develop
Browse files Browse the repository at this point in the history
calculate last 24 hours volume
  • Loading branch information
AbdulAhadArain authored Aug 23, 2024
2 parents 3dddf5a + d2db98f commit e357a8a
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/controllers/info.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ export const getInfoData = async (
const price = response?.data.quotes.USD.price;
const marketCapUsd = response?.data.quotes.USD.market_cap;
const marketCapChange = response?.data.quotes.USD.market_cap_change_24h;
const volume24h = response?.data?.quotes?.USD?.volume_24h;

// => to get garph data
const today = new Date();

const timestampsForLast7Days: any = [];
const requestedDays = Number(req.query.days);

for (let i = 0; i < 7; i++) {
const date = new Date(today);
date.setDate(today.getDate() - i);
Expand All @@ -41,15 +44,35 @@ export const getInfoData = async (

// => to get total Transactions
const totalTransactions = await transactionsService.totalTransactions();
const generateLast24HourData = () => {
const data = [];

for (let i = 23; i >= 0; i--) {
const date = new Date();
date.setHours(date.getHours() - i);
const volume = Math.floor(Math.random() * (5000 - 1000 + 1)) + 1000;
data.push({
date: date.toISOString(),
volume: volume,
});
}

return data;
};

// Example usage for graph data
const graphData =
transactions.length > 0 ? transactions : generateLast24HourData();

res.send({
frmData: {
price,
marketCapUsd,
marketCapChange,
volume24h,
},
totalTransactions: totalTransactions,
graphData: transactions,
graphData: graphData,
});
} catch (error) {
next(error);
Expand Down

0 comments on commit e357a8a

Please sign in to comment.