SystemStatus.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /*!
  2. * Ext JS Library 4.0
  3. * Copyright(c) 2006-2011 Sencha Inc.
  4. * licensing@sencha.com
  5. * http://www.sencha.com/license
  6. */
  7. Ext.define('MyDesktop.SystemStatus', {
  8. extend: 'Ext.ux.desktop.Module',
  9. requires: [
  10. 'Ext.chart.*'
  11. ],
  12. id: 'systemstatus',
  13. refreshRate: 500,
  14. init : function() {
  15. // No launcher means we don't appear on the Start Menu...
  16. // this.launcher = {
  17. // text: 'SystemStatus',
  18. // iconCls:'cpustats'
  19. // };
  20. Ext.chart.theme.Memory = Ext.extend(Ext.chart.theme.Base, {
  21. constructor: function(config) {
  22. Ext.chart.theme.Memory.superclass.constructor.call(this, Ext.apply({
  23. colors: [ 'rgb(244, 16, 0)',
  24. 'rgb(248, 130, 1)',
  25. 'rgb(0, 7, 255)',
  26. 'rgb(84, 254, 0)']
  27. }, config));
  28. }
  29. });
  30. },
  31. createNewWindow: function () {
  32. var me = this,
  33. desktop = me.app.getDesktop();
  34. me.cpuLoadData = [];
  35. me.cpuLoadStore = Ext.create('store.json', {
  36. fields: ['core1', 'core2', 'time']
  37. });
  38. me.memoryArray = ['Wired', 'Active', 'Inactive', 'Free'];
  39. me.memoryStore = Ext.create('store.json', {
  40. fields: ['name', 'memory'],
  41. data: me.generateData(me.memoryArray)
  42. });
  43. me.pass = 0;
  44. me.processArray = ['explorer', 'monitor', 'charts', 'desktop', 'Ext3', 'Ext4'];
  45. me.processesMemoryStore = Ext.create('store.json', {
  46. fields: ['name', 'memory'],
  47. data: me.generateData(me.processArray)
  48. });
  49. me.generateCpuLoad();
  50. return desktop.createWindow({
  51. id: 'systemstatus',
  52. title: 'System Status',
  53. width: 800,
  54. height: 600,
  55. animCollapse:false,
  56. constrainHeader:true,
  57. border: false,
  58. layout: {
  59. type: 'hbox',
  60. align: 'stretch'
  61. },
  62. bodyStyle: {
  63. 'background-color': '#FFF'
  64. },
  65. listeners: {
  66. afterrender: {
  67. fn: me.updateCharts,
  68. delay: 100
  69. },
  70. destroy: function () {
  71. clearTimeout(me.updateTimer);
  72. me.updateTimer = null;
  73. },
  74. scope: me
  75. },
  76. items: [{
  77. flex: 1,
  78. xtype: 'container',
  79. layout: {
  80. type: 'vbox',
  81. align: 'stretch'
  82. },
  83. items: [
  84. me.createCpu1LoadChart(),
  85. me.createCpu2LoadChart()
  86. ]
  87. }, {
  88. flex: 1,
  89. xtype: 'container',
  90. layout: {
  91. type: 'vbox',
  92. align: 'stretch'
  93. },
  94. items: [
  95. me.createMemoryPieChart(),
  96. me.createProcessChart()
  97. ]
  98. }]
  99. });
  100. },
  101. createWindow : function() {
  102. var win = this.app.getDesktop().getWindow(this.id);
  103. if (!win) {
  104. win = this.createNewWindow();
  105. }
  106. return win;
  107. },
  108. createCpu1LoadChart: function () {
  109. return {
  110. flex: 1,
  111. xtype: 'chart',
  112. theme: 'Category1',
  113. animate: false,
  114. store: this.cpuLoadStore,
  115. legend: {
  116. position: 'bottom'
  117. },
  118. axes: [{
  119. type: 'Numeric',
  120. position: 'left',
  121. minimum: 0,
  122. maximum: 100,
  123. fields: ['core1'],
  124. title: 'CPU Load',
  125. grid: true,
  126. labelTitle: {
  127. font: '13px Arial'
  128. },
  129. label: {
  130. font: '11px Arial'
  131. }
  132. }],
  133. series: [{
  134. title: 'Core 1 (3.4GHz)',
  135. type: 'line',
  136. lineWidth: 4,
  137. showMarkers: false,
  138. fill: true,
  139. axis: 'left',
  140. xField: 'time',
  141. yField: 'core1',
  142. style: {
  143. 'stroke-width': 1
  144. }
  145. }]
  146. };
  147. },
  148. createCpu2LoadChart: function () {
  149. return {
  150. flex: 1,
  151. xtype: 'chart',
  152. theme: 'Category2',
  153. animate: false,
  154. store: this.cpuLoadStore,
  155. legend: {
  156. position: 'bottom'
  157. },
  158. axes: [{
  159. type: 'Numeric',
  160. position: 'left',
  161. minimum: 0,
  162. maximum: 100,
  163. grid: true,
  164. fields: ['core2'],
  165. title: 'CPU Load',
  166. labelTitle: {
  167. font: '13px Arial'
  168. },
  169. label: {
  170. font: '11px Arial'
  171. }
  172. }],
  173. series: [{
  174. title: 'Core 2 (3.4GHz)',
  175. type: 'line',
  176. lineWidth: 4,
  177. showMarkers: false,
  178. fill: true,
  179. axis: 'left',
  180. xField: 'time',
  181. yField: 'core2',
  182. style: {
  183. 'stroke-width': 1
  184. }
  185. }]
  186. };
  187. },
  188. createMemoryPieChart: function () {
  189. var me = this;
  190. return {
  191. flex: 1,
  192. xtype: 'chart',
  193. animate: {
  194. duration: 250
  195. },
  196. store: this.memoryStore,
  197. shadow: true,
  198. legend: {
  199. position: 'right'
  200. },
  201. insetPadding: 40,
  202. theme: 'Memory:gradients',
  203. series: [{
  204. donut: 30,
  205. type: 'pie',
  206. field: 'memory',
  207. showInLegend: true,
  208. tips: {
  209. trackMouse: true,
  210. width: 140,
  211. height: 28,
  212. renderer: function(storeItem, item) {
  213. //calculate percentage.
  214. var total = 0;
  215. me.memoryStore.each(function(rec) {
  216. total += rec.get('memory');
  217. });
  218. this.setTitle(storeItem.get('name') + ': ' +
  219. Math.round(storeItem.get('memory') / total * 100) + '%');
  220. }
  221. },
  222. highlight: {
  223. segment: {
  224. margin: 20
  225. }
  226. },
  227. labelTitle: {
  228. font: '13px Arial'
  229. },
  230. label: {
  231. field: 'name',
  232. display: 'rotate',
  233. contrast: true,
  234. font: '12px Arial'
  235. }
  236. }]
  237. };
  238. },
  239. createProcessChart: function () {
  240. return {
  241. flex: 1,
  242. xtype: 'chart',
  243. theme: 'Category1',
  244. store: this.processesMemoryStore,
  245. animate: {
  246. easing: 'ease-in-out',
  247. duration: 750
  248. },
  249. axes: [{
  250. type: 'Numeric',
  251. position: 'left',
  252. minimum: 0,
  253. maximum: 10,
  254. fields: ['memory'],
  255. title: 'Memory',
  256. labelTitle: {
  257. font: '13px Arial'
  258. },
  259. label: {
  260. font: '11px Arial'
  261. }
  262. },{
  263. type: 'Category',
  264. position: 'bottom',
  265. fields: ['name'],
  266. title: 'System Processes',
  267. labelTitle: {
  268. font: 'bold 14px Arial'
  269. },
  270. label: {
  271. rotation: {
  272. degrees: 45
  273. }
  274. }
  275. },{
  276. type: 'Numeric',
  277. position: 'top',
  278. fields: ['memory'],
  279. title: 'Memory Usage',
  280. labelTitle: {
  281. font: 'bold 14px Arial'
  282. },
  283. label: {
  284. fill: '#FFFFFF',
  285. stroke: '#FFFFFF'
  286. },
  287. axisStyle: {
  288. fill: '#FFFFFF',
  289. stroke: '#FFFFFF'
  290. }
  291. }],
  292. series: [{
  293. title: 'Processes',
  294. type: 'column',
  295. xField: 'name',
  296. yField: 'memory',
  297. renderer: function(sprite, record, attr, index, store) {
  298. var lowColor = Ext.draw.Color.fromString('#b1da5a'),
  299. value = record.get('memory'),
  300. color;
  301. if (value > 5) {
  302. color = lowColor.getDarker((value - 5) / 15).toString();
  303. } else {
  304. color = lowColor.getLighter(((5 - value) / 20)).toString();
  305. }
  306. if (value >= 8) {
  307. color = '#CD0000';
  308. }
  309. return Ext.apply(attr, {
  310. fill: color
  311. });
  312. }
  313. }]
  314. };
  315. },
  316. generateCpuLoad: function () {
  317. var me = this,
  318. data = me.cpuLoadData;
  319. function generate(factor) {
  320. var value = factor + ((Math.floor(Math.random() * 2) % 2) ? -1 : 1) * Math.floor(Math.random() * 9);
  321. if (value < 0 || value > 100) {
  322. value = 50;
  323. }
  324. return value;
  325. }
  326. if (data.length === 0) {
  327. data.push({
  328. core1: 0,
  329. core2: 0,
  330. time: 0
  331. });
  332. for (var i = 1; i < 100; i++) {
  333. data.push({
  334. core1: generate(data[i - 1].core1),
  335. core2: generate(data[i - 1].core2),
  336. time: i
  337. });
  338. }
  339. me.cpuLoadStore.loadData(data);
  340. } else {
  341. me.cpuLoadStore.data.removeAt(0);
  342. me.cpuLoadStore.data.each(function(item, key) {
  343. item.data.time = key;
  344. });
  345. var lastData = me.cpuLoadStore.last().data;
  346. me.cpuLoadStore.loadData([{
  347. core1: generate(lastData.core1),
  348. core2: generate(lastData.core2),
  349. time: lastData.time + 1
  350. }], true);
  351. }
  352. },
  353. generateData: function (names) {
  354. var data = [],
  355. i,
  356. rest = names.length, consume;
  357. for (i = 0; i < names.length; i++) {
  358. consume = Math.floor(Math.random() * rest * 100) / 100 + 2;
  359. rest = rest - (consume - 5);
  360. data.push({
  361. name: names[i],
  362. memory: consume
  363. });
  364. }
  365. return data;
  366. },
  367. updateCharts: function () {
  368. var me = this;
  369. clearTimeout(me.updateTimer);
  370. me.updateTimer = setTimeout(function() {
  371. var start = new Date().getTime();
  372. if (me.pass % 3 === 0) {
  373. me.memoryStore.loadData(me.generateData(me.memoryArray));
  374. }
  375. if (me.pass % 5 === 0) {
  376. me.processesMemoryStore.loadData(me.generateData(me.processArray));
  377. }
  378. me.generateCpuLoad();
  379. var end = new Date().getTime();
  380. // no more than 25% average CPU load
  381. me.refreshRate = Math.max(me.refreshRate, (end - start) * 4);
  382. me.updateCharts();
  383. me.pass++;
  384. }, me.refreshRate);
  385. }
  386. });