Skip to content

Commit

Permalink
removed provider view
Browse files Browse the repository at this point in the history
  • Loading branch information
jeryongchan committed Jan 29, 2024
1 parent 543a00f commit f0f9e65
Show file tree
Hide file tree
Showing 9 changed files with 195 additions and 289 deletions.
2 changes: 1 addition & 1 deletion .erb/configs/webpack.config.base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ const configuration: webpack.Configuration = {
// There is no need to add aliases here, the paths in tsconfig get mirrored
plugins: [new TsconfigPathsPlugins()],
alias: {
react: path.resolve('./node_modules/react')
react: path.resolve('./src/node_modules/react')
}
},

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

215 changes: 80 additions & 135 deletions src/renderer/components/common/groupData.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ const filterByDate = (entries, startDate, endDate) => {
});
};

const filterByRole = (entries, selectedRole, selfDid) => {
return entries.filter((entry) => {
if (selectedRole === 'client' && entry.po_did === selfDid) {
const filterByRole = (entries, selectedRole) => {
return entries.filter(() => {
if (selectedRole === 'client') {
return true;
}
if (selectedRole === 'host' && entry.host_po_did === selfDid) {
if (selectedRole === 'host') {
return true;
}
if (selectedRole === 'both') {
Expand All @@ -37,166 +37,111 @@ function getGroupKey(entryDate, groupBy) {
})} ${entryDate.getFullYear()}`;
}

function initializeAccumulator(groupKey, isProvider) {
if (isProvider) {
return {
date: groupKey,
client_resource_cpu: 0,
client_resource_memory: 0,
client_duration: 0,
client_network_reliability: 0,
client_price: 0,
client_count: 0,
host_resource_cpu: 0,
host_resource_memory: 0,
host_duration: 0,
host_network_reliability: 0,
host_price: 0,
host_count: 0,
};
}
function initializeAccumulator(groupKey) {
return {
date: groupKey,
resource_cpu: 0,
resource_memory: 0,
duration: 0,
network_reliability: 0,
price: 0,
count: 0,
client_resource_cpu: 0,
client_resource_memory: 0,
client_duration: 0,
client_network_reliability: 0,
client_price: 0,
client_count: 0,
host_resource_cpu: 0,
host_resource_memory: 0,
host_duration: 0,
host_network_reliability: 0,
host_price: 0,
host_count: 0,
};
}

function updateAccumulator(accumulator, entry, groupKey, isProvider, selfDid) {
if (isProvider) {
if (entry.po_did === selfDid) {
accumulator[groupKey].client_count += 1;
accumulator[groupKey].client_resource_cpu += Number(entry.resource_cpu);
accumulator[groupKey].client_resource_memory += Number(
entry.resource_memory
);
accumulator[groupKey].client_duration += Number(entry.duration);
accumulator[groupKey].client_network_reliability += Number(
entry.network_reliability
);
accumulator[groupKey].client_price += Number(entry.price);
} else if (entry.host_po_did === selfDid) {
accumulator[groupKey].host_count += 1;
accumulator[groupKey].host_resource_cpu += Number(entry.resource_cpu);
accumulator[groupKey].host_resource_memory += Number(
entry.resource_memory
);
accumulator[groupKey].host_duration += Number(entry.duration);
accumulator[groupKey].host_network_reliability += Number(
entry.network_reliability
);
accumulator[groupKey].host_price += Number(entry.price);
}
} else {
accumulator[groupKey].count += 1;
accumulator[groupKey].resource_cpu += Number(entry.resource_cpu);
accumulator[groupKey].resource_memory += Number(entry.resource_memory);
accumulator[groupKey].duration += Number(entry.duration);
accumulator[groupKey].network_reliability += Number(
function updateAccumulator(accumulator, entry, groupKey) {
if (entry.role === 'client') {
accumulator[groupKey].client_count += 1;
accumulator[groupKey].client_resource_cpu += Number(entry.resource_cpu);
accumulator[groupKey].client_resource_memory += Number(
entry.resource_memory
);
accumulator[groupKey].client_duration += Number(entry.duration);
accumulator[groupKey].client_network_reliability += Number(
entry.network_reliability
);
accumulator[groupKey].price += Number(entry.price);
accumulator[groupKey].client_price += Number(entry.price);
} else if (entry.role === 'host') {
accumulator[groupKey].host_count += 1;
accumulator[groupKey].host_resource_cpu += Number(entry.resource_cpu);
accumulator[groupKey].host_resource_memory += Number(entry.resource_memory);
accumulator[groupKey].host_duration += Number(entry.duration);
accumulator[groupKey].host_network_reliability += Number(
entry.network_reliability
);
accumulator[groupKey].host_price += Number(entry.price);
}
return accumulator;
}

function groupData(
data,
startDate,
endDate,
groupBy,
selectedRole,
selfDid,
appRole
) {
function groupData(data, startDate, endDate, groupBy, selectedRole) {
const dataFilteredByDate = filterByDate(data, startDate, endDate);
const dataFilteredByRoleAndDate = filterByRole(
dataFilteredByDate,
selectedRole,
selfDid
selectedRole
);

const groupedDataAccumulator = dataFilteredByRoleAndDate.reduce(
(accumulator, entry) => {
const entryDate = new Date(entry.transaction_start_datetime * 1000);
const groupKey = getGroupKey(entryDate, groupBy);
accumulator[groupKey] =
accumulator[groupKey] ||
initializeAccumulator(groupKey, appRole === 'provider');
return updateAccumulator(
accumulator,
entry,
groupKey,
appRole === 'provider',
selfDid
);
accumulator[groupKey] || initializeAccumulator(groupKey);
return updateAccumulator(accumulator, entry, groupKey);
},
{}
);
const groupedData = Object.values(groupedDataAccumulator).map((group) => {
if (appRole === 'provider') {
return {
// ...group,
// Averages and totals for the provider's client data
client_avg_resource_cpu:
group.client_count > 0
? group.client_resource_cpu / group.client_count
: 0,
client_avg_resource_memory:
group.client_count > 0
? group.client_resource_memory / group.client_count
: 0,
client_total_duration: group.client_duration,
client_avg_network_reliability:
group.client_count > 0
? group.client_network_reliability / group.client_count
: 0,
client_total_price: group.client_price,
return {
// Averages and totals for client data
client_avg_resource_cpu:
group.client_count > 0
? group.client_resource_cpu / group.client_count
: 0,
client_avg_resource_memory:
group.client_count > 0
? group.client_resource_memory / group.client_count
: 0,
client_total_duration: group.client_duration,
client_avg_network_reliability:
group.client_count > 0
? group.client_network_reliability / group.client_count
: 0,
client_total_price: group.client_price,

// Averages and totals for the provider's host data
host_avg_resource_cpu:
group.host_count > 0 ? group.host_resource_cpu / group.host_count : 0,
host_avg_resource_memory:
group.host_count > 0
? group.host_resource_memory / group.host_count
: 0,
host_total_duration: group.host_duration,
host_avg_network_reliability:
group.host_count > 0
? group.host_network_reliability / group.host_count
: 0,
host_total_price: group.host_price,
// Averages and totals for host data
host_avg_resource_cpu:
group.host_count > 0 ? group.host_resource_cpu / group.host_count : 0,
host_avg_resource_memory:
group.host_count > 0
? group.host_resource_memory / group.host_count
: 0,
host_total_duration: group.host_duration,
host_avg_network_reliability:
group.host_count > 0
? group.host_network_reliability / group.host_count
: 0,
host_total_price: group.host_price,

// Combined averages for both client and host
avg_resource_cpu:
(group.client_resource_cpu + group.host_resource_cpu) /
(group.client_count + group.host_count),
avg_resource_memory:
(group.client_resource_memory + group.host_resource_memory) /
(group.client_count + group.host_count),
total_duration: group.client_duration + group.host_duration,
avg_network_reliability:
(group.client_network_reliability + group.host_network_reliability) /
(group.client_count + group.host_count),
total_price: group.client_price + group.host_price,
half_total_price: (group.client_price + group.host_price) / 2,
date: group.date,
};
}
// Data for non-provider
return {
// ...group,
avg_resource_cpu: group.count > 0 ? group.resource_cpu / group.count : 0,
// Combined averages for both client and host
avg_resource_cpu:
(group.client_resource_cpu + group.host_resource_cpu) /
(group.client_count + group.host_count),
avg_resource_memory:
group.count > 0 ? group.resource_memory / group.count : 0,
(group.client_resource_memory + group.host_resource_memory) /
(group.client_count + group.host_count),
total_duration: group.client_duration + group.host_duration,
avg_network_reliability:
group.count > 0 ? group.network_reliability / group.count : 0,
total_duration: group.duration,
total_price: group.price,
(group.client_network_reliability + group.host_network_reliability) /
(group.client_count + group.host_count),
total_price: group.client_price + group.host_price,
half_total_price: (group.client_price + group.host_price) / 2,
date: group.date,
};
});
Expand Down
12 changes: 11 additions & 1 deletion src/renderer/components/payment/Payment.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useEffect, useState } from 'react';
import { MetaMaskSDK, SDKProvider } from '@metamask/sdk';
import { Box, Typography } from '@mui/material';
import { MetaMaskSDK, SDKProvider } from '../../../node_modules/@metamask/sdk';
import Web3 from '../../../node_modules/web3';
import QRCodePopover from './QRCodePopover';
import WalletDisconnected from './WalletDisconnected';
import WalletConnected from './WalletConnected';
Expand Down Expand Up @@ -29,6 +30,14 @@ const Payment = () => {
setErrorDialogOpen(false);
};

useEffect(()=>{
console.log("sdk", sdk)
}, [sdk])

useEffect(()=>{
console.log("provider", provider)
}, [provider])

useEffect(() => {
const getClientBalance = async () => {
if (provider && account.length === 42) {
Expand All @@ -44,6 +53,7 @@ const Payment = () => {
}, [provider, account]);

const handleConnect = async () => {
console.log('Web3', Web3)
const clientSdk = new MetaMaskSDK({
shouldShimWeb3: false,
storage: {
Expand Down
8 changes: 5 additions & 3 deletions src/renderer/components/payment/handlePay.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import Web3 from 'web3';
import { AbiItem } from 'web3-utils';
// import { AbiItem } from '../../../web3-utils';
// import { AbiItem } from '../../../node_modules/web3-utils';
// import { AbiItem } from '../../../node_modules/web3-utils';
import Web3 from '../../../node_modules/web3';
import paymentContractAbi from './PaymentContract.json';

const handlePay = async (
Expand All @@ -10,7 +12,7 @@ const handlePay = async (
) => {
try {
const web3 = new Web3(provider);
const paymentContract = paymentContractAbi as AbiItem[];
const paymentContract = paymentContractAbi;
const contract = new web3.eth.Contract(paymentContract, contractAddress);
const did = window.electron.store.get('did');
const amountToSend = web3.utils.toWei(amount.toString(), 'ether');
Expand Down
Loading

0 comments on commit f0f9e65

Please sign in to comment.