Skip to content

Commit

Permalink
fix: polling data optimization and unique key fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
norbertsuski committed Jul 16, 2020
1 parent 9ba5c19 commit df0f54d
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 32 deletions.
36 changes: 26 additions & 10 deletions components/status-page.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,22 @@ export default class StatusPage extends React.PureComponent {
this.setupDataPolling();
}

setupDataPolling = () => {
componentWillUnmount() {
this.stopPollingData();
}

stopPollingData() {
if (this.StatusPageNetwork) {
this.StatusPageNetwork.clear();
}
}

setupDataPolling = () => {
this.stopPollingData();

const { refreshRate, accountId } = this.props;
const { editedHostProvider, editedHostName, editedNrqlQuery } = this.state;

if (editedHostProvider === NRQL_PROVIDER_NAME) {
this.StatusPageNetwork = new NRQLHelper(
editedNrqlQuery,
Expand All @@ -88,8 +97,14 @@ export default class StatusPage extends React.PureComponent {
editedHostProvider
);

this.StatusPageNetwork.pollCurrentIncidents(this.setIncidentsData);
this.StatusPageNetwork.pollSummaryData(this.setSummaryData);
const isSameDataSource = this.StatusPageNetwork.checkIfTheSameDataSource();

if (isSameDataSource) {
this.StatusPageNetwork.pollCurrentIncidents(this.setData);
} else {
this.StatusPageNetwork.pollCurrentIncidents(this.setIncidentsData);
this.StatusPageNetwork.pollSummaryData(this.setSummaryData);
}
}

this.FormatService = new FormatService(editedHostProvider);
Expand Down Expand Up @@ -292,7 +307,7 @@ export default class StatusPage extends React.PureComponent {
refreshRate: refreshRate,
hostname: hostname,
provider: provider,
nrqlQuery: nrqlQuery,
nrqlQuery: this.state.editedNrqlQuery,
accountId: this.props.accountId,
timelineItemIndex: selectedIndex
}
Expand Down Expand Up @@ -346,6 +361,7 @@ export default class StatusPage extends React.PureComponent {
this.props.editHostName()(hostNameObject);
e.stopPropagation();
this.handleTileSettingsAnimation();
this.setupDataPolling();
}

handleSettingsButtonMouseLeave = () => {
Expand Down Expand Up @@ -563,9 +579,9 @@ export default class StatusPage extends React.PureComponent {
this.handleTileClick(
statusPageIoSummaryData,
refreshRate,
hostname.hostName,
hostname.provider,
hostname.nrqlQuery
this.state.editedHostName,
this.state.editedHostProvider,
this.state.editedNrqlQuery
)
}
>
Expand Down Expand Up @@ -637,9 +653,9 @@ export default class StatusPage extends React.PureComponent {
this.handleTileClick(
statusPageIoSummaryData,
refreshRate,
hostname.hostName,
hostname.provider,
hostname.nrqlQuery,
this.state.editedHostName,
this.state.editedHostProvider,
this.state.editedNrqlQuery,
i
);
}}
Expand Down
44 changes: 26 additions & 18 deletions nerdlets/service-details/service-details.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ export default class ServiceDetails extends React.PureComponent {
hostname: PropTypes.string,
provider: PropTypes.string.isRequired,
refreshRate: PropTypes.number,
timelineItemIndex: PropTypes.object,
timelineItemIndex: PropTypes.number,
nrqlQuery: PropTypes.string,
accountId: PropTypes.string
accountId: PropTypes.number
};

constructor(props) {
Expand All @@ -39,14 +39,20 @@ export default class ServiceDetails extends React.PureComponent {
}

componentDidUpdate(prevProps) {
const { timelineItemIndex, hostname, refreshRate, provider } = this.props;
const {
timelineItemIndex,
hostname,
refreshRate,
provider,
nrqlQuery
} = this.props;

if (prevProps.timelineItemIndex !== timelineItemIndex) {
// eslint-disable-next-line react/no-did-update-set-state
this.setState({ expandedTimelineItem: timelineItemIndex });
}

if (prevProps.hostname !== hostname) {
if (prevProps.hostname !== hostname || prevProps.nrqlQuery !== nrqlQuery) {
// eslint-disable-next-line react/no-did-update-set-state
this.setState({ currentIncidents: undefined });
this.setupTimelinePolling(hostname, refreshRate, provider);
Expand All @@ -58,7 +64,7 @@ export default class ServiceDetails extends React.PureComponent {

this.FormatService = new FormatService(provider);

if (NRQL_PROVIDER_NAME === provider) {
if (provider === NRQL_PROVIDER_NAME) {
const { nrqlQuery, accountId } = this.props;
this.statusPageNetwork = new NRQLHelper(
nrqlQuery,
Expand Down Expand Up @@ -122,19 +128,21 @@ export default class ServiceDetails extends React.PureComponent {
}

buildTimelineItemDetails(incident) {
const incident_updates = incident.incident_updates.map(incident_update => {
return (
<li
key={incident_update.created_at}
className="timeline-item-contents-item"
>
<span className="key">
{dayjs(incident_update.display_at).format('h:mm a')}:
</span>
<span className="value">{incident_update.body}</span>
</li>
);
});
const incident_updates = incident.incident_updates.map(
(incident_update, index) => {
return (
<li
key={`${incident_update.created_at}-${index}`}
className="timeline-item-contents-item"
>
<span className="key">
{dayjs(incident_update.display_at).format('h:mm a')}:
</span>
<span className="value">{incident_update.body}</span>
</li>
);
}
);

return incident_updates;
}
Expand Down
1 change: 0 additions & 1 deletion nerdlets/status-page-dashboard/main-page.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,6 @@ export default class StatusPagesDashboard extends React.PureComponent {
searchQuery,
hostNames,
requestForHostnamesMade,
keyObject,
entityGuid
} = this.state;

Expand Down
18 changes: 15 additions & 3 deletions utilities/network.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,21 @@ export default class Network {
this.statusPageUrl = statusPageUrl;
this.refreshRateInSeconds = refreshRateInSeconds;
this.provider = provider;
this.setTimeoutId = undefined;
this.setTimeoutIds = [];
}

clear = () => {
clearTimeout(this.setTimeoutId);
this.setTimeoutIds.forEach(timeoutId => {
clearTimeout(timeoutId);
});

this.setTimeoutIds = [];
};

async _fetchAndPopulateData(url, callbackSetterFunction) {
let networkResponse;

console.log('polling network');
try {
networkResponse = await axios.get(url);
} catch {
Expand All @@ -28,7 +33,7 @@ export default class Network {
}

_pollData(url, callbackSetterFunction, callbackBeforePolling) {
this.setTimeoutId = setTimeout(async () => {
const setTimeoutId = setTimeout(async () => {
callbackBeforePolling && callbackBeforePolling();

try {
Expand All @@ -39,6 +44,8 @@ export default class Network {
this._pollData(url, callbackSetterFunction);
}
}, this.refreshRateInSeconds * 1000);

this.setTimeoutIds.push(setTimeoutId);
}

async pollSummaryData(callbackSetterFunction) {
Expand All @@ -56,6 +63,11 @@ export default class Network {
this._pollData(url, callbackSetterFunction, callbackBeforePolling);
}

checkIfTheSameDataSource() {
const provider = getProvider(this.provider);
return provider.summaryUrl === provider.incidentUrl;
}

// helper function to get correct url
// pass either 'summaryUrl' or 'incidentUrl'
_getUrl(providerUrlProperty) {
Expand Down

0 comments on commit df0f54d

Please sign in to comment.