Skip to content

Commit

Permalink
Removed deprecated endpoints, updated country list
Browse files Browse the repository at this point in the history
  • Loading branch information
dylancm4 committed May 2, 2024
1 parent 6301072 commit ae4e58b
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 26 deletions.
38 changes: 30 additions & 8 deletions web/src/Components/BlocksCard/BlocksCard.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,14 @@ import Constants from '../../constants';
* Invoked by React immediately after a component is mounted (inserted into the tree).
* @public
*/
componentDidMount() {
async componentDidMount() {
// Get blocksPerSecond in order to set prevBlockHeight for the first time.
const blocksPerSecond = await this.getBlockTime();

// Update the block height using intervals.
this.pollForBlockHeight();
this.pollForBlockHeight(blocksPerSecond);
this.interval = setInterval(
() => { this.pollForBlockHeight() },
() => { this.pollForBlockHeight(blocksPerSecond) },
Constants.BLOCKS_CARD_POLL_INTERVAL_MS);
}

Expand Down Expand Up @@ -102,17 +105,20 @@ import Constants from '../../constants';
* Update the block height.
* @private
*/
pollForBlockHeight() {
const url = 'https://ic-api.internetcomputer.org/api/v3/block-heights';
pollForBlockHeight(blocksPerSecond) {
const url = 'https://ic-api.internetcomputer.org/api/v3/metrics/block-height';
axios.get(url)
.then(res => {
if (res.data.block_height.length === 1 && res.data.block_height[0].length === 2) {
if (res.data.block_height.length === 2) {
let { blockHeight } = this.state;
const newBlockHeight = res.data.block_height[0][1];
const newBlockHeight = parseInt(res.data.block_height[1]);
if (newBlockHeight > blockHeight) {
this.setState(prevState => ({
prevBlockHeight:
prevState.prevBlockHeight !== -1 ? prevState.blockHeight : newBlockHeight,
prevState.prevBlockHeight !== -1 ?
prevState.blockHeight :
// When there is no prevBlockHeight, estimate it based on blocksPerSecond.
newBlockHeight - blocksPerSecond * Constants.BLOCKS_CARD_POLL_INTERVAL_MS / 1000,
blockHeight: newBlockHeight,
error: 0
}));
Expand All @@ -125,6 +131,22 @@ import Constants from '../../constants';
}));
});
}

/**
* Get the block time.
* @private
*/
async getBlockTime() {
const url = 'https://ic-api.internetcomputer.org/api/v3/metrics/block-rate';
try {
const res = await axios.get(url);
if (res.data.block_rate.length === 1 && res.data.block_rate[0].length === 2) {
return parseFloat(res.data.block_rate[0][1]);
}
} catch {
}
return 45; // default fallback if the block rate couldn't be retrieved
}
}

export default BlocksCard;
22 changes: 11 additions & 11 deletions web/src/Components/BlocksChart/BlocksChart.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,14 +165,14 @@ class BlocksChart extends BarChart {
*/
pollForInitialBlocks() {
let endDate = new Date();
endDate = new Date(endDate.getTime() - 1 * 60000); // 1 minute ago to avoid API time discrepancy
const startDate = new Date(endDate.getTime() - 2 * 60000); // 2 minutes ago
const seconds = Constants.BLOCKS_CHART_POLL_INTERVAL_MS / 1000;
endDate = new Date(endDate.getTime() - 5 * 60 * 1000); // 5 minutes ago to avoid API time discrepancy
const startDate = new Date(endDate.getTime() - 60 * 60 * 1000); // 65 minutes ago
const seconds = Constants.BLOCKS_CHART_POLL_INTERVAL_MS / 1000; // 1 minute
const url =
`https://ic-api.internetcomputer.org/api/metrics/block?start=${Math.floor(startDate.getTime() / 1000)}&end=${Math.floor(endDate.getTime() / 1000)}&step=${seconds}`;
`https://ic-api.internetcomputer.org/api/v3/metrics/block-height?start=${Math.floor(startDate.getTime() / 1000)}&end=${Math.floor(endDate.getTime() / 1000)}&step=${seconds}`;
axios.get(url)
.then(res => {
let values = res.data.block;
let values = res.data.block_height;
values.pop(); // Workaround: Throw away last value, always higher than it should be.
// Use values[0] to get the starting block height.
let prevHeight = Math.floor(values[0][1]);
Expand Down Expand Up @@ -200,15 +200,15 @@ class BlocksChart extends BarChart {
* @private
*/
async pollForMoreBlocks() {
// Get data from 1 minute ago to avoid API time discrepancy.
const startDate = new Date((new Date()).getTime() - 61 * 1000);
const endDate = new Date((new Date()).getTime() - 60 * 1000);
const seconds = 1;
// * 5!!!
const endDate = new Date((new Date()).getTime() - 5 * 60 * 1000 ); // 5 minutes ago to avoid API time discrepancy
const startDate = new Date(endDate.getTime() - 60 * 1000); // 6 minutes ago
const seconds = Constants.BLOCKS_CHART_POLL_INTERVAL_MS / 1000; // 1 minute
const url =
`https://ic-api.internetcomputer.org/api/metrics/block?start=${Math.floor(startDate.getTime() / 1000)}&end=${Math.floor(endDate.getTime() / 1000)}&step=${seconds}`;
`https://ic-api.internetcomputer.org/api/v3/metrics/block-height?start=${Math.floor(startDate.getTime() / 1000)}&end=${Math.floor(endDate.getTime() / 1000)}&step=${seconds}`;
await axios.get(url)
.then(res => {
const values = res.data.block;
const values = res.data.block_height;
if (values.length >= 2) {
const prevHeight = Math.floor(values[values.length - 2][1]);
const curHeight = Math.floor(values[values.length - 1][1]);
Expand Down
7 changes: 4 additions & 3 deletions web/src/Components/HomePage/HomePage.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import IcpMetricsTable from '../IcpMetricsTable/IcpMetricsTable';
import NetworkMetricsTable from '../NetworkMetricsTable/NetworkMetricsTable';
import BlocksChart from '../BlocksChart/BlocksChart';
import CanistersChart from '../CanistersChart/CanistersChart';
import MessagesChart from '../MessagesChart/MessagesChart';
//pricing!!!import PriceChart from '../PriceChart/PriceChart';
import StakedChart from '../StakedChart/StakedChart';
import { Breakpoints } from '../../utils/breakpoint';
Expand Down Expand Up @@ -360,13 +359,14 @@ class HomePage extends TrackablePage {
alignItems='flex-start'
breakpoint={breakpoint}
>
<GridChart item breakpoint={breakpoint}>
<GridChartWide item breakpoint={breakpoint}>
<Fade
timeout={500}
>
<CanistersChart chartHeight={chartHeight} breakpoint={breakpoint} />
</Fade>
</GridChart>
</GridChartWide>
{/* MessagesChart has been deprecated.
<GridChart item breakpoint={breakpoint}>
<Fade
delay={50}
Expand All @@ -375,6 +375,7 @@ class HomePage extends TrackablePage {
<MessagesChart chartHeight={chartHeight} breakpoint={breakpoint} />
</Fade>
</GridChart>
*/}
</GridSectionCharts2>
</Fragment>
);
Expand Down
3 changes: 2 additions & 1 deletion web/src/Components/MessagesChart/MessagesChart.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import PropTypes from 'prop-types';
import { withTheme } from 'styled-components';
import axios from 'axios';
import BarChart from '../BarChart/BarChart';
import Constants from '../../constants';
import roundDownDateToHour from '../../utils/roundDownDateToHour';

/**
* THIS COMPONENT HAS BEEN DEPRECATED.
* This component displays a number of messages chart with data retrieved from
* ic-api.internetcomputer.org.
*/
Expand Down Expand Up @@ -77,6 +77,7 @@ class MessagesChart extends BarChart {
const startDate = new Date(endDate.getTime());
startDate.setDate(endDate.getDate() - 1);
const secondsInHour = 60 * 60;
// This API endpoint no longer exists.
const url =
`https://ic-api.internetcomputer.org/api/v3/messages-counts-over-time-deprecated?&start=${Math.floor(startDate.getTime() / 1000)}&end=${Math.floor(endDate.getTime() / 1000)}&step=${secondsInHour}`;
axios.get(url)
Expand Down
4 changes: 2 additions & 2 deletions web/src/constants.js

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

21 changes: 20 additions & 1 deletion web/src/utils/getLocationsByCountry.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,30 @@ export default async function getLocationsByCountry() {
cityToCountryMap.set('Allentown', 'USA');
cityToCountryMap.set('Antwerp', 'Belgium');
cityToCountryMap.set('Atlanta', 'USA');
cityToCountryMap.set('Barreiro', 'Portugal');
cityToCountryMap.set('Bogota', 'Colombia');
cityToCountryMap.set('Boston', 'USA');
cityToCountryMap.set('Brussels', 'Belgium');
cityToCountryMap.set('Bucharest', 'Romania');
cityToCountryMap.set('CABA', 'Argentina');
cityToCountryMap.set('California', 'USA');
cityToCountryMap.set('Cape Town', 'South Africa');
cityToCountryMap.set('Chicago', 'USA');
cityToCountryMap.set('Colombo', 'Sri Lanka');
cityToCountryMap.set('Dallas', 'USA');
cityToCountryMap.set('Frankfurt', 'Germany');
cityToCountryMap.set('Fremont', 'USA');
cityToCountryMap.set('Gauteng', 'South Africa');
cityToCountryMap.set('Geneva', 'Switzerland');
cityToCountryMap.set('Greater Noida', 'India');
cityToCountryMap.set('HongKong', 'China'); // sic
cityToCountryMap.set('Houston', 'USA');
cityToCountryMap.set('Jacksonville', 'USA');
cityToCountryMap.set('Las Vegas', 'USA');
cityToCountryMap.set('Lisbon', 'Portugal');
cityToCountryMap.set('Ljubljana', 'Slovenia');
cityToCountryMap.set('London', 'United Kingdom');
cityToCountryMap.set('Madrid', 'Spain');
cityToCountryMap.set('Maribor', 'Slovenia');
cityToCountryMap.set('Marseille', 'France');
cityToCountryMap.set('Melbourne', 'Australia');
Expand All @@ -45,24 +53,35 @@ export default async function getLocationsByCountry() {
cityToCountryMap.set('New York', 'USA');
cityToCountryMap.set('Normandie', 'France');
cityToCountryMap.set('Orlando', 'USA');
cityToCountryMap.set('Panama City', 'Panama');
cityToCountryMap.set('Panvel', 'India');
cityToCountryMap.set('Paris', 'France');
cityToCountryMap.set('Phoenix', 'USA');
cityToCountryMap.set('Portland', 'USA');
cityToCountryMap.set('Praha', 'Czech Republic');
cityToCountryMap.set('Quebec', 'Canada');
cityToCountryMap.set('Quebec l', 'Canada'); // sic
cityToCountryMap.set('Queensland', 'Australia');
cityToCountryMap.set('Riga', 'Latvia');
cityToCountryMap.set('San Jose', 'USA');
cityToCountryMap.set('San José', 'Costa Rica');
cityToCountryMap.set('Seoul', 'South Korea');
cityToCountryMap.set('Siauliai', 'Lithuania');
cityToCountryMap.set('Singapore', 'Singapore');
cityToCountryMap.set('Sterling', 'USA');
cityToCountryMap.set('South Moravian Region', 'Czech Republic');
cityToCountryMap.set('Stockholm', 'Sweden');
cityToCountryMap.set('Tallinn', 'Estonia');
cityToCountryMap.set('Tampa', 'USA');
cityToCountryMap.set('Tbilisi', 'Georgia');
cityToCountryMap.set('Tel Aviv', 'Israel');
cityToCountryMap.set('Tokyo', 'Japan');
cityToCountryMap.set('Toronto', 'Canada');
cityToCountryMap.set('Vancouver', 'Canada');
cityToCountryMap.set('Utah', 'USA');
cityToCountryMap.set('Vancouver', 'Canada');
cityToCountryMap.set('Vilnius', 'Lithuania');
cityToCountryMap.set('Warszawa', 'Poland');
cityToCountryMap.set('Zagreb', 'Croatia');
cityToCountryMap.set('Zurich', 'Switzerland');

try {
Expand Down

0 comments on commit ae4e58b

Please sign in to comment.