<commit_before>var db = require('../../server/services/db');
var eventsDal = require('../../server/api/events/dal');
var getPoints = require('../../client/js/components/lib/getPoints');

exports.sumPoints = function (evs) {
  // Return sum of points of given events.
  return evs.reduce(function (acc, ev) {
    return acc + getPoints(ev);
  }, 0);
};

exports.computePoints = function (username, callback) {
  // Compute scenepoints for single user. Scenepoints are computed
  // from events.

  eventsDal.getAllOfUser(username, function (err, evs) {
    if (err) {
      return callback(err);
    }

    var points = exports.sumPoints(evs);
    var typerr;

    // Assert
    if (typeof points !== 'number' || isNaN(points)) {
      typerr = new Error('Invalid scene points sum: ' + points);
      return callback(typerr);
    }

    return callback(null, points);
  });
};

exports.computePointsAndStore = function (username, callback) {
  // Parameters:
  //   username
  //   callback
  //     function (err, points)
  exports.computePoints(username, function (err, points) {
    if (err) {
      return callback(err);
    }

    var coll = db.get().collection('users');

    var q = { name: username };
    var u = {
      $set: {
        points: points,
      },
    };

    coll.updateOne(q, u, function (err2) {
      if (err2) {
        return callback(err2);
      }

      return callback(null, points);
    });
  });
};<commit_msg>Worker computes 7 days and 30 days totals<commit_after>var db = require('../../server/services/db');
var eventsDal = require('../../server/api/events/dal');
var getPoints = require('../../client/js/components/lib/getPoints');

exports.sumPoints = function (evs) {
  // Return sum of points of given events.
  return evs.reduce(function (acc, ev) {
    return acc + getPoints(ev);
  }, 0);
};

exports.computePoints = function (username, callback) {
  // Compute scenepoints for single user. Scenepoints are computed
  // from events.

  eventsDal.getAllOfUser(username, function (err, evs) {
    if (err) {
      return callback(err);
    }

    // Compute UNIX timestamp and ensure order.
    var evsTimeUnix = evs.map(function (ev) {
      return Object.assign({}, ev, {
        timeUnix: new Date(ev.time),
      });
    }).sort(function (eva, evb) {
      return eva.timeUnix - evb.timeUnix;
    });

    // Event time frames
    var d30 = 30 * 24 * 60 * 60 * 1000; // eslint-disable-line no-magic-numbers
    var d7 = 7 * 24 * 60 * 60 * 1000; // eslint-disable-line no-magic-numbers
    var unix30daysAgo = Date.now() - d30;
    var unix7daysAgo = Date.now() - d7;
    var evs30days = evsTimeUnix.filter(function (ev) {
      return ev.timeUnix > unix30daysAgo;
    });
    var evs7days = evsTimeUnix.filter(function (ev) {
      return ev.timeUnix > unix7daysAgo;
    });

    // Point Categories
    var ps = {
      allTime: exports.sumPoints(evs),
      days30: exports.sumPoints(evs30days),
      days7: exports.sumPoints(evs7days),
    };

    var isOk = function (num) {
      return typeof num === 'number' && !isNaN(num);
    };

    // Assert
    var typerr;
    if (!isOk(ps.allTime) || !isOk(ps.days30) || !isOk(ps.days7)) {
      typerr = new Error('Invalid scene points sum: ' + JSON.stringify(ps));
      return callback(typerr);
    }

    return callback(null, ps);
  });
};

exports.computePointsAndStore = function (username, callback) {
  //
  // Parameters:
  //   username
  //   callback
  //     function (err)
  //
  exports.computePoints(username, function (err, pointCategories) {
    if (err) {
      return callback(err);
    }

    var coll = db.get().collection('users');

    var q = { name: username };
    var u = {
      $set: {
        points: pointCategories.allTime,
        points30days: pointCategories.days30,
        points7days: pointCategories.days7,
      },
    };

    coll.updateOne(q, u, function (err2) {
      if (err2) {
        return callback(err2);
      }

      return callback(null);
    });
  });
};
{'fraction_non_alphanumeric': 0.14948839530821062, 'fraction_numerical': 0.0159720489143998, 'mean_word_length': 2.518876207199298, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '2057672', 'n_tokens_mistral': 1368, 'n_tokens_neox': 1298, 'n_words': 315}