<commit_before>const fs = require('fs');
const async = require('async');
const moment = require('moment-timezone');
const api = require('../lib/football-data-api')();

module.exports = function(server) {

  var Game = server.model('Game')
    , utils = server.utils
    , _ = utils._
    ;

  /**
   * Changed games.
   */
  var changedGames = function(games) {
    // Transform the scores in Integer.
    games = _.map(games, function(g) {
      g.score[0] = parseInt(g.score[0]);
      g.score[1] = parseInt(g.score[1]);
      return g;
    });

    // Filter the played games.
    games = _.filter(games, function(g) { return !_.isNaN(parseInt(g.score[0])) });

    return games;
  }

  /**
   * Update all scores.
   */
  var updateScores = function(games, callback) {
    games = changedGames(games);
    if (games.length === 0) return callback(null, 0);

    var nbUpdated = 0;
    async.each(games, function(item, cb) {
      Game.updateScore(item, function(e, isUpdated) {
        if (isUpdated) nbUpdated += 1;
        cb(e);
      });
    }, function(e) {
      callback(e, nbUpdated);
    });
  }

  /**
   * Broadcast the games.
   */
  var broadcastGames = function(date) {
    Game.findByDate(date, function(e, games) {
      if (e) console.error('Error getting the games : ', e);
      else
        server.io.sockets.emit('games:update', games);
    });
  }

  /**
   * Broadcast the bets results.
   */
  var broadcastBets = function(date) {
    Game.getBetsPoints(date, function(e, bets) {
      if (e) console.error('Error getting the bets : ', e);
      else {
        server.io.sockets.emit('bets:update', bets);
      }
    });
  }

  /**
   * Listen to maybe-new scores.
   */
  server.redisSub.on("message", (channel, message) => {
    let results = utils.parseJSON(message);
    if (!Array.isArray(results)) {
      console.error("Results from Redis is not an array:", results);
      return
    }
    updateScores(results, function(e, nbUpdated) {
      console.log('Score update:', new Date(), nbUpdated);
      if (e) console.error('Error updating the scores : ', e);
      if (!e && nbUpdated > 0) {
        broadcastGames(date);
        broadcastBets(date);
      }<commit_msg>ingest one score at a time<commit_after>const fs = require('fs');
const async = require('async');
const moment = require('moment-timezone');
const api = require('../lib/football-data-api')();

module.exports = function(server) {

  var Game = server.model('Game')
    , utils = server.utils
    , _ = utils._
    ;

  /**
   * Changed games.
   */
  var changedGames = function(games) {
    // Transform the scores in Integer.
    games = _.map(games, function(g) {
      g.score[0] = parseInt(g.score[0]);
      g.score[1] = parseInt(g.score[1]);
      return g;
    });

    // Filter the played games.
    games = _.filter(games, function(g) { return !_.isNaN(parseInt(g.score[0])) });

    return games;
  }

  /**
   * Update all scores.
   */
  var updateScores = function(games, callback) {
    games = changedGames(games);
    if (games.length === 0) return callback(null, 0);

    var nbUpdated = 0;
    async.each(games, function(item, cb) {
      Game.updateScore(item, function(e, isUpdated) {
        if (isUpdated) nbUpdated += 1;
        cb(e);
      });
    }, function(e) {
      callback(e, nbUpdated);
    });
  }

  /**
   * Broadcast the games.
   */
  var broadcastGames = function(date) {
    Game.findByDate(date, function(e, games) {
      if (e) console.error('Error getting the games : ', e);
      else
        server.io.sockets.emit('games:update', games);
    });
  }

  /**
   * Broadcast the bets results.
   */
  var broadcastBets = function(date) {
    Game.getBetsPoints(date, function(e, bets) {
      if (e) console.error('Error getting the bets : ', e);
      else {
        server.io.sockets.emit('bets:update', bets);
      }
    });
  }

  /**
   * Listen to maybe-new scores.
   */
  server.redisSub.on("message", (channel, message) => {
    let res = utils.parseJSON(message);
    updateScores([res], function(e, nbUpdated) {
      console.log('Score update:', new Date(), nbUpdated);
      if (e) console.error('Error updating the scores : ', e);
      if (!e && nbUpdated > 0) {
        broadcastGames(date);
        broadcastBets(date);
      }
{'fraction_non_alphanumeric': 0.16294227188081936, 'fraction_numerical': 0.004655493482309125, 'mean_word_length': 2.56597510373444, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '1843758', 'n_tokens_mistral': 1457, 'n_tokens_neox': 1412, 'n_words': 320}