app.js 5.8 KB

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