servergraph.vue 8.1 KB

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