From f0f9e65dc8aee739d7361b1128b48bea4ea0f9d4 Mon Sep 17 00:00:00 2001 From: jeryongchan Date: Mon, 29 Jan 2024 09:38:06 +0800 Subject: [PATCH] removed provider view --- .erb/configs/webpack.config.base.ts | 2 +- package-lock.json | 2 +- src/renderer/components/common/groupData.tsx | 215 +++++++----------- src/renderer/components/payment/Payment.tsx | 12 +- src/renderer/components/payment/handlePay.tsx | 8 +- .../components/transactions/TxnDashboard.tsx | 104 +++------ .../linechart/CustomLineChart.tsx | 51 ++--- .../linechart/DataKeySelectorPopover.tsx | 89 ++++---- src/renderer/utils/cryptoUtils.tsx | 1 + 9 files changed, 195 insertions(+), 289 deletions(-) diff --git a/.erb/configs/webpack.config.base.ts b/.erb/configs/webpack.config.base.ts index 6861879..2444264 100644 --- a/.erb/configs/webpack.config.base.ts +++ b/.erb/configs/webpack.config.base.ts @@ -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') } }, diff --git a/package-lock.json b/package-lock.json index 089d2ff..e86b240 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,5 +1,5 @@ { - "name": "mec_anywhere_desktop_cleanup", + "name": "test_meca", "lockfileVersion": 3, "requires": true, "packages": { diff --git a/src/renderer/components/common/groupData.tsx b/src/renderer/components/common/groupData.tsx index 2f0ff5f..37f2b31 100644 --- a/src/renderer/components/common/groupData.tsx +++ b/src/renderer/components/common/groupData.tsx @@ -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') { @@ -37,87 +37,54 @@ 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( @@ -125,78 +92,56 @@ function groupData( 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, }; }); diff --git a/src/renderer/components/payment/Payment.tsx b/src/renderer/components/payment/Payment.tsx index 8f8dbf4..01903e9 100644 --- a/src/renderer/components/payment/Payment.tsx +++ b/src/renderer/components/payment/Payment.tsx @@ -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'; @@ -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) { @@ -44,6 +53,7 @@ const Payment = () => { }, [provider, account]); const handleConnect = async () => { + console.log('Web3', Web3) const clientSdk = new MetaMaskSDK({ shouldShimWeb3: false, storage: { diff --git a/src/renderer/components/payment/handlePay.tsx b/src/renderer/components/payment/handlePay.tsx index 4d5906b..32394fd 100644 --- a/src/renderer/components/payment/handlePay.tsx +++ b/src/renderer/components/payment/handlePay.tsx @@ -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 ( @@ -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'); diff --git a/src/renderer/components/transactions/TxnDashboard.tsx b/src/renderer/components/transactions/TxnDashboard.tsx index d7ffcb1..3323b60 100644 --- a/src/renderer/components/transactions/TxnDashboard.tsx +++ b/src/renderer/components/transactions/TxnDashboard.tsx @@ -15,7 +15,6 @@ import { addDummyHistory, findHostHistory, findClientHistory, - findPoHistory, } from '../../services/TransactionServices'; import Transitions from '../transitions/Transition'; import { @@ -44,33 +43,38 @@ const TxnDashboard: React.FC = ({ appRole }) => { 1 }px`; - const fetchAndSetData = async (accessToken: string, role: string) => { + function combineHistories(hostDidHistory, clientDidHistory) { + const hostWithRole = hostDidHistory.map((item) => ({ + ...item, + role: 'host', + })); + const clientWithRole = clientDidHistory.map((item) => ({ + ...item, + role: 'client', + })); + return [...hostWithRole, ...clientWithRole]; + } + + const fetchAndSetData = async (accessToken: string) => { setIsLoading(true); try { - let didHistoryResponse; + const hostDidHistoryResponse = await findHostHistory(accessToken, did); + const clientDidHistoryResponse = await findClientHistory( + accessToken, + did + ); + const hostDidHistory = await hostDidHistoryResponse?.json(); + const clientDidHistory = await clientDidHistoryResponse?.json(); + const transactionHistory = combineHistories( + hostDidHistory, + clientDidHistory + ); + console.log('transactionHistory', transactionHistory); - switch (role) { - case 'provider': - didHistoryResponse = await findPoHistory(accessToken, did); - break; - case 'host': - didHistoryResponse = await findHostHistory(accessToken, did); - break; - case 'client': - didHistoryResponse = await findClientHistory(accessToken, did); - break; - default: - console.error('Unknown role:', role); - } - if (didHistoryResponse) { - const responseBody = await didHistoryResponse.json(); - if (responseBody.length > 0) { - setHasData(true); - } - setData(responseBody); - } else { - console.error('No response from didHistory'); + if (transactionHistory.length > 0) { + setHasData(true); } + setData(transactionHistory); } catch (error) { console.error('Error fetching data:', error); } finally { @@ -87,23 +91,7 @@ const TxnDashboard: React.FC = ({ appRole }) => { } }; - const handleAddHostDummyData = async () => { - const { accessToken } = reduxStore.getState().userReducer; - if (accessToken) { - const addDummyHistoryResponse = await addDummyHistory(accessToken, { - host_did: did, - }); - if (!addDummyHistoryResponse) { - console.error('Invalid dummy history response'); - return; - } - await fetchAndSetData(accessToken, appRole); - } else { - console.error('Invalid access token or did'); - } - }; - - const handleAddClientDummyData = async () => { + const handleAddDummyData = async () => { const { accessToken } = reduxStore.getState().userReducer; if (accessToken) { const addDummyClientResponse = await addDummyHistory(accessToken, { @@ -113,48 +101,26 @@ const TxnDashboard: React.FC = ({ appRole }) => { console.error('Invalid dummy history response'); return; } - await fetchAndSetData(accessToken, appRole); - } else { - console.error('Invalid access token or did'); - } - }; - - const handleAddProviderDummyData = async () => { - const { accessToken } = reduxStore.getState().userReducer; - if (accessToken) { - const hostPoResponse = await addDummyHistory(accessToken, { - host_po_did: did, - }); - const clientPoResponse = await addDummyHistory(accessToken, { - client_po_did: did, + const addDummyHostResponse = await addDummyHistory(accessToken, { + host_did: did, }); - if (!hostPoResponse || !clientPoResponse) { + if (!addDummyHostResponse) { console.error('Invalid dummy history response'); return; } - await fetchAndSetData(accessToken, appRole); + await fetchAndSetData(accessToken); } else { console.error('Invalid access token or did'); } }; - const handleAddDummyData = async (role: string) => { - if (role === 'provider') { - return handleAddProviderDummyData(); - } else if (role === 'host') { - return handleAddHostDummyData(); - } else if (role === 'client') { - return handleAddClientDummyData(); - } - }; - useEffect(() => { const credential = JSON.parse(window.electron.store.get('credential')); const retrieveData = async () => { // await new Promise((resolve) => setTimeout(resolve, 500)); if (credential) { const { accessToken } = reduxStore.getState().userReducer; - await fetchAndSetData(accessToken, appRole); + await fetchAndSetData(accessToken); } else { console.error('Credential or DID is missing'); } @@ -244,7 +210,7 @@ const TxnDashboard: React.FC = ({ appRole }) => {