lundi 6 juillet 2015

Optimize a hasMany search for moment isBetween dates

I have a route model which hasMany departuredates. In the view the user can enter a startDate and an endDate. With that information all routes which have a departuredate between those dates are displayed. The problem is the amount of data. There are more than 5,000 routes with each up to 20 departuredates. Which means that my current implementation (see below routesFilteredByDate) is slow.

Because all departuredate.values are incremented (e.g. ID 1 is the 2015-01-01, ID 2 is the 2015-01-02, ID 3 is the 2015-01-03) I wondered if it would be possible to search for the departuredate.value by the startDate and endDate and than search all route.departuredates for those IDs (e.g. from ID 250 to 264).

Questions:

  • Is that possible? How?
  • Does it make sense performance wise?
  • Are there more performant ways of solving this problem on the client?

app/route/model.js

import DS from 'ember-data';

export default DS.Model.extend({
  name: DS.attr('string'),
  departuredates: DS.hasMany('departuredate', { async: true }),
});

app/departuredate/model.js

import DS from 'ember-data';

export default DS.Model.extend({
  value: DS.attr('date')
});

controller.js

routesFilteredByDate: Ember.computed('startDate','endDate', \
      'model.routes.@each.departuredates.@each.value', function() {
  var startDate = this.get('startDate');
  var endDate = this.get('endDate');
  var routes = this.get('model.routes');

  var searchStartDate = moment(startDate, "DD.MM.YYYY");
  var searchEndDate = moment(endDate, "DD.MM.YYYY");

  routes = routes.filter(
    route =>
    route.get('departuredates').find(
      departuredate => moment(departuredate.get('value')). \
                       isBetween(searchStartDate, searchEndDate)
    )
  );

  return routes;
}),




Aucun commentaire:

Enregistrer un commentaire