<commit_before>/**
 * Process list data
 */

import { fromJS, List as list, Map as map } from 'immutable';
import { YMD } from '../../misc/date';
import {
  LIST_COLS_SHORT, LIST_COLS_PAGES
} from '../../misc/const';
import { TransactionsList } from '../../misc/data';
import { formatAge } from '../../misc/format';
import {
  getGainComparisons, addPriceHistory, getFormattedHistory, getXRange
} from './funds';

/**
 * process list page data response
 * @param {Record} reduction: app state
 * @param {integer} pageIndex: page index
 * @param {object} raw: api JSON data
 * @returns {Record} modified reduction
 */
export const processPageDataList = (reduction, pageIndex, raw) => {
  const numRows = raw.data.length;
  const numCols = LIST_COLS_PAGES[pageIndex].length;
  const total = raw.total;

  const data = map({
    numRows,
    numCols,
    total
  });

  const rows = list(raw.data.map(item => {
    return map({
      id: item.I,
      cols: list(LIST_COLS_SHORT[pageIndex].map(col => {
        if (col === 'd') {
          return new YMD(item[col]);
        }
        return item[col];
      }))
    });
  }));

  return reduction.setIn(['appState', 'pages', pageIndex], map({ data, rows }));
};

export const getFundsCachedValue = (reduction, pageIndex, history) => {
  const ageText = formatAge(
    new Date().getTime() / 1000 - history.get('totalTime') - history.get('startTime'));
  const lastItem = history.get('history').last();
  const value = lastItem.get(1).map((price, key) => {
    const transactions = history.getIn(['funds', 'transactions', key]);
    const transactionsList = new TransactionsList(transactions, false, true);
    const units = transactionsList.getTotalUnits();
    return units * price;
  }).reduce((a, b) => a + b, 0);

  return reduction.setIn(['appState', 'other', 'fundsCachedValue'], map({ ageText, value }));
};

export const processPageDataFunds = (reduction, pageIndex, data) => {
  let newReduction = processPageDataList(reduction, pageIndex, data);
  const history = fromJS(data.history);

  const transactionsKey = LIST_COLS_PAGES[pageIndex].indexOf('transactions');
  const rows = getGainComparisons(newReduction.getIn(
    ['appState', 'pages', pageIndex, 'rows']
  ).map(row => {
    const transactionsJson = row.getIn(['cols', transactionsKey]);
    const transactions = new TransactionsList(transactionsJson);<commit_msg>Use different value for fund age<commit_after>/**
 * Process list data
 */

import { fromJS, List as list, Map as map } from 'immutable';
import { YMD } from '../../misc/date';
import {
  LIST_COLS_SHORT, LIST_COLS_PAGES
} from '../../misc/const';
import { TransactionsList } from '../../misc/data';
import { formatAge } from '../../misc/format';
import {
  getGainComparisons, addPriceHistory, getFormattedHistory, getXRange
} from './funds';

/**
 * process list page data response
 * @param {Record} reduction: app state
 * @param {integer} pageIndex: page index
 * @param {object} raw: api JSON data
 * @returns {Record} modified reduction
 */
export const processPageDataList = (reduction, pageIndex, raw) => {
  const numRows = raw.data.length;
  const numCols = LIST_COLS_PAGES[pageIndex].length;
  const total = raw.total;

  const data = map({
    numRows,
    numCols,
    total
  });

  const rows = list(raw.data.map(item => {
    return map({
      id: item.I,
      cols: list(LIST_COLS_SHORT[pageIndex].map(col => {
        if (col === 'd') {
          return new YMD(item[col]);
        }
        return item[col];
      }))
    });
  }));

  return reduction.setIn(['appState', 'pages', pageIndex], map({ data, rows }));
};

export const getFundsCachedValue = (reduction, pageIndex, history) => {
  const lastItem = history.get('history').last();

  const valueTime = history.get('startTime') + lastItem.get(0);
  const ageText = formatAge(new Date().getTime() / 1000 - valueTime);

  const value = lastItem.get(1).map((price, key) => {
    const transactions = history.getIn(['funds', 'transactions', key]);
    const transactionsList = new TransactionsList(transactions, false, true);
    const units = transactionsList.getTotalUnits();
    return units * price;
  }).reduce((a, b) => a + b, 0);

  return reduction.setIn(['appState', 'other', 'fundsCachedValue'], map({ ageText, value }));
};

export const processPageDataFunds = (reduction, pageIndex, data) => {
  let newReduction = processPageDataList(reduction, pageIndex, data);
  const history = fromJS(data.history);

  const transactionsKey = LIST_COLS_PAGES[pageIndex].indexOf('transactions');
  const rows = getGainComparisons(newReduction.getIn(
    ['appState', 'pages', pageIndex, 'rows']
  ).map(row => {
    const transactionsJson = row.getIn(['cols', transactionsKey]);
    const transactions = new TransactionsList(transactionsJson);
{'fraction_non_alphanumeric': 0.15535193669304456, 'fraction_numerical': 0.0027072053311120365, 'mean_word_length': 3.911042944785276, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, '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': '4870720', 'n_tokens_mistral': 1521, 'n_tokens_neox': 1476, 'n_words': 388}