2019-05-20 06:28:23 -05:00
|
|
|
import React, { PureComponent } from 'react';
|
2021-04-01 07:15:23 -05:00
|
|
|
import { css, cx } from '@emotion/css';
|
2019-09-10 04:30:25 -05:00
|
|
|
import tinycolor from 'tinycolor2';
|
2019-07-06 01:05:53 -05:00
|
|
|
|
2021-04-23 03:06:42 -05:00
|
|
|
import { LogMessageAnsi, Themeable, withTheme, getLogRowStyles, Icon, Button } from '@grafana/ui';
|
2020-04-27 08:28:06 -05:00
|
|
|
import { GrafanaTheme, LogRowModel, TimeZone, dateTimeFormat } from '@grafana/data';
|
2019-05-20 06:28:23 -05:00
|
|
|
|
2020-05-18 11:03:29 -05:00
|
|
|
import { ElapsedTime } from './ElapsedTime';
|
2019-05-20 06:28:23 -05:00
|
|
|
|
|
|
|
const getStyles = (theme: GrafanaTheme) => ({
|
|
|
|
logsRowsLive: css`
|
|
|
|
label: logs-rows-live;
|
2019-09-09 12:09:06 -05:00
|
|
|
font-family: ${theme.typography.fontFamily.monospace};
|
|
|
|
font-size: ${theme.typography.size.sm};
|
2019-05-20 06:28:23 -05:00
|
|
|
display: flex;
|
|
|
|
flex-flow: column nowrap;
|
2020-12-15 08:18:12 -06:00
|
|
|
height: 60vh;
|
2021-01-27 08:28:32 -06:00
|
|
|
overflow-y: scroll;
|
2019-05-20 06:28:23 -05:00
|
|
|
:first-child {
|
|
|
|
margin-top: auto !important;
|
|
|
|
}
|
|
|
|
`,
|
2019-09-30 07:44:15 -05:00
|
|
|
logsRowFade: css`
|
2019-05-20 06:28:23 -05:00
|
|
|
label: logs-row-fresh;
|
|
|
|
color: ${theme.colors.text};
|
2021-01-20 00:59:48 -06:00
|
|
|
background-color: ${tinycolor(theme.palette.blue95).setAlpha(0.25).toString()};
|
2019-09-09 12:09:06 -05:00
|
|
|
animation: fade 1s ease-out 1s 1 normal forwards;
|
|
|
|
@keyframes fade {
|
|
|
|
from {
|
2021-01-20 00:59:48 -06:00
|
|
|
background-color: ${tinycolor(theme.palette.blue95).setAlpha(0.25).toString()};
|
2019-09-09 12:09:06 -05:00
|
|
|
}
|
|
|
|
to {
|
|
|
|
background-color: transparent;
|
|
|
|
}
|
|
|
|
}
|
2019-05-20 06:28:23 -05:00
|
|
|
`,
|
|
|
|
logsRowsIndicator: css`
|
|
|
|
font-size: ${theme.typography.size.md};
|
2019-09-09 12:09:06 -05:00
|
|
|
padding-top: ${theme.spacing.sm};
|
2019-05-20 06:28:23 -05:00
|
|
|
display: flex;
|
|
|
|
align-items: center;
|
|
|
|
`,
|
2019-09-03 04:23:39 -05:00
|
|
|
button: css`
|
|
|
|
margin-right: ${theme.spacing.sm};
|
|
|
|
`,
|
2019-12-20 04:21:04 -06:00
|
|
|
fullWidth: css`
|
|
|
|
width: 100%;
|
|
|
|
`,
|
2019-05-20 06:28:23 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
export interface Props extends Themeable {
|
2019-11-01 10:38:34 -05:00
|
|
|
logRows?: LogRowModel[];
|
2019-06-26 06:35:23 -05:00
|
|
|
timeZone: TimeZone;
|
2019-05-20 06:28:23 -05:00
|
|
|
stopLive: () => void;
|
2019-09-03 04:23:39 -05:00
|
|
|
onPause: () => void;
|
|
|
|
onResume: () => void;
|
|
|
|
isPaused: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface State {
|
2019-11-01 10:38:34 -05:00
|
|
|
logRowsToRender?: LogRowModel[];
|
2019-05-20 06:28:23 -05:00
|
|
|
}
|
|
|
|
|
2019-09-03 04:23:39 -05:00
|
|
|
class LiveLogs extends PureComponent<Props, State> {
|
2019-11-26 03:01:32 -06:00
|
|
|
private liveEndDiv: HTMLDivElement | null = null;
|
2019-12-16 09:07:36 -06:00
|
|
|
private scrollContainerRef = React.createRef<HTMLTableSectionElement>();
|
2019-09-03 04:23:39 -05:00
|
|
|
|
|
|
|
constructor(props: Props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
2019-11-01 10:38:34 -05:00
|
|
|
logRowsToRender: props.logRows,
|
2019-09-03 04:23:39 -05:00
|
|
|
};
|
|
|
|
}
|
2019-05-20 06:28:23 -05:00
|
|
|
|
2019-09-05 07:04:01 -05:00
|
|
|
static getDerivedStateFromProps(nextProps: Props, state: State) {
|
2019-09-03 04:23:39 -05:00
|
|
|
if (!nextProps.isPaused) {
|
|
|
|
return {
|
|
|
|
// We update what we show only if not paused. We keep any background subscriptions running and keep updating
|
|
|
|
// our state, but we do not show the updates, this allows us start again showing correct result after resuming
|
|
|
|
// without creating a gap in the log results.
|
2019-11-01 10:38:34 -05:00
|
|
|
logRowsToRender: nextProps.logRows,
|
2019-09-03 04:23:39 -05:00
|
|
|
};
|
|
|
|
} else {
|
|
|
|
return null;
|
2019-05-20 06:28:23 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-03 04:23:39 -05:00
|
|
|
/**
|
|
|
|
* Handle pausing when user scrolls up so that we stop resetting his position to the bottom when new row arrives.
|
|
|
|
* We do not need to throttle it here much, adding new rows should be throttled/buffered itself in the query epics
|
|
|
|
* and after you pause we remove the handler and add it after you manually resume, so this should not be fired often.
|
|
|
|
*/
|
|
|
|
onScroll = (event: React.SyntheticEvent) => {
|
|
|
|
const { isPaused, onPause } = this.props;
|
|
|
|
const { scrollTop, clientHeight, scrollHeight } = event.currentTarget;
|
|
|
|
const distanceFromBottom = scrollHeight - (scrollTop + clientHeight);
|
|
|
|
if (distanceFromBottom >= 5 && !isPaused) {
|
|
|
|
onPause();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
rowsToRender = () => {
|
|
|
|
const { isPaused } = this.props;
|
2019-11-26 03:01:32 -06:00
|
|
|
let { logRowsToRender: rowsToRender = [] } = this.state;
|
2019-09-03 04:23:39 -05:00
|
|
|
if (!isPaused) {
|
|
|
|
// A perf optimisation here. Show just 100 rows when streaming and full length when the streaming is paused.
|
|
|
|
rowsToRender = rowsToRender.slice(-100);
|
|
|
|
}
|
|
|
|
return rowsToRender;
|
|
|
|
};
|
|
|
|
|
2019-05-20 06:28:23 -05:00
|
|
|
render() {
|
2019-09-03 04:23:39 -05:00
|
|
|
const { theme, timeZone, onPause, onResume, isPaused } = this.props;
|
2019-05-20 06:28:23 -05:00
|
|
|
const styles = getStyles(theme);
|
2019-08-26 01:11:07 -05:00
|
|
|
const { logsRow, logsRowLocalTime, logsRowMessage } = getLogRowStyles(theme);
|
2019-05-20 06:28:23 -05:00
|
|
|
|
|
|
|
return (
|
2019-09-20 06:00:11 -05:00
|
|
|
<div>
|
2019-12-20 04:21:04 -06:00
|
|
|
<table className={styles.fullWidth}>
|
2019-12-16 09:07:36 -06:00
|
|
|
<tbody
|
|
|
|
onScroll={isPaused ? undefined : this.onScroll}
|
|
|
|
className={cx(['logs-rows', styles.logsRowsLive])}
|
|
|
|
ref={this.scrollContainerRef}
|
|
|
|
>
|
|
|
|
{this.rowsToRender().map((row: LogRowModel) => {
|
|
|
|
return (
|
|
|
|
<tr className={cx(logsRow, styles.logsRowFade)} key={row.uid}>
|
2020-04-27 08:28:06 -05:00
|
|
|
<td className={cx(logsRowLocalTime)}>{dateTimeFormat(row.timeEpochMs, { timeZone })}</td>
|
2020-11-11 07:58:45 -06:00
|
|
|
<td className={cx(logsRowMessage)}>{row.hasAnsi ? <LogMessageAnsi value={row.raw} /> : row.entry}</td>
|
2019-12-16 09:07:36 -06:00
|
|
|
</tr>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
<tr
|
2021-01-20 00:59:48 -06:00
|
|
|
ref={(element) => {
|
2019-12-16 09:07:36 -06:00
|
|
|
this.liveEndDiv = element;
|
|
|
|
// This is triggered on every update so on every new row. It keeps the view scrolled at the bottom by
|
|
|
|
// default.
|
|
|
|
if (this.liveEndDiv && !isPaused) {
|
2021-01-27 08:28:32 -06:00
|
|
|
this.scrollContainerRef.current?.scrollTo(0, this.scrollContainerRef.current.scrollHeight);
|
2019-12-16 09:07:36 -06:00
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</tbody>
|
|
|
|
</table>
|
2021-04-23 03:06:42 -05:00
|
|
|
<div className={styles.logsRowsIndicator}>
|
|
|
|
<Button variant="secondary" onClick={isPaused ? onResume : onPause} className={styles.button}>
|
2020-04-12 15:20:02 -05:00
|
|
|
<Icon name={isPaused ? 'play' : 'pause'} />
|
2019-09-03 04:23:39 -05:00
|
|
|
|
|
|
|
{isPaused ? 'Resume' : 'Pause'}
|
2021-04-23 03:06:42 -05:00
|
|
|
</Button>
|
|
|
|
<Button variant="secondary" onClick={this.props.stopLive} className={styles.button}>
|
2020-04-16 06:49:58 -05:00
|
|
|
<Icon name="square-shape" size="lg" type="mono" />
|
2019-09-09 12:09:06 -05:00
|
|
|
Exit live mode
|
2021-04-23 03:06:42 -05:00
|
|
|
</Button>
|
2019-09-03 04:23:39 -05:00
|
|
|
{isPaused || (
|
|
|
|
<span>
|
2019-11-01 10:38:34 -05:00
|
|
|
Last line received: <ElapsedTime resetKey={this.props.logRows} humanize={true} /> ago
|
2019-09-03 04:23:39 -05:00
|
|
|
</span>
|
|
|
|
)}
|
2019-05-20 06:28:23 -05:00
|
|
|
</div>
|
2019-09-20 06:00:11 -05:00
|
|
|
</div>
|
2019-05-20 06:28:23 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const LiveLogsWithTheme = withTheme(LiveLogs);
|