Skip to content

Commit

Permalink
Limit number of events used to assess level
Browse files Browse the repository at this point in the history
With large numbers of historical events the application starts to lag
when assessing the users level. This provides a fix that keeps
"good-enough" functionality.

Profiling found that most of the time is in creating new event objects
when we unfold them per key.

Mostly addresses #10
  • Loading branch information
Graham42 committed Apr 15, 2017
1 parent 19cb671 commit 763cc95
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions app/scripts/services/level_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,22 @@ const LevelService = {
// average time of < Y s
// success rate > Z

// only consider the last 1500 events for performance reasons
// 1500 picked for 12 semitones in an octave * 100 success events / 0.8 for 80% success rate
let eventsToAssess = events.slice(-1500);

const thresholdSettings = AppFreezer.get().settings.pitchReading.automaticDifficulty;

optThresholds = optThresholds || {
amount: thresholdSettings.amount,
accuracy: thresholdSettings.accuracyGoal,
time: thresholdSettings.timeGoal
};
const unfoldedEvents = _.flatMap(events, (event) => {

const filteredEvents = eventsToAssess.filter((event) =>
event.keys.some(this.levelContainsKey.bind(this, level))
);
const unfoldedEvents = _.flatMap(filteredEvents, (event) => {
return event.keys.map((key) => {
const subEvent = {
...event,
Expand All @@ -113,19 +121,15 @@ const LevelService = {
return subEvent;
});
});
const filteredEvents = unfoldedEvents.filter((event) =>
this.levelContainsKey(level, event.key)
);

const eventsByKey = _.groupBy(filteredEvents, "key");
const eventsByKey = _.groupBy(unfoldedEvents, "key");
if (_.size(eventsByKey) < this.getNotesOfLevel(level)) {
return false;
}
const evaluation = _.map(eventsByKey, (events, key) => {
const successPartition = _.partition(events, "success")[0];
const successPartition = _.partition(eventsToAssess, "success")[0];

const eventsLength = events.length;
const accuracy = successPartition.length / events.length;
const eventsLength = eventsToAssess.length;
const accuracy = successPartition.length / eventsToAssess.length;
const time = _.sum(successPartition.map((el) => el.time)) / successPartition.length;

const meetsLength = eventsLength >= optThresholds.amount;
Expand Down

0 comments on commit 763cc95

Please sign in to comment.