<commit_before>const moment = require('moment');
const _ = require('lodash');

/**
 * @ngInject
 */
function ReportCtrl($scope, SocketService) {

  //set defaults
  $scope.from = moment().subtract(1, 'days').startOf('day').toDate();
  $scope.to = moment().subtract(1, 'days').endOf('day').toDate();

  $scope.getSummary = () => new Promise((resolve) =>
    SocketService.socket.get(`/heartbeat/summary?where={"createdAt":{"lessThan": "${$scope.to.toISOString()}", "greaterThan": "${$scope.from.toISOString()}"}}`, response => resolve(_.map(response.data, (row) => {

      row.maleInUseMax = row.maleInUse.max;
      row.maleInUseMean = row.maleInUse.mean;
      row.maleInUseMin = row.maleInUse.min;
      delete row.maleInUse;

      row.femaleInUseMax = row.femaleInUse.max;
      row.femaleInUseMean = row.femaleInUse.mean;
      row.femaleInUseMin = row.femaleInUse.min;
      delete row.femaleInUse;

      row.maleOutOfCommissionMax = row.maleOutOfCommission.max;
      row.maleOutOfCommissionMean = row.maleOutOfCommission.mean;
      row.maleOutOfCommissionMin = row.maleOutOfCommission.min;
      delete row.maleOutOfCommission;

      row.femaleOutOfCommissionMax = row.femaleOutOfCommission.max;
      row.femaleOutOfCommissionMean = row.femaleOutOfCommission.mean;
      row.femaleOutOfCommissionMin = row.femaleOutOfCommission.min;
      delete row.femaleOutOfCommission;

      return row;
    })))
  );
  $scope.summaryHeaders = ["centre", "maleInUseMax", "maleInUseMean", "maleInUseMin", "femaleInUseMax", "femaleInUseMean", "femaleInUseMin", "maleOutOfCommissionMax", "maleOutOfCommissionMean", "maleOutOfCommissionMin", "femaleOutOfCommissionMax", "femaleOutOfCommissionMean", "femaleOutOfCommissionMin"];

  $scope.getRaw = () => new Promise((resolve) =>
    SocketService.socket.get(`/heartbeat?limit=${Number.MAX_SAFE_INTEGER}&where={"createdAt":{"lessThan": "${$scope.to.toISOString()}", "greaterThan": "${$scope.from.toISOString()}"}}`, response => resolve(_.map(response.data, (row) => {

      row.timestamp = row.attributes.timestamp;
      row.centre = row.attributes.centre.attributes.name;
      row.maleInUse = row.attributes.maleInUse;
      row.femaleInUse = row.attributes.femaleInUse;
      row.maleOutOfCommission = row.attributes.maleOutOfCommission;
      row.femaleOutOfCommission = row.attributes.femaleOutOfCommission;
      delete row.links;
      delete row.id;
      delete row.attributes;
      return row;
    })))
  );
  $scope.rawHeaders = ["timestamp", "centre", "maleInUse", "femaleInUse", "maleOutOfCommission", "femaleOutOfCommission"];

}

export default {
  name: 'ReportController',
  fn: ReportCtrl
};<commit_msg>fetch and load into the CSV the daily occupancy figures<commit_after>const moment = require('moment');
const _ = require('lodash');

/**
 * @ngInject
 */
