# All files / src/utils data-utils.js

100% Statements 54/54
82.86% Branches 29/35
100% Functions 17/17
100% Lines 52/52

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169

    4x

    21x
    21x

    18x

    21x

    70x

    3x

    13x
    13x

    13x
    53x
    53x

    53x
    45x
    45x

    8x

    13x

    1x
    3x

    25x
    7x
    7x

    18x
    18x
    86x
    46x
    46x
    11x

    35x

    40x

    2x
    2x

    2x
    2x
    6x
    6x
    6x
    4x
    4x

    2x

    28x
    22x
    23x
    23x
    25x

    25x
    25x

    23x

    5x
    5x
    20x
    5x

     /* eslint no-param-reassign: 0 */
    import {
      TS,
      ENTITY_ID,
      EVENT_NAME,
      META,
      FILTERED_EVENTS,
    } from '../constants';

    /*
     * Creates an event with standard shape from a raw record/event object
     */
    export function createEvent(rawEvent, accessors) {
      return {
        [TS]: accessors[TS](rawEvent),
        [EVENT_NAME]: accessors[EVENT_NAME](rawEvent),
        [ENTITY_ID]: accessors[ENTITY_ID](rawEvent),
        [META]: Object.entries(rawEvent).reduce((result, [key, value]) => {
          try { // try to parse json for ease of parsing downstream
            result[key] = JSON.parse(value);
          } catch (e) {
            result[key] = value;
          }
          return result;
        }, {}),
      };
    }

    /*
     * Returns a unique node id for a given (eventId, depth) tuple
     */
    export function getEventUuid(event, index) {
      return `${event[EVENT_NAME]}__${index}__${event[ENTITY_ID]}`;
    }

    /*
     * Event comparator for sorting events
     */
    function eventSortComparator(a, b) {
      return a[TS] - b[TS];
    }

    /*
     * Bins all events in the passed array by entity Id.
     * **note** that this method shallow copies all events
     *
     * @param {Array} array of events
     *
     */
    export function binEventsByEntityId(events, ignoreEventTypes = {}) {
      const entityEvents = {};
      const ignoredEvents = {};

      events.forEach((event) => {
        const type = event[EVENT_NAME];
        const id = event[ENTITY_ID];

        if (!ignoreEventTypes[type]) {
          entityEvents[id] = entityEvents[id] || [];
          entityEvents[id].push({ ...event });
        } else {
          ignoredEvents[id] = event;
        }
      });

      return { entityEvents, ignoredEvents };
    }

    /*
     * Maps raw events to a consistent shape and sorts them by TS
     *
     * Accessors should be an object with the following shape:
     *  {
     *   [TS]: fn(e) => date, the ts of the event
     *   [EVENT_NAME]: fn(e) => name,
     *   [ENTITY_ID]: fn(e) => id,
     *  }
     */
    export function cleanEvents(rawEvents, accessors) {
      return rawEvents
        .map(event => createEvent(event, accessors))
        .sort(eventSortComparator);
    }

    /*
     * Returns the index for the Nth occurence (1-index-based)
     * of elements that passes the passed filter fn. If n is negative, will return
     * the index of the Nth occurence relative to the end of the array
     *
     * @param {Array<any>} array, array to find element within
     * @param {number} n, the 1-index based occurrence of the element of interest
     * @param {fn(element) => bool} filter, the function to test if an element matches
     */
    export function findNthIndexOfX(array, n = 1, filter) {
      if (n < 0) {
        const revIndex = findNthIndexOfX([...array].reverse(), -n, filter);
        return revIndex === -1 ? -1 : array.length - 1 - revIndex;
      }
      let occurrences = 0;
      return array.findIndex((event) => {
        if (filter(event)) {
          occurrences += 1;
          if (occurrences === n) {
            return true;
          }
          return false;
        }
        return false;
      });
    }

    /*
     * Given a node, returns all events from all entities included in the node
     */
    export function collectSequencesFromNode(node, entityEvents) {
      const entitiesSeen = {};
      const sequences = [];

      Eif (node && node.events) {
        Object.keys(node.events).forEach((eventId) => {
          const event = node.events[eventId];
          const entityId = event.ENTITY_ID;
          if (!entitiesSeen[entityId]) {
            sequences.push(entityEvents[entityId]); // push all events from this entity
            entitiesSeen[entityId] = true;
          }
        });
      }

      return sequences;
    }

    function recursivelyCountEvents(nodes, eventCounts = {}) {
      if (!nodes || Object.keys(nodes).length === 0) return;
      Object.entries(nodes).forEach(([nodeName, node]) => {
        Eif (node.events && Object.keys(node.events).length > 0) {
          Object.values(node.events).forEach((event) => {
            const name = nodeName === FILTERED_EVENTS ? FILTERED_EVENTS : event[EVENT_NAME];

            eventCounts[name] = eventCounts[name] || 0;
            eventCounts[name] += nodeName === FILTERED_EVENTS
              ? Object.keys(Object.values(event)).length
              : 1;
          });
          recursivelyCountEvents(node.children, eventCounts);
        }
      });
    }

    /*
     * Recursively traverses the graph from the starting node, counting events by type along the way
     * Returns the following meta data:
     *    countLookup: {Object} EVENT_NAME: count
     *    countArray: {Array<Object>} array of { label, value }
     *    countTotal: {Number}
     *
    }
     */
    export function getEventCountLookup(nodes) {
      const eventCountLookup = {};
      recursivelyCountEvents(nodes, eventCountLookup);
      const eventCountTotal = Object.values(eventCountLookup).reduce((sum, curr) => sum + curr, 0);
      return {
        eventCountTotal,
        eventCountLookup,
      };
    }