servergraph.vue 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. console.log(this.chartElement);
  124. this.chart = new Chart(this.chartElement, this.params);
  125. },
  126. update: function () {
  127. if(this.maxplayers > 0){
  128. this.params.options.scales.yAxes[0].ticks.max = parseInt(this.maxplayers);
  129. }else if(this.$root.servers &&
  130. this.$root.servers[parseInt(this.$root.serversId[this.$route.params.id])].maxPlayers){
  131. this.params.options.scales.yAxes[0].ticks.max = this.$root.servers[parseInt(this.$root.serversId[this.$route.params.id])].maxPlayers;
  132. }
  133. switch (this.type) {
  134. case 0:
  135. this.params.options.scales.xAxes[0].time.displayFormat = "H:mm:ss";
  136. this.params.options.scales.xAxes[0].time.tooltipFormat = "H:mm:ss";
  137. break;
  138. case 1:
  139. this.params.options.scales.xAxes[0].time.displayFormat = "D";
  140. this.params.options.scales.xAxes[0].time.tooltipFormat = "D.MM H:mm:ss";
  141. break;
  142. case 2:
  143. this.params.options.scales.xAxes[0].time.displayFormat = "D.MM.YY";
  144. this.params.options.scales.xAxes[0].time.tooltipFormat = "D.MM.YY H:mm:ss";
  145. break;
  146. default:
  147. }
  148. this.params.data = this.graphdata;
  149. this.chart.update();
  150. },
  151. updateData: function (type) {
  152. type = type || 0;
  153. this.graphdata = {
  154. labels: [],
  155. datasets: [
  156. {
  157. label: "Traffic",
  158. data: null
  159. }
  160. ]
  161. };
  162. if(this.server === -1){
  163. return -1;
  164. }
  165. var self = this;
  166. var date = new Date();
  167. var year = date.getUTCFullYear();
  168. var month = date.getUTCMonth() + 1;
  169. var day = date.getUTCDate();
  170. var url = '/api/trafic/';
  171. url += this.server;
  172. url += "/";
  173. url += day;
  174. url += "/";
  175. url += month;
  176. url += "/";
  177. url += year;
  178. switch (type) {
  179. case 0:
  180. url += "/day";
  181. break;
  182. case 1:
  183. url += "/month";
  184. break;
  185. case 2:
  186. url += "/year";
  187. break;
  188. default:
  189. url += "/day";
  190. }
  191. // console.log(url);
  192. $.getJSON(url)
  193. .done(function (obj) {
  194. if(obj.length <= 0){
  195. return -1;
  196. }
  197. self.graphdata.datasets[0].data = [];
  198. self.graphdata.datasets = [];
  199. var firstStamp = new Date(obj[0].createdAt);
  200. self.params.options.scales.xAxes[0].time.min = firstStamp;
  201. self.stamp = firstStamp;
  202. var dataset = {
  203. label: "Traffic",
  204. data: [],
  205. labels: []
  206. };
  207. // console.log(dataset);
  208. dataset = Object.assign({}, generateTemplate(), dataset)
  209. var lastStamp = firstStamp/1000;
  210. var gap = firstStamp/1000 - new Date(obj[1].createdAt)/1000;
  211. gap = Math.abs(Math.floor(gap));
  212. obj.forEach(function (el) {
  213. var stamp = new Date(el.createdAt) - firstStamp;
  214. stamp /= 1000;
  215. // console.log("Gap: ", gap);
  216. // console.log("Stamp: ", stamp);
  217. // console.log("lastStamp: ", lastStamp);
  218. // console.log(stamp-lastStamp);
  219. if(stamp-lastStamp > 2*gap){
  220. // console.log("Dataset ", dataset);
  221. self.graphdata.datasets.push(dataset);
  222. dataset = {
  223. label: "Traffic",
  224. data: [],
  225. labels: []
  226. };
  227. dataset = Object.assign({}, generateTemplate(), dataset)
  228. }
  229. lastStamp = stamp;
  230. stamp /= 60;
  231. stamp = Math.floor(stamp);
  232. stamp = new Date(el.createdAt);
  233. self.params.options.scales.xAxes[0].time.max = stamp;
  234. dataset.data.push({
  235. x: stamp,
  236. y: el.players
  237. });
  238. // self.graphdata.datasets[0].data.push({
  239. // x: stamp,
  240. // y: el.players
  241. // });
  242. // dataset.labels.push(new Date(el.createdAt).toLocaleTimeString());
  243. //self.labels[stamp] = new Date(el.createdAt).toLocaleTimeString();
  244. });
  245. // console.log("Dataset ", dataset);
  246. // self.graphdata.datasets.push(dataset);
  247. self.graphdata.datasets.push(dataset);
  248. self.graphdata = JSON.parse(JSON.stringify(self.graphdata));
  249. // console.log("Graphdata ", self.graphdata);
  250. self.update();
  251. });
  252. }
  253. }
  254. }
  255. </script>