all files / src/ logger.js

59.7% Statements 40/67
52.94% Branches 9/17
53.33% Functions 8/15
61.9% Lines 39/63
1 branch Ignored     
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       12× 12× 24×   24×       12×                                                                                                         14×       12× 12× 12× 12× 12× 12× 12× 12× 12× 12×   12×     12×     12× 12×   12×   12×              
import $ from 'jquery';
 
export const LOG_ACTIONS_PAGE_LOAD = 'page_load_perf';
export const LOG_ACTIONS_LOAD_EVENT = 'load_events';
export const LOG_ACTIONS_RENDER_EVENT = 'render_events';
 
const handlers = {};
 
export const Logger = {
  start(log) {
    log.setAttribute('startAt', new Date().getTime() - this.getTimestamp());
    log.eventNames.forEach((eventName) => {
      if (!handlers[eventName]) {
        handlers[eventName] = [];
      }
      handlers[eventName].push(log.addEvent.bind(log));
    });
  },
 
  append(eventName, eventBody) {
    return handlers[eventName].length &&
      handlers[eventName].forEach(handler => (handler(eventName, eventBody)));
  },
 
  end(log) {
    log.setAttribute('duration', new Date().getTime() - log.startAt);
    this.send(log);
 
    log.eventNames.forEach((eventName) => {
      if (handlers[eventName].length) {
        const index = handlers[eventName]
          .findIndex(handler => (handler === log.addEvent));
        handlers[eventName].splice(index, 1);
      }
    });
  },
 
  send(log) {
    const { impressionId, actionType, source, sourceId, events, startAt, duration } = log;
    const requestPrams = [];
    requestPrams.push(['impression_id', impressionId]);
    switch (source) {
      case 'dashboard':
        requestPrams.push(['dashboard_id', sourceId]);
        break;
      case 'slice':
        requestPrams.push(['slice_id', sourceId]);
        break;
      default:
        break;
    }
    let url = '/superset/log/';
    if (requestPrams.length) {
      url += '?' + requestPrams.map(([k, v]) => (k + '=' + v)).join('&');
    }
    const eventData = {};
    for (const eventName in events) {
      eventData[eventName] = [];
      events[eventName].forEach((event) => {
        eventData[eventName].push(event);
      });
    }
 
    $.ajax({
      url,
      method: 'POST',
      dataType: 'json',
      data: {
        source: 'client',
        type: actionType,
        started_time: startAt,
        duration,
        events: JSON.stringify(eventData),
      },
    });
  },
 
  getTimestamp() {
    return Math.round(window.performance.now());
  },
};
 
export class ActionLog {
  constructor({ impressionId, actionType, source, sourceId, eventNames, sendNow }) {
    this.impressionId = impressionId;
    this.source = source;
    this.sourceId = sourceId;
    this.actionType = actionType;
    this.eventNames = eventNames;
    this.sendNow = sendNow || false;
    this.startAt = 0;
    this.duration = 0;
    this.events = {};
 
    this.addEvent = this.addEvent.bind(this);
  }
 
  setAttribute(name, value) {
    this[name] = value;
  }
 
  addEvent(eventName, eventBody) {
    if (E!this.events[eventName]) {
      this.events[eventName] = [];
    }
    this.events[eventName].push(eventBody);
 
    if (Ithis.sendNow) {
      this.setAttribute('duration', new Date().getTime() - this.startAt);
      Logger.send(this);
      this.events = {};
    }
  }
}