2023-08-13 17:09:40 +00:00
|
|
|
import { useCurrentStateAndParams } from '@uirouter/react';
|
|
|
|
import { useMemo } from 'react';
|
|
|
|
|
|
|
|
import { createStore } from '@/react/kubernetes/datatables/default-kube-datatable-store';
|
2024-04-18 07:14:09 +00:00
|
|
|
import { EnvironmentId } from '@/react/portainer/environments/types';
|
2023-08-13 17:09:40 +00:00
|
|
|
|
|
|
|
import { useTableState } from '@@/datatables/useTableState';
|
|
|
|
|
2024-04-18 07:14:09 +00:00
|
|
|
import { EventsDatatable } from '../../components/EventsDatatable';
|
|
|
|
import { useEvents } from '../../queries/useEvents';
|
|
|
|
import { AppKind } from '../types';
|
2024-10-24 23:28:05 +00:00
|
|
|
import { useApplication } from '../queries/useApplication';
|
|
|
|
import { useApplicationServices } from '../queries/useApplicationServices';
|
|
|
|
import { useApplicationPods } from '../queries/useApplicationPods';
|
2023-08-13 17:09:40 +00:00
|
|
|
|
|
|
|
const storageKey = 'k8sAppEventsDatatable';
|
|
|
|
const settingsStore = createStore(storageKey, { id: 'Date', desc: true });
|
|
|
|
|
|
|
|
export function ApplicationEventsDatatable() {
|
|
|
|
const tableState = useTableState(settingsStore, storageKey);
|
|
|
|
const {
|
|
|
|
params: {
|
|
|
|
namespace,
|
|
|
|
name,
|
2024-04-18 07:14:09 +00:00
|
|
|
'resource-type': appKind,
|
2023-08-13 17:09:40 +00:00
|
|
|
endpointId: environmentId,
|
|
|
|
},
|
|
|
|
} = useCurrentStateAndParams();
|
|
|
|
|
2024-04-18 07:14:09 +00:00
|
|
|
const { relatedEvents, isInitialLoading } = useApplicationEvents(
|
2023-08-13 17:09:40 +00:00
|
|
|
environmentId,
|
|
|
|
namespace,
|
|
|
|
name,
|
2024-04-18 07:14:09 +00:00
|
|
|
appKind,
|
|
|
|
{
|
|
|
|
autoRefreshRate: tableState.autoRefreshRate,
|
|
|
|
}
|
2023-08-13 17:09:40 +00:00
|
|
|
);
|
2024-04-18 07:14:09 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<EventsDatatable
|
|
|
|
dataset={relatedEvents}
|
|
|
|
tableState={tableState}
|
|
|
|
isLoading={isInitialLoading}
|
|
|
|
data-cy="k8sAppDetail-eventsTable"
|
|
|
|
noWidget
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function useApplicationEvents(
|
|
|
|
environmentId: EnvironmentId,
|
|
|
|
namespace: string,
|
|
|
|
name: string,
|
|
|
|
appKind?: AppKind,
|
|
|
|
options?: { autoRefreshRate?: number; yaml?: boolean }
|
|
|
|
) {
|
|
|
|
const { data: application, ...applicationQuery } = useApplication(
|
2023-08-13 17:09:40 +00:00
|
|
|
environmentId,
|
|
|
|
namespace,
|
|
|
|
name,
|
2024-04-18 07:14:09 +00:00
|
|
|
appKind
|
2023-08-13 17:09:40 +00:00
|
|
|
);
|
2024-04-18 07:14:09 +00:00
|
|
|
const servicesQuery = useApplicationServices(
|
2023-08-13 17:09:40 +00:00
|
|
|
environmentId,
|
|
|
|
namespace,
|
|
|
|
name,
|
|
|
|
application
|
|
|
|
);
|
2024-04-18 07:14:09 +00:00
|
|
|
const podsQuery = useApplicationPods(
|
2023-08-13 17:09:40 +00:00
|
|
|
environmentId,
|
|
|
|
namespace,
|
2024-04-18 07:14:09 +00:00
|
|
|
name,
|
|
|
|
application
|
2023-08-13 17:09:40 +00:00
|
|
|
);
|
|
|
|
|
2024-04-18 07:14:09 +00:00
|
|
|
const { data: events, ...eventsQuery } = useEvents(environmentId, {
|
|
|
|
namespace,
|
|
|
|
queryOptions: {
|
|
|
|
autoRefreshRate: options?.autoRefreshRate
|
|
|
|
? options.autoRefreshRate * 1000
|
|
|
|
: undefined,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2023-08-13 17:09:40 +00:00
|
|
|
// related events are events that have the application id, or the id of a service or pod from the application
|
|
|
|
const relatedEvents = useMemo(() => {
|
2024-04-18 07:14:09 +00:00
|
|
|
const serviceIds = servicesQuery.data?.map(
|
|
|
|
(service) => service?.metadata?.uid
|
|
|
|
);
|
|
|
|
const podIds = podsQuery.data?.map((pod) => pod?.metadata?.uid);
|
2023-08-13 17:09:40 +00:00
|
|
|
return (
|
|
|
|
events?.filter(
|
|
|
|
(event) =>
|
|
|
|
event.involvedObject.uid === application?.metadata?.uid ||
|
|
|
|
serviceIds?.includes(event.involvedObject.uid) ||
|
|
|
|
podIds?.includes(event.involvedObject.uid)
|
|
|
|
) || []
|
|
|
|
);
|
2024-04-18 07:14:09 +00:00
|
|
|
}, [application?.metadata?.uid, events, podsQuery.data, servicesQuery.data]);
|
2023-08-13 17:09:40 +00:00
|
|
|
|
2024-04-18 07:14:09 +00:00
|
|
|
const isInitialLoading =
|
|
|
|
applicationQuery.isInitialLoading ||
|
|
|
|
servicesQuery.isInitialLoading ||
|
|
|
|
podsQuery.isInitialLoading ||
|
|
|
|
eventsQuery.isInitialLoading;
|
|
|
|
|
|
|
|
return { relatedEvents, isInitialLoading };
|
2023-08-13 17:09:40 +00:00
|
|
|
}
|