2019-01-22 01:59:22 -06:00
|
|
|
import React, { PureComponent } from 'react';
|
|
|
|
import _ from 'lodash';
|
2019-06-27 06:21:04 -05:00
|
|
|
// @ts-ignore
|
2019-01-22 01:59:22 -06:00
|
|
|
import Highlighter from 'react-highlight-words';
|
|
|
|
import classnames from 'classnames';
|
|
|
|
|
2019-05-22 16:10:05 -05:00
|
|
|
import { calculateFieldStats, getParser } from 'app/core/logs_model';
|
2019-01-22 02:22:38 -06:00
|
|
|
import { LogLabels } from './LogLabels';
|
2019-02-22 06:13:10 -06:00
|
|
|
import { findHighlightChunksInText } from 'app/core/utils/text';
|
2019-01-22 02:22:38 -06:00
|
|
|
import { LogLabelStats } from './LogLabelStats';
|
2019-02-07 10:44:09 -06:00
|
|
|
import { LogMessageAnsi } from './LogMessageAnsi';
|
2019-05-20 01:44:37 -05:00
|
|
|
import { css, cx } from 'emotion';
|
|
|
|
import {
|
|
|
|
LogRowContextProvider,
|
|
|
|
LogRowContextRows,
|
|
|
|
HasMoreContextRows,
|
|
|
|
LogRowContextQueryErrors,
|
|
|
|
} from './LogRowContextProvider';
|
2019-07-06 01:05:53 -05:00
|
|
|
import { ThemeContext, selectThemeVariant, GrafanaTheme, DataQueryResponse } from '@grafana/ui';
|
|
|
|
|
|
|
|
import { LogRowModel, LogLabelStatsModel, LogsParser, TimeZone } from '@grafana/data';
|
2019-05-20 01:44:37 -05:00
|
|
|
import { LogRowContext } from './LogRowContext';
|
|
|
|
import tinycolor from 'tinycolor2';
|
2019-01-22 01:59:22 -06:00
|
|
|
|
2019-01-22 02:22:38 -06:00
|
|
|
interface Props {
|
2019-01-22 01:59:22 -06:00
|
|
|
highlighterExpressions?: string[];
|
|
|
|
row: LogRowModel;
|
|
|
|
showDuplicates: boolean;
|
2019-03-25 09:23:54 -05:00
|
|
|
showLabels: boolean;
|
2019-06-26 06:35:23 -05:00
|
|
|
showTime: boolean;
|
|
|
|
timeZone: TimeZone;
|
2019-01-22 01:59:22 -06:00
|
|
|
getRows: () => LogRowModel[];
|
|
|
|
onClickLabel?: (label: string, value: string) => void;
|
2019-05-20 01:44:37 -05:00
|
|
|
onContextClick?: () => void;
|
2019-05-22 16:10:05 -05:00
|
|
|
getRowContext?: (row: LogRowModel, options?: any) => Promise<DataQueryResponse>;
|
2019-05-20 01:44:37 -05:00
|
|
|
className?: string;
|
2019-01-22 01:59:22 -06:00
|
|
|
}
|
|
|
|
|
2019-01-22 02:22:38 -06:00
|
|
|
interface State {
|
2019-01-22 01:59:22 -06:00
|
|
|
fieldCount: number;
|
|
|
|
fieldLabel: string;
|
2019-01-22 02:22:38 -06:00
|
|
|
fieldStats: LogLabelStatsModel[];
|
2019-01-22 01:59:22 -06:00
|
|
|
fieldValue: string;
|
|
|
|
parsed: boolean;
|
|
|
|
parser?: LogsParser;
|
|
|
|
parsedFieldHighlights: string[];
|
|
|
|
showFieldStats: boolean;
|
2019-05-20 01:44:37 -05:00
|
|
|
showContext: boolean;
|
2019-01-22 01:59:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders a highlighted field.
|
|
|
|
* When hovering, a stats icon is shown.
|
|
|
|
*/
|
2019-06-27 06:21:04 -05:00
|
|
|
const FieldHighlight = (onClick: any) => (props: any) => {
|
2019-01-22 01:59:22 -06:00
|
|
|
return (
|
|
|
|
<span className={props.className} style={props.style}>
|
|
|
|
{props.children}
|
|
|
|
<span className="logs-row__field-highlight--icon fa fa-signal" onClick={() => onClick(props.children)} />
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2019-05-20 01:44:37 -05:00
|
|
|
const logRowStyles = css`
|
|
|
|
position: relative;
|
|
|
|
/* z-index: 0; */
|
|
|
|
/* outline: none; */
|
|
|
|
`;
|
|
|
|
|
|
|
|
const getLogRowWithContextStyles = (theme: GrafanaTheme, state: State) => {
|
|
|
|
const outlineColor = selectThemeVariant(
|
|
|
|
{
|
|
|
|
light: theme.colors.white,
|
|
|
|
dark: theme.colors.black,
|
|
|
|
},
|
|
|
|
theme.type
|
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
row: css`
|
|
|
|
z-index: 1;
|
|
|
|
outline: 9999px solid
|
2019-06-27 06:21:04 -05:00
|
|
|
${tinycolor(outlineColor as tinycolor.ColorInput)
|
2019-05-20 01:44:37 -05:00
|
|
|
.setAlpha(0.7)
|
|
|
|
.toRgbString()};
|
|
|
|
`,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2019-01-22 01:59:22 -06:00
|
|
|
/**
|
|
|
|
* Renders a log line.
|
|
|
|
*
|
|
|
|
* When user hovers over it for a certain time, it lazily parses the log line.
|
|
|
|
* Once a parser is found, it will determine fields, that will be highlighted.
|
|
|
|
* When the user requests stats for a field, they will be calculated and rendered below the row.
|
|
|
|
*/
|
2019-01-22 02:22:38 -06:00
|
|
|
export class LogRow extends PureComponent<Props, State> {
|
2019-01-22 01:59:22 -06:00
|
|
|
mouseMessageTimer: NodeJS.Timer;
|
|
|
|
|
2019-06-27 06:21:04 -05:00
|
|
|
state: any = {
|
2019-01-22 01:59:22 -06:00
|
|
|
fieldCount: 0,
|
|
|
|
fieldLabel: null,
|
|
|
|
fieldStats: null,
|
|
|
|
fieldValue: null,
|
|
|
|
parsed: false,
|
|
|
|
parser: undefined,
|
|
|
|
parsedFieldHighlights: [],
|
|
|
|
showFieldStats: false,
|
2019-05-20 01:44:37 -05:00
|
|
|
showContext: false,
|
2019-01-22 01:59:22 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
clearTimeout(this.mouseMessageTimer);
|
|
|
|
}
|
|
|
|
|
|
|
|
onClickClose = () => {
|
|
|
|
this.setState({ showFieldStats: false });
|
|
|
|
};
|
|
|
|
|
|
|
|
onClickHighlight = (fieldText: string) => {
|
|
|
|
const { getRows } = this.props;
|
|
|
|
const { parser } = this.state;
|
|
|
|
const allRows = getRows();
|
|
|
|
|
|
|
|
// Build value-agnostic row matcher based on the field label
|
|
|
|
const fieldLabel = parser.getLabelFromField(fieldText);
|
|
|
|
const fieldValue = parser.getValueFromField(fieldText);
|
|
|
|
const matcher = parser.buildMatcher(fieldLabel);
|
|
|
|
const fieldStats = calculateFieldStats(allRows, matcher);
|
|
|
|
const fieldCount = fieldStats.reduce((sum, stat) => sum + stat.count, 0);
|
|
|
|
|
|
|
|
this.setState({ fieldCount, fieldLabel, fieldStats, fieldValue, showFieldStats: true });
|
|
|
|
};
|
|
|
|
|
|
|
|
onMouseOverMessage = () => {
|
2019-05-20 02:51:57 -05:00
|
|
|
if (this.state.showContext || this.isTextSelected()) {
|
2019-05-20 01:44:37 -05:00
|
|
|
// When showing context we don't want to the LogRow rerender as it will mess up state of context block
|
|
|
|
// making the "after" context to be scrolled to the top, what is desired only on open
|
|
|
|
// The log row message needs to be refactored to separate component that encapsulates parsing and parsed message state
|
|
|
|
return;
|
|
|
|
}
|
2019-01-22 01:59:22 -06:00
|
|
|
// Don't parse right away, user might move along
|
|
|
|
this.mouseMessageTimer = setTimeout(this.parseMessage, 500);
|
|
|
|
};
|
|
|
|
|
|
|
|
onMouseOutMessage = () => {
|
2019-05-20 01:44:37 -05:00
|
|
|
if (this.state.showContext) {
|
|
|
|
// See comment in onMouseOverMessage method
|
|
|
|
return;
|
|
|
|
}
|
2019-01-22 01:59:22 -06:00
|
|
|
clearTimeout(this.mouseMessageTimer);
|
|
|
|
this.setState({ parsed: false });
|
|
|
|
};
|
|
|
|
|
|
|
|
parseMessage = () => {
|
|
|
|
if (!this.state.parsed) {
|
|
|
|
const { row } = this.props;
|
|
|
|
const parser = getParser(row.entry);
|
|
|
|
if (parser) {
|
|
|
|
// Use parser to highlight detected fields
|
|
|
|
const parsedFieldHighlights = parser.getFields(this.props.row.entry);
|
|
|
|
this.setState({ parsedFieldHighlights, parsed: true, parser });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-05-20 02:51:57 -05:00
|
|
|
isTextSelected() {
|
|
|
|
if (!window.getSelection) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const selection = window.getSelection();
|
|
|
|
|
|
|
|
if (!selection) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return selection.anchorNode !== null && selection.isCollapsed === false;
|
|
|
|
}
|
|
|
|
|
2019-05-20 01:44:37 -05:00
|
|
|
toggleContext = () => {
|
|
|
|
this.setState(state => {
|
|
|
|
return {
|
|
|
|
showContext: !state.showContext,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
onContextToggle = (e: React.SyntheticEvent<HTMLElement>) => {
|
|
|
|
e.stopPropagation();
|
|
|
|
this.toggleContext();
|
|
|
|
};
|
|
|
|
|
|
|
|
renderLogRow(
|
|
|
|
context?: LogRowContextRows,
|
|
|
|
errors?: LogRowContextQueryErrors,
|
|
|
|
hasMoreContextRows?: HasMoreContextRows,
|
|
|
|
updateLimit?: () => void
|
|
|
|
) {
|
2019-01-22 01:59:22 -06:00
|
|
|
const {
|
|
|
|
getRows,
|
|
|
|
highlighterExpressions,
|
|
|
|
onClickLabel,
|
|
|
|
row,
|
|
|
|
showDuplicates,
|
|
|
|
showLabels,
|
2019-06-26 06:35:23 -05:00
|
|
|
timeZone,
|
|
|
|
showTime,
|
2019-01-22 01:59:22 -06:00
|
|
|
} = this.props;
|
|
|
|
const {
|
|
|
|
fieldCount,
|
|
|
|
fieldLabel,
|
|
|
|
fieldStats,
|
|
|
|
fieldValue,
|
|
|
|
parsed,
|
|
|
|
parsedFieldHighlights,
|
|
|
|
showFieldStats,
|
2019-05-20 01:44:37 -05:00
|
|
|
showContext,
|
2019-01-22 01:59:22 -06:00
|
|
|
} = this.state;
|
2019-02-22 06:13:10 -06:00
|
|
|
const { entry, hasAnsi, raw } = row;
|
2019-01-22 01:59:22 -06:00
|
|
|
const previewHighlights = highlighterExpressions && !_.isEqual(highlighterExpressions, row.searchWords);
|
|
|
|
const highlights = previewHighlights ? highlighterExpressions : row.searchWords;
|
2019-05-13 02:58:26 -05:00
|
|
|
const needsHighlighter = highlights && highlights.length > 0 && highlights[0] && highlights[0].length > 0;
|
2019-01-22 01:59:22 -06:00
|
|
|
const highlightClassName = classnames('logs-row__match-highlight', {
|
|
|
|
'logs-row__match-highlight--preview': previewHighlights,
|
|
|
|
});
|
2019-06-26 06:35:23 -05:00
|
|
|
const showUtc = timeZone === 'utc';
|
2019-02-07 10:44:09 -06:00
|
|
|
|
2019-01-22 01:59:22 -06:00
|
|
|
return (
|
2019-05-20 01:44:37 -05:00
|
|
|
<ThemeContext.Consumer>
|
|
|
|
{theme => {
|
|
|
|
const styles = this.state.showContext
|
|
|
|
? cx(logRowStyles, getLogRowWithContextStyles(theme, this.state).row)
|
|
|
|
: logRowStyles;
|
|
|
|
return (
|
|
|
|
<div className={`logs-row ${this.props.className}`}>
|
|
|
|
{showDuplicates && (
|
|
|
|
<div className="logs-row__duplicates">{row.duplicates > 0 ? `${row.duplicates + 1}x` : null}</div>
|
|
|
|
)}
|
|
|
|
<div className={row.logLevel ? `logs-row__level logs-row__level--${row.logLevel}` : ''} />
|
2019-06-26 06:35:23 -05:00
|
|
|
{showTime && showUtc && (
|
|
|
|
<div className="logs-row__localtime" title={`Local: ${row.timeLocal} (${row.timeFromNow})`}>
|
|
|
|
{row.timeUtc}
|
2019-05-20 01:44:37 -05:00
|
|
|
</div>
|
|
|
|
)}
|
2019-06-26 06:35:23 -05:00
|
|
|
{showTime && !showUtc && (
|
|
|
|
<div className="logs-row__localtime" title={`${row.timeUtc} (${row.timeFromNow})`}>
|
2019-05-20 01:44:37 -05:00
|
|
|
{row.timeLocal}
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
{showLabels && (
|
|
|
|
<div className="logs-row__labels">
|
|
|
|
<LogLabels getRows={getRows} labels={row.uniqueLabels} onClickLabel={onClickLabel} />
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
<div
|
|
|
|
className="logs-row__message"
|
|
|
|
onMouseEnter={this.onMouseOverMessage}
|
|
|
|
onMouseLeave={this.onMouseOutMessage}
|
|
|
|
>
|
|
|
|
<div
|
|
|
|
className={css`
|
|
|
|
position: relative;
|
|
|
|
`}
|
|
|
|
>
|
|
|
|
{showContext && context && (
|
|
|
|
<LogRowContext
|
|
|
|
row={row}
|
|
|
|
context={context}
|
|
|
|
errors={errors}
|
|
|
|
hasMoreContextRows={hasMoreContextRows}
|
|
|
|
onOutsideClick={this.toggleContext}
|
|
|
|
onLoadMoreContext={() => {
|
|
|
|
if (updateLimit) {
|
|
|
|
updateLimit();
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
<span className={styles}>
|
|
|
|
{parsed && (
|
|
|
|
<Highlighter
|
2019-06-06 06:55:04 -05:00
|
|
|
style={{ whiteSpace: 'pre-wrap' }}
|
2019-05-20 01:44:37 -05:00
|
|
|
autoEscape
|
|
|
|
highlightTag={FieldHighlight(this.onClickHighlight)}
|
|
|
|
textToHighlight={entry}
|
|
|
|
searchWords={parsedFieldHighlights}
|
|
|
|
highlightClassName="logs-row__field-highlight"
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
{!parsed && needsHighlighter && (
|
|
|
|
<Highlighter
|
2019-06-06 06:55:04 -05:00
|
|
|
style={{ whiteSpace: 'pre-wrap' }}
|
2019-05-20 01:44:37 -05:00
|
|
|
textToHighlight={entry}
|
|
|
|
searchWords={highlights}
|
|
|
|
findChunks={findHighlightChunksInText}
|
|
|
|
highlightClassName={highlightClassName}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
{hasAnsi && !parsed && !needsHighlighter && <LogMessageAnsi value={raw} />}
|
|
|
|
{!hasAnsi && !parsed && !needsHighlighter && entry}
|
|
|
|
{showFieldStats && (
|
|
|
|
<div className="logs-row__stats">
|
|
|
|
<LogLabelStats
|
|
|
|
stats={fieldStats}
|
|
|
|
label={fieldLabel}
|
|
|
|
value={fieldValue}
|
|
|
|
onClickClose={this.onClickClose}
|
|
|
|
rowCount={fieldCount}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</span>
|
|
|
|
{row.searchWords && row.searchWords.length > 0 && (
|
|
|
|
<span
|
|
|
|
onClick={this.onContextToggle}
|
|
|
|
className={css`
|
|
|
|
visibility: hidden;
|
|
|
|
white-space: nowrap;
|
|
|
|
position: relative;
|
|
|
|
z-index: ${showContext ? 1 : 0};
|
|
|
|
cursor: pointer;
|
|
|
|
.logs-row:hover & {
|
|
|
|
visibility: visible;
|
|
|
|
margin-left: 10px;
|
|
|
|
text-decoration: underline;
|
|
|
|
}
|
|
|
|
`}
|
|
|
|
>
|
|
|
|
{showContext ? 'Hide' : 'Show'} context
|
|
|
|
</span>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</div>
|
2019-01-22 01:59:22 -06:00
|
|
|
</div>
|
2019-05-20 01:44:37 -05:00
|
|
|
);
|
|
|
|
}}
|
|
|
|
</ThemeContext.Consumer>
|
2019-01-22 01:59:22 -06:00
|
|
|
);
|
|
|
|
}
|
2019-05-20 01:44:37 -05:00
|
|
|
|
|
|
|
render() {
|
|
|
|
const { showContext } = this.state;
|
|
|
|
|
|
|
|
if (showContext) {
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<LogRowContextProvider row={this.props.row} getRowContext={this.props.getRowContext}>
|
|
|
|
{({ result, errors, hasMoreContextRows, updateLimit }) => {
|
|
|
|
return <>{this.renderLogRow(result, errors, hasMoreContextRows, updateLimit)}</>;
|
|
|
|
}}
|
|
|
|
</LogRowContextProvider>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.renderLogRow();
|
|
|
|
}
|
2019-01-22 01:59:22 -06:00
|
|
|
}
|