app.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. var React = require('react');
  2. var Backbone = require('backbone');
  3. var $ = jQuery = require('jquery');
  4. var _ = require('underscore');
  5. require('backbone-react-component');
  6. require('backbone-relational');
  7. Backbone.$ = $;
  8. var page = require('./page.js');
  9. var bootstrap = require('bootstrap');
  10. config = {
  11. url: "http://katrin.kit.edu/newstatus/katrin/api/sql/",
  12. server: "orca",
  13. };
  14. var ToggleContent = React.createClass({
  15. render: function() {
  16. var rightAlign = {
  17. marginRight: '10px',
  18. float: 'right'
  19. }
  20. var leftAlign = {
  21. marginLeft: '10px',
  22. float: 'left'
  23. };
  24. return (
  25. <div className={'toggle-content'}>
  26. <div><b style={leftAlign}>ID</b> <span style={rightAlign}>{this.props.id}</span></div><br/>
  27. <div><b style={leftAlign}>IP</b> <span style={rightAlign}>{this.props.ip_address}</span></div><br/>
  28. <div><b style={leftAlign}>Time to go</b> <span style={rightAlign}>{this.props.timeToGo}</span></div><br/>
  29. </div>
  30. );
  31. }
  32. });
  33. var MyComponent = React.createClass({
  34. mixins: [Backbone.React.Component.mixin],
  35. getInitialSate: function() {
  36. return {showMore: false}
  37. },
  38. onClick: function () {
  39. this.setState({showMore: !this.state.showMore})
  40. },
  41. millisecondsToStr: function (milliseconds) {
  42. // TIP: to find current time in milliseconds, use:
  43. // var current_time_milliseconds = new Date().getTime();
  44. function numberEnding (number) {
  45. return (number > 1) ? 's' : '';
  46. }
  47. var temp = Math.floor(milliseconds / 1000);
  48. var years = Math.floor(temp / 31536000);
  49. if (years) {
  50. return years + ' year' + numberEnding(years);
  51. }
  52. //TODO: Months! Maybe weeks?
  53. var days = Math.floor((temp %= 31536000) / 86400);
  54. if (days) {
  55. return days + ' day' + numberEnding(days);
  56. }
  57. var hours = Math.floor((temp %= 86400) / 3600);
  58. if (hours) {
  59. return hours + ' hour' + numberEnding(hours);
  60. }
  61. var minutes = Math.floor((temp %= 3600) / 60);
  62. if (minutes) {
  63. return minutes + ' minute' + numberEnding(minutes);
  64. }
  65. var seconds = temp % 60;
  66. if (seconds) {
  67. return seconds + ' second' + numberEnding(seconds);
  68. }
  69. return 'less than a second'; //'just now' //or other string you like;
  70. },
  71. render: function () {
  72. var arrowStyle = {
  73. display: 'block',
  74. marginLeft: 'auto',
  75. marginRight: 'auto',
  76. width: '20px'
  77. };
  78. var rightAlign = {
  79. marginRight: '10px',
  80. float: 'right'
  81. }
  82. var leftAlign = {
  83. marginLeft: '10px',
  84. float: 'left'
  85. };
  86. return (
  87. <div>
  88. <h4>{this.props.name}</h4>
  89. <div><b style={leftAlign}>Experiment</b> <span style={rightAlign}>{this.props.experiment}</span></div><br/>
  90. <div><b style={leftAlign}>State</b> <span style={rightAlign}>{this.props.state ? 'Running' : 'Stopped'}</span></div><br/>
  91. <div><b style={leftAlign}>Run time</b> <span style={rightAlign}>{this.millisecondsToStr(this.props.uptime*1000)}</span></div><br/>
  92. { this.state.showMore
  93. ? <div><ToggleContent {...this.props} /> <img src="./images/arrow_up.png" alt="toggle" style={arrowStyle} onClick={this.onClick} /></div>
  94. : <img src="./images/arrow_down.png" alt="toggle" style={arrowStyle} onClick={this.onClick} /> }
  95. </div>);
  96. }
  97. });
  98. var Run = Backbone.RelationalModel.extend({
  99. parse: function(resp) {
  100. if (typeof resp.data === 'undefined')
  101. return resp;
  102. else
  103. return resp.data[0];
  104. }
  105. });
  106. var Runs = Backbone.Collection.extend({
  107. model: Run,
  108. parse: function(resp) {
  109. return resp.data;
  110. }
  111. });
  112. var Machine = Backbone.Model.extend({
  113. parse: function(resp) {
  114. if (typeof resp.data === 'undefined')
  115. return resp;
  116. else
  117. return resp.data[0];
  118. }
  119. });
  120. var Machines = Backbone.Collection.extend({
  121. model: Machine,
  122. parse: function(resp) {
  123. return resp.data;
  124. },
  125. });
  126. function setup0() {
  127. machines.fetch({
  128. error: function(c) {
  129. console.log('oops');
  130. },
  131. success: function(c) {
  132. _.each(c.models, function(model, key) {
  133. var _id = 'block' + (++key);
  134. React.render(<MyComponent model={model} />, document.getElementById(_id));
  135. });
  136. }
  137. });
  138. }
  139. function setup() {
  140. $.when( machines.fetch(), runs.fetch()).then( function(){
  141. var run_info = {}
  142. _.each(runs.models, function(run) {
  143. var run = run.attributes;
  144. run_info[run.machine_id] = run;
  145. });
  146. _.each(machines.models, function(machine, key) {
  147. var run = run_info[machine.attributes.machine_id];
  148. _.extend(machines.models[key].attributes, run);
  149. React.render(<MyComponent model={machines.models[key]} />, document.getElementById('block'+(key+1)));
  150. });
  151. });
  152. }
  153. function update() {
  154. $.when( machines.fetch(), runs.fetch()).then( function(){
  155. var run_info = {}
  156. _.each(runs.models, function(run) {
  157. run_info[run.machine_id] = run.attributes;
  158. });
  159. _.each(machines.models, function(machine, key) {
  160. var run = run_info[machine.attributes.machine_id];
  161. _.extend(machines.models[key].attributes, run);
  162. });
  163. });
  164. }
  165. var url = config.url + config.server + "/";
  166. machines = new Machines({});
  167. machines.url = url+'machines';
  168. runs = new Runs({});
  169. runs.url = url + 'runs';
  170. //setup0();
  171. setup();
  172. setInterval(function() {
  173. update();
  174. }, 10000);
  175. $(document).ready(function() {
  176. console.log('ready');
  177. page.toggleSidebar();
  178. });