Bug 1527824 - refactor and replace usage of PushModel.getList (#4713)

update usage in SelectorCard and in other components
This commit is contained in:
Sarah Clements 2019-02-27 10:26:50 -08:00 коммит произвёл GitHub
Родитель 8aa5d30cad
Коммит 7245a93d32
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
13 изменённых файлов: 112 добавлений и 172 удалений

Просмотреть файл

@ -1,5 +1,7 @@
import Cookies from 'js-cookie';
import { processErrorMessage } from './errorMessage';
const generateHeaders = function generateHeaders() {
return new Headers({
'X-CSRFToken': Cookies.get('csrftoken'),
@ -48,9 +50,17 @@ export const getData = async function getData(url) {
.startsWith('text/html');
if (contentType && failureStatus) {
return { data: { [failureStatus]: response.statusText }, failureStatus };
const errorMessage = processErrorMessage(
`${failureStatus}: ${response.statusText}`,
failureStatus,
);
return { data: errorMessage, failureStatus };
}
const data = await response.json();
let data = await response.json();
if (failureStatus) {
data = processErrorMessage(data, failureStatus);
}
return { data, failureStatus };
};

Просмотреть файл

@ -53,7 +53,6 @@ const Layout = props => {
errorMessages.length > 0) && (
<ErrorMessages
failureMessage={failureMessage}
failureStatus={tableFailureStatus || graphFailureStatus}
errorMessages={errorMessages}
/>
)}

Просмотреть файл

@ -192,7 +192,7 @@ export class PushesClass extends React.Component {
};
poll = () => {
this.pushIntervalId = setInterval(() => {
this.pushIntervalId = setInterval(async () => {
const { notify } = this.props;
const { pushList } = this.state;
// these params will be passed in each time we poll to remain
@ -216,15 +216,15 @@ export class PushesClass extends React.Component {
}
// We will either have a ``revision`` param, but no push for it yet,
// or a ``fromchange`` param because we have at least 1 push already.
PushModel.getList(pushPollingParams).then(async resp => {
if (resp.ok) {
const data = await resp.json();
this.addPushes(data);
this.fetchNewJobs();
} else {
notify('Error fetching new push data', 'danger', { sticky: true });
}
});
const { data, failureStatus } = await PushModel.getList(
pushPollingParams,
);
if (!failureStatus) {
this.addPushes(data);
this.fetchNewJobs();
} else {
notify('Error fetching new push data', 'danger', { sticky: true });
}
}
}, pushPollInterval);
};
@ -265,7 +265,7 @@ export class PushesClass extends React.Component {
* Get the next batch of pushes based on our current offset.
* @param count How many to fetch
*/
fetchPushes = count => {
fetchPushes = async count => {
const { notify } = this.props;
const { oldestPushTimestamp } = this.state;
// const isAppend = (repoData.pushes.length > 0);
@ -284,17 +284,13 @@ export class PushesClass extends React.Component {
delete options.tochange;
options.push_timestamp__lte = oldestPushTimestamp;
}
return PushModel.getList(options)
.then(async resp => {
if (resp.ok) {
const data = await resp.json();
this.addPushes(data.results.length ? data : { results: [] });
} else {
notify('Error retrieving push data!', 'danger', { sticky: true });
}
})
.then(() => this.setValue({ loadingPushes: false }));
const { data, failureStatus } = await PushModel.getList(options);
if (!failureStatus) {
this.addPushes(data.results.length ? data : { results: [] });
} else {
notify('Error retrieving push data!', 'danger', { sticky: true });
}
return this.setValue({ loadingPushes: false });
};
addPushes = data => {

Просмотреть файл

@ -69,13 +69,13 @@ class SimilarJobsTab extends React.Component {
const pushIds = [...new Set(newSimilarJobs.map(job => job.push_id))];
// get pushes and revisions for the given ids
let pushList = { results: [] };
const resp = await PushModel.getList({
const { data, failureStatus } = await PushModel.getList({
id__in: pushIds.join(','),
count: thMaxPushFetchSize,
});
if (resp.ok) {
pushList = await resp.json();
if (!failureStatus) {
pushList = data;
// decorate the list of jobs with their result sets
const pushes = pushList.results.reduce(
(acc, push) => ({ ...acc, [push.id]: push }),
@ -98,11 +98,9 @@ class SimilarJobsTab extends React.Component {
this.showJobInfo(newSimilarJobs[0]);
}
} else {
notify(
`Error fetching similar jobs push data: ${resp.message}`,
'danger',
{ sticky: true },
);
notify(`Error fetching similar jobs push data: ${data}`, 'danger', {
sticky: true,
});
}
}
this.setState({ isLoading: false });

Просмотреть файл

@ -465,29 +465,27 @@ perf.controller('AlertsCtrl', [
});
});
$q.all(Object.keys(resultSetToSummaryMap).map(repo =>
PushModel.getList({ repo, id__in: Object.keys(resultSetToSummaryMap[repo]).join(',') })
.then(async (response) => {
const { results } = await response.json();
results.forEach((resultSet) => {
resultSet.dateStr = dateFilter(
resultSet.push_timestamp * 1000, thDateFormat);
// want at least 14 days worth of results for relative comparisons
const timeRange = phTimeRangeValues[repo] ? phTimeRangeValues[repo] : phDefaultTimeRangeValue;
resultSet.timeRange = Math.max(timeRange,
phTimeRanges.map(timeRange => timeRange.value).find(
t => ((Date.now() / 1000.0) - resultSet.push_timestamp) < t));
resultSetToSummaryMap[repo][resultSet.id].forEach(
(summary) => {
if (summary.push_id === resultSet.id) {
summary.resultSetMetadata = resultSet;
} else if (summary.prev_push_id === resultSet.id) {
summary.prevResultSetMetadata = resultSet;
}
});
});
}),
)).then(() => {
$q.all(Object.keys(resultSetToSummaryMap).map(async repo => {
// TODO utilize failureStatus from PushModel.getList for error handling
const { data } = await PushModel.getList({ repo, id__in: Object.keys(resultSetToSummaryMap[repo]).join(',') });
data.results.forEach((resultSet) => {
resultSet.dateStr = dateFilter(
resultSet.push_timestamp * 1000, thDateFormat);
// want at least 14 days worth of results for relative comparisons
const timeRange = phTimeRangeValues[repo] ? phTimeRangeValues[repo] : phDefaultTimeRangeValue;
resultSet.timeRange = Math.max(timeRange,
phTimeRanges.map(timeRange => timeRange.value).find(
t => ((Date.now() / 1000.0) - resultSet.push_timestamp) < t));
resultSetToSummaryMap[repo][resultSet.id].forEach(
(summary) => {
if (summary.push_id === resultSet.id) {
summary.resultSetMetadata = resultSet;
} else if (summary.prev_push_id === resultSet.id) {
summary.prevResultSetMetadata = resultSet;
}
});
});
})).then(() => {
// for all complete summaries, fill in job and pushlog links
// and downstream summaries
alertSummaries.forEach((summary) => {

Просмотреть файл

@ -27,6 +27,23 @@ const createNoiseMetric = (cmap, name, compareResults) => {
}
}
async function verifyRevision(project, revision, rsid, $scope) {
const { data, failureStatus } = await PushModel.getList({ repo: project.name, commit_revision: revision })
if (failureStatus) {
return $scope.errors.push(data);
}
if (!data.results.length) {
return $scope.errors.push('No results found for this revision');
}
const resultSet = data.results[0];
// TODO: this is a bit hacky to pass in 'original' as a text string
if (rsid === 'original') {
$scope.originalResultSet = resultSet;
} else {
$scope.newResultSet = resultSet;
}
}
perf.controller('CompareResultsCtrl', [
'$state', '$stateParams', '$scope',
'$httpParamSerializer', '$q',
@ -207,28 +224,6 @@ perf.controller('CompareResultsCtrl', [
return displayResults(originalResults.data, newResults.data);
}
// TODO: duplicated in comparesubtestctrl
function verifyRevision(project, revision, rsid) {
return PushModel.getList({ repo: project.name, commit_revision: revision })
.then(async (resp) => {
if (resp.ok) {
const { results } = await resp.json();
const resultSet = results[0];
// TODO: this is a bit hacky to pass in 'original' as a text string
if (rsid === 'original') {
$scope.originalResultSet = resultSet;
} else {
$scope.newResultSet = resultSet;
}
} else {
const error = await resp.text();
$scope.errors.push(error);
}
}).catch((error) => {
$scope.errors.push(error);
});
}
function updateURL() {
const params = {
@ -279,11 +274,11 @@ perf.controller('CompareResultsCtrl', [
$scope.frameworks = frameworks;
});
$q.all([loadRepositories, loadFrameworks]).then(function ([repos]) {
$q.all([loadRepositories, loadFrameworks]).then(async function ([repos]) {
$scope.errors = [];
// validation works only for revision to revision comparison
if ($stateParams.originalRevision) {
$scope.errors = validateQueryParams($stateParams);
$scope.errors = await validateQueryParams($stateParams);
if ($scope.errors.length > 0) {
$scope.dataLoading = false;
@ -312,10 +307,10 @@ perf.controller('CompareResultsCtrl', [
$scope.newRevision = $stateParams.newRevision;
// always need to verify the new revision, only sometimes the original
const verifyPromises = [verifyRevision($scope.newProject, $scope.newRevision, 'new')];
const verifyPromises = [verifyRevision($scope.newProject, $scope.newRevision, 'new', $scope)];
if ($stateParams.originalRevision) {
$scope.originalRevision = $stateParams.originalRevision;
verifyPromises.push(verifyRevision($scope.originalProject, $scope.originalRevision, 'original'));
verifyPromises.push(verifyRevision($scope.originalProject, $scope.originalRevision, 'original', $scope));
} else {
$scope.timeRanges = phTimeRanges;
$scope.selectedTimeRange = $scope.timeRanges.find(timeRange =>
@ -351,24 +346,6 @@ perf.controller('CompareSubtestResultsCtrl', [
'$httpParamSerializer',
function CompareSubtestResultsCtrl($state, $stateParams, $scope, $q,
$httpParamSerializer) {
// TODO: duplicated from comparectrl
function verifyRevision(project, revision, rsid) {
return PushModel.getList({ repo: project.name, commit_revision: revision })
.then(async (resp) => {
const { results } = await resp.json();
const resultSet = results[0];
// TODO: this is a bit hacky to pass in 'original' as a text string
if (rsid === 'original') {
$scope.originalResultSet = resultSet;
} else {
$scope.newResultSet = resultSet;
}
$scope.$apply();
},
function (error) {
$scope.errors.push(error);
});
}
function displayResults(rawResultsMap, newRawResultsMap) {
@ -528,10 +505,10 @@ perf.controller('CompareSubtestResultsCtrl', [
$scope.dataLoading = true;
RepositoryModel.getList().then((repos) => {
RepositoryModel.getList().then(async (repos) => {
$scope.errors = [];
if ($stateParams.originalRevision) {
$scope.errors = validateQueryParams($stateParams);
$scope.errors = await validateQueryParams($stateParams);
if ($scope.errors.length > 0) {
$scope.dataLoading = false;
@ -548,10 +525,10 @@ perf.controller('CompareSubtestResultsCtrl', [
$scope.newSignature = $stateParams.newSignature;
// always need to verify the new revision, only sometimes the original
const verifyPromises = [verifyRevision($scope.newProject, $scope.newRevision, 'new')];
const verifyPromises = [verifyRevision($scope.newProject, $scope.newRevision, 'new', $scope)];
if ($stateParams.originalRevision) {
$scope.originalRevision = $stateParams.originalRevision;
verifyPromises.push(verifyRevision($scope.originalProject, $scope.originalRevision, 'original'));
verifyPromises.push(verifyRevision($scope.originalProject, $scope.originalRevision, 'original', $scope));
} else {
$scope.timeRanges = phTimeRanges;
$scope.selectedTimeRange = $scope.timeRanges.find(timeRange =>
@ -629,18 +606,14 @@ perf.controller('CompareSubtestDistributionCtrl', ['$scope', '$stateParams', '$q
$scope.newSubtestSignature = $stateParams.newSubtestSignature;
$scope.dataLoading = true;
const loadRepositories = RepositoryModel.getList();
const fetchAndDrawReplicateGraph = function (project, revision, subtestSignature, target) {
const fetchAndDrawReplicateGraph = async function (project, revision, subtestSignature, target) {
const replicateData = {};
return PushModel.getList({ repo: project, commit_revision: revision })
.then(async (resp) => {
const { results } = await resp.json();
replicateData.resultSet = results[0];
return PerfSeriesModel.getSeriesData(project, {
signatures: subtestSignature,
push_id: replicateData.resultSet.id,
});
}).then((perfDatumList) => {
const { data } = await PushModel.getList({ repo: project, commit_revision: revision });
replicateData.resultSet = data.results[0];
return PerfSeriesModel.getSeriesData(project, {
signatures: subtestSignature,
push_id: replicateData.resultSet.id,
}).then((perfDatumList) => {
if (!perfDatumList[subtestSignature]) {
replicateData.replicateDataError = true;
return;

Просмотреть файл

@ -471,23 +471,18 @@ perf.controller('GraphsCtrl', [
if (rev && rev.length === 12) {
highlightPromises = [...new Set([
...highlightPromises,
...$scope.seriesList.map((series) => {
...$scope.seriesList.map(async (series) => {
if (series.visible) {
return PushModel.getList({
const { data: results, failureStatus } = await PushModel.getList({
repo: series.projectName,
revision: rev,
}).then(async (resp) => {
if (resp.ok) {
const { results } = await resp.json();
if (results.length) {
});
if (!failureStatus && results.length) {
addHighlightedDatapoint(series, results[0].id);
$scope.$apply();
}
// ignore cases where no push exists
// for revision
}
});
// ignore cases where no push exists
// for revision
}
return null;
})])];

Просмотреть файл

@ -28,7 +28,7 @@ const convertDates = function convertDates(locationParams) {
};
export default class PushModel {
static getList(options = {}) {
static async getList(options = {}) {
const transformedOptions = convertDates(options);
const repoName = transformedOptions.repo;
delete transformedOptions.repo;
@ -51,7 +51,7 @@ export default class PushModel {
// fetch the maximum number of pushes
params.count = thMaxPushFetchSize;
}
return fetch(
return getData(
`${getProjectUrl(pushEndpoint, repoName)}${createQueryParams(params)}`,
);
}

Просмотреть файл

@ -95,7 +95,6 @@ export default class CompareSelectorView extends React.Component {
{(failureStatus || errorMessages.length > 0) && (
<ErrorMessages
failureMessage={data}
failureStatus={failureStatus}
errorMessages={errorMessages}
/>
)}

Просмотреть файл

@ -20,10 +20,8 @@ import {
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faCheck } from '@fortawesome/free-solid-svg-icons';
import { createQueryParams, pushEndpoint } from '../helpers/url';
import { getData } from '../helpers/http';
import PushModel from '../models/push';
import { genericErrorMessage } from '../helpers/constants';
import { getProjectUrl } from '../helpers/location';
export default class SelectorCard extends React.Component {
constructor(props) {
@ -74,14 +72,9 @@ export default class SelectorCard extends React.Component {
this.setState({ disabled: true });
}
const url = `${getProjectUrl(
pushEndpoint,
selectedRepo,
)}${createQueryParams({
full: true,
count: 10,
})}`;
const { data, failureStatus } = await getData(url);
const { data, failureStatus } = await PushModel.getList({
repo: selectedRepo,
});
if (failureStatus) {
updateState({ errorMessages: [genericErrorMessage] });
@ -156,12 +149,10 @@ export default class SelectorCard extends React.Component {
if (!existingRevision) {
this.setState({ validating: 'Validating...' });
const url = `${getProjectUrl(
pushEndpoint,
selectedRepo,
)}${createQueryParams({ commit_revision: value })}`;
const { data: revisions, failureStatus } = await getData(url);
const { data: revisions, failureStatus } = await PushModel.getList({
repo: selectedRepo,
commit_revision: value,
});
if (failureStatus || revisions.meta.count === 0) {
return this.setState({

Просмотреть файл

@ -299,12 +299,12 @@ export const validateQueryParams = async function validateQueryParams(params) {
if (
!failureStatus &&
data.find(project => project.name === originalProject)
!data.find(project => project.name === originalProject)
) {
errors.push(`Invalid project, doesn't exist ${originalProject}`);
}
if (!failureStatus && data.find(project => project.name === newProject)) {
if (!failureStatus && !data.find(project => project.name === newProject)) {
errors.push(`Invalid project, doesn't exist ${newProject}`);
}

Просмотреть файл

@ -22,7 +22,6 @@ export default class Health extends React.Component {
repo: params.get('repo'),
healthData: null,
failureMessage: null,
failureStatus: null,
};
}
@ -47,20 +46,13 @@ export default class Health extends React.Component {
const { data, failureStatus } = await PushModel.getHealth(repo, revision);
const newState = !failureStatus
? { healthData: data }
: { failureMessage: data, failureStatus };
: { failureMessage: data };
this.setState(newState);
};
render() {
const {
healthData,
user,
repo,
revision,
failureMessage,
failureStatus,
} = this.state;
const { healthData, user, repo, revision, failureMessage } = this.state;
const overallResult = healthData
? resultColorMap[healthData.result]
: 'none';
@ -102,12 +94,7 @@ export default class Health extends React.Component {
</Table>
</div>
)}
{failureMessage && (
<ErrorMessages
failureMessage={failureMessage}
failureStatus={failureStatus}
/>
)}
{failureMessage && <ErrorMessages failureMessage={failureMessage} />}
{!failureMessage && !healthData && <Spinner />}
</Container>
</React.Fragment>

Просмотреть файл

@ -2,12 +2,8 @@ import React from 'react';
import PropTypes from 'prop-types';
import { Alert } from 'reactstrap';
import { processErrorMessage } from '../helpers/errorMessage';
const ErrorMessages = ({ failureMessage, failureStatus, errorMessages }) => {
const messages = errorMessages.length
? errorMessages
: [processErrorMessage(failureMessage, failureStatus)];
const ErrorMessages = ({ failureMessage, errorMessages }) => {
const messages = errorMessages.length ? errorMessages : [failureMessage];
return (
<div>
@ -25,13 +21,11 @@ ErrorMessages.propTypes = {
PropTypes.object,
PropTypes.arrayOf(PropTypes.string),
]),
failureStatus: PropTypes.number,
errorMessages: PropTypes.arrayOf(PropTypes.string),
};
ErrorMessages.defaultProps = {
failureMessage: null,
failureStatus: null,
errorMessages: [],
};