import PropTypes from 'prop-types'; import React, { useCallback, useEffect } from 'react'; import { Divider, Flex, FlexItem, Button, DescriptionList, DescriptionListTerm, DescriptionListGroup, DescriptionListDescription, } from '@patternfly/react-core'; import { TableComposable, TableText, Tr, Tbody, Td, } from '@patternfly/react-table'; import { useSelector, useDispatch } from 'react-redux'; import { selectAPIResponse } from 'foremanReact/redux/API/APISelectors'; import CardTemplate from 'foremanReact/components/HostDetails/Templates/CardItem/CardTemplate'; import RelativeDateTime from 'foremanReact/components/common/dates/RelativeDateTime'; import SkeletonLoader from 'foremanReact/components/common/SkeletonLoader'; import DefaultLoaderEmptyState from 'foremanReact/components/HostDetails/DetailsCard/DefaultLoaderEmptyState'; import { statusFormatter } from 'foremanReact/components/HostDetails/Tabs/ReportsTab/helpers'; import { translate as __ } from 'foremanReact/common/I18n'; import { getReportByIdAction } from './ConfigStatusCardActions'; import { FOREMAN_PUPPET_LAST_REPORT_KEY } from './ConfigStatusCardConstants'; import './styles.scss'; const cardHeaderDivider = () => ( ); const generateCardHeader = (allReports = [], reportsCount) => reportsCount > 0 ? ( <> {__('Last configuration status')} {statusFormatter('failed', allReports[0])} {cardHeaderDivider()} {statusFormatter('failed_restarts', allReports[0])} {cardHeaderDivider()} {statusFormatter('restarted', allReports[0])} {cardHeaderDivider()} {statusFormatter('applied', allReports[0])} {cardHeaderDivider()} {statusFormatter('skipped', allReports[0])} {cardHeaderDivider()} {statusFormatter('pending', allReports[0])} ) : ( <> {__('No configuration status available')} ); const createPuppetMetricsTableElement = (name, value = '--') => ( <> {name} {value} ); const createPuppetMetricsTable = (metrics = undefined) => ( {createPuppetMetricsTableElement(__('Failed'), metrics.failed)} {createPuppetMetricsTableElement(__('Changed'), metrics.changed)} {createPuppetMetricsTableElement(__('Scheduled'), metrics.scheduled)} {createPuppetMetricsTableElement( __('Failed to start'), metrics.failed_to_start )} {createPuppetMetricsTableElement(__('Restarted'), metrics.restarted)} {createPuppetMetricsTableElement( __('Corrective Change'), metrics.corrective_change )} {createPuppetMetricsTableElement(__('Skipped'), metrics.skipped)} {createPuppetMetricsTableElement( __('Out of sync'), metrics.out_of_sync )} {createPuppetMetricsTableElement(__('Total'), metrics.total)} ); const ConfigStatusCard = ({ hostName, parentStatus }) => { const dispatch = useDispatch(); // get already fetched results from reports tab const API_KEY = `get-reports-${hostName}`; const { reports, itemCount } = useSelector(state => selectAPIResponse(state, API_KEY) ); // we need to fetch the last Puppet report to get all Puppet metrics const getLastReport = useCallback(() => { if (hostName && reports?.length) dispatch(getReportByIdAction(reports[0].id)); }, [hostName, reports, dispatch]); useEffect(() => { getLastReport(); }, [hostName, reports]); const { metrics } = useSelector(state => selectAPIResponse(state, FOREMAN_PUPPET_LAST_REPORT_KEY) ); return ( {__('Puppet metrics')} } > {metrics && createPuppetMetricsTable(metrics.resources)} ); }; ConfigStatusCard.propTypes = { hostName: PropTypes.string, parentStatus: PropTypes.string, }; ConfigStatusCard.defaultProps = { hostName: undefined, parentStatus: undefined, }; export default ConfigStatusCard;