grafana/public/app/features/explore/useLiveTailControls.ts
Abdul 3a013cbe48
Explore: Clear live logs (#64237)
* feat: add clear logs to explorer's live logs

* refactor live logs pause/resume updating logic

* add tests for clearing live logs

* rm unused imports

* move `onClear` live logs logic to redux

* move clear logs tests to `query.test`

* use utils `sortLogsRows` and Button's icon props

* rename `filterLogRows` and add `clearedAt` as an arg

* mv clearedAt type closer to live tailing items and add jsdoc

* mv `filterLogRowsByTime` to `/utils` and use it at `LiveLogs` component

* make `Exit live` button consistent with other actions

* use `sortLogRows` and fix timestamp by id on `makeLogs`

* change clear live logs from based on timestamp to index on logRows

* assign `null` to `clearedAtIndex` on first batch of logs live

* move `isFirstStreaming` implementation to `clearLogs` reducer

* fix `clearLogs` tests

* Update public/app/features/explore/state/query.ts

Co-authored-by: Ivana Huckova <30407135+ivanahuckova@users.noreply.github.com>

---------

Co-authored-by: Ivana Huckova <30407135+ivanahuckova@users.noreply.github.com>
2023-04-20 10:21:28 +02:00

66 lines
2.0 KiB
TypeScript

import React, { useCallback } from 'react';
import { RefreshPicker } from '@grafana/ui';
import { useDispatch } from 'app/types';
import { ExploreId } from '../../types';
import { setPausedStateAction, runQueries, clearLogs } from './state/query';
import { changeRefreshInterval } from './state/time';
/**
* Hook that gives you all the functions needed to control the live tailing.
*/
export function useLiveTailControls(exploreId: ExploreId) {
const dispatch = useDispatch();
const pause = useCallback(() => {
dispatch(setPausedStateAction({ exploreId, isPaused: true }));
}, [exploreId, dispatch]);
const resume = useCallback(() => {
dispatch(setPausedStateAction({ exploreId, isPaused: false }));
}, [exploreId, dispatch]);
const stop = useCallback(() => {
// We need to pause here first because there is transition where we are not live but live logs are still shown
// to cross fade with the normal view. This will prevent reordering of the logs in the live view during the
// transition.
pause();
// TODO referencing this from perspective of refresh picker when there is designated button for it now is not
// great. Needs a bit of refactoring.
dispatch(changeRefreshInterval(exploreId, RefreshPicker.offOption.value));
dispatch(runQueries(exploreId));
}, [exploreId, dispatch, pause]);
const start = useCallback(() => {
dispatch(changeRefreshInterval(exploreId, RefreshPicker.liveOption.value));
}, [exploreId, dispatch]);
const clear = useCallback(() => {
dispatch(clearLogs({ exploreId }));
}, [exploreId, dispatch]);
return {
pause,
resume,
stop,
start,
clear,
};
}
type Props = {
exploreId: ExploreId;
children: (controls: ReturnType<typeof useLiveTailControls>) => React.ReactElement;
};
/**
* If you can't use the hook you can use this as a render prop pattern.
*/
export function LiveTailControls(props: Props) {
const controls = useLiveTailControls(props.exploreId);
return props.children(controls);
}