Create a JS server that backs up data a week ago

I need to create a function that can display a metric by pulling it at different hours of the day from an external source a week ago. I have my server installed, which uses a method that pulls the metric from an external source in the hours between 6:00 and 17:00. The function at 6am is shown below:

//get metric at 6 am var millisTill6 = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 6, 0, 0, 0) - now; if (millisTill6 < 0) { millisTill6 += 86400000; // try again tomorrow } setTimeout(function() { //get metric }, millisTill6); 

There are 11 other methods, similar to the above for hours from 7 am to 5 pm, which I wrote to track the metric at all these points.

What I'm trying to do now is to store and archive the data that I collect during each day, so that I can refer to daily data for the day one week before the current day (for example, if it's Monday, access to the data recorded on the last Monday). My initial thought was to create another method called millisTillMidnight , which translated this data into different arrays every day, but I was not able to get this method to work. Ideally, I should be able to display hourly data from a metric in my application a week before the current day of the week.

EDIT:

We worked on this problem and still have not figured out how to do it. I skipped the server url and method to get the metric to make this question more general. Here is the code I used:

 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(); }); } 
+6
source share
1 answer

I do not think that I understand the questions, but I believe that you are asking how to find out the milliseconds before midnight. If so, use the following ... Sorry in advance if I misunderstood.

 var now = new Date(); var midnightDate = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 24, 0, 0, 0); console.log(midnightDate); //This is midnight... ie. if run on July 6th, this is - Thu Jul 07 2016 00:00:00 GMT-0700 (PDT) var millsTillMidnight = midnightDate - new Date(); console.log(millsTillMidnight); 

Hope this helps.

+1
source

All Articles