Skip to content

Commit

Permalink
Fixed bar chart month issue, code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ArthurHoeke committed Jun 12, 2024
1 parent 7c650cd commit 63a930c
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 39 deletions.
Binary file removed front-end/figma/sketch.fig
Binary file not shown.
2 changes: 1 addition & 1 deletion front-end/src/app/pages/dashboard/dashboard.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ <h3>{{dashboardService.getIntegerOfTotalRewardsToday()}}<span>.{{dashboardServic
Overview
</a>
</li>
<li>
<li *ngIf="isAdmin()">
<a [routerLink]="['/settings']" class="nav-link">
<svg xmlns="http://www.w3.org/2000/svg" width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="ai ai-Gear"><path d="M14 3.269C14 2.568 13.432 2 12.731 2H11.27C10.568 2 10 2.568 10 3.269v0c0 .578-.396 1.074-.935 1.286-.085.034-.17.07-.253.106-.531.23-1.162.16-1.572-.249v0a1.269 1.269 0 0 0-1.794 0L4.412 5.446a1.269 1.269 0 0 0 0 1.794v0c.41.41.48 1.04.248 1.572a7.946 7.946 0 0 0-.105.253c-.212.539-.708.935-1.286.935v0C2.568 10 2 10.568 2 11.269v1.462C2 13.432 2.568 14 3.269 14v0c.578 0 1.074.396 1.286.935.034.085.07.17.105.253.231.531.161 1.162-.248 1.572v0a1.269 1.269 0 0 0 0 1.794l1.034 1.034a1.269 1.269 0 0 0 1.794 0v0c.41-.41 1.04-.48 1.572-.249.083.037.168.072.253.106.539.212.935.708.935 1.286v0c0 .701.568 1.269 1.269 1.269h1.462c.701 0 1.269-.568 1.269-1.269v0c0-.578.396-1.074.935-1.287.085-.033.17-.068.253-.104.531-.232 1.162-.161 1.571.248v0a1.269 1.269 0 0 0 1.795 0l1.034-1.034a1.269 1.269 0 0 0 0-1.794v0c-.41-.41-.48-1.04-.249-1.572.037-.083.072-.168.106-.253.212-.539.708-.935 1.286-.935v0c.701 0 1.269-.568 1.269-1.269V11.27c0-.701-.568-1.269-1.269-1.269v0c-.578 0-1.074-.396-1.287-.935a7.755 7.755 0 0 0-.105-.253c-.23-.531-.16-1.162.249-1.572v0a1.269 1.269 0 0 0 0-1.794l-1.034-1.034a1.269 1.269 0 0 0-1.794 0v0c-.41.41-1.04.48-1.572.249a7.913 7.913 0 0 0-.253-.106C14.396 4.343 14 3.847 14 3.27v0z"/><path d="M16 12a4 4 0 1 1-8 0 4 4 0 0 1 8 0z"/></svg>
Settings
Expand Down
98 changes: 60 additions & 38 deletions front-end/src/app/services/dashboard/dashboard.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,8 @@ export class DashboardService {
positionClass: "toast-top-left"
});


//setup income pie
let incomePerValidator: any = await this.apiService.getIncomeDistribution();

incomePerValidator = incomePerValidator['data'];
Expand All @@ -246,45 +248,58 @@ export class DashboardService {

Chart.getChart("incomePieChart")?.update("normal");

//calculate combined rewards
let combinedDailyRewards = [0, 0, 0, 0, 0, 0, 0];
let combinedMonthlyRewards = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
for (let i = 0; i < this.validatorList.length; i++) {
const val = this.validatorList[i];
const net = this.networkList[val.networkId - 1];
//calculate combined rewards for bar chart and daily income

const dailyRewards = new Array(7).fill(0); // Pre-fill daily rewards with zeros
const monthlyRewards = new Array(12).fill(0); // Pre-fill monthly rewards with zeros

for (let i2 = 0; i2 < val['weeklyRewardList'].length; i2++) {
const rewardObj = val['weeklyRewardList'][i2];
for (const validator of this.validatorList) {
const network = this.networkList.find((n: { id: any; }) => n.id === validator.networkId);

const dayIndex = this.setWeekdayIndex(rewardObj['weekday']);
combinedDailyRewards[dayIndex] += net['price'] * this.calculateDecimals(rewardObj['total_amount'], net['decimals']);
if (!network) {
console.warn(`Network not found for validator: ${validator.name}`);
continue;
}

for (let i3 = 0; i3 < 11; i3++) {
let rewardObj = null;
if (val['monthlyRewardList'][i3] != undefined) {
rewardObj = val['monthlyRewardList'][i3]['total_reward']
} else {
rewardObj = 0;
// Iterate through validator's weekly rewards (if available)
if (validator.weeklyRewardList) {
for (const item of validator.weeklyRewardList) {
// Get the day as a number
const dayIndex = this.setWeekdayIndex(item.weekday);
dailyRewards[dayIndex] += network.price * this.calculateDecimals(item.total_amount, network.decimals);
}
}

// Iterate through validator's monthly rewards (if available)
if (validator.monthlyRewardList) {
for (const item of validator.monthlyRewardList) {
// Get the month as a number (assuming month is a property within the item)
const dataMonth = parseInt(item.month, 10);

// Ensure dataMonth is within the valid range (1-12)
if (dataMonth < 1 || dataMonth > 12) {
console.error("Invalid month provided in validator data:", item.month);
continue; // Skip to the next item in the loop
}

combinedMonthlyRewards[i3] += net['price'] * this.calculateDecimals(rewardObj, net['decimals'])
// Update the specific month based on dataMonth (zero-based index)
const monthIndex = dataMonth - 1;
const reward = item.total_reward || 0;
monthlyRewards[monthIndex] += network.price * this.calculateDecimals(reward, network.decimals);
}
}
}

this.dailyIncomeData.datasets.forEach((dataset) => {
dataset.data = combinedDailyRewards;
});

this.monthlyIncomeData.datasets.forEach((dataset) => {
dataset.data = combinedMonthlyRewards;
});
// Update chart data sets
this.dailyIncomeData.datasets.forEach(dataset => dataset.data = dailyRewards);
this.monthlyIncomeData.datasets.forEach(dataset => dataset.data = monthlyRewards);

Chart.getChart("combinedDailyRewardChart")?.update("normal");
Chart.getChart("combinedMonthlyRewardChart")?.update("normal");
// Update charts
Chart.getChart("combinedDailyRewardChart")?.update();
Chart.getChart("combinedMonthlyRewardChart")?.update();

let dayNumber = this.setWeekdayIndex(new Date().getDay());
this.totalRewardsToday = combinedDailyRewards[dayNumber];
this.totalRewardsToday = dailyRewards[dayNumber];

await this.updateEventList();

Expand Down Expand Up @@ -401,33 +416,41 @@ export class DashboardService {
let parts = totalRewards.split('.');
// If there's no decimal part, return directly
if (parts.length !== 2) return "$" + this.addThousandSeperator(totalRewards);

let integerPart = parts[0];
return "$" + this.addThousandSeperator(integerPart);
}

public getDecimalOfTotalRewardsToday() {
let totalRewards = (this.totalRewardsToday).toFixed(2);
let parts = totalRewards.split('.');
// If there's no decimal part, return an empty string
if (parts.length !== 2) return "";

let decimalPart = parts[1];
return `${decimalPart}`;
}

private updateMonthlyRewardList(data: any, tokenPrice: any, decimals: any) {
const combinedMonthlyRewards = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];

for (let i = 0; i < 11; i++) {
let rewardObj = null;
if (data[i] != undefined) {
rewardObj = data[i]['total_reward']
} else {
rewardObj = 0;
for (const item of data) {
// Get the month as a number (assuming month is a property within the item)
const dataMonth = parseInt(item.month, 10);

// Ensure dataMonth is within the valid range (1-12)
if (dataMonth < 1 || dataMonth > 12) {
console.error("Invalid month provided in data:", item.month);
continue; // Skip to the next item in the loop
}

combinedMonthlyRewards[i] += tokenPrice * this.calculateDecimals(rewardObj, decimals)
// Update the specific month based on dataMonth (zero-based index)
const monthIndex = dataMonth - 1;
let rewardObj = 0;
if (item) {
rewardObj = item['total_reward'];
}
combinedMonthlyRewards[monthIndex] += tokenPrice * this.calculateDecimals(rewardObj, decimals);
}

this.monthlyIncomeData.datasets.forEach((dataset) => {
Expand All @@ -438,7 +461,6 @@ export class DashboardService {
}



private updateWeeklyRewardList(data: any, tokenPrice: any, decimals: any) {
const weeklyRewardList = [0, 0, 0, 0, 0, 0, 0];

Expand Down

0 comments on commit 63a930c

Please sign in to comment.