Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Reduce API calls when changing filters. Closes #2231 #2232

Merged
merged 4 commits into from Feb 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -9,6 +9,7 @@ require('./workflow-filters.scss');
interface WorkflowFilterProps {
workflows: models.Workflow[];
namespace: string;
phaseItems: string[];
selectedPhases: string[];
selectedLabels: string[];
onChange: (namespace: string, selectedPhases: string[], labels: string[]) => void;
Expand Down Expand Up @@ -57,7 +58,7 @@ export class WorkflowFilters extends React.Component<WorkflowFilterProps, {}> {

private getPhaseItems(workflows: models.Workflow[]) {
const phasesMap = new Map<string, number>();
Object.values(models.NODE_PHASE).forEach(value => phasesMap.set(value, 0));
this.props.phaseItems.forEach(value => phasesMap.set(value, 0));
workflows.filter(wf => wf.status.phase).forEach(wf => phasesMap.set(wf.status.phase, (phasesMap.get(wf.status.phase) || 0) + 1));
const results = new Array<{name: string; count: number}>();
phasesMap.forEach((val, key) => {
Expand Down
39 changes: 26 additions & 13 deletions ui/src/app/workflows/components/workflows-list/workflows-list.tsx
Expand Up @@ -25,6 +25,8 @@ require('./workflows-list.scss');

interface State {
loading: boolean;
initialized: boolean;
managedNamespace: boolean;
namespace: string;
selectedPhases: string[];
selectedLabels: string[];
Expand All @@ -42,14 +44,16 @@ export class WorkflowsList extends BasePage<RouteComponentProps<any>, State> {
super(props, context);
this.state = {
loading: true,
initialized: false,
managedNamespace: false,
namespace: this.props.match.params.namespace || '',
selectedPhases: this.queryParams('phase'),
selectedLabels: this.queryParams('label')
};
}

public componentDidMount(): void {
this.fetchWorkflows();
this.fetchWorkflows(this.state.namespace, this.state.selectedPhases, this.state.selectedLabels);
}

public componentWillUnmount(): void {
Expand Down Expand Up @@ -90,6 +94,7 @@ export class WorkflowsList extends BasePage<RouteComponentProps<any>, State> {
<WorkflowFilters
workflows={this.state.workflows}
namespace={this.state.namespace}
phaseItems={Object.values(models.NODE_PHASE)}
selectedPhases={this.state.selectedPhases}
selectedLabels={this.state.selectedLabels}
onChange={(namespace, selectedPhases, selectedLabels) => this.changeFilters(namespace, selectedPhases, selectedLabels)}
Expand All @@ -115,24 +120,33 @@ export class WorkflowsList extends BasePage<RouteComponentProps<any>, State> {
);
}

private fetchWorkflows(): void {
private fetchWorkflows(namespace: string, selectedPhases: string[], selectedLabels: string[]): void {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change to use passed in variables because it seems like setState is async and using this.state.xxx right after setState does not return latest value.

if (this.subscription) {
this.subscription.unsubscribe();
}
services.info
.get()
.then(info => {
if (info.managedNamespace && info.managedNamespace !== this.state.namespace) {
this.setState({namespace: info.managedNamespace});
let workflowList;
let newNamespace = namespace;
if (!this.state.initialized) {
workflowList = services.info.get().then(info => {
if (info.managedNamespace) {
newNamespace = info.managedNamespace;
}
return services.workflows.list(this.state.namespace, this.state.selectedPhases, this.state.selectedLabels);
})
this.setState({initialized: true, managedNamespace: info.managedNamespace ? true : false});
return services.workflows.list(newNamespace, selectedPhases, selectedLabels);
});
} else {
if (this.state.managedNamespace) {
newNamespace = this.state.namespace;
}
workflowList = services.workflows.list(newNamespace, selectedPhases, selectedLabels);
}
workflowList
.then(list => list.items)
.then(list => list || [])
.then(workflows => this.setState({workflows}))
.then(workflows => this.setState({workflows, namespace: newNamespace, selectedPhases, selectedLabels}))
.then(() => {
this.subscription = services.workflows
.watch({namespace: this.state.namespace, phases: this.state.selectedPhases, labels: this.state.selectedLabels})
.watch({namespace: newNamespace, phases: selectedPhases, labels: selectedLabels})
.map(workflowChange => {
const workflows = this.state.workflows;
if (!workflowChange) {
Expand Down Expand Up @@ -167,7 +181,6 @@ export class WorkflowsList extends BasePage<RouteComponentProps<any>, State> {
}

private changeFilters(namespace: string, selectedPhases: string[], selectedLabels: string[]) {
this.setState({namespace, selectedPhases, selectedLabels});
const params = new URLSearchParams();
selectedPhases.forEach(phase => {
params.append('phase', phase);
Expand All @@ -180,7 +193,7 @@ export class WorkflowsList extends BasePage<RouteComponentProps<any>, State> {
url += '?' + params.toString();
}
history.pushState(null, '', uiUrl(url));
this.fetchWorkflows();
this.fetchWorkflows(namespace, selectedPhases, selectedLabels);
}

private renderWorkflows(ctx: any) {
Expand Down