servergraph.vue 7.8 KB

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