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

86.78% Statements 105/121
74.58% Branches 44/59
84.21% Functions 16/19
88.5% Lines 100/113

    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
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310

    75x

    75x

    17x
    17x
    17x
    17x
    17x

    17x
    17x

    17x
    58x
    58x
    58x
    58x

    58x
    58x
    58x
    58x
    58x
    58x
    58x
    58x

    58x
    58x

    58x
    58x
    58x
    58x
    58x

    17x
    17x
    17x
    17x
    17x
    12x
    12x
    12x
    12x

    12x
    12x
    12x
    12x
    12x
    12x
    12x
    12x

    12x
    12x

    12x
    12x
    12x
    12x

    28x

    22x
    23x
    23x
    25x

    23x
    23x

    23x

    26x
    6x
    6x

    6x
    6x
    6x

    5x

    5x
    23x
    23x

    5x

    6x

    5x
    5x
    5x

    5x

    5x
    6x
    6x
    6x
    6x
    6x

    5x
    5x

    5x

    5x

    5x

    5x

    5x
    5x

    5x

    5x

    5x

    20x

    3x
    3x
    3x
    6x
    6x

    3x

     /* eslint no-param-reassign: 1 */
    import { mean as d3Mean } from 'd3-array';

    import {
      binEventsByEntityId,
      getEventUuid,
      getEventCountLookup,
    } from './data-utils';

    import {
      TS,
      TS0,
      TS_PREV,
      TS_NEXT,
      EVENT_NAME,
      EVENT_UUID,
      EVENT_COUNT,
      ELAPSED_MS,
      ELAPSED_MS_ROOT,
      FILTERED_EVENTS,
    } from '../constants';

    /*
     * Returns a unique node id for a given (eventName, depth) tuple
     */
    export function getNodeId(eventName, depth) {
      return `${eventName}__${depth}`;
    }

    /*
     * Returns the node corresponding to the passed event and depth
     * Initializes the node if it doesn't already exist
     */
    export function getNodeFromEvent(allNodes, id, eventName, depth) {
      const node = allNodes[id] || { // lazy init
        id,
        name: eventName,
        parent: null,
        children: {},
        events: {},
        depth,
        [EVENT_COUNT]: 0,
      };

      return node;
    }

    /*
     * Parses all events associcated with a single entity/user, building the graph
     * in the process
     */
    export function buildNodesFromEntityEvents(events, startIndex, nodes) {
      // @todo refactor this to be more DRY
      let depth = 0;
      let index = startIndex;
      let firstNode = null;
      let tempNode = null;
      let currNode = null;
      let event;
      let eventName;
      let eventUuid;
      const ts0 = events[startIndex] && events[startIndex][TS];
      let nodeId = '';

      // traverse events >= starting index
      while (index < events.length) {
        event = events[index];
        eventName = event[EVENT_NAME];
        eventUuid = getEventUuid(event, index);
        nodeId += eventName;

        tempNode = getNodeFromEvent(nodes, nodeId, eventName, depth);
        tempNode.events[eventUuid] = event;
        event[EVENT_UUID] = eventUuid;
        event[TS0] = ts0;
        event[TS_PREV] = (events[index - 1] || {})[TS];
        event[TS_NEXT] = (events[index + 1] || {})[TS];
        event[ELAPSED_MS] = depth === 0 ? 0 : (event[TS] - event[TS_PREV]);
        event[ELAPSED_MS_ROOT] = event[TS] - ts0;

        tempNode.parent = currNode;
        if (currNode) currNode.children[tempNode.id] = tempNode;

        currNode = tempNode;
        firstNode = firstNode || currNode;
        nodes[currNode.id] = currNode;
        depth += 1;
        index += 1;
      }

      // traverse events < starting index
      nodeId = `-${(firstNode && firstNode.id) || ''}`;
      currNode = firstNode;
      index = startIndex - 1;
      depth = -1;
      while (index >= 0) {
        event = events[index];
        eventName = event[EVENT_NAME];
        eventUuid = getEventUuid(event, index);
        nodeId += eventName;

        tempNode = getNodeFromEvent(nodes, nodeId, eventName, depth);
        tempNode.events[eventUuid] = event;
        event[EVENT_UUID] = eventUuid;
        event[TS0] = ts0;
        event[TS_PREV] = (events[index - 1] || {})[TS];
        event[TS_NEXT] = (events[index + 1] || {})[TS];
        event[ELAPSED_MS] = event[TS] - event[TS_NEXT];
        event[ELAPSED_MS_ROOT] = event[TS] - ts0;

        tempNode.parent = currNode;
        Eif (currNode) currNode.children[tempNode.id] = tempNode;

        currNode = tempNode;
        nodes[currNode.id] = currNode;
        depth -= 1;
        index -= 1;
      }
    }

    /*
     * Recursively adds metadata attributes to the passed nodes, recurses on node.children:
     *  EVENT_COUNT
     *  ELAPSED_MS (mean)
     *  ELAPSED_MS_ROOT (mean)
     */
    export function addMetaDataToNodes(nodes, allNodes) {
      if (!nodes || !Object.keys(nodes).length) return;

      Object.keys(nodes).forEach((id) => {
        const node = nodes[id];
        node[EVENT_COUNT] = Object.keys(node.events || {}).length;
        node[ELAPSED_MS] = d3Mean(Object.values(node.events || {}), d => d[ELAPSED_MS]);

        /*
         * if you simply compute the mean of ELAPSED_MS_ROOT across all events,
         * leaf nodes may have am ELAPSED_MS_ROOT that is LESS than the leaf's parent node
         * eg parent node has 3 events:
         *    2 with very long elapsed to root + 1 with a shorter value -> long average
         *    if leaf node only includes the event with a shorter value -> less than parent avg
         *
         * building ELAPSED_MS_ROOT from the sum of ELAPSED_MS prevents this
         */
        Eif (node.parent) {
          node[ELAPSED_MS_ROOT] = node[ELAPSED_MS] + node.parent[ELAPSED_MS_ROOT];
        } else {
          node[ELAPSED_MS_ROOT] = d3Mean(Object.values(node.events || {}), d => d[ELAPSED_MS_ROOT]);
        }

        addMetaDataToNodes(node.children, allNodes); // recurse
      });
    }

    export function getRoot(nodes) {
      const children = Object.keys(nodes).filter(n => nodes[n] && nodes[n].depth === 0);
      const root = {};
      return Object.assign(root, {
        name: 'root',
        id: 'root',
        parent: null,
        depth: NaN,
        events: {},
        ELAPSED_MS_ROOT: 0,
        ELAPSED_MS: 0,
        children: children.reduce((result, currNodeKey) => {
          nodes[currNodeKey].parent = root;
          result[currNodeKey] = nodes[currNodeKey];
          return result;
        }, {}),
      });
    }

    /*
     * Iterates over all nodes and trims the graph of nodes with fewer
     * than the min specified event count.  * Specifically this function deletes all references to
     * such nodes in allNodes + and in node.childrenThis is useful for simplifying the vis + DOM perf.
     */
    export function trimNodesByEventCount({
      allNodes,
      minEventCount = 1,
      metaData = {
        hiddenEvents: {},
        hiddenNodes: {},
      },
    }) {
      Iif (!allNodes || Object.keys(allNodes).length === 0) return metaData;

      Object.entries(allNodes).forEach(([nodeName, node]) => {
        const count = node.events ? Object.keys(node.events).length : 0;
        Iif (count < minEventCount) {
          metaData.hiddenNodes[nodeName] = node;
          Object.assign(metaData.hiddenEvents, node.events);

          delete allNodes[nodeName];
          if (node.parent) delete node.parent.children[nodeName];
        }
      });

      return metaData;
    }

    /*
     * Parses raw events and builds a graph of 'aggregate' nodes.
     * raw events should be
     *    *cleaned* meaning they have ts, entity id, and event name keys
     *    in no particular order, they are binned by entity id and sorted by TS
     */
    export function buildGraph({
      cleanedEvents,
      getStartIndex = () => 0,
      ignoreEventTypes = {},
      minEventCount,
    }) {
      console.time('buildGraph');
      const nodes = {};
      const filteredEvents = {};

      // note this shallow copies
      const { entityEvents, ignoredEvents } = binEventsByEntityId(cleanedEvents, ignoreEventTypes);

      Object.keys(entityEvents).forEach((id) => {
        const events = entityEvents[id];
        const initialEventIndex = getStartIndex(events);
        entityEvents[id].zeroIndex = initialEventIndex;
        Eif (initialEventIndex > -1 && typeof events[initialEventIndex] !== 'undefined') {
          buildNodesFromEntityEvents(events, initialEventIndex, nodes);
        } else {
          // keep a ref to all events so that event type meta data includes all events
          let i = 0;
          filteredEvents[id] = events.reduce((all, curr) => {
            all[i] = curr;
            i += 1;
            return all;
          }, {});
        }
      });

      const root = getRoot(nodes);
      addMetaDataToNodes(root.children, nodes);

      const { hiddenNodes, hiddenEvents } = trimNodesByEventCount({
        allNodes: nodes,
        minEventCount,
      });

      console.log(
        'hidden',
        Object.keys(hiddenNodes).length, 'nodes',
        Object.keys(hiddenEvents).length, 'events',
      );

      // given that a node size respresents an event count, the "filtered" node should
      // contain only one event / the root event for the filtered sequence
      const numFiltered = Object.keys(filteredEvents).length;

      Iif (numFiltered) {
        nodes[FILTERED_EVENTS] = {
          ...getNodeFromEvent(nodes, FILTERED_EVENTS, FILTERED_EVENTS, 0),
          [EVENT_COUNT]: numFiltered,
          [ELAPSED_MS]: 0,
          [ELAPSED_MS_ROOT]: 0,
          events: filteredEvents,
          parent: root,
        };

        root.children[FILTERED_EVENTS] = nodes[FILTERED_EVENTS];
      }

      root[EVENT_COUNT] = Object.keys(root.children)
        .reduce((sum, curr) => sum + nodes[curr][EVENT_COUNT], 0);

      const { eventCountLookup, eventCountTotal } = getEventCountLookup(root.children);

      console.timeEnd('buildGraph');

      return {
        root,
        nodes,
        entityEvents,
        filtered: numFiltered,
        metaData: {
          hiddenNodes,
          hiddenEvents: {
            ...hiddenEvents,
            ...ignoredEvents,
          },
          eventCountLookup,
          eventCountTotal,
          eventCountArray: Object.entries(eventCountLookup)
            .map(([label, value]) => ({
              label,
              value,
            })),
        },
      };
    }

    /*
     * Given a node, returns an array of ancestors from the root to the node
     */
    export function ancestorsFromNode(node) {
      let curr = node;
      const ancestors = [];
      while (curr && curr.id !== 'root') {
        ancestors.push(curr);
        curr = curr.parent;
      }
      return ancestors.reverse();
    }