servergraph.vue 6.4 KB

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