




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
第如何利用moment處理時(shí)間戳并計(jì)算時(shí)間的差值項(xiàng)目使用nodejs寫服務(wù)端,有個(gè)功能就是統(tǒng)計(jì)代理服務(wù)器流量,然后把統(tǒng)計(jì)的數(shù)據(jù)通過(guò)echarts渲染到頁(yè)面。
當(dāng)然統(tǒng)計(jì)數(shù)據(jù)這里用到了定時(shí)器,在使用的是
var
schedule=
require(
'node-schedule');
有興趣的同學(xué)可以在npm上搜一搜關(guān)于js定時(shí)任務(wù)的事,其實(shí)都大同小異,差不多都是運(yùn)用corn表達(dá)式。
以下是我的定時(shí)從代理服務(wù)器獲取數(shù)據(jù)并存庫(kù)。
schedule.scheduleJob('*/15*****',function(){
console.log('timer!!!!!!!!!!');
vardataObj1={};
iplists.forEach(function(ele,index){
varreq=http.request("http://"+ele+":14567/stat",function(res){
dataObj1.time=newDate(res.headers.date);
dataObj1.ip=req.getHeader("host").split(":")[0];
res.setEncoding('utf-8');
vartempData='';
res.on('data',function(chunk){
tempData+=chunk;
varresultObj=JSON.parse(tempData);
dataObj1.flow=resultObj.bw15s;
varflow1=newflowrank1({
ip:dataObj1.ip,
flow:dataObj1.flow,
time:newDate(dataObj1.time)
flow1.save(function(err,flow1){
if(err){
console.log(err);
return;
req.on("error",function(err){
console.log(err);
req.end()
現(xiàn)在來(lái)展示需要根據(jù)前端傳過(guò)來(lái)的時(shí)間戳來(lái)篩選出數(shù)據(jù)的代碼,處理時(shí)間我用到了moment這個(gè)類庫(kù),基本包含了時(shí)間所有的處理方法。
總結(jié)以下moment的幾個(gè)常用的函數(shù):
moment().startOf('year');//settoJanuary1st,12:00amthisyear
moment().startOf('month');//settothefirstofthismonth,12:00am
moment().startOf('quarter');//settothebeginningofthecurrentquarter,1stdayofmonths,12:00am
moment().startOf('week');//settothefirstdayofthisweek,12:00am
moment().startOf('isoWeek');//settothefirstdayofthisweekaccordingtoISO8601,12:00am
moment().startOf('day');//setto12:00amtoday
moment().startOf('date');//setto12:00amtoday
moment().startOf('hour');//settonow,butwith0mins,0secs,and0ms
moment().startOf('minute');//settonow,butwith0secondsand0milliseconds
moment().startOf('second');//sameasmoment().milliseconds(0);
moment().diff(Moment|String|Number|Date|Array);
moment().diff(Moment|String|Number|Date|Array,String);
moment().diff(Moment|String|Number|Date|Array,String,Boolean);
vara=moment([2008,9]);
varb=moment([2007,0]);
a.diff(b,'years');//1
a.diff(b,'years',true);//1.75
moment().add(Number,String);
moment().add(Duration);
moment().add(Object);
varmoment=require('moment');
varstarttime=moment(moment.unix(parseInt(req.query.starttime)).toDate());
console.log("==============");
console.log(moment(moment.unix(parseInt(req.query.starttime)).toDate()));
varendtime=moment(moment.unix(parseInt(req.query.endtime)).toDate());
console.log(moment(moment.unix(parseInt(req.query.endtime)).toDate()));
console.log(endtime.diff(starttime,'hour'));
console.log(endtime.diff(starttime,'months'));
console.log(endtime.diff(starttime,'months'));
*查詢小于1天的數(shù)據(jù)
if(endtime.diff(starttime,'hour')=24){
console.log("flowrank1");
flowrank1.find({
ip:req.query.ip,
time:{
$gt:moment.unix(req.query.starttime).toDate(),
$lte:moment.unix(req.query.endtime).toDate()
_id:0,
ip:1,
flow:1,
time:1
function(err,doc){
if(err){
console.log("err!!!!!")
console.log(err);
returnres.end(JSON.stringify(retcode.operateDbErr));
varresult=retcode.res_ok;
result.data=doc;
console.log(doc)
res.end(JSON.stringify(result));
}elseif(endtime.diff(starttime,'months')==0){
console.log("flowrank2!!!!");
flowrank2.find({
ip:req.query.ip,
time:{
$gt:moment.unix(req.query.starttime).toDate(),
$lte:moment.unix(req.query.endtime).toDate()
_id:0,
ip:1,
flow:1,
time:1
function(err,doc){
if(err){
console.log("err!!!!!")
console.log(err);
returnres.end(JSON.stringify(retcode.operateDbErr));
varresult=retcode.res_ok;
result.data=doc;
console.log(doc)
res.end(JSON.stringify(result));
}elseif(endtime.diff(starttime,'months')=1){
console.log("inflowrank3");
flowrank3.find({
ip:req.query.ip,
time:{
$gt:moment.unix(req.query.starttime).toDate(),
$lte:moment.unix(req.query.endtime).toDate()
_id:0,
ip:1,
flow:1,
time:1
function(err,doc){
if(err){
console.log("err!!!!!")
console.log(err);
returnres.end
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 頭頸外科課件耳內(nèi)鏡
- 頭部CTA掃描課件
- 海洋生物海洋生物免疫多樣性合同
- 2025-2030中國(guó)裁斷機(jī)行業(yè)經(jīng)銷商網(wǎng)絡(luò)優(yōu)化與管理模式報(bào)告
- 銀行跳槽面試題目及答案
- 電力系統(tǒng)狀態(tài)監(jiān)測(cè)軟件合同
- 2025-2030中國(guó)礦產(chǎn)資源壓覆土地補(bǔ)償糾紛解決機(jī)制報(bào)告
- 銀行入職金融類考試題及答案
- 2025-2030中國(guó)電梯導(dǎo)軌型鋼行業(yè)數(shù)字化轉(zhuǎn)型與智能制造趨勢(shì)報(bào)告
- 核反應(yīng)堆堆芯核物理實(shí)驗(yàn)評(píng)價(jià)合同
- 機(jī)電安裝工程施工現(xiàn)場(chǎng)安全管理臺(tái)賬
- 伯克利-利特溫(組織績(jī)效與變革因果關(guān)系)組織診斷+模型案例、工具解析
- 中建三局建造工程師工作指南
- 合肥申領(lǐng)居住證的申請(qǐng)
- 鄉(xiāng)衛(wèi)生院醫(yī)療質(zhì)量與安全管理考核表(綜合科)
- 創(chuàng)三甲醫(yī)療質(zhì)量
- 圖解學(xué)習(xí)解讀《全國(guó)護(hù)理事業(yè)發(fā)展規(guī)劃(2021-2025年)》課件
- 26個(gè)字母練字帖打印
- 語(yǔ)文大單元教學(xué)的設(shè)計(jì)思路
- 裝訂質(zhì)量要求及檢驗(yàn)標(biāo)準(zhǔn)
- 小學(xué)生必背古詩(shī)75首(注音版)
評(píng)論
0/150
提交評(píng)論