function ReportCtrl($scope, SocketService) {
  const socketGet = (url) => new Promise((resolve) => SocketService.socket.get(url, resolve))
  const formatReasons = (gender, row) => _.join(_.flatMap(row[gender] || [], (value, key) => `${key}: ${value}`), ', ')
  //set defaults
  $scope.from = moment().subtract(1, 'days').startOf('day').toDate();
  $scope.to = moment().subtract(1, 'days').endOf('day').toDate();

  $scope.getSummary = () =>
    socketGet(`/heartbeat/summary?where={"createdAt":{"lessThan": "${$scope.to.toISOString()}", "greaterThan": "${$scope.from.toISOString()}"}}`)
      .then(response =>_.map(response.data, (row) => {

        row.maleInUseMax = row.maleInUse.max;
        row.maleInUseMean = row.maleInUse.mean;
        row.maleInUseMin = row.maleInUse.min;
        delete row.maleInUse;

        row.femaleInUseMax = row.femaleInUse.max;
        row.femaleInUseMean = row.femaleInUse.mean;
        row.femaleInUseMin = row.femaleInUse.min;
        delete row.femaleInUse;

        row.maleOutOfCommissionMax = row.maleOutOfCommission.max;
        row.maleOutOfCommissionMean = row.maleOutOfCommission.mean;
        row.maleOutOfCommissionMin = row.maleOutOfCommission.min;
        delete row.maleOutOfCommission;

        row.femaleOutOfCommissionMax = row.femaleOutOfCommission.max;
        row.femaleOutOfCommissionMean = row.femaleOutOfCommission.mean;
        row.femaleOutOfCommissionMin = row.femaleOutOfCommission.min;
        delete row.femaleOutOfCommission;

        return row;
      }))

      .then(rows =>
        socketGet(`/bedevent/summary?where={"timestamp":{"lessThan": "${$scope.to.toISOString()}", "greaterThan": "${$scope.from.toISOString()}"}}`)
          .then(response => {
            _.forEach(response.data, (value, key) => {
              let row = _.findIndex(rows, {centre: key});
              if (row < 0) {
                row = rows.push({
                    centre: key,
                    maleInUseMax: null,
                    maleInUseMean: null,
                    maleInUseMin: null,
                    femaleInUseMax: null,
                    femaleInUseMean: null,
                    femaleInUseMin: null,
                    maleOutOfCommissionMax: null,
                    maleOutOfCommissionMean: null,
                    maleOutOfCommissionMin: null,
                    femaleOutOfCommissionMax: null,
                    femaleOutOfCommissionMean: null,
                    femaleOutOfCommissionMin: null
                  }) - 1;
              }
              rows[row].maleBedReasons = formatReasons('male', value);
              rows[row].femaleBedReasons = formatReasons('female', value);
            })
            return rows;
          })
      );

  $scope.summaryHeaders = ["centre", "maleInUseMax", "maleInUseMean", "maleInUseMin", "femaleInUseMax", "femaleInUseMean", "femaleInUseMin", "maleOutOfCommissionMax", "maleOutOfCommissionMean", "maleOutOfCommissionMin", "femaleOutOfCommissionMax", "femaleOutOfCommissionMean", "femaleOutOfCommissionMin", "maleBedReasons", "femaleBedReasons"];

  $scope.getRaw = () => new Promise((resolve) =>
    SocketService.socket.get(`/heartbeat?limit=${Number.MAX_SAFE_INTEGER}&where={"createdAt":{"lessThan": "${$scope.to.toISOString()}", "greaterThan": "${$scope.from.toISOString()}"}}`, response => resolve(_.map(response.data, (row) => {

      row.timestamp = row.attributes.timestamp;
      row.centre = row.attributes.centre.attributes.name;
      row.maleInUse = row.attributes.maleInUse;
      row.femaleInUse = row.attributes.femaleInUse;
      row.maleOutOfCommission = row.attributes.maleOutOfCommission;
      row.femaleOutOfCommission = row.attributes.femaleOutOfCommission;
      delete row.links;
      delete row.id;
      delete row.attributes;
      return row;
    })))
  );
  $scope.rawHeaders = ["timestamp", "centre", "maleInUse", "femaleInUse", "maleOutOfCommission", "femaleOutOfCommission"];

}

export default {
  name: 'ReportController',
  fn: ReportCtrl
};
{'fraction_non_alphanumeric': 0.14578154700979676, 'fraction_numerical': 0.000877321245796169, 'mean_word_length': 3.3650287172941926, 'pattern_counts': {'":': 15, '<': 4, '<?xml version=': 0, '>': 25, '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': '6860948', 'n_tokens_mistral': 2272, 'n_tokens_neox': 2030, 'n_words': 315}