/* * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ import * as React from 'react'; import API from '@aws-amplify/api'; import { Logger } from '@aws-amplify/core'; import { Grid, Row, Col, Button, PageHeader, Breadcrumb, BreadcrumbItem, Alert, ProgressBar, Table, Glyphicon, Pager, Form, FormGroup, ControlLabel, FormControl } from 'react-bootstrap'; import { LinkContainer } from 'react-router-bootstrap'; import { LOGGING_LEVEL} from '../components/CustomUtil'; // Properties interface IProps { history?: any; match?: any; location?: any; getApiToken: Function; } // States interface IState { token: string; automationExecutions: AutomationExecution[]; isLoading: boolean; error: string; itemsPerPage: number; queryKeys: object[]; queryCurrentPosition: number; } // AutomationExecution interface interface AutomationExecution { automationExecutionId: string; status: AutomationExecutionStatus; } // Automation execution status enum enum AutomationExecutionStatus { inProgress = 'InProgress', success = 'Success' } // Query type enum enum QueryType { prev, new, next } // External variables const LOGGER = new Logger('ParentExecutions', LOGGING_LEVEL); const API_NAME = 'operations-conductor-api'; class AutomationExecutions extends React.Component { constructor(props: Readonly) { super(props); this.state = { token: '', automationExecutions: [], isLoading: false, error: '', itemsPerPage: 10, queryKeys: [{}], queryCurrentPosition: 0 }; } componentDidMount() { this.setApiToken().then(() => { this.getAutomationExecutions(QueryType.new); }).catch((error) => { this.handleError('Error occurred while setting API token', error); }); } // Sets API token setApiToken = async () => { let token = await this.props.getApiToken(); this.setState({ token }); }; // Gets automation executions getAutomationExecutions = async (queryType: QueryType) => { const { taskId, parentExecutionId } = this.props.match.params; this.setState({ isLoading: true, error: '', automationExecutions: [], }); let path = `/tasks/${taskId}/executions/${parentExecutionId}`; let params = { headers: { 'Authorization': this.state.token }, body: { itemsPerPage: this.state.itemsPerPage, lastKey: {} } }; let { queryCurrentPosition, queryKeys } = this.state; switch (queryType) { case QueryType.prev: params.body.lastKey = queryKeys[queryCurrentPosition - 1]; break; case QueryType.next: params.body.lastKey = queryKeys[queryCurrentPosition + 1]; break; default: break; } try { let automationExecutions = await API.post(API_NAME, path, params); switch (queryType) { case QueryType.prev: this.setState((prevState) => ({ queryCurrentPosition: prevState.queryCurrentPosition - 1 })); break; case QueryType.next: if (automationExecutions.LastEvaluatedKey) { queryKeys.push(automationExecutions.LastEvaluatedKey); } this.setState((prevState) => ({ queryCurrentPosition: prevState.queryCurrentPosition + 1, queryKeys })); break; default: queryKeys = [{}]; if (automationExecutions.LastEvaluatedKey) { queryKeys.push(automationExecutions.LastEvaluatedKey); } this.setState({ queryCurrentPosition: 0, queryKeys }); break; } this.setState({ automationExecutions: automationExecutions.Items }); } catch (error) { this.handleError('Error occurred while getting automation executions.', error); } finally { this.setState({ isLoading: false }); } }; // Handles error handleError = (message: string, error: any) => { if (error.response !== undefined) { LOGGER.error(message, error.response.data.message); if (error.response.data.statusCode === 404) { this.props.history.push(`/tasks/${this.props.match.params.taskId}`); } this.setState({ error: error.response.data.message }); } else { LOGGER.error(message, error.message); this.setState({ error: error.message }); } }; // Handles value changes handleItemsPerChange = (event: any) => { this.setState({ itemsPerPage: parseInt(event.target.value) }, () => { this.getAutomationExecutions(QueryType.new); }); }; render() { return (
Tasks {`Task Detail: ${this.props.location.state.taskName}`} Task Execution ID: {this.props.match.params.parentExecutionId} Task Automation Executions - {this.props.match.params.parentExecutionId}
Task ID {this.props.match.params.taskId}
Task Execution ID {this.props.match.params.parentExecutionId}
Items
  { this.state.isLoading && } { this.state.automationExecutions.length === 0 && !this.state.isLoading && } { this.state.automationExecutions.map((automationExecution: AutomationExecution) => { return ( ); }) }
Automation Execution ID Status
Loading...
No automation execution found.
{automationExecution.automationExecutionId} { AutomationExecutionStatus.inProgress === automationExecution.status && {automationExecution.status} } { AutomationExecutionStatus.success === automationExecution.status && {automationExecution.status} }
this.getAutomationExecutions(QueryType.prev)}> Previous Page { !this.state.isLoading && { this.state.automationExecutions.length === 0 && No Automation Execution } { this.state.automationExecutions.length > 0 && Automation Executions {this.state.itemsPerPage * this.state.queryCurrentPosition + 1} - {this.state.itemsPerPage * this.state.queryCurrentPosition + this.state.automationExecutions.length} } } this.getAutomationExecutions(QueryType.next)}> Next Page { this.state.isLoading && } { this.state.error && Error:
{this.state.error}
}
); } } export default AutomationExecutions;