servergraph.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. chart: {}
  55. // datasetTempalte:{
  56. // label: "Traffic",
  57. // fill: true,
  58. // // lineTension: 0.1,
  59. // // backgroundColor: "rgba(75,192,192,0.4)",
  60. // // borderColor: "rgba(75,192,192,1)",
  61. // // borderCapStyle: 'butt',
  62. // // borderDash: [],
  63. // // borderDashOffset: 0.0,
  64. // // borderJoinStyle: 'miter',
  65. // // pointBorderColor: "rgba(75,192,192,1)",
  66. // // pointBackgroundColor: "#fff",
  67. // // pointBorderWidth: 1,
  68. // // pointHoverRadius: 5,
  69. // // pointHoverBackgroundColor: "rgba(75,192,192,1)",
  70. // // pointHoverBorderColor: "rgba(220,220,220,1)",
  71. // // pointHoverBorderWidth: 2,
  72. // // pointRadius: 1,
  73. // // pointHitRadius: 10,
  74. // data: [],
  75. // // spanGaps: false,
  76. // },
  77. }
  78. },
  79. methods: {
  80. update: function () {
  81. this.params = {
  82. type: 'line',
  83. data: this.graphdata,
  84. options: {
  85. responsive: true,
  86. legend:{
  87. display: false
  88. },
  89. scales: {
  90. yAxes: [{
  91. ticks: {
  92. beginAtZero:true
  93. }
  94. }],
  95. xAxes: [{
  96. display: true,
  97. type: 'linear',
  98. position: 'bottom'
  99. }]
  100. }
  101. }
  102. };
  103. if(this.maxplayers > 0){
  104. this.params.options.scales.yAxes[0].ticks.max = parseInt(this.maxplayers);
  105. }else if(this.$root.servers[parseInt(this.$root.serversId[this.$route.params.id])].maxPlayers){
  106. this.params.options.scales.yAxes[0].ticks.max = this.$root.servers[parseInt(this.$root.serversId[this.$route.params.id])].maxPlayers;
  107. }
  108. // console.log(this.$root.servers[parseInt(this.$root.serversId[this.$route.params.id])]);
  109. // console.log(this.$root);
  110. // console.log("Params ", this.params);
  111. // console.log(JSON.stringify(this.params));
  112. this.chart = new Chart(this.$el, this.params);
  113. },
  114. updateData: function () {
  115. this.graphdata = {
  116. labels: [],
  117. datasets: [
  118. {
  119. label: "Traffic",
  120. data: null
  121. }
  122. ]
  123. };
  124. if(this.server === -1){
  125. return -1;
  126. }
  127. var self = this;
  128. var req = new XMLHttpRequest();
  129. var date = new Date();
  130. var year = date.getUTCFullYear();
  131. var month = date.getUTCMonth() + 1;
  132. var day = date.getUTCDate();
  133. var url = '/api/trafic/';
  134. url += this.server;
  135. url += "/";
  136. url += day;
  137. url += "/";
  138. url += month;
  139. url += "/";
  140. url += year;
  141. url += "/day";
  142. // console.log(url);
  143. req.open('GET', url);
  144. req.onreadystatechange = function () {
  145. if (req.readyState == XMLHttpRequest.DONE &&
  146. req.status === 200) {
  147. var obj = JSON.parse(req.response);
  148. if(obj.length <= 0){
  149. return -1;
  150. }
  151. self.graphdata.datasets[0].data = [];
  152. self.graphdata.datasets = [];
  153. var firstStamp = new Date(obj[0].createdAt);
  154. var dataset = {
  155. label: "Traffic",
  156. data: [],
  157. labels: []
  158. };
  159. // console.log(dataset);
  160. dataset = Object.assign({}, generateTemplate(), dataset)
  161. var lastStamp = firstStamp/1000;
  162. var gap = firstStamp/1000 - new Date(obj[1].createdAt)/1000;
  163. gap = Math.abs(Math.floor(gap));
  164. obj.forEach(function (el) {
  165. var stamp = new Date(el.createdAt) - firstStamp;
  166. stamp /= 1000;
  167. // console.log("Gap: ", gap);
  168. // console.log("Stamp: ", stamp);
  169. // console.log("lastStamp: ", lastStamp);
  170. // console.log(stamp-lastStamp);
  171. if(stamp-lastStamp > 2*gap){
  172. // console.log("Dataset ", dataset);
  173. self.graphdata.datasets.push(dataset);
  174. dataset = {
  175. label: "Traffic",
  176. data: [],
  177. labels: []
  178. };
  179. dataset = Object.assign({}, generateTemplate(), dataset)
  180. }
  181. lastStamp = stamp;
  182. stamp /= 60;
  183. stamp = Math.floor(stamp);
  184. dataset.data.push({
  185. x: stamp,
  186. y: el.players
  187. });
  188. // self.graphdata.datasets[0].data.push({
  189. // x: stamp,
  190. // y: el.players
  191. // });
  192. // dataset.labels.push(new Date(el.createdAt).toLocaleTimeString());
  193. self.labels[stamp] = new Date(el.createdAt).toLocaleTimeString();
  194. });
  195. // console.log("Dataset ", dataset);
  196. // self.graphdata.datasets.push(dataset);
  197. self.graphdata.datasets.push(dataset);
  198. self.graphdata = JSON.parse(JSON.stringify(self.graphdata));
  199. // console.log("Graphdata ", self.graphdata);
  200. self.update();
  201. }
  202. };
  203. req.send(null);
  204. }
  205. }
  206. }
  207. </script>