Отслеживание даты начала данных в БД.
[squid-reports.git] / web / squid.js
index dbcf25b9aafe0a6e3b1ce5cacbbbcf66bf9e0573..8c85cca80d44497397d0bb22cf06987ac18cbf05 100644 (file)
@@ -1,8 +1,5 @@
 urlbase="./api.php"
 
-refresh = 500
-online_history = 60
-
 graph_colors = [
 "salmon", "lightcoral", "crimson", "red", "darkred", "orangered",
 "gold", "orange", "yellow", "darkkhaki", "lime", "green", "greenyellow",
@@ -36,14 +33,29 @@ online_connections = null
 online_hosts = []
 
 timer = null
+start_date = null
 
 assigned_colors = []
 
-var d = new Date();
-var curr_day = d.getDate();
-var curr_month = d.getMonth() + 1;
-var curr_year = d.getFullYear();
-today = curr_year + "-" + curr_month + "-" + curr_day;
+current_time = null
+time_labels = []
+
+function toDate(d) {
+  var curr_day = d.getDate();
+  if (curr_day<10) { curr_day = '0' + curr_day; }
+  var curr_month = d.getMonth() + 1;
+  if (curr_month<10) { curr_month = '0' + curr_month; }
+  var curr_year = d.getFullYear();
+  return curr_year + "-" + curr_month + "-" + curr_day
+}
+
+function Today() {
+  var dToday = new Date();
+  dToday.setHours(0,0,0,0)
+  return dToday
+}
+
+today = toDate(Today());
 
 date_from = today
 date_to = today
@@ -130,6 +142,12 @@ function UpdatePageProps(props) {
   var logo = document.getElementById("brand");
   logo.innerText = props["site-header"];
 
+  refresh = props["online_refresh"]
+  online_history = props["online_history"]
+
+  start_date = new Date(props["start_date"])
+  start_date.setHours(0,0,0,0)
+
   cats = props["cats"]
 
   for (var i in res["columns"]) { 
@@ -402,11 +420,18 @@ function AddTraffic(label,b) {
 function ProduceOnline(res) {
 
   if (!online_traffic) {
+    current_time = null
     online_traffic = []
+    time_labels = []
+    cur = Date.now()
+    for (i = 1; i<=online_history; i++) {
+      cur = cur - refresh
+      time_labels.unshift(cur)
+    }
     config = {
       type: 'line',
       data: {
-        labels: new Array(online_history).fill('*'),
+        labels: time_labels,
         datasets: online_traffic,
       },
       options: {
@@ -416,6 +441,7 @@ function ProduceOnline(res) {
         responsive: true,
         scales: {
           xAxes: [{
+            type: 'time',
             scaleLabel: {
               display: true,
             }
@@ -427,6 +453,7 @@ function ProduceOnline(res) {
             },
             scaleLabel: {
               display: true,
+              labelString: "Скорость, Кбит/c"
             }
           }]
         }
@@ -434,13 +461,23 @@ function ProduceOnline(res) {
     }
     DrawGraph(config) 
   }
+
+  var delta
+
+  if (current_time) {
+    delta = (Date.now() - current_time)/1000
+  } else {
+    delta = 1
+  }
+  
+  current_time = Date.now()
+
+  time_labels.shift()
+  time_labels.push(current_time)
   
   for (i = online_traffic.length-1; i>=0; i--) {
     online_traffic[i].data.shift()
     online_traffic[i].data.push(0)
-    if (Math.max.apply(null,online_traffic[i].data) == 0) {
-      online_traffic.splice(i,1)
-    }
   }
   
   dictionary = res["dictionary"]
@@ -483,11 +520,17 @@ function ProduceOnline(res) {
     
     new_online_connections[idx] = bytes
     
-    AddTraffic(username,bytes-last_bytes)
+    AddTraffic(username,8*(bytes-last_bytes)/(1024*delta))
   }
   
   online_connections = new_online_connections
 
+  for (i = online_traffic.length-1; i>=0; i--) {
+    if (Math.max.apply(null,online_traffic[i].data) == 0) {
+      online_traffic.splice(i,1)
+    }
+  }
+
   headerHTML = Macro("header-row",{DATA:MergeTH(dictionary)})
   
   reportHTML = Macro("report-table",{HEADER:headerHTML,LINES:innerHTML})
@@ -589,7 +632,7 @@ function Rerun() {
 }
 
 function Online() {
-  timer = null
+  CancelRefresh()
   RefreshFilterPane()
   var header = document.getElementById("report-name") 
   header.innerText = "Активные соединения" 
@@ -607,6 +650,43 @@ function UpdateDates() {
 
 }
 
+function DateLeft() {
+  UpdateDates()
+  var dFrom = new Date(date_from)
+  var dTo = new Date(date_to)
+  var delta = Math.round(((dTo.getTime() - dFrom.getTime()))/(86400*1000))
+  dTo.setDate(dTo.getDate() - (1 + delta))
+  dFrom.setDate(dFrom.getDate() - (1 + delta))
+  if (dTo<start_date) {
+    dTo = start_date
+  }
+  if (dFrom<start_date) {
+    dFrom = start_date
+  }
+  date_from = toDate(dFrom)
+  date_to = toDate(dTo)
+  SetDates()
+}
+
+function DateRight() {
+  dToday = new Date();
+  UpdateDates()
+  var dFrom = new Date(date_from)
+  var dTo = new Date(date_to)
+  var delta = Math.round(((dTo.getTime() - dFrom.getTime()))/(86400*1000))
+  dFrom.setDate(dFrom.getDate() + 1 + delta)
+  dTo.setDate(dTo.getDate() + 1 + delta)
+  if (dTo>Today()) {
+    dTo = Today()
+  }
+  if (dFrom>Today()) {
+    dFrom = Today()
+  }
+  date_from = toDate(dFrom)
+  date_to = toDate(dTo)
+  SetDates()
+}
+
 function SetFilter(name,select) {
   current_filter[name] = select.value;
 }