<commit_before>EvtPerJobPerDate = new Mongo.Collection('evtPerJobPerDate');

/*
 * Mongo aggregation is not available in mini-mongo (on client).
 */
if (Meteor.isServer) {
  Meteor.methods({
    compileYearTotal: function (breakdownType, selectedJobs, period) {
      return compileTotal(breakdownType, selectedJobs, period, "P1Y");
    },
    compileMonthTotal: function (breakdownType, selectedJobs, period) {
      return compileTotal(breakdownType, selectedJobs, period, "P1M");
    },
    compileDayTotal: function (breakdownType, selectedJobs, period) {
      return compileTotal(breakdownType, selectedJobs, period, "P1D");
    },
    compileDailyAgg: function(breakdownType, selectedJobs, date, tzOffset) {
      //FIXME 1. date filter (in range date-1y)
      var all;
      var tzOffsetMs = tzOffset * 60 * 1000; //from min to ms
      switch(breakdownType) {
        case "rtBreakdown":
          all = Events.aggregate(
            [
              {$match: {jobId : {$in: selectedJobs}}},
              {$group: {
                  _id: { $dateToString: { format: "%Y-%m-%d", date: { $subtract: [ "$etime", tzOffsetMs ]} } },
                  val: {$avg: "$responseTime"}
                  }
              }
            ]
          );
        break;
        case "errorBreakdown":
          all = Events.aggregate(
            [
              {$match: {jobId : {$in: selectedJobs}}},
              { $project: {
                jobId: "$jobId",
                etime: "$etime",
                prob: {$cond: [{$eq: ['$isProblematic', true]}, 1, 0]},
              }},
              {$group: {
                  _id: { $dateToString: { format: "%Y-%m-%d", date: { $subtract: [ "$etime", tzOffsetMs ]} } },
                  valAll: {$sum: 1},
                  valProb: {$sum: "$prob"}
                  }
              }
            ]
          );
        break;
      }

      // [ { _id: "yyyy-mm-dd",
      //   avg: 258.6075581395349 },
      //   ...]
      //   to
      //   {
      //    "2014-04-29": 123.7,
      //    "2014-04-30: 153.4,
      //    ...
      //    }
      var dat = {};
      all.forEach(function(elem){
        var date = elem._id;
        dat[date] = breakdownType === "rtBreakdown"?elem.val:(elem.valProb/elem.valAll)*100;
      });

      return dat;
    }

  });
}

function compileTotal(breakdownType, selectedJobs, period, range) {
  var indicator;
  var periodEnd = period;
  var periodStart;
  var total;

  if (!selectedJobs || selectedJobs.length === 0) {
    return 0;
  }

  switch(range) {
    default:
    case "P1Y":
      periodStart = new Date(periodEnd.getFullYear() -1, periodEnd.getMonth(), periodEnd.getDate());
      break;
    case "P1M":
      periodStart = new Date(periodEnd.getFullYear(), periodEnd.getMonth()-1, periodEnd.getDate());
      break;
    case "P1D":
      periodStart = new Date(periodEnd.getFullYear(), periodEnd.getMonth(), periodEnd.getDate(), 0, 0, 0, 0);
      break;
  }

  switch(breakdownType) {
    case "rtBreakdown":
      var agg = Events.aggregate(
        [
          {
            $match: {
              jobId : {$in: selectedJobs},
              etime: {
                $gte: periodStart
              }
            }
          },
          {$group: {
              _id:  0,
              avgRT: {$avg: "$responseTime"},
              count: {$sum: 1}
            }
          }
        ]
      );
      indicator = 0 < agg.length ? agg[0].avgRT : null;
      total = 0 < agg.length ? agg[0].count : null;
      break;
    case "errorBreakdown":
      indicator = Events.find({
        jobId : {$in: selectedJobs},
        isProblematic: true,
        etime: {
          $gte: periodStart
        }
      }).count();
      total = Events.find({
        jobId: {$in: selectedJobs},
        etime: {
          $gte: periodStart
        }
      }).count();
      break;
  }

  return {val: indicator, total: total};
}
<commit_msg>Set time restriction for compileDailyAgg<commit_after>EvtPerJobPerDate = new Mongo.Collection('evtPerJobPerDate');

/*
 * Mongo aggregation is not available in mini-mongo (on client).
 */
