




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
第如何利用moment處理時間戳并計算時間的差值項目使用nodejs寫服務端,有個功能就是統(tǒng)計代理服務器流量,然后把統(tǒng)計的數(shù)據(jù)通過echarts渲染到頁面。
當然統(tǒng)計數(shù)據(jù)這里用到了定時器,在使用的是
var
schedule=
require(
'node-schedule');
有興趣的同學可以在npm上搜一搜關(guān)于js定時任務的事,其實都大同小異,差不多都是運用corn表達式。
以下是我的定時從代理服務器獲取數(shù)據(jù)并存庫。
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)在來展示需要根據(jù)前端傳過來的時間戳來篩選出數(shù)據(jù)的代碼,處理時間我用到了moment這個類庫,基本包含了時間所有的處理方法。
總結(jié)以下moment的幾個常用的函數(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. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 藤制品企業(yè)的品牌形象傳播策略考核試卷
- 電機在消防設備的緊急響應考核試卷
- 人教版《分數(shù)的概念與應用》課件
- 谷物儲存的糧食儲存容量提升考核試卷
- 塑料制品的環(huán)保法規(guī)遵守考核試卷
- 新車上戶需要什么手續(xù)和上戶流程
- 環(huán)境衛(wèi)生課件
- 網(wǎng)絡安全防護在能源交易平臺的數(shù)據(jù)保護評估考核試卷
- 耐火土石礦山綠色開采與循環(huán)經(jīng)濟考核試卷
- 2025年石英或云母填充塑料項目建議書
- 2022年巫山縣教師進城考試筆試題庫及答案解析
- 青少年模擬法庭劇本(敲詐勒索)
- 萬用表校準報告
- 新聞采訪與寫作(馬工程筆記)
- DB32∕T 1703-2011 科技成果轉(zhuǎn)化服務規(guī)范總則
- SQ-02-綠色食品種植產(chǎn)品調(diào)查表0308
- 視頻結(jié)構(gòu)化大數(shù)據(jù)平臺解決方案
- SolidWorks、CAD三維建模練習習題圖
- 光伏發(fā)電項目安全專項投資估算方案
- 大學生物技術(shù)分析教材與教學設計
- 皮爾遜Ⅲ型曲線模比系數(shù)計算(共享版).xls
評論
0/150
提交評論