创建节点JS服务器,用于存储和显示基于星期几的数据 [英] Create Node JS Server that stores and displays data based on the day of the week

查看:129
本文介绍了创建节点JS服务器,用于存储和显示基于星期几的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个函数,该函数可以显示一周前从外部源获取的一天中不同时间段的指标。我现在设置服务器的方式是使用一种方法,在上午6点和上午7点的时间从外部来源提取指标。然后,我需要将这些数据存储一周,然后显示当天的数据,从那天开始每周午夜。



我试图存储和存档我每天收集的数据,以便我可以参考当天的一周前一天的日常数据(例如,如果今天是星期一,则访问上一个星期一记录的数据)。我最初的想法是创建一个名为 millisTillMidnight 的方法,该方法每天在午夜附近将这些数据全天记录到不同的数组中,但我一直无法获得该方法工作。理想情况下,我需要能够显示我的应用程序中的每小时数据,从一周之前的一周到当天的当天。



以下是代码我的服务器,我将作为一个Heroku应用程序托管。我怎样才能构建这个服务器,以便部署时,数据可以全天记录,存储一周,然后显示一周,从记录的那一天开始?

  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]; //显示数组


var tmp_stat_a = [0,0]; //保存当天的度量数据

var m_stat_a = [1,1]; // monday archive
var t_stat_a = [2,2]; //星期二档案
var w_stat_a = [3,3]; //星期三档案
var th_stat_a = [4,4]; //星期四档案
var f_stat_a = [5,5]; //星期五存档
var sa_stat_a = [6,6]; //星期六存档
var s_stat_a = [7,7]; //星期日档案


// -------------------------------- --------------------
函数onRequest(req,res){

var Url = // URL

request(url,function(error,response,body){

var data = error;
var status = 404;

if(!错误){

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] = //获取指标

},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] = //获取指标

},millisTill7);


var millisTillMidnight = new Date(now.getFullYear(),now.getMonth(),now.getDate(),23,58,0,0) - now; //存档temp数组并在午夜显示第二天的数据
if(millisTillMidnight <0){
millisTillMidnight + = 86400000;
}
setTimeout(function(){

var d = new Date();
var n = d.getDay();
$ b (i = 0; i <2; i ++)
{
s_stat_a [i] = tmp_stat_a [i ]; //归档临时数组

stat_a [i] = m_stat_a [i]; //将显示数组设置为上个星期归档第二天
}
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]

};

$ b res.writeHead(status,{'Content-Type':'application / json',Access-Control-Allow-Origin:*});
res.write(JSON.stringify(data));
res.end();
});
}

为了使这个问题更一般化,我们省略了请求URL和Pull Metric Method 。无意义的值已经初始化在数组中,所以我可以知道哪个数组首先被输出。

解决方案

一个外部模块,如节点时间表?定期的问题可能会更容易。例如,使用这个模块,你可以定期执行一些任务
,设置你想要的间隔,比如1周。


需要考虑的另一点是时区。当您在另一个tz中将代码提交给服务器时可能会出现问题,并且失败。 瞬间时区模块是一个很好的资源。

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.

解决方案

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.

这篇关于创建节点JS服务器,用于存储和显示基于星期几的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