if (Meteor.isServer) {
  Meteor.methods({
    compileYearTotal: function (breakdownType, selectedJobs, period) {
      return compileTotal(breakdownType, selectedJobs, period, "P1Y");
    },
    compileMonthTotal: function (breakdownType, selectedJobs, period) {
      return compileTotal(breakdownType, selectedJobs, period, "P1M");
    },
    compileDayTotal: function (breakdownType, selectedJobs, period) {
      return compileTotal(breakdownType, selectedJobs, period, "P1D");
    },
    compileDailyAgg: function(breakdownType, selectedJobs, date, tzOffset) {
      var all;
      var tzOffsetMs = tzOffset * 60 * 1000; //from min to ms
      var periodEnd = date;
      var periodStart = new Date(periodEnd.getFullYear() -1, periodEnd.getMonth(), periodEnd.getDate());
      switch(breakdownType) {
        case "rtBreakdown":
          all = Events.aggregate(
            [
              {$match: {jobId : {$in: selectedJobs}, etime: {$gte: periodStart}}},
              {$group: {
                  _id: { $dateToString: { format: "%Y-%m-%d", date: { $subtract: [ "$etime", tzOffsetMs ]} } },
                  val: {$avg: "$responseTime"}
                }
              }
            ]
          );
        break;
        case "errorBreakdown":
          all = Events.aggregate(
            [
              {$match: {jobId : {$in: selectedJobs}, etime: {$gte: periodStart}}},
              { $project: {
                jobId: "$jobId",
                etime: "$etime",
                prob: {$cond: [{$eq: ['$isProblematic', true]}, 1, 0]},
              }},
              {$group: {
                  _id: { $dateToString: { format: "%Y-%m-%d", date: { $subtract: [ "$etime", tzOffsetMs ]} } },
                  valAll: {$sum: 1},
                  valProb: {$sum: "$prob"}
                  }
              }
            ]
          );
        break;
      }

      // [ { _id: "yyyy-mm-dd",
      //   avg: 258.6075581395349 },
      //   ...]
      //   to
      //   {
      //    "2014-04-29": 123.7,
      //    "2014-04-30: 153.4,
      //    ...
      //    }
      var dat = {};
      all.forEach(function(elem){
        var date = elem._id;
        dat[date] = breakdownType === "rtBreakdown"?elem.val:(elem.valProb/elem.valAll)*100;
      });

      return dat;
    }

  });
}

function compileTotal(breakdownType, selectedJobs, period, range) {
  var indicator;
  var periodEnd = period;
  var periodStart;
  var total;

  if (!selectedJobs || selectedJobs.length === 0) {
    return 0;
  }

  switch(range) {
    default:
    case "P1Y":
      periodStart = new Date(periodEnd.getFullYear() -1, periodEnd.getMonth(), periodEnd.getDate());
      break;
    case "P1M":
      periodStart = new Date(periodEnd.getFullYear(), periodEnd.getMonth()-1, periodEnd.getDate());
      break;
    case "P1D":
      periodStart = new Date(periodEnd.getFullYear(), periodEnd.getMonth(), periodEnd.getDate(), 0, 0, 0, 0);
      break;
  }

  switch(breakdownType) {
    case "rtBreakdown":
      var agg = Events.aggregate(
        [
          {
            $match: {
              jobId : {$in: selectedJobs},
              etime: {
                $gte: periodStart
              }
            }
          },
          {$group: {
              _id:  0,
              avgRT: {$avg: "$responseTime"},
              count: {$sum: 1}
            }
          }
        ]
      );
      indicator = 0 < agg.length ? agg[0].avgRT : null;
      total = 0 < agg.length ? agg[0].count : null;
      break;
    case "errorBreakdown":
      indicator = Events.find({
        jobId : {$in: selectedJobs},
        isProblematic: true,
        etime: {
          $gte: periodStart
        }
      }).count();
      total = Events.find({
        jobId: {$in: selectedJobs},
        etime: {
          $gte: periodStart
        }
      }).count();
      break;
  }

  return {val: indicator, total: total};
}

{'fraction_non_alphanumeric': 0.14784343056594365, 'fraction_numerical': 0.01832460732984293, 'mean_word_length': 1.6832775919732441, 'pattern_counts': {'":': 16, '<': 7, '<?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': '3203684', 'n_tokens_mistral': 2640, 'n_tokens_neox': 2442, 'n_words': 529}