<commit_before>'use strict';

const db = require('../models/');
const request = require('superagent');

module.exports = {
  // Full data dump, all states, areas & temps hierarchically
  index (req, res) {
    let stateCount = 0;
    const stateArr = [];
    db.state.findAll().then((states) => {
      states.forEach((state, stateItr) => {
        stateCount = stateCount < stateItr ? stateItr : stateCount;
        stateArr[stateItr] = state.dataValues;
        stateArr[stateItr].areas = [];
        db.area.findAll({
          where: {
            stateId: state.dataValues.id
          }
        }).then((areas) => {
          areas.forEach((area, areaItr) => {
            stateArr[stateItr].areas[areaItr] = area.dataValues;
            stateArr[stateItr].areas[areaItr].temps = [];
            db.temp.findAll({
              where: {
                areaId: area.dataValues.id
              }
            }).then((temps) => {
              temps.forEach((temp, tempItr) => {
                stateArr[stateItr].areas[areaItr].temps[tempItr] = temp.dataValues;
              });
              if (stateCount === stateItr) res.send(stateArr);
            });
          });
        });
      });
    });
  },

  states (req, res) {
    let stateCount = 0;
    const stateArr = [];
    db.state.findAll().then((states) => {
      states.forEach((state, stateItr) => {
        stateCount = stateCount < stateItr ? stateItr : stateCount;
        stateArr[stateItr] = state.dataValues;
        db.area.findAll({
          where: {
            stateId: state.dataValues.id
          }
        }).then((areas) => {
          areas.forEach((area) => {
            db.temp.findAll({
              where: {
                areaId: area.dataValues.id
              },
              attributes: [
                [db.sequelize.fn('AVG', db.sequelize.col('heat')), 'heat'],
                [db.sequelize.fn('AVG', db.sequelize.col('cool')), 'cool']
              ]
            }).then((temps) => {
              stateArr[stateItr].temps = temps;
              if (stateCount === stateItr) res.send(stateArr);
            });<commit_msg>Fleshed out by-state request to include noheat and nocool counts as well, need to restructure all calls to use sequelize methods instead of janky nested calls as they do now<commit_after>'use strict';

const db = require('../models/');
const request = require('superagent');

module.exports = {
  // Full data dump, all states, areas & temps hierarchically
  index (req, res) {
    let stateCount = 0;
    const stateArr = [];
    db.state.findAll().then((states) => {
      states.forEach((state, stateItr) => {
        stateCount = stateCount < stateItr ? stateItr : stateCount;
        stateArr[stateItr] = state.dataValues;
        stateArr[stateItr].areas = [];
        db.area.findAll({
          where: {
            stateId: state.dataValues.id
          }
        }).then((areas) => {
          areas.forEach((area, areaItr) => {
            stateArr[stateItr].areas[areaItr] = area.dataValues;
            stateArr[stateItr].areas[areaItr].temps = [];
            db.temp.findAll({
              where: {
                areaId: area.dataValues.id
              }
            }).then((temps) => {
              temps.forEach((temp, tempItr) => {
                stateArr[stateItr].areas[areaItr].temps[tempItr] = temp.dataValues;
              });
              if (stateCount === stateItr) res.send(stateArr);
            });
          });
        });
      });
    });
  },

  states (req, res) {
    let stateCount = 0;
    const stateArr = [];
    db.state.findAll().then((states) => {
      states.forEach((state, stateItr) => {
        stateCount = stateCount < stateItr ? stateItr : stateCount;
        stateArr[stateItr] = state.dataValues;
        db.area.findAll({
          where: {
            stateId: state.dataValues.id
          }
        }).then((areas) => {
          areas.forEach((area) => {
            db.temp.findAll({
              where: {
                areaId: area.dataValues.id
              }
            }).then((temps) => {
              let heat = 0,
                  cool = 0,
                  noheat = 0,
                  nocool = 0,
                  heatItr = 0,
                  coolItr = 0;
              temps.forEach((temp) => {
                heat += temp.dataValues.heat;
                cool += temp.dataValues.cool;
                if (temp.dataValues.noheat) {
                  ++noheat;
                } else {
                  ++heatItr;
                }
                if (temp.dataValues.nocool) {
                  ++nocool;
                } else {
                  ++coolItr;
                }
              });
              heat = heat / heatItr;
              cool = cool / coolItr;
              stateArr[stateItr].data = {
                heat: heat,
                cool: cool,
                noheat: noheat,
                nocool: nocool
              };
              if (stateCount === stateItr) res.send(stateArr);
            });
{'fraction_non_alphanumeric': 0.13793103448275862, 'fraction_numerical': 0.0019817677368212444, 'mean_word_length': 1.4559610705596107, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 26, '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': '1829640', 'n_tokens_mistral': 1511, 'n_tokens_neox': 1459, 'n_words': 265}