Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a JS server that archives data from a week ago [duplicate]

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 between 6am and 5 pm. The function for 6 am 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 more methods similar to the one above for the hours from 7 am to 5 pm that I have written to keep track of the metric at all of these times.

What I am trying to do now is 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 another method called millisTillMidnight that transitioned this data into different arrays each day, 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.

EDIT:

Have been working on this problem and still have not figured out how to get this working. I have omitted the server URL and method to get the metric to make this question more general. Here is the code that I have been using:

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();
    });
}
like image 743
Roger99 Avatar asked Jul 04 '16 17:07

Roger99


1 Answers

I don't think I understand the questions but I believe you are asking for how to find out milliseconds till midnight.. If that is the case use the following... Pardon 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);

I hope this helps.

like image 147
spooky Avatar answered Nov 12 '22 12:11

spooky