<commit_before>import _ from 'lodash';
import Promise from 'bluebird';
import AppDispatcher from '../dispatchers/app-dispatcher';
import ProductConstants from '../constants/product-constants';
import { products, user } from '../lib/sprintly-client';
import FiltersAction from '../actions/filter-actions';
import {PUSHER_KEY, CHANNEL_PREFIX} from '../config';
import {EventEmitter} from 'events';

let columnCollections = {};

let productVelocity = {};

var ProductStore = module.exports = _.assign({}, EventEmitter.prototype, {
  emitChange() {
    this.emit('change');
  },

  addChangeListener(callback) {
    this.on('change', callback);
  },

  removeChangeListener(callback) {
    this.removeListener('change', callback);
  },

  getAll() {
    let activeProducts = products.where({ archived: false });
    return _.sortBy(_.invoke(activeProducts, 'toJSON'), 'name');
  },

  getProduct(id) {
    let product = products.get(id);
    if (!product) {
      return false;
    }
    return {
      members: product.members.toJSON(),
      product: product.toJSON(),
      tags: product.tags.toJSON(),
      velocity: productVelocity[id] || null
    };
  },

  getItems(productId, status) {
    let items = ProductStore.getItemsCollection(productId, status);
    if (!items) {
      return;
    }

    let itemsJSON = _.compact(_.map(items.sort().toJSON(), function(model) {
      if (model.parent) {
        return;
      } else {
        return model;
      }
    }));

    let [sortField, sortDirection] = ProductStore.getSortCriteria(items);

    return {
      items: itemsJSON,
      limit: items.config.get('limit'),
      offset: items.config.get('offset'),
      sortDirection,
      sortField
    };
  },

  getItemsCollection(productId, status) {
    let collection = columnCollections[`${productId}-${status}`];
    return collection;
  },

  getSortCriteria(collection) {
    let orderBy = collection.config.get('order_by');
    let direction;
    let field = orderBy;

    switch(orderBy) {
      case 'stale':
        field = 'last_modified';
        direction = 'asc';
        break;
      case 'recent':
        field = 'last_modified';
        direction = 'desc';
        break;
      case 'oldest':
        field = 'created_at';
        direction = 'asc';
        break;
      case 'newest':
        field = 'created_at'
        direction = 'desc';
        break;
      case 'priority':
        direction = 'asc';
        break;
      default:
        break;
    }

    return [field, direction]
  },

  refreshColumns() {
    Promise.all(_.map(columnCollections, function(column) {
      column.fetch();
    })).then(function() {
      ProductStore.emitChange();
    });
  }
});


var internals = ProductStore.internals = {
  initProduct(product) {
    product.items.on('change:status', function(model) {
      let status = model.get('status');
      let collection = product.getItemsByStatus(status);
      let config = collection.config.toJSON();
      // Prevent "jumpy" items
      model.set(internals.getUpdatedTimestamps(model, status), { silent: true });

      // let [activeFilterCount, matchingFilters] = internals.matchesFilter(model, config);
      // if (activeFilterCount === 0 || activeFilterCount === matchingFilters.length) {
        // Swap items between status collections.
        let previousStatus = model.previous('status');
        let previousCollection = product.getItemsByStatus(previousStatus);
        if (previousCollection) {
          previousCollection.remove(model);
        }
        if (collection) {
          collection.add(model);
        }<commit_msg>correctly remove items from previous collection<commit_after>import _ from 'lodash';
import Promise from 'bluebird';
import AppDispatcher from '../dispatchers/app-dispatcher';
import ProductConstants from '../constants/product-constants';
import { products, user } from '../lib/sprintly-client';
import FiltersAction from '../actions/filter-actions';
import {PUSHER_KEY, CHANNEL_PREFIX} from '../config';
import {EventEmitter} from 'events';

let columnCollections = {};

let productVelocity = {};

var ProductStore = module.exports = _.assign({}, EventEmitter.prototype, {
  emitChange() {
    this.emit('change');
  },

  addChangeListener(callback) {
    this.on('change', callback);
  },

  removeChangeListener(callback) {
    this.removeListener('change', callback);
  },

  getAll() {
    let activeProducts = products.where({ archived: false });
    return _.sortBy(_.invoke(activeProducts, 'toJSON'), 'name');
  },

  getProduct(id) {
    let product = products.get(id);
    if (!product) {
      return false;
    }
    return {
      members: product.members.toJSON(),
      product: product.toJSON(),
      tags: product.tags.toJSON(),
      velocity: productVelocity[id] || null
    };
  },

  getItems(productId, status) {
    let items = ProductStore.getItemsCollection(productId, status);
    if (!items) {
      return;
    }

    let itemsJSON = _.compact(_.map(items.sort().toJSON(), function(model) {
      if (model.parent) {
        return;
      } else {
        return model;
      }
    }));

    let [sortField, sortDirection] = ProductStore.getSortCriteria(items);

    return {
      items: itemsJSON,
      limit: items.config.get('limit'),
      offset: items.config.get('offset'),
      sortDirection,
      sortField
    };
  },

  getItemsCollection(productId, status) {
    let collection = columnCollections[`${productId}-${status}`];
    return collection;
  },

  getSortCriteria(collection) {
    let orderBy = collection.config.get('order_by');
    let direction;
    let field = orderBy;

    switch(orderBy) {
      case 'stale':
        field = 'last_modified';
        direction = 'asc';
        break;
      case 'recent':
        field = 'last_modified';
        direction = 'desc';
        break;
      case 'oldest':
        field = 'created_at';
        direction = 'asc';
        break;
      case 'newest':
        field = 'created_at'
        direction = 'desc';
        break;
      case 'priority':
        direction = 'asc';
        break;
      default:
        break;
    }

    return [field, direction]
  },

  refreshColumns() {
    Promise.all(_.map(columnCollections, function(column) {
      column.fetch();
    })).then(function() {
      ProductStore.emitChange();
    });
  }
});


var internals = ProductStore.internals = {
  initProduct(product) {
    product.items.on('change:status', function(model) {
      let status = model.get('status');
      let collection = product.getItemsByStatus(status);
      let config = collection.config.toJSON();
      // Prevent "jumpy" items
      model.set(internals.getUpdatedTimestamps(model, status), { silent: true });

      // let [activeFilterCount, matchingFilters] = internals.matchesFilter(model, config);
      // if (activeFilterCount === 0 || activeFilterCount === matchingFilters.length) {
        // Swap items between status collections.
        let previousStatus = model.previous('status');
        let previousCollection = product.getItemsByStatus(previousStatus);
        if (previousCollection) {
          previousCollection.remove(model.id);
        }
        if (collection) {
          collection.add(model);
        }
        ProductStore.emitChange();
{'fraction_non_alphanumeric': 0.13668768851110502, 'fraction_numerical': 0.00027419797093501506, 'mean_word_length': 2.7856772184743126, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '91963', 'n_tokens_mistral': 2087, 'n_tokens_neox': 2055, 'n_words': 446}