Tasks.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. Copyright (C) 2017 Cloudbase Solutions SRL
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. import React, { Component, PropTypes } from 'react';
  15. import withStyles from 'isomorphic-style-loader/lib/withStyles';
  16. import moment from 'moment';
  17. import Table from '../Table';
  18. import s from './Tasks.scss';
  19. import TextTruncate from 'react-text-truncate';
  20. import LoadingIcon from "../LoadingIcon/LoadingIcon";
  21. import ProgressBar from '../ProgressBar';
  22. import Helper from '../Helper';
  23. function hasProgress(msg) {
  24. if (msg.indexOf('progress:') > -1) {
  25. let progressStr = msg.substr(msg.indexOf('progress:'))
  26. let value = progressStr.match(/(100|[0-9]{1,2})%/)
  27. return value[1]
  28. } else {
  29. return false
  30. }
  31. }
  32. class Tasks extends Component {
  33. static propTypes = {
  34. tasks: PropTypes.array
  35. }
  36. constructor(props) {
  37. super(props)
  38. this.headers = [
  39. { label: "Task", key: 'task_type', width: 1 },
  40. { label: "Instance", key: 'instance', width: 1 },
  41. { label: "Latest Message", key: 'latest_message', width: 2 },
  42. { label: "Timestamp", key: 'timestamp', width: 1 }
  43. ]
  44. this.state = {
  45. listItems: []
  46. }
  47. }
  48. componentWillMount() {
  49. this.componentWillReceiveProps(this.props)
  50. }
  51. componentWillReceiveProps(newProps) {
  52. let listItems = []
  53. if (newProps.tasks) {
  54. newProps.tasks.forEach((item) => {
  55. let latestMessage
  56. if (item.progress_updates.length && item.progress_updates[item.progress_updates.length - 1]) {
  57. latestMessage = item.progress_updates[item.progress_updates.length - 1].message
  58. } else {
  59. latestMessage = "-"
  60. }
  61. let progressUpdates = []
  62. if (item.progress_updates.length) {
  63. let first = true
  64. if (item.progress_updates[0] !== null) {
  65. item.progress_updates.sort((a, b) => moment(a.created_at).isAfter(moment(b.created_at)))
  66. }
  67. for (let i = item.progress_updates.length - 1; i >= 0; i--) {
  68. let date = "-"
  69. if (item.progress_updates[i]) {
  70. let createdAt = Helper.getTimeObject(item.progress_updates[i].created_at)
  71. date = moment(createdAt).format("YYYY-MM-DD HH:mm:ss")
  72. progressUpdates.push(
  73. <div key={"progress_" + i} className={first ? " first" : ""}>
  74. <span>{date}</span>
  75. <span>
  76. {item.progress_updates[i] && item.progress_updates[i].message}
  77. {hasProgress(item.progress_updates[i].message) &&
  78. <ProgressBar progress={hasProgress(item.progress_updates[i].message)} />
  79. }
  80. </span>
  81. </div>)
  82. first = false
  83. }
  84. }
  85. if (progressUpdates.length == 0) {
  86. progressUpdates = "N/A"
  87. }
  88. } else {
  89. progressUpdates = "N/A"
  90. }
  91. let taskDetails = (<div className={s.taskDetails}>
  92. <div className={s.group}>
  93. <div className={s.detailTitle}>Status</div>
  94. <div className={s.detailValue}><span className={"status-pill " + item.status}>{item.status}</span></div>
  95. </div>
  96. <div className={s.group}>
  97. <div className={s.detailTitle}>ID</div>
  98. <div className={s.detailValue}>{item.id}</div>
  99. </div>
  100. <div className={s.group}>
  101. <div className={s.detailTitle}>Exception details</div>
  102. <div className={s.detailValue}>
  103. {item.exception_details && item.exception_details.length ?
  104. (<TextTruncate line={10} text={item.exception_details} truncateText="..." />) : "N/A"}
  105. </div>
  106. </div>
  107. <div className={s.group}>
  108. <div className={s.detailTitle}>Depends on</div>
  109. <div className={s.detailValue}>{item.depends_on && item.depends_on[0] ? item.depends_on[0] : "N/A"}</div>
  110. </div>
  111. <div className={s.group + " " + s.progressUpdates}>
  112. <div className={s.detailTitle}>Progress Updates</div>
  113. <div className={s.detailValue}>{progressUpdates}</div>
  114. </div>
  115. </div>)
  116. let taskType = item.task_type.replace(/_/g, " ").toLowerCase()
  117. taskType = taskType.charAt(0).toUpperCase() + taskType.slice(1)
  118. let newItem = {
  119. task_type: (<span>
  120. <span className={"taskIcon " + item.status} />
  121. <TextTruncate line={1} truncateText="..." text={taskType} />
  122. </span>),
  123. instance: <TextTruncate line={1} text={item.instance} truncateText="..." />,
  124. latest_message: <TextTruncate line={1} truncateText="..." text={latestMessage} />,
  125. timestamp: item.created_at ? Helper.getTimeObject(moment(item.created_at)).format("YYYY-MM-DD HH:mm:ss") :
  126. "-",
  127. detailView: taskDetails,
  128. openState: item.status === 'RUNNING'
  129. }
  130. listItems.push(newItem)
  131. }, this)
  132. }
  133. this.setState({ listItems: listItems })
  134. }
  135. render() {
  136. return (
  137. <div className={s.root}>
  138. { this.state ?
  139. (<div className={s.container}>
  140. <Table
  141. headerItems={this.headers}
  142. listItems={this.state ? this.state.listItems : null}
  143. customClassName={s.table}
  144. show={this.state !== null}
  145. />
  146. </div>) : <LoadingIcon />
  147. }
  148. </div>
  149. );
  150. }
  151. }
  152. export default withStyles(Tasks, s);