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

fix(#14022): restrict multiple run now calls #14119

Merged
merged 1 commit into from
Nov 27, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,12 @@ import {

const AppRunsHistory = forwardRef(
(
{ appData, maxRecords, showPagination = true }: AppRunsHistoryProps,
{
appData,
maxRecords,
showPagination = true,
runsData,
}: AppRunsHistoryProps,
ref
) => {
const { t } = useTranslation();
Expand Down Expand Up @@ -271,8 +276,13 @@ const AppRunsHistory = forwardRef(
}));

useEffect(() => {
fetchAppHistory();
}, [fqn, pageSize]);
if (runsData) {
setAppRunsHistoryData(runsData);
setIsLoading(false);
} else {
fetchAppHistory();
}
}, [fqn, pageSize, runsData]);

return (
<Row gutter={[16, 16]}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ export interface AppRunsHistoryProps {
maxRecords?: number;
appData?: App;
showPagination?: boolean;
runsData?: AppRunRecordWithId[];
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* limitations under the License.
*/
import { Button, Col, Divider, Modal, Row, Space, Typography } from 'antd';
import { AxiosError } from 'axios';
import cronstrue from 'cronstrue';
import React, {
useCallback,
Expand All @@ -24,13 +25,26 @@ import {
AppScheduleClass,
AppType,
} from '../../../generated/entity/applications/app';
import { PipelineType } from '../../../generated/entity/services/ingestionPipelines/ingestionPipeline';
import { Status } from '../../../generated/entity/applications/appRunRecord';
import {
PipelineState,
PipelineStatus,
PipelineType,
} from '../../../generated/entity/services/ingestionPipelines/ingestionPipeline';
import { Paging } from '../../../generated/type/paging';
import { getApplicationRuns } from '../../../rest/applicationAPI';
import { getIngestionPipelineByFqn } from '../../../rest/ingestionPipelineAPI';
import { getStatusFromPipelineState } from '../../../utils/ApplicationUtils';
import { getIngestionFrequency } from '../../../utils/CommonUtils';
import { getEpochMillisForPastDays } from '../../../utils/date-time/DateTimeUtils';
import { showErrorToast } from '../../../utils/ToastUtils';
import TestSuiteScheduler from '../../AddDataQualityTest/components/TestSuiteScheduler';
import Loader from '../../Loader/Loader';
import AppRunsHistory from '../AppRunsHistory/AppRunsHistory.component';
import { AppRunsHistoryRef } from '../AppRunsHistory/AppRunsHistory.interface';
import {
AppRunRecordWithId,
AppRunsHistoryRef,
} from '../AppRunsHistory/AppRunsHistory.interface';
import { AppScheduleProps } from './AppScheduleProps.interface';

const AppSchedule = ({
Expand All @@ -44,6 +58,22 @@ const AppSchedule = ({
const appRunsHistoryRef = useRef<AppRunsHistoryRef>(null);
const [isPipelineDeployed, setIsPipelineDeployed] = useState(false);
const [isLoading, setIsLoading] = useState(true);
const [appRunsHistoryData, setAppRunsHistoryData] = useState<
AppRunRecordWithId[]
>([]);

const isExternalApp = useMemo(
() => appData?.appType === AppType.External,
[appData]
);

const isAppRunning = useMemo(() => {
if (appRunsHistoryData.length > 0) {
return appRunsHistoryData[0].status === Status.Running;
} else {
return false;
}
}, [appRunsHistoryData]);

const fetchPipelineDetails = useCallback(async () => {
setIsLoading(true);
Expand All @@ -67,6 +97,60 @@ const AppSchedule = ({
}
}, [appData]);

const fetchAppHistory = useCallback(
async (pagingOffset?: Paging) => {
try {
setIsLoading(true);

if (isExternalApp) {
const currentTime = Date.now();
// past 30 days
const startDay = getEpochMillisForPastDays(30);

const { data } = await getApplicationRuns(
appData.fullyQualifiedName ?? '',
{
startTs: startDay,
endTs: currentTime,
}
);

setAppRunsHistoryData(
data
.map((item) => ({
...item,
status: getStatusFromPipelineState(
(item as PipelineStatus).pipelineState ?? PipelineState.Failed
),
id: (item as PipelineStatus).runId ?? '',
}))
.slice(0, 1)
);
} else {
const { data } = await getApplicationRuns(
appData.fullyQualifiedName ?? '',
{
offset: pagingOffset?.offset ?? 0,
limit: 1,
}
);

setAppRunsHistoryData(
data.map((item) => ({
...item,
id: `${item.appId}-${item.runType}-${item.timestamp}`,
}))
);
}
} catch (err) {
showErrorToast(err as AxiosError);
} finally {
setIsLoading(false);
}
},
[appData]
);

const cronString = useMemo(() => {
if (appData.appSchedule) {
const cronExp =
Expand All @@ -91,7 +175,7 @@ const AppSchedule = ({

const onAppTrigger = async () => {
await onDemandTrigger();
appRunsHistoryRef.current?.refreshAppHistory();
await fetchAppHistory();
};

const appRunHistory = useMemo(() => {
Expand All @@ -104,6 +188,7 @@ const AppSchedule = ({
appData={appData}
maxRecords={1}
ref={appRunsHistoryRef}
runsData={appRunsHistoryData}
showPagination={false}
/>
);
Expand All @@ -122,10 +207,11 @@ const AppSchedule = ({
{t('message.no-ingestion-pipeline-found')}
</Typography.Text>
);
}, [appData, isPipelineDeployed, appRunsHistoryRef]);
}, [appData, isPipelineDeployed, appRunsHistoryRef, appRunsHistoryData]);

useEffect(() => {
fetchPipelineDetails();
fetchAppHistory();
}, []);

if (isLoading) {
Expand Down Expand Up @@ -170,7 +256,6 @@ const AppSchedule = ({
{appData.appType === AppType.External && (
<Button
data-testid="deploy-button"
disabled={appData.deleted}
type="primary"
onClick={onDeployTrigger}>
{t('label.deploy')}
Expand All @@ -179,15 +264,14 @@ const AppSchedule = ({

<Button
data-testid="edit-button"
disabled={appData.deleted}
type="primary"
onClick={() => setShowModal(true)}>
{t('label.edit')}
</Button>

<Button
data-testid="run-now-button"
disabled={appData.deleted}
disabled={isAppRunning}
type="primary"
onClick={onAppTrigger}>
{t('label.run-now')}
Expand Down