servergraph.vue 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <template>
  2. <div class="container">
  3. <div class="row" id="dateControls">
  4. <div class="col-7">
  5. <input class="form-control" type="date" v-model.lazy="date">
  6. </div>
  7. <div class="col-2">
  8. <button class="form-control" type="button" @click="today">Today</button>
  9. </div>
  10. <div class="col-3">
  11. <select class="form-control" name="type" v-model.number="type">
  12. <option value="0">Day</option>
  13. <option value="1">Month</option>
  14. <option value="2">Year</option>
  15. </select>
  16. </div>
  17. </div>
  18. <canvas id="graph"></canvas>
  19. </div>
  20. </template>
  21. <script>
  22. var generateTemplate = function(){
  23. var rand = function(){
  24. return Math.floor(Math.random()*256);
  25. }
  26. var color = "rgba("+rand()+","+rand()+","+rand()+",0.1)";
  27. // console.log(color);
  28. return {
  29. label: "Traffic",
  30. fill: true,
  31. lineTension: 0.1,
  32. backgroundColor: "rgba(75,192,192,0.4)",
  33. borderColor: color,
  34. borderCapStyle: 'butt',
  35. borderDash: [],
  36. borderDashOffset: 0.0,
  37. borderJoinStyle: 'miter',
  38. pointBorderColor: color, //"rgba(75,192,192,1)",
  39. pointBackgroundColor: "#fff",
  40. // pointBorderWidth: 1,
  41. // pointHoverRadius: 5,
  42. // pointHoverBackgroundColor: "rgba(75,192,192,1)",
  43. // pointHoverBorderColor: "rgba(220,220,220,1)",
  44. // pointHoverBorderWidth: 2,
  45. pointRadius: 1,
  46. pointHitRadius: 10,
  47. data: [],
  48. spanGaps: true,
  49. };
  50. };
  51. import Chart from "chart.js";
  52. export default {
  53. created: function () {
  54. this.updateData();
  55. },
  56. mounted: function () {
  57. this.initData();
  58. this.update();
  59. },
  60. props: {
  61. server: {
  62. type: Number,
  63. default:-1
  64. },
  65. maxplayers: {
  66. type: Number,
  67. default:-1
  68. }
  69. },
  70. data: function () {
  71. return {
  72. chart: {},
  73. type: 0,
  74. date: new Date().toISOString().split("T")[0],
  75. // labels: {},
  76. stamp: null
  77. // datasetTempalte:{
  78. // label: "Traffic",
  79. // fill: true,
  80. // // lineTension: 0.1,
  81. // // backgroundColor: "rgba(75,192,192,0.4)",
  82. // // borderColor: "rgba(75,192,192,1)",
  83. // // borderCapStyle: 'butt',
  84. // // borderDash: [],
  85. // // borderDashOffset: 0.0,
  86. // // borderJoinStyle: 'miter',
  87. // // pointBorderColor: "rgba(75,192,192,1)",
  88. // // pointBackgroundColor: "#fff",
  89. // // pointBorderWidth: 1,
  90. // // pointHoverRadius: 5,
  91. // // pointHoverBackgroundColor: "rgba(75,192,192,1)",
  92. // // pointHoverBorderColor: "rgba(220,220,220,1)",
  93. // // pointHoverBorderWidth: 2,
  94. // // pointRadius: 1,
  95. // // pointHitRadius: 10,
  96. // data: [],
  97. // // spanGaps: false,
  98. // },
  99. }
  100. },
  101. watch:{
  102. type: function (val) {
  103. this.updateData();
  104. this.update();
  105. },
  106. date: function (val, old) {
  107. if(val === ""){
  108. this.date = old;
  109. }
  110. if(val === old){
  111. return 0;
  112. }
  113. var self = this;
  114. if(this.dateInt){
  115. clearTimeout(this.dateInt);
  116. }
  117. this.dateInt = setTimeout(function () {
  118. console.log(val);
  119. self.updateData();
  120. self.update();
  121. }, 300)
  122. }
  123. },
  124. methods: {
  125. today: function () {
  126. this.date = new Date().toISOString().split("T")[0];
  127. },
  128. initData: function () {
  129. this.params = {
  130. type: 'line',
  131. data: {},
  132. options: {
  133. responsive: true,
  134. legend:{
  135. display: false
  136. },
  137. scales: {
  138. yAxes: [{
  139. ticks: {
  140. beginAtZero:true
  141. }
  142. }],
  143. xAxes: [{
  144. display: true,
  145. type: 'time',
  146. time: {
  147. tooltipFormat: "H:mm:ss",
  148. displayFormat: "H:mm:ss"
  149. },
  150. position: 'bottom'
  151. }]
  152. }
  153. }
  154. };
  155. this.chartElement = $(this.$el).find("#graph");
  156. this.chart = new Chart(this.chartElement, this.params);
  157. },
  158. update: function () {
  159. if(this.maxplayers > 0){
  160. this.params.options.scales.yAxes[0].ticks.max = parseInt(this.maxplayers);
  161. }else if(this.$root.servers[parseInt(this.$root.serversId[this.$route.params.id])] &&
  162. this.$root.servers[parseInt(this.$root.serversId[this.$route.params.id])].maxPlayers){
  163. this.params.options.scales.yAxes[0].ticks.max = this.$root.servers[parseInt(this.$root.serversId[this.$route.params.id])].maxPlayers;
  164. }
  165. switch (this.type) {
  166. case 0:
  167. this.params.options.scales.xAxes[0].time.displayFormat = "H:mm:ss";
  168. this.params.options.scales.xAxes[0].time.tooltipFormat = "H:mm:ss";
  169. break;
  170. case 1:
  171. this.params.options.scales.xAxes[0].time.displayFormat = "D";
  172. this.params.options.scales.xAxes[0].time.tooltipFormat = "D.MM H:mm:ss";
  173. break;
  174. case 2:
  175. this.params.options.scales.xAxes[0].time.displayFormat = "D.MM.YY";
  176. this.params.options.scales.xAxes[0].time.tooltipFormat = "D.MM.YY H:mm:ss";
  177. break;
  178. default:
  179. }
  180. this.params.data = this.graphdata;
  181. this.chart.update();
  182. },
  183. updateData: function (type) {
  184. this.type = this.type || 0;
  185. this.graphdata = {
  186. labels: [],
  187. datasets: [
  188. {
  189. label: "Traffic",
  190. data: null
  191. }
  192. ]
  193. };
  194. if(this.server === -1){
  195. return -1;
  196. }
  197. var self = this;
  198. var date = new Date(this.date);
  199. var year = date.getUTCFullYear();
  200. var month = date.getUTCMonth() + 1;
  201. var day = date.getUTCDate();
  202. var url = '/api/trafic/';
  203. url += this.server;
  204. url += "/";
  205. url += day;
  206. url += "/";
  207. url += month;
  208. url += "/";
  209. url += year;
  210. switch (this.type) {
  211. case 0:
  212. url += "/day";
  213. break;
  214. case 1:
  215. url += "/month";
  216. break;
  217. case 2:
  218. url += "/year";
  219. break;
  220. default:
  221. url += "/day";
  222. }
  223. // console.log(url);
  224. $.getJSON(url)
  225. .done(function (obj) {
  226. if(obj.length <= 0){
  227. return -1;
  228. }
  229. self.graphdata.datasets[0].data = [];
  230. self.graphdata.datasets = [];
  231. var firstStamp = new Date(obj[0].createdAt);
  232. self.params.options.scales.xAxes[0].time.min = firstStamp;
  233. self.stamp = firstStamp;
  234. var dataset = {
  235. label: "Traffic",
  236. data: [],
  237. labels: []
  238. };
  239. // console.log(dataset);
  240. dataset = Object.assign({}, generateTemplate(), dataset)
  241. var lastStamp = firstStamp/1000;
  242. var gap = firstStamp/1000 - new Date(obj[1].createdAt)/1000;
  243. gap = Math.abs(Math.floor(gap));
  244. obj.forEach(function (el) {
  245. var stamp = new Date(el.createdAt) - firstStamp;
  246. stamp /= 1000;
  247. // console.log("Gap: ", gap);
  248. // console.log("Stamp: ", stamp);
  249. // console.log("lastStamp: ", lastStamp);
  250. // console.log(stamp-lastStamp);
  251. if(stamp-lastStamp > 2*gap){
  252. // console.log("Dataset ", dataset);
  253. self.graphdata.datasets.push(dataset);
  254. dataset = {
  255. label: "Traffic",
  256. data: [],
  257. labels: []
  258. };
  259. dataset = Object.assign({}, generateTemplate(), dataset)
  260. }
  261. lastStamp = stamp;
  262. stamp /= 60;
  263. stamp = Math.floor(stamp);
  264. stamp = new Date(el.createdAt);
  265. self.params.options.scales.xAxes[0].time.max = stamp;
  266. dataset.data.push({
  267. x: stamp,
  268. y: el.players
  269. });
  270. // self.graphdata.datasets[0].data.push({
  271. // x: stamp,
  272. // y: el.players
  273. // });
  274. // dataset.labels.push(new Date(el.createdAt).toLocaleTimeString());
  275. //self.labels[stamp] = new Date(el.createdAt).toLocaleTimeString();
  276. });
  277. // console.log("Dataset ", dataset);
  278. // self.graphdata.datasets.push(dataset);
  279. self.graphdata.datasets.push(dataset);
  280. self.graphdata = JSON.parse(JSON.stringify(self.graphdata));
  281. // console.log("Graphdata ", self.graphdata);
  282. self.update();
  283. });
  284. }
  285. }
  286. }
  287. </script>