servergraph.vue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <template>
  2. <canvas id="graph"></canvas>
  3. </template>
  4. <script>
  5. var generateTemplate = function(){
  6. var rand = function(){
  7. return Math.floor(Math.random()*256);
  8. }
  9. var color = "rgba("+rand()+","+rand()+","+rand()+",0.1)";
  10. // console.log(color);
  11. return {
  12. label: "Traffic",
  13. fill: true,
  14. lineTension: 0.1,
  15. backgroundColor: "rgba(75,192,192,0.4)",
  16. borderColor: color,
  17. borderCapStyle: 'butt',
  18. borderDash: [],
  19. borderDashOffset: 0.0,
  20. borderJoinStyle: 'miter',
  21. pointBorderColor: color, //"rgba(75,192,192,1)",
  22. pointBackgroundColor: "#fff",
  23. // pointBorderWidth: 1,
  24. // pointHoverRadius: 5,
  25. // pointHoverBackgroundColor: "rgba(75,192,192,1)",
  26. // pointHoverBorderColor: "rgba(220,220,220,1)",
  27. // pointHoverBorderWidth: 2,
  28. pointRadius: 1,
  29. pointHitRadius: 10,
  30. data: [],
  31. spanGaps: true,
  32. };
  33. };
  34. import Chart from "chart.js";
  35. export default {
  36. created: function () {
  37. this.updateData();
  38. },
  39. mounted: function () {
  40. //this.update();
  41. },
  42. props: {
  43. server: {
  44. type: Number,
  45. default:-1
  46. },
  47. maxplayers: {
  48. type: Number,
  49. default:-1
  50. }
  51. },
  52. data: function () {
  53. return {
  54. // datasetTempalte:{
  55. // label: "Traffic",
  56. // fill: true,
  57. // // lineTension: 0.1,
  58. // // backgroundColor: "rgba(75,192,192,0.4)",
  59. // // borderColor: "rgba(75,192,192,1)",
  60. // // borderCapStyle: 'butt',
  61. // // borderDash: [],
  62. // // borderDashOffset: 0.0,
  63. // // borderJoinStyle: 'miter',
  64. // // pointBorderColor: "rgba(75,192,192,1)",
  65. // // pointBackgroundColor: "#fff",
  66. // // pointBorderWidth: 1,
  67. // // pointHoverRadius: 5,
  68. // // pointHoverBackgroundColor: "rgba(75,192,192,1)",
  69. // // pointHoverBorderColor: "rgba(220,220,220,1)",
  70. // // pointHoverBorderWidth: 2,
  71. // // pointRadius: 1,
  72. // // pointHitRadius: 10,
  73. // data: [],
  74. // // spanGaps: false,
  75. // },
  76. }
  77. },
  78. methods: {
  79. update: function () {
  80. this.params = {
  81. type: 'line',
  82. data: this.graphdata,
  83. options: {
  84. responsive: true,
  85. legend:{
  86. display: false
  87. },
  88. scales: {
  89. yAxes: [{
  90. ticks: {
  91. beginAtZero:true
  92. }
  93. }],
  94. xAxes: [{
  95. display: true,
  96. type: 'linear',
  97. position: 'bottom'
  98. }]
  99. }
  100. }
  101. };
  102. if(this.maxplayers > 0){
  103. this.params.options.scales.yAxes[0].ticks.max = parseInt(this.maxplayers);
  104. }else if(this.$root.servers[parseInt(this.$root.serversId[this.$route.params.id])].maxPlayers){
  105. this.params.options.scales.yAxes[0].ticks.max = this.$root.servers[parseInt(this.$root.serversId[this.$route.params.id])].maxPlayers;
  106. }
  107. // console.log(this.$root.servers[parseInt(this.$root.serversId[this.$route.params.id])]);
  108. // console.log(this.$root);
  109. // console.log("Params ", this.params);
  110. // console.log(JSON.stringify(this.params));
  111. this.chart = new Chart(this.$el, this.params);
  112. },
  113. updateData: function () {
  114. this.graphdata = {
  115. labels: [],
  116. datasets: [
  117. {
  118. label: "Traffic",
  119. data: null
  120. }
  121. ]
  122. };
  123. if(this.server === -1){
  124. return -1;
  125. }
  126. var self = this;
  127. var req = new XMLHttpRequest();
  128. var date = new Date();
  129. var year = date.getUTCFullYear();
  130. var month = date.getUTCMonth() + 1;
  131. var day = date.getUTCDate();
  132. var url = '/api/trafic/';
  133. url += this.server;
  134. url += "/";
  135. url += day;
  136. url += "/";
  137. url += month;
  138. url += "/";
  139. url += year;
  140. url += "/day";
  141. // console.log(url);
  142. req.open('GET', url);
  143. req.onreadystatechange = function () {
  144. if (req.readyState == XMLHttpRequest.DONE &&
  145. req.status === 200) {
  146. var obj = JSON.parse(req.response);
  147. self.graphdata.datasets[0].data = [];
  148. self.graphdata.datasets = [];
  149. var firstStamp = new Date(obj[0].createdAt);
  150. var dataset = {
  151. label: "Traffic",
  152. data: []
  153. };
  154. // console.log(dataset);
  155. dataset = Object.assign({}, generateTemplate(), dataset)
  156. var lastStamp = firstStamp/1000;
  157. var gap = firstStamp/1000 - new Date(obj[1].createdAt)/1000;
  158. gap = Math.abs(Math.floor(gap));
  159. obj.forEach(function (el) {
  160. var stamp = new Date(el.createdAt) - firstStamp;
  161. stamp /= 1000;
  162. // console.log("Gap: ", gap);
  163. // console.log("Stamp: ", stamp);
  164. // console.log("lastStamp: ", lastStamp);
  165. // console.log(stamp-lastStamp);
  166. if(stamp-lastStamp > 2*gap){
  167. // console.log("Dataset ", dataset);
  168. self.graphdata.datasets.push(dataset);
  169. dataset = {
  170. label: "Traffic",
  171. data: []
  172. };
  173. dataset = Object.assign({}, generateTemplate(), dataset)
  174. }
  175. lastStamp = stamp;
  176. stamp /= 60;
  177. stamp = Math.floor(stamp);
  178. dataset.data.push({
  179. x: stamp,
  180. y: el.players
  181. });
  182. // self.graphdata.datasets[0].data.push({
  183. // x: stamp,
  184. // y: el.players
  185. // });
  186. self.graphdata.labels.push(new Date(el.createdAt).toLocaleTimeString());
  187. });
  188. // console.log("Dataset ", dataset);
  189. // self.graphdata.datasets.push(dataset);
  190. self.graphdata.datasets.push(dataset);
  191. self.graphdata = JSON.parse(JSON.stringify(self.graphdata));
  192. // console.log("Graphdata ", self.graphdata);
  193. self.update();
  194. }
  195. };
  196. req.send(null);
  197. }
  198. }
  199. }
  200. </script>