<commit_before>/**
 * Carries out actions for the stocks list
 */

import { List as list, Map as map } from 'immutable';
import buildMessage from '../messageBuilder';
import {
  EF_STOCKS_LIST_REQUESTED, EF_STOCKS_PRICES_REQUESTED
} from '../constants/effects';
import { STOCK_INDICES, STOCKS_GRAPH_RESOLUTION } from '../misc/config';

export const rLoadStocksPrices = reduction => {
  const symbols = reduction.getIn(['appState', 'other', 'stocksList', 'stocks'])
  .map(item => item.get('code'))
  .concat(
    reduction.getIn(['appState', 'other', 'stocksList', 'indices'])
    .map(item => item.get('code'))
  ).join(',');

  return reduction.set('effects', reduction.get('effects').push(buildMessage(
    EF_STOCKS_PRICES_REQUESTED, symbols
  )));
};

export const rLoadStocksList = reduction => {
  return reduction.set('effects', reduction.get('effects').push(buildMessage(
    EF_STOCKS_LIST_REQUESTED, reduction.getIn(['appState', 'user', 'apiKey'])
  )));
};
export const rHandleStocksListResponse = (reduction, response) => {
  const indices = map(STOCK_INDICES).map((item, code) => {
    return map({
      code,
      name: item,
      gain: 0,
      up: false,
      down: false
    });
  });

  const stocks = map(response.stocks.map(item => {
    return [item[0], map({
      code: item[0],
      name: item[1],
      weight: item[2] / response.total,
      gain: 0,
      up: false,
      down: false
    })];
  }));

  return rLoadStocksPrices(reduction
  .setIn(['appState', 'other', 'stocksList', 'indices'], indices)
  .setIn(['appState', 'other', 'stocksList', 'stocks'], stocks));
};

const updateStock = (item, row, loadedInitial) => {
  const newGain = parseFloat(row.cp);
  const up = loadedInitial && newGain > item.get('gain');
  const down = loadedInitial && newGain < item.get('gain');
  return item.set('gain', newGain).set('up', up).set('down', down);
};
export const rHandleStocksPricesResponse = (reduction, response) => {
  let indices = reduction.getIn(['appState', 'other', 'stocksList', 'indices'])<commit_msg>Handle NaN stock prices<commit_after>/**
 * Carries out actions for the stocks list
 */

import { List as list, Map as map } from 'immutable';
import buildMessage from '../messageBuilder';
import {
  EF_STOCKS_LIST_REQUESTED, EF_STOCKS_PRICES_REQUESTED
} from '../constants/effects';
import { STOCK_INDICES, STOCKS_GRAPH_RESOLUTION } from '../misc/config';

export const rLoadStocksPrices = reduction => {
  const symbols = reduction.getIn(['appState', 'other', 'stocksList', 'stocks'])
  .map(item => item.get('code'))
  .concat(
    reduction.getIn(['appState', 'other', 'stocksList', 'indices'])
    .map(item => item.get('code'))
  ).join(',');

  return reduction.set('effects', reduction.get('effects').push(buildMessage(
    EF_STOCKS_PRICES_REQUESTED, symbols
  )));
};

export const rLoadStocksList = reduction => {
  return reduction.set('effects', reduction.get('effects').push(buildMessage(
    EF_STOCKS_LIST_REQUESTED, reduction.getIn(['appState', 'user', 'apiKey'])
  )));
};
export const rHandleStocksListResponse = (reduction, response) => {
  const indices = map(STOCK_INDICES).map((item, code) => {
    return map({
      code,
      name: item,
      gain: 0,
      up: false,
      down: false
    });
  });

  const stocks = map(response.stocks.map(item => {
    return [item[0], map({
      code: item[0],
      name: item[1],
      weight: item[2] / response.total,
      gain: 0,
      up: false,
      down: false
    })];
  }));

  return rLoadStocksPrices(reduction
  .setIn(['appState', 'other', 'stocksList', 'indices'], indices)
  .setIn(['appState', 'other', 'stocksList', 'stocks'], stocks));
};

const updateStock = (item, row, loadedInitial) => {
  let newGain = parseFloat(row.cp);
  if (isNaN(newGain)) {
    newGain = 0;
  }
  const up = loadedInitial && newGain > item.get('gain');
  const down = loadedInitial && newGain < item.get('gain');
  return item.set('gain', newGain).set('up', up).set('down', down);
};
export const rHandleStocksPricesResponse = (reduction, response) => {
  let indices = reduction.getIn(['appState', 'other', 'stocksList', 'indices'])
{'fraction_non_alphanumeric': 0.17401783562304168, 'fraction_numerical': 0.0031332851289467343, 'mean_word_length': 4.15527950310559, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 23, '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': '356069', 'n_tokens_mistral': 1397, 'n_tokens_neox': 1354, 'n_words': 310}