问题
I need to create a function that can display a metric pulled at different hours of the day from an outside source one week ago. The way I have my server currently set up is using a method that pulls a metric from an outside source at the hours of 6 am and 7 am. I then need to store this data for a week, and then display the data from that day starting at midnight a week from that day.
I am trying to store and archive the data I collect over the course of each day so that I can reference the daily data from the day one week prior to the current day (ex. if today is monday, access the data recorded last monday). My initial thought was to create a method called millisTillMidnight
that transitioned this data I keep track of throughout the day into different arrays each day near midnight, but I have not been able to get that method to work. Ideally, I need to be able to display the hourly data from the metric in my application from one week prior to the current day of the week.
Below is the code to my server that I will be hosting as a heroku app. How can I build this server so that when deployed, data can be recorded throughout the day, stored for a week, and then displayed a week from the day it was recorded?
var http = require('http');
var request = require('request');
var server = http.createServer(onRequest);
var port = Number(process.env.PORT || 3000)
server.listen(port);
var stat_a = [9, 9]; //display array
var tmp_stat_a = [0, 0]; //Holds the metric data for the day
var m_stat_a = [1, 1]; //monday archive
var t_stat_a = [2, 2]; //tuesday archive
var w_stat_a = [3, 3]; //wednesday archive
var th_stat_a = [4, 4]; //thursday archive
var f_stat_a = [5, 5]; //friday archive
var sa_stat_a = [6, 6]; //saturday archive
var s_stat_a = [7, 7]; //sunday archive
//----------------------------------------------------
function onRequest(req, res){
var Url = //URL
request(Url, function (error, response, body) {
var data = error;
var status = 404;
if(!error){
var now = new Date();
var millisTill6 = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 6, 0, 0, 0) - now;//6 AM
if (millisTill6 < 0) {
millisTill6 += 86400000;
}
setTimeout(function(){
tmp_stat_a[0] = //get metric
}, millisTill6);
var millisTill7 = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 7, 0, 0, 0) - now; //7 AM
if (millisTill7 < 0) {
millisTill7 += 86400000;
}
setTimeout(function(){
tmp_stat_a[1] = //get metric
}, millisTill7);
var millisTillMidnight = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 23, 58, 0, 0) - now; //archive temp array and display data for next day at midnight
if (millisTillMidnight < 0) {
millisTillMidnight += 86400000;
}
setTimeout(function(){
var d = new Date();
var n = d.getDay();
if(n == 0) //SUNDAY
{
for(i=0; i<2; i++)
{
s_stat_a[i] = tmp_stat_a[i]; //archive temp array
stat_a[i] = m_stat_a[i]; //set display array to last weeks archive for the next day
}
console.log("0");
}
else if(n == 1) //MONDAY
{
for(i=0; i<2; i++)
{
m_stat_a[i] = tmp_stat_a[i];
stat_a[i] = t_stat_a[i];
}
console.log("1");
}
else if(n == 2) //TUESDAY
{
for(i=0; i<2; i++)
{
t_stat_a[i] = tmp_stat_a[i];
stat_a[i] = w_stat_a[i];
}
console.log("2");
}
else if(n == 3) //WEDNESDAY
{
for(i=0; i<2; i++)
{
w_stat_a[i] = tmp_stat_a[i];
stat_a[i] = th_stat_a[i];
}
console.log("3");
}
else if(n == 4) //THURSDAY
{
for(i=0; i<2; i++)
{
th_stat_a[i] = tmp_stat_a[i];
stat_a[i] = f_stat_a[i];
}
console.log("4");
}
else if(n == 5) //FRIDAY
{
for(i=0; i<2; i++)
{
f_stat_a[i] = tmp_stat_a[i];
stat_a[i] = sa_stat_a[i];
}
console.log("5");
}
else if(n == 6) //SATURDAY
{
for(i=0; i<2; i++)
{
sa_stat_a[i] = tmp_stat_a[i];
stat_a[i] = s_stat_a[i];
}
console.log("6");
}
}, millisTillMidnight);
status = 200;
data = {
aa: stat_a[0],
ab: stat_a[1]
};
}
res.writeHead(status, { 'Content-Type': 'application/json', "Access-Control-Allow-Origin":"*" });
res.write(JSON.stringify(data));
res.end();
});
}
The request URL and Pull Metric Method have been omitted to make this question more general. Meaningless values have been put in the arrays initally so I can tell which array is being outputted at first.
回答1:
Did you try use an external module, like node-schedule? The periodic issues may be easier with it. With this module you may, for example, execute some task periodically, setting the intervall you want, say, 1 week.
Another point to consider is the timezone. It may be a problem when you commit the code to server in another tz and it fails your counts. moment-timezone module is a good resource.
回答2:
Something along these lines might help. Applying node cron to set the interval:
var net = require('net');
var port = Number(process.env.PORT || 3000)
var timeInMs = Date.now();
var weekInMs = 604800000;
var server = net.createServer(function (socket) {
//if time in Ms is time from first ping to now then...
socket.end(timeInMs + '\r\n');
});
server.listen(port);
The cron job:
var CronJob = require('cron').CronJob;
new CronJob('* * * * * *', function() {
console.log('You will see this message every second');
}, null, true, 'time zone');
来源:https://stackoverflow.com/questions/38361754/create-node-js-server-that-stores-and-displays-data-based-on-the-day-of-the-week