<commit_before>var express = require('express'),
	rp = require('request-promise'),
	firebase = require("firebase"),
	PythonShell = require('python-shell'),
	json2csv = require('json2csv'),
	fs = require('fs'),
	path = require('path'),
	secrets = require("./secrets");

firebase.initializeApp(secrets.fbconfig);
var database = firebase.database();

module.exports = {
	home: function(req, res) {
		console.log("hey");
		res.sendFile(path.join(__dirname + '/index.html'));
	},

	//Checks if there are 7 days of data, if so look for suggestions and report them
	suggestions: function(req, res) {
		console.log('sstart')
		var numPoints = 0;
		var userDB = database.ref('users').orderByChild('uid').equalTo(req.params.uid).ref;
		userDB.child('dreamLogs').once('value').then(function(snapshot) {
			numPoints = snapshot.numChildren();
			if(numPoints >= 7) {
				//Gather data for csv
				var data = {
					'index': [],
					'heat': [],
					'light': [],
					'humidity': [],
					'quality': []
				};
				var userDB = database.ref('users').orderByChild('uid').equalTo(req.params.uid).ref;
				userDB.child('dreamLogs').once('value').then(function(snapshot) { //get parent
					var tempCounter = 0;
					snapshot.forEach(function(childSnapshot) { //loop through children
						data.index.push(1+tempCounter);
						data.heat.push(childSnapshot.val().roomData.tempAvg);
						data.light.push(childSnapshot.val().roomData.lightAvg);
						data.humidity.push(childSnapshot.val().roomData.humidAvg);
						data.humidity.push(childSnapshot.val().sleepQuality);
						tempCounter++;
					});
					console.log('sdatadone');

					//Make csv
					var csv = json2csv({ data: data, fields: ['index', 'heat', 'light', 'humidity', 'quality'] });
					fs.writeFile('suggestionData.csv', csv, function(err) {
					  if(err) {
							res.status(500).send(err);
							throw err; //do we need a return here somewhere?
						}
					  console.log('file saved');

						//Launch python script
						var pyoptions = {
							mode: 'json',
						}
						PythonShell.run('linear_regression_engine.py', pyoptions, function(err, results) {
			  			if(err) {
								res.status(500).send(err);
								throw err; //do we need a return here somewhere?
							}
						  // results is an array consisting of messages collected during execution
							return status(200).send(results);
						}); //pyshell
					}); //writefile
				}); //parent
			}
		}); //firstchild
		res.status(204).send({});
	},

	//Returns the current day's stats or else empty
	dayStats: function(req, res) {
		var currDate = new Date().toISOString().slice(0,10);
		var userDB = database.ref('users').orderByChild('uid').equalTo(req.params.uid).ref;
		userDB.child('dreamLogs').limitToLast(1).once('value').then(function(snapshot) {
			var dreamLogObj = Object.keys(snapshot.val())[0];
			if(currDate == snapshot.val()[dreamLogObj].date) {
				res.status(200).send(snapshot.val()[dreamLogObj]);
			} else {
				res.status(204).send({});
			}
		});
	},

	pastDayStats: function(req, res) {
		var userDB = database.ref('users').orderByChild('uid').equalTo(req.params.uid).ref;
		userDB.child('dreamLogs').limitToLast(parseInt(req.params.ago)+1).once('value').then(function(snapshot) { //get parent
			snapshot.forEach(function(childSnapshot) { //loop through children
				//Calulate difference in days
				var currDate = new Date(new Date().toISOString().slice(0,10));
				var logDate = new Date(childSnapshot.val().date);
				var utc1 = Date.UTC(logDate.getFullYear(), logDate.getMonth(), logDate.getDate());
				var utc2 = Date.UTC(currDate.getFullYear(), currDate.getMonth(), currDate.getDate());
				var diffDays = Math.floor((utc2 - utc1) / 1000 / 60 / 60 / 24);
				//add to array if within last 7, including today
				if(diffDays == req.params.ago) {
					return res.status(200).send(childSnapshot.val());
				}
			});
			res.status(204).send({});
		});
	},

	//Returns array of 7 objects from most recent to oldest day 7; empty if no data for that day
	weekStats: function(req, res) {
		var ret = [{}, {}, {}, {}, {}, {}, {}];
		var userDB = database.ref('users').orderByChild('uid').equalTo(req.params.uid).ref;
		userDB.child('dreamLogs').limitToLast(7).once('value').then(function(snapshot) { //get parent
			snapshot.forEach(function(childSnapshot) { //loop through children
				//Calulate difference in days
				var currDate = new Date(new Date().toISOString().slice(0,10));
				var logDate = new Date(childSnapshot.val().date);
				var utc1 = Date.UTC(logDate.getFullYear(), logDate.getMonth(), logDate.getDate());
				var utc2 = Date.UTC(currDate.getFullYear(), currDate.getMonth(), currDate.getDate());
				var diffDays = Math.floor((utc2 - utc1) / 1000 / 60 / 60 / 24);
				//add to array if within last 7, including today
				if(diffDays < 7) {
					ret[diffDays] = childSnapshot.val();
				}
			});
			res.status(200).send(ret);<commit_msg>needed with last commit<commit_after>var express = require('express'),
	rp = require('request-promise'),
	firebase = require("firebase"),
	PythonShell = require('python-shell'),
	json2csv = require('json2csv'),
	fs = require('fs'),
	path = require('path'),
	secrets = require("./secrets");

firebase.initializeApp(secrets.fbconfig);
var database = firebase.database();

module.exports = {
	home: function(req, res) {
		console.log("hey");
		res.sendFile(path.join(__dirname + '/index.html'));
	},

	//Checks if there are 7 days of data, if so look for suggestions and report them
	suggestions: function(req, res) {
		console.log('sstart')
		var numPoints = 0;
		var userDB = database.ref('users').orderByChild('uid').equalTo(req.params.uid).ref;
		userDB.child('dreamLogs').once('value').then(function(snapshot) {
			numPoints = snapshot.numChildren();
			if(numPoints >= 7) {
				//Gather data for csv
				var data = {
					'index': [],
					'heat': [],
					'light': [],
					'humidity': [],
					'quality': []
				};
				var userDB = database.ref('users').orderByChild('uid').equalTo(req.params.uid).ref;
				userDB.child('dreamLogs').once('value').then(function(snapshot) { //get parent
					var tempCounter = 0;
					snapshot.forEach(function(childSnapshot) { //loop through children
						data.index.push(1+tempCounter);
						data.heat.push(childSnapshot.val().roomData.tempAvg);
						data.light.push(childSnapshot.val().roomData.lightAvg);
						data.humidity.push(childSnapshot.val().roomData.humidAvg);
						data.humidity.push(childSnapshot.val().sleepQuality);
						tempCounter++;
					});
					console.log('sdatadone');

					//Make csv
					var csv = json2csv({ data: data, fields: ['index', 'heat', 'light', 'humidity', 'quality'] });
					fs.writeFile('suggestionData.csv', csv, function(err) {
					  if(err) {
							res.status(500).send(err);
							throw err; //do we need a return here somewhere?
						}
					  console.log('file saved');

						//Launch python script
						var pyoptions = {
							mode: 'json',
						}
						PythonShell.run('linear_regression_engine.py', pyoptions, function(err, results) {
			  			if(err) {
								res.status(500).send(err);
								throw err; //do we need a return here somewhere?
							}
						  // results is an array consisting of messages collected during execution
							return status(200).send(results);
						}); //pyshell
					}); //writefile
				}); //parent
			}
		}); //firstchild
		res.status(204).send({});
	},

	//Returns the current day's stats or else empty
	dayStats: function(req, res) {
		var currDate = new Date().toISOString().slice(0,10);
		var userDB = database.ref('users').orderByChild('uid').equalTo(req.params.uid).ref;
		userDB.child('dreamLogs').limitToLast(1).once('value').then(function(snapshot) {
			var dreamLogObj = Object.keys(snapshot.val())[0];
			if(currDate == snapshot.val()[dreamLogObj].date) {
				res.status(200).send(snapshot.val()[dreamLogObj]);
			} else {
				res.status(204).send({});
			}
		});
	},

	pastDayStats: function(req, res) {
		var userDB = database.ref('users').orderByChild('uid').equalTo(req.params.uid).ref;
		userDB.child('dreamLogs').limitToLast(parseInt(req.params.ago)+1).once('value').then(function(snapshot) { //get parent
			snapshot.forEach(function(childSnapshot) { //loop through children
				//Calulate difference in days
				var currDate = new Date(new Date().toISOString().slice(0,10));
				var logDate = new Date(childSnapshot.val().date);
				var utc1 = Date.UTC(logDate.getFullYear(), logDate.getMonth(), logDate.getDate());
				var utc2 = Date.UTC(currDate.getFullYear(), currDate.getMonth(), currDate.getDate());
				var diffDays = Math.floor((utc2 - utc1) / 1000 / 60 / 60 / 24);
				//add to array if within last 7, including today
				if(diffDays == req.params.ago) {
					res.status(200).send(childSnapshot.val());
					return;
				}
			});
			res.status(204).send({});
		});
	},

	//Returns array of 7 objects from most recent to oldest day 7; empty if no data for that day
	weekStats: function(req, res) {
		var ret = [{}, {}, {}, {}, {}, {}, {}];
		var userDB = database.ref('users').orderByChild('uid').equalTo(req.params.uid).ref;
		userDB.child('dreamLogs').limitToLast(7).once('value').then(function(snapshot) { //get parent
			snapshot.forEach(function(childSnapshot) { //loop through children
				//Calulate difference in days
				var currDate = new Date(new Date().toISOString().slice(0,10));
				var logDate = new Date(childSnapshot.val().date);
				var utc1 = Date.UTC(logDate.getFullYear(), logDate.getMonth(), logDate.getDate());
				var utc2 = Date.UTC(currDate.getFullYear(), currDate.getMonth(), currDate.getDate());
				var diffDays = Math.floor((utc2 - utc1) / 1000 / 60 / 60 / 24);
				//add to array if within last 7, including today
				if(diffDays < 7) {
					ret[diffDays] = childSnapshot.val();
				}
			});
			res.status(200).send(ret);
{'fraction_non_alphanumeric': 0.17420998980632008, 'fraction_numerical': 0.01651376146788991, 'mean_word_length': 3.5611343561134357, 'pattern_counts': {'":': 0, '<': 5, '<?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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '293275', 'n_tokens_mistral': 3906, 'n_tokens_neox': 3154, 'n_words': 646}