events_processing.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import _ from 'lodash';
  2. /**
  3. * This function converts annotation events into set
  4. * of single events and regions (event consist of two)
  5. * @param annotations
  6. * @param options
  7. */
  8. export function makeRegions(annotations, options) {
  9. const [regionEvents, singleEvents] = _.partition(annotations, 'regionId');
  10. const regions = getRegions(regionEvents, options.range);
  11. annotations = _.concat(regions, singleEvents);
  12. return annotations;
  13. }
  14. function getRegions(events, range) {
  15. const regionEvents = _.filter(events, event => {
  16. return event.regionId;
  17. });
  18. let regions = _.groupBy(regionEvents, 'regionId');
  19. regions = _.compact(
  20. _.map(regions, regionEvents => {
  21. const regionObj = _.head(regionEvents);
  22. if (regionEvents && regionEvents.length > 1) {
  23. regionObj.timeEnd = regionEvents[1].time;
  24. regionObj.isRegion = true;
  25. return regionObj;
  26. } else {
  27. if (regionEvents && regionEvents.length) {
  28. // Don't change proper region object
  29. if (!regionObj.time || !regionObj.timeEnd) {
  30. // This is cut region
  31. if (isStartOfRegion(regionObj)) {
  32. regionObj.timeEnd = range.to.valueOf() - 1;
  33. } else {
  34. // Start time = null
  35. regionObj.timeEnd = regionObj.time;
  36. regionObj.time = range.from.valueOf() + 1;
  37. }
  38. regionObj.isRegion = true;
  39. }
  40. return regionObj;
  41. }
  42. }
  43. })
  44. );
  45. return regions;
  46. }
  47. function isStartOfRegion(event): boolean {
  48. return event.id && event.id === event.regionId;
  49. }
  50. export function dedupAnnotations(annotations) {
  51. let dedup = [];
  52. // Split events by annotationId property existence
  53. const events = _.partition(annotations, 'id');
  54. const eventsById = _.groupBy(events[0], 'id');
  55. dedup = _.map(eventsById, eventGroup => {
  56. if (eventGroup.length > 1 && !_.every(eventGroup, isPanelAlert)) {
  57. // Get first non-panel alert
  58. return _.find(eventGroup, event => {
  59. return event.eventType !== 'panel-alert';
  60. });
  61. } else {
  62. return _.head(eventGroup);
  63. }
  64. });
  65. dedup = _.concat(dedup, events[1]);
  66. return dedup;
  67. }
  68. function isPanelAlert(event) {
  69. return event.eventType === 'panel-alert';
  70. }