<commit_before>/* See license.txt for terms of usage */

define(function(require, exports/*, module*/) {

"use strict";

// WebSockets Monitor
const { types } = require("../actions/frames");
const { types: selectionTypes } = require("../actions/selection");

/**
 * Initial state definition
 */
function getInitialState() {
  return {
    frames: [],
    selection: null,
    filter: {
      text: "",
      frames: null
    },
    summary: {
      totalSize: 0,
      startTime: 0,
      endTime: 0,
      frameCount: 0
    }
  };
}

function frames(state = getInitialState(), action) {
  switch (action.type) {
  case types.ADD_FRAME:
    return addFrames(state, [action.frame]);

  case types.ADD_FRAMES:
    return addFrames(state, action.frames);

  case types.FILTER_FRAMES:
    return filterFrames(state, action.filter);

  case types.CLEAR:
    return clear(state);

  case selectionTypes.SELECT_FRAME:
    return selectFrame(state, action.frame);

  case selectionTypes.SELECT_NEXT_FRAME:
    return selectNextFrame(state);

  case selectionTypes.SELECT_PREV_FRAME:
    return selectPrevFrame(state);

  default:
    return state;
  }
}

// Helpers

function addFrames(state, newFrames) {
  const maxEntries = Options.get("max_entries");

  var frames = [...state.frames, ...newFrames];

  if (frames.length > maxEntries) {
    frames.splice(0, frames.length - maxEntries);
  }

  var { totalSize, frameCount, startTime, endTime } = state.summary;

  // Update summary info
  newFrames.forEach(frame => {
    var data = frame.data;
    if (data) {
      totalSize += data.payload ? data.payload.length : 0;
      startTime = startTime ? startTime : data.timeStamp;
      endTime = data.timeStamp;
    }

    if (frame.type == "frame") {
      frameCount++;
    }
  });

  // Return new state
  var newState = Object.assign({}, state, {
    frames: frames,
    summary: {
      totalSize: totalSize,
      startTime: startTime,
      endTime: endTime,
      frameCount: frameCount
    }
  });

  // Apply filter on incoming frames.
  return filterFrames(newState, newState.filter);
}

function filterFrames(state, filter) {
  var { frames } = state;
  var summary = null;

  if (filter.text) {
    summary = {
      totalSize: 0,
      startTime: 0,
      endTime: 0,
      frameCount: 0
    };

    frames = frames.filter(frame => {
      var data = frame.data;
      if (data.payload && data.payload.indexOf(filter.text) != -1) {
        summary.totalSize += data.payload.length;
        summary.startTime = summary.startTime ? summary.startTime : data.timeStamp;
        summary.endTime = data.timeStamp;
        summary.frameCount++;
        return true;
      }
    });
  }

  if (filter.webSocketSerialID) {
    summary = {
      totalSize: 0,
      startTime: 0,
      endTime: 0,
      frameCount: 0
    };

    frames = frames.filter(frame => {
      var data = frame.data;
      if (frame.webSocketSerialID === filter.webSocketSerialID) {
        summary.totalSize += data.payload.length;
        summary.startTime = summary.startTime ? summary.startTime : data.timeStamp;
        summary.endTime = data.timeStamp;
        summary.frameCount++;
        return true;
      }
    });
  }

  return Object.assign({}, state, {
    filter: {
      text: filter.text,
      webSocketSerialID: filter.webSocketSerialID,
      frames: frames,
      summary: summary,
    }
  });
}

function clear(state) {
  // All data are cleared except of the current filter.
  var newState = getInitialState();
  newState.filter.text = state.filter.text;
  newState.filter.webSocketSerialID = state.filter.webSocketSerialID;
  return newState;
}

// Selection

function selectFrame(state, frame) {
  return Object.assign({}, state, {
    selection: frame
  });
}

function selectNextFrame(state) {
  if (!state.frames.length) {
    return state;
  }<commit_msg>Tweaks to reducers<commit_after>/* See license.txt for terms of usage */

define(function(require, exports/*, module*/) {

"use strict";

// WebSockets Monitor
const { types } = require("../actions/frames");
const { types: selectionTypes } = require("../actions/selection");

/**
 * Initial state definition
 */
function getInitialState() {
  return {
    frames: [],
    selection: null,
    filter: {
      text: "",
      frames: null
    },
    summary: {
      totalSize: 0,
      startTime: 0,
      endTime: 0,
      frameCount: 0
    }
  };
}

function frames(state = getInitialState(), action) {
  switch (action.type) {
  case types.ADD_FRAME:
    return addFrames(state, [action.frame]);

  case types.ADD_FRAMES:
    return addFrames(state, action.frames);

  case types.FILTER_FRAMES:
    return filterFrames(state, action.filter);

  case types.CLEAR:
    return clear(state);

  case selectionTypes.SELECT_FRAME:
    return selectFrame(state, action.frame);

  case selectionTypes.SELECT_NEXT_FRAME:
    return selectNextFrame(state);

  case selectionTypes.SELECT_PREV_FRAME:
    return selectPrevFrame(state);

  default:
    return state;
  }
}

// Helpers

function addFrames(state, newFrames) {
  const maxEntries = Options.get("max_entries");

  var frames = [...state.frames, ...newFrames];

  if (frames.length > maxEntries) {
    frames.splice(0, frames.length - maxEntries);
  }

  var { totalSize, frameCount, startTime, endTime } = state.summary;

  // Update summary info
  newFrames.forEach(frame => {
    var data = frame.data;
    if (data) {
      totalSize += data.payload ? data.payload.length : 0;
      startTime = startTime ? startTime : data.timeStamp;
      endTime = data.timeStamp;
    }

    if (frame.type == "frame") {
      frameCount++;
    }
  });

  // Return new state
  var newState = Object.assign({}, state, {
    frames: frames,
    summary: {
      totalSize: totalSize,
      startTime: startTime,
      endTime: endTime,
      frameCount: frameCount
    }
  });

  // Apply filter on incoming frames.
  return filterFrames(newState, newState.filter);
}

function filterFrames(state, filter) {
  var { frames } = state;
  var summary = null;

  if (filter.text) {
    summary = {
      totalSize: 0,
      startTime: 0,
      endTime: 0,
      frameCount: 0
    };

    frames = frames.filter(frame => {
      var data = frame.data;

      // Exclude where data is null (events). Events have no payload
      if (data && data.payload && data.payload.indexOf(filter.text) != -1) {
        summary.totalSize += data.payload.length;
        summary.startTime = summary.startTime ? summary.startTime : data.timeStamp;
        summary.endTime = data.timeStamp;
        summary.frameCount++;
        return true;
      }
    });
  }

  if (filter.webSocketSerialID) {
    summary = {
      totalSize: 0,
      startTime: 0,
      endTime: 0,
      frameCount: 0
    };

    frames = frames.filter(frame => {
      var data = frame.data;
      if (frame.webSocketSerialID === filter.webSocketSerialID) {

        // If data is null, this is not an actual frame, but an event
        // like "connect" or "disconnect". We still want to keep it
        // in the list, though.
        if (data) {
          summary.totalSize += data.payload.length;
          summary.startTime = summary.startTime ? summary.startTime : data.timeStamp;
          summary.endTime = data.timeStamp;
          summary.frameCount++;
        }
        return true;
      }
    });
  }

  return Object.assign({}, state, {
    filter: {
      text: filter.text,
      webSocketSerialID: filter.webSocketSerialID,
      frames: frames,
      summary: summary,
    }
  });
}

function clear(state) {
  // All data are cleared except of the current text filter.
  var newState = getInitialState();
  newState.filter.text = state.filter.text;
  return newState;
}

// Selection

function selectFrame(state, frame) {
  return Object.assign({}, state, {
    selection: frame
  });
}

function selectNextFrame(state) {
  if (!state.frames.length) {
    return state;
  }
{'fraction_non_alphanumeric': 0.12438625204582651, 'fraction_numerical': 0.003776910487221453, 'mean_word_length': 2.6356979405034324, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3706961', 'n_tokens_mistral': 2501, 'n_tokens_neox': 2417, 'n_words': 587}