Logs.tsx 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. import _ from 'lodash';
  2. import React, { PureComponent } from 'react';
  3. import * as rangeUtil from 'app/core/utils/rangeutil';
  4. import { RawTimeRange } from '@grafana/ui';
  5. import {
  6. LogsDedupDescription,
  7. LogsDedupStrategy,
  8. LogsModel,
  9. dedupLogRows,
  10. filterLogLevels,
  11. LogLevel,
  12. LogsMetaKind,
  13. } from 'app/core/logs_model';
  14. import { Switch } from 'app/core/components/Switch/Switch';
  15. import ToggleButtonGroup, { ToggleButton } from 'app/core/components/ToggleButtonGroup/ToggleButtonGroup';
  16. import Graph from './Graph';
  17. import { LogLabels } from './LogLabels';
  18. import { LogRow } from './LogRow';
  19. const PREVIEW_LIMIT = 100;
  20. const graphOptions = {
  21. series: {
  22. stack: true,
  23. bars: {
  24. show: true,
  25. lineWidth: 5,
  26. // barWidth: 10,
  27. },
  28. // stack: true,
  29. },
  30. yaxis: {
  31. tickDecimals: 0,
  32. },
  33. };
  34. function renderMetaItem(value: any, kind: LogsMetaKind) {
  35. if (kind === LogsMetaKind.LabelsMap) {
  36. return (
  37. <span className="logs-meta-item__labels">
  38. <LogLabels labels={value} plain />
  39. </span>
  40. );
  41. }
  42. return value;
  43. }
  44. interface Props {
  45. data: LogsModel;
  46. exploreId: string;
  47. highlighterExpressions: string[];
  48. loading: boolean;
  49. range?: RawTimeRange;
  50. scanning?: boolean;
  51. scanRange?: RawTimeRange;
  52. onChangeTime?: (range: RawTimeRange) => void;
  53. onClickLabel?: (label: string, value: string) => void;
  54. onStartScanning?: () => void;
  55. onStopScanning?: () => void;
  56. }
  57. interface State {
  58. dedup: LogsDedupStrategy;
  59. deferLogs: boolean;
  60. hiddenLogLevels: Set<LogLevel>;
  61. renderAll: boolean;
  62. showLabels: boolean | null; // Tristate: null means auto
  63. showLocalTime: boolean;
  64. showUtc: boolean;
  65. }
  66. export default class Logs extends PureComponent<Props, State> {
  67. deferLogsTimer: NodeJS.Timer;
  68. renderAllTimer: NodeJS.Timer;
  69. state = {
  70. dedup: LogsDedupStrategy.none,
  71. deferLogs: true,
  72. hiddenLogLevels: new Set(),
  73. renderAll: false,
  74. showLabels: null,
  75. showLocalTime: true,
  76. showUtc: false,
  77. };
  78. componentDidMount() {
  79. // Staged rendering
  80. if (this.state.deferLogs) {
  81. const { data } = this.props;
  82. const rowCount = data && data.rows ? data.rows.length : 0;
  83. // Render all right away if not too far over the limit
  84. const renderAll = rowCount <= PREVIEW_LIMIT * 2;
  85. this.deferLogsTimer = setTimeout(() => this.setState({ deferLogs: false, renderAll }), rowCount);
  86. }
  87. }
  88. componentDidUpdate(prevProps, prevState) {
  89. // Staged rendering
  90. if (prevState.deferLogs && !this.state.deferLogs && !this.state.renderAll) {
  91. this.renderAllTimer = setTimeout(() => this.setState({ renderAll: true }), 2000);
  92. }
  93. }
  94. componentWillUnmount() {
  95. clearTimeout(this.deferLogsTimer);
  96. clearTimeout(this.renderAllTimer);
  97. }
  98. onChangeDedup = (dedup: LogsDedupStrategy) => {
  99. this.setState(prevState => {
  100. if (prevState.dedup === dedup) {
  101. return { dedup: LogsDedupStrategy.none };
  102. }
  103. return { dedup };
  104. });
  105. };
  106. onChangeLabels = (event: React.SyntheticEvent) => {
  107. const target = event.target as HTMLInputElement;
  108. this.setState({
  109. showLabels: target.checked,
  110. });
  111. };
  112. onChangeLocalTime = (event: React.SyntheticEvent) => {
  113. const target = event.target as HTMLInputElement;
  114. this.setState({
  115. showLocalTime: target.checked,
  116. });
  117. };
  118. onChangeUtc = (event: React.SyntheticEvent) => {
  119. const target = event.target as HTMLInputElement;
  120. this.setState({
  121. showUtc: target.checked,
  122. });
  123. };
  124. onToggleLogLevel = (rawLevel: string, hiddenRawLevels: Set<string>) => {
  125. const hiddenLogLevels: Set<LogLevel> = new Set(Array.from(hiddenRawLevels).map(level => LogLevel[level]));
  126. this.setState({ hiddenLogLevels });
  127. };
  128. onClickScan = (event: React.SyntheticEvent) => {
  129. event.preventDefault();
  130. this.props.onStartScanning();
  131. };
  132. onClickStopScan = (event: React.SyntheticEvent) => {
  133. event.preventDefault();
  134. this.props.onStopScanning();
  135. };
  136. render() {
  137. const {
  138. data,
  139. exploreId,
  140. highlighterExpressions,
  141. loading = false,
  142. onClickLabel,
  143. range,
  144. scanning,
  145. scanRange,
  146. } = this.props;
  147. const { dedup, deferLogs, hiddenLogLevels, renderAll, showLocalTime, showUtc } = this.state;
  148. let { showLabels } = this.state;
  149. const hasData = data && data.rows && data.rows.length > 0;
  150. const showDuplicates = dedup !== LogsDedupStrategy.none;
  151. // Filtering
  152. const filteredData = filterLogLevels(data, hiddenLogLevels);
  153. const dedupedData = dedupLogRows(filteredData, dedup);
  154. const dedupCount = dedupedData.rows.reduce((sum, row) => sum + row.duplicates, 0);
  155. const meta = [...data.meta];
  156. if (dedup !== LogsDedupStrategy.none) {
  157. meta.push({
  158. label: 'Dedup count',
  159. value: dedupCount,
  160. kind: LogsMetaKind.Number,
  161. });
  162. }
  163. // Staged rendering
  164. const processedRows = dedupedData.rows;
  165. const firstRows = processedRows.slice(0, PREVIEW_LIMIT);
  166. const lastRows = processedRows.slice(PREVIEW_LIMIT);
  167. // Check for labels
  168. if (showLabels === null) {
  169. if (hasData) {
  170. showLabels = data.rows.some(row => _.size(row.uniqueLabels) > 0);
  171. } else {
  172. showLabels = true;
  173. }
  174. }
  175. const scanText = scanRange ? `Scanning ${rangeUtil.describeTimeRange(scanRange)}` : 'Scanning...';
  176. // React profiler becomes unusable if we pass all rows to all rows and their labels, using getter instead
  177. const getRows = () => processedRows;
  178. return (
  179. <div className="logs-panel">
  180. <div className="logs-panel-graph">
  181. <Graph
  182. data={data.series}
  183. height="100px"
  184. range={range}
  185. id={`explore-logs-graph-${exploreId}`}
  186. onChangeTime={this.props.onChangeTime}
  187. onToggleSeries={this.onToggleLogLevel}
  188. userOptions={graphOptions}
  189. />
  190. </div>
  191. <div className="logs-panel-options">
  192. <div className="logs-panel-controls">
  193. <Switch label="Timestamp" checked={showUtc} onChange={this.onChangeUtc} transparent />
  194. <Switch label="Local time" checked={showLocalTime} onChange={this.onChangeLocalTime} transparent />
  195. <Switch label="Labels" checked={showLabels} onChange={this.onChangeLabels} transparent />
  196. <ToggleButtonGroup label="Dedup" transparent={true}>
  197. {Object.keys(LogsDedupStrategy).map((dedupType, i) => (
  198. <ToggleButton
  199. key={i}
  200. value={dedupType}
  201. onChange={this.onChangeDedup}
  202. selected={dedup === dedupType}
  203. tooltip={LogsDedupDescription[dedupType]}
  204. >
  205. {dedupType}
  206. </ToggleButton>
  207. ))}
  208. </ToggleButtonGroup>
  209. </div>
  210. </div>
  211. {hasData &&
  212. meta && (
  213. <div className="logs-panel-meta">
  214. {meta.map(item => (
  215. <div className="logs-panel-meta__item" key={item.label}>
  216. <span className="logs-panel-meta__label">{item.label}:</span>
  217. <span className="logs-panel-meta__value">{renderMetaItem(item.value, item.kind)}</span>
  218. </div>
  219. ))}
  220. </div>
  221. )}
  222. <div className="logs-rows">
  223. {hasData &&
  224. !deferLogs && // Only inject highlighterExpression in the first set for performance reasons
  225. firstRows.map(row => (
  226. <LogRow
  227. key={row.key + row.duplicates}
  228. getRows={getRows}
  229. highlighterExpressions={highlighterExpressions}
  230. row={row}
  231. showDuplicates={showDuplicates}
  232. showLabels={showLabels}
  233. showLocalTime={showLocalTime}
  234. showUtc={showUtc}
  235. onClickLabel={onClickLabel}
  236. />
  237. ))}
  238. {hasData &&
  239. !deferLogs &&
  240. renderAll &&
  241. lastRows.map(row => (
  242. <LogRow
  243. key={row.key + row.duplicates}
  244. getRows={getRows}
  245. row={row}
  246. showDuplicates={showDuplicates}
  247. showLabels={showLabels}
  248. showLocalTime={showLocalTime}
  249. showUtc={showUtc}
  250. onClickLabel={onClickLabel}
  251. />
  252. ))}
  253. {hasData && deferLogs && <span>Rendering {dedupedData.rows.length} rows...</span>}
  254. </div>
  255. {!loading &&
  256. !hasData &&
  257. !scanning && (
  258. <div className="logs-panel-nodata">
  259. No logs found.
  260. <a className="link" onClick={this.onClickScan}>
  261. Scan for older logs
  262. </a>
  263. </div>
  264. )}
  265. {scanning && (
  266. <div className="logs-panel-nodata">
  267. <span>{scanText}</span>
  268. <a className="link" onClick={this.onClickStopScan}>
  269. Stop scan
  270. </a>
  271. </div>
  272. )}
  273. </div>
  274. );
  275. }
  276. }