result_transformer.ts 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import _ from 'lodash';
  2. import moment from 'moment';
  3. import { LogLevel, LogsModel, LogRow } from 'app/core/logs_model';
  4. export function getLogLevel(line: string): LogLevel {
  5. if (!line) {
  6. return undefined;
  7. }
  8. let level: LogLevel;
  9. Object.keys(LogLevel).forEach(key => {
  10. if (!level) {
  11. const regexp = new RegExp(`\\b${key}\\b`, 'i');
  12. if (regexp.test(line)) {
  13. level = LogLevel[key];
  14. }
  15. }
  16. });
  17. return level;
  18. }
  19. export function processEntry(entry: { line: string; timestamp: string }, stream): LogRow {
  20. const { line, timestamp } = entry;
  21. const { labels } = stream;
  22. const key = `EK${timestamp}${labels}`;
  23. const time = moment(timestamp);
  24. const timeFromNow = time.fromNow();
  25. const timeLocal = time.format('YYYY-MM-DD HH:mm:ss');
  26. const logLevel = getLogLevel(line);
  27. return {
  28. key,
  29. logLevel,
  30. timeFromNow,
  31. timeLocal,
  32. entry: line,
  33. searchWords: [stream.search],
  34. timestamp: timestamp,
  35. };
  36. }
  37. export function processStreams(streams, limit?: number): LogsModel {
  38. const combinedEntries = streams.reduce((acc, stream) => {
  39. return [...acc, ...stream.entries.map(entry => processEntry(entry, stream))];
  40. }, []);
  41. const sortedEntries = _.chain(combinedEntries)
  42. .sortBy('timestamp')
  43. .reverse()
  44. .slice(0, limit || combinedEntries.length)
  45. .value();
  46. return { rows: sortedEntries };
  47. }