grafana/public/app/features/explore/LiveLogs.tsx

167 lines
5.5 KiB
TypeScript
Raw Normal View History

import React, { PureComponent } from 'react';
import { css, cx } from '@emotion/css';
import tinycolor from 'tinycolor2';
import { LogMessageAnsi, Themeable, withTheme, getLogRowStyles, Icon, Button } from '@grafana/ui';
DateTime: adding support to select preferred timezone for presentation of date and time values. (#23586) * added moment timezone package. * added a qnd way of selecting timezone. * added a first draft to display how it can be used. * fixed failing tests. * made moment.local to be in utc when running tests. * added tests to verify that the timeZone support works as expected. * Fixed so we use the formatter in the graph context menu. * changed so we will format d3 according to timeZone. * changed from class base to function based for easier consumption. * fixed so tests got green. * renamed to make it shorter. * fixed formatting in logRow. * removed unused value. * added time formatter to flot. * fixed failing tests. * changed so history will use the formatting with support for timezone. * added todo. * added so we append the correct abbrivation behind time. * added time zone abbrevation in timepicker. * adding timezone in rangeutil tool. * will use timezone when formatting range. * changed so we use new functions to format date so timezone is respected. * wip - dashboard settings. * changed so the time picker settings is in react. * added force update. * wip to get the react graph to work. * fixed formatting and parsing on the timepicker. * updated snap to be correct. * fixed so we format values properly in time picker. * make sure we pass timezone on all the proper places. * fixed so we use correct timeZone in explore. * fixed failing tests. * fixed so we always parse from local to selected timezone. * removed unused variable. * reverted back. * trying to fix issue with directive. * fixed issue. * fixed strict null errors. * fixed so we still can select default. * make sure we reads the time zone from getTimezone
2020-04-27 08:28:06 -05:00
import { GrafanaTheme, LogRowModel, TimeZone, dateTimeFormat } from '@grafana/data';
import { ElapsedTime } from './ElapsedTime';
const getStyles = (theme: GrafanaTheme) => ({
logsRowsLive: css`
label: logs-rows-live;
font-family: ${theme.typography.fontFamily.monospace};
font-size: ${theme.typography.size.sm};
display: flex;
flex-flow: column nowrap;
height: 60vh;
overflow-y: scroll;
:first-child {
margin-top: auto !important;
}
`,
2019-09-30 07:44:15 -05:00
logsRowFade: css`
label: logs-row-fresh;
color: ${theme.colors.text};
background-color: ${tinycolor(theme.palette.blue95).setAlpha(0.25).toString()};
animation: fade 1s ease-out 1s 1 normal forwards;
@keyframes fade {
from {
background-color: ${tinycolor(theme.palette.blue95).setAlpha(0.25).toString()};
}
to {
background-color: transparent;
}
}
`,
logsRowsIndicator: css`
font-size: ${theme.typography.size.md};
padding-top: ${theme.spacing.sm};
display: flex;
align-items: center;
`,
button: css`
margin-right: ${theme.spacing.sm};
`,
fullWidth: css`
width: 100%;
`,
});
export interface Props extends Themeable {
logRows?: LogRowModel[];
timeZone: TimeZone;
stopLive: () => void;
onPause: () => void;
onResume: () => void;
isPaused: boolean;
}
interface State {
logRowsToRender?: LogRowModel[];
}
class LiveLogs extends PureComponent<Props, State> {
private liveEndDiv: HTMLDivElement | null = null;
2019-12-16 09:07:36 -06:00
private scrollContainerRef = React.createRef<HTMLTableSectionElement>();
constructor(props: Props) {
super(props);
this.state = {
logRowsToRender: props.logRows,
};
}
static getDerivedStateFromProps(nextProps: Props, state: State) {
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.
logRowsToRender: nextProps.logRows,
};
} else {
return null;
}
}
/**
* 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;
let { logRowsToRender: rowsToRender = [] } = this.state;
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;
};
render() {
const { theme, timeZone, onPause, onResume, isPaused } = this.props;
const styles = getStyles(theme);
const { logsRow, logsRowLocalTime, logsRowMessage } = getLogRowStyles(theme);
return (
<div>
<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}>
DateTime: adding support to select preferred timezone for presentation of date and time values. (#23586) * added moment timezone package. * added a qnd way of selecting timezone. * added a first draft to display how it can be used. * fixed failing tests. * made moment.local to be in utc when running tests. * added tests to verify that the timeZone support works as expected. * Fixed so we use the formatter in the graph context menu. * changed so we will format d3 according to timeZone. * changed from class base to function based for easier consumption. * fixed so tests got green. * renamed to make it shorter. * fixed formatting in logRow. * removed unused value. * added time formatter to flot. * fixed failing tests. * changed so history will use the formatting with support for timezone. * added todo. * added so we append the correct abbrivation behind time. * added time zone abbrevation in timepicker. * adding timezone in rangeutil tool. * will use timezone when formatting range. * changed so we use new functions to format date so timezone is respected. * wip - dashboard settings. * changed so the time picker settings is in react. * added force update. * wip to get the react graph to work. * fixed formatting and parsing on the timepicker. * updated snap to be correct. * fixed so we format values properly in time picker. * make sure we pass timezone on all the proper places. * fixed so we use correct timeZone in explore. * fixed failing tests. * fixed so we always parse from local to selected timezone. * removed unused variable. * reverted back. * trying to fix issue with directive. * fixed issue. * fixed strict null errors. * fixed so we still can select default. * make sure we reads the time zone from getTimezone
2020-04-27 08:28:06 -05:00
<td className={cx(logsRowLocalTime)}>{dateTimeFormat(row.timeEpochMs, { timeZone })}</td>
<td className={cx(logsRowMessage)}>{row.hasAnsi ? <LogMessageAnsi value={row.raw} /> : row.entry}</td>
2019-12-16 09:07:36 -06:00
</tr>
);
})}
<tr
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) {
this.scrollContainerRef.current?.scrollTo(0, this.scrollContainerRef.current.scrollHeight);
2019-12-16 09:07:36 -06:00
}
}}
/>
</tbody>
</table>
<div className={styles.logsRowsIndicator}>
<Button variant="secondary" onClick={isPaused ? onResume : onPause} className={styles.button}>
<Icon name={isPaused ? 'play' : 'pause'} />
&nbsp;
{isPaused ? 'Resume' : 'Pause'}
</Button>
<Button variant="secondary" onClick={this.props.stopLive} className={styles.button}>
<Icon name="square-shape" size="lg" type="mono" />
&nbsp; Exit live mode
</Button>
{isPaused || (
<span>
Last line received: <ElapsedTime resetKey={this.props.logRows} humanize={true} /> ago
</span>
)}
</div>
</div>
);
}
}
export const LiveLogsWithTheme = withTheme(LiveLogs);