浏览代码

- added boolean type to config ("start updating on start")
- added pretty trafic prototype

Ksawery Kuklinski 8 年之前
父节点
当前提交
b6605ca810
共有 2 个文件被更改,包括 88 次插入3 次删除
  1. 40 2
      engine.js
  2. 48 1
      index.js

+ 40 - 2
engine.js

@@ -150,6 +150,43 @@ const tsquery = require('./ts-query').query;
       }).then(function(servers) {
       }).then(function(servers) {
         callback(servers);
         callback(servers);
       });
       });
+    },
+    getTrafficByRange: function (from, to, id, callback) {
+      db.traffic.findAll({where: {
+          serverId: id,
+          createdAt: {
+            $gt: from,
+            $lt: to
+          }
+        },
+        attributes: trafficAtributes
+      }).then(function(records) {
+        callback(records);
+      });
+    },
+    pretty: {
+      day: function (date, id, callback) {
+        var from = new Date(date.toDateString()),
+            to = new Date(from.valueOf() + 1000*60*60*24);
+        exports.statusGrabber.getTrafficByRange(from, to, id, callback);
+      },
+      month: function (date, id, callback) {
+        var from = new Date(date.toDateString());
+        from.setUTCDate(1);
+        var to = new Date(from);
+        for(var m = to.getUTCMonth(); to.getUTCMonth() === m;){
+          to = new Date(to.valueOf() + 1000*60*60*24);
+        }
+        exports.statusGrabber.getTrafficByRange(from, to, id, callback);
+      },
+      year: function (date, id, callback) {
+        var from = new Date(date.toDateString());
+        from.setUTCDate(1);
+        from.setUTCMonth(0);
+        var to = new Date(from);
+        to.setUTCFullYear(to.getUTCFullYear() + 1);
+        exports.statusGrabber.getTrafficByRange(from, to, id, callback);
+      }
     }
     }
   };
   };
 }
 }
@@ -190,9 +227,10 @@ const tsquery = require('./ts-query').query;
   const defaultConfig = {
   const defaultConfig = {
     lang: "en",
     lang: "en",
     updateInterval : 1000 * 60 * 15,
     updateInterval : 1000 * 60 * 15,
-    port: 8080
+    port: 8080,
+    updateState: true
   };
   };
-  
+
   exports.configMenager = {
   exports.configMenager = {
     config: {},
     config: {},
     load: function () {
     load: function () {

+ 48 - 1
index.js

@@ -49,8 +49,14 @@ app.post("/api/config", function (req, res) {
       continue;
       continue;
     }
     }
     if(engine.configMenager.config.hasOwnProperty(key)){
     if(engine.configMenager.config.hasOwnProperty(key)){
-      if (typeof(ngine.configMenager.config[key]) === "number") {
+      if (typeof(engine.configMenager.config[key]) === "number") {
         engine.configMenager.config[key] = parseInt(req.body[key]);
         engine.configMenager.config[key] = parseInt(req.body[key]);
+      }else if(typeof(engine.configMenager.config[key]) === "boolean"){
+        if(req.body[key] === "true"){
+          engine.configMenager.config[key] = true;
+        }else if(req.body[key] === "false"){
+          engine.configMenager.config[key] = false;
+        }
       }else{
       }else{
         engine.configMenager.config[key] = req.body[key];
         engine.configMenager.config[key] = req.body[key];
       }
       }
@@ -189,6 +195,38 @@ app.get("/api/trafic/get/:id", function (req, res) {
   });
   });
 });
 });
 
 
+var newDate = function(req){
+  var date = new Date(0);
+  date.setUTCFullYear(parseInt(req.params.year));
+  date.setUTCMonth(parseInt(req.params.month) - 1);
+  date.setUTCDate(parseInt(req.params.day));
+  return date;
+};
+
+app.get("/api/trafic/:id/:day/:month/:year/day", function (req, res) {
+  var date = newDate(req);
+  var id = parseInt(req.params.id);
+  engine.statusGrabber.pretty.day(date, id, function (records) {
+    res.send(JSON.stringify(records));
+  });
+});
+
+app.get("/api/trafic/:id/:day/:month/:year/month", function (req, res) {
+  var date = newDate(req);
+  var id = parseInt(req.params.id);
+  engine.statusGrabber.pretty.month(date, id, function (records) {
+    res.send(JSON.stringify(records));
+  });
+});
+
+app.get("/api/trafic/:id/:day/:month/:year/year", function (req, res) {
+  var date = newDate(req);
+  var id = parseInt(req.params.id);
+  engine.statusGrabber.pretty.year(date, id, function (records) {
+    res.send(JSON.stringify(records));
+  });
+});
+
 app.get("/api/status", function (req, res) {
 app.get("/api/status", function (req, res) {
   engine.serverMenager.list(function (data) {
   engine.serverMenager.list(function (data) {
     async.map(data, function (data, callback) {
     async.map(data, function (data, callback) {
@@ -255,11 +293,19 @@ app.get("/api/details", function (req, res) {
   });
   });
 });
 });
 
 
+app.all("/api/*", function (req, res) {
+  res.send(JSON.stringify({
+    success:false,
+    msg: "No method founded"
+  }));
+});
+
 app.use(function(req, res, next){
 app.use(function(req, res, next){
   res.render('index');
   res.render('index');
 });
 });
 
 
 var onInit = function () {
 var onInit = function () {
+  if(engine.configMenager.config.updateState){
   updateInterval
   updateInterval
     .setInterval(engine.configMenager.config.updateInterval)
     .setInterval(engine.configMenager.config.updateInterval)
     .setFunction(function () {
     .setFunction(function () {
@@ -278,6 +324,7 @@ var onInit = function () {
       });
       });
     })
     })
     .start();
     .start();
+  }
 };
 };
 
 
 var onExit = function () {
 var onExit = function () {