Tasks.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. function hasProgress(msg) {
  23. if (msg.indexOf('progress:') > -1) {
  24. let progressStr = msg.substr(msg.indexOf('progress:'))
  25. let value = progressStr.match(/(100|[0-9]{1,2})%/)
  26. return value[1]
  27. } else {
  28. return false
  29. }
  30. }
  31. class Tasks extends Component {
  32. static propTypes = {
  33. tasks: PropTypes.array
  34. }
  35. constructor(props) {
  36. super(props)
  37. this.headers = [
  38. { label: "Task", key: 'task_type', width: 1 },
  39. { label: "Instance", key: 'instance', width: 1 },
  40. { label: "Latest Message", key: 'latest_message', width: 2 },
  41. { label: "Timestamp", key: 'timestamp', width: 1 }
  42. ]
  43. this.state = {
  44. listItems: []
  45. }
  46. }
  47. componentWillMount() {
  48. this.componentWillReceiveProps(this.props)
  49. }
  50. componentWillReceiveProps(newProps, oldProps) {
  51. let listItems = []
  52. if (newProps.tasks) {
  53. newProps.tasks.forEach((item) => {
  54. let latestMessage
  55. if (item.progress_updates.length && item.progress_updates[item.progress_updates.length - 1]) {
  56. latestMessage = item.progress_updates[item.progress_updates.length - 1].message
  57. } else {
  58. latestMessage = "-"
  59. }
  60. let progressUpdates = []
  61. if (item.progress_updates.length) {
  62. let first = true
  63. if (item.progress_updates[0] !== null) {
  64. item.progress_updates.sort((a, b) => moment(a.created_at).isAfter(moment(b.created_at)))
  65. }
  66. for (let i = item.progress_updates.length - 1; i >= 0; i--) {
  67. let date = "-"
  68. if (item.progress_updates[i]) {
  69. date = moment(item.progress_updates[i].created_at).format("YYYY-MM-DD HH:mm:ss")
  70. progressUpdates.push(
  71. <div key={"progress_" + i} className={first ? " first" : ""}>
  72. <span>{date}</span>
  73. <span>
  74. {item.progress_updates[i] && item.progress_updates[i].message}
  75. {hasProgress(item.progress_updates[i].message) &&
  76. <ProgressBar progress={hasProgress(item.progress_updates[i].message)}/>
  77. }
  78. </span>
  79. </div>)
  80. first = false
  81. }
  82. }
  83. if (progressUpdates.length == 0) {
  84. progressUpdates = "N/A"
  85. }
  86. } else {
  87. progressUpdates = "N/A"
  88. }
  89. let taskDetails = (<div className={s.taskDetails}>
  90. <div className={s.group}>
  91. <div className={s.detailTitle}>Status</div>
  92. <div className={s.detailValue}><span className={"status-pill " + item.status}>{item.status}</span></div>
  93. </div>
  94. <div className={s.group}>
  95. <div className={s.detailTitle}>ID</div>
  96. <div className={s.detailValue}>{item.id}</div>
  97. </div>
  98. <div className={s.group}>
  99. <div className={s.detailTitle}>Exception details</div>
  100. <div className={s.detailValue}>
  101. {item.exception_details && item.exception_details.length ?
  102. (<TextTruncate line={10} text={item.exception_details} truncateText="..."/>) : "N/A"}
  103. </div>
  104. </div>
  105. <div className={s.group}>
  106. <div className={s.detailTitle}>Depends on</div>
  107. <div className={s.detailValue}>{item.depends_on && item.depends_on[0] ? item.depends_on[0] : "N/A"}</div>
  108. </div>
  109. <div className={s.group + " " + s.progressUpdates}>
  110. <div className={s.detailTitle}>Progress Updates</div>
  111. <div className={s.detailValue}>{progressUpdates}</div>
  112. </div>
  113. </div>)
  114. let taskType = item.task_type.replace(/_/g, " ").toLowerCase()
  115. taskType = taskType.charAt(0).toUpperCase() + taskType.slice(1)
  116. let newItem = {
  117. task_type: (<span>
  118. <span className={"taskIcon " + item.status}/>
  119. <TextTruncate line={1} truncateText="..." text={taskType}/>
  120. </span>),
  121. instance: <TextTruncate line={1} text={item.instance} truncateText="..."/>,
  122. latest_message: <TextTruncate line={1} truncateText="..." text={latestMessage}/>,
  123. timestamp: item.updated_at ? moment(item.updated_at).format("YYYY-MM-DD HH:mm:ss") : "-",
  124. detailView: taskDetails,
  125. openState: item.status === 'RUNNING'
  126. }
  127. listItems.push(newItem)
  128. }, this)
  129. }
  130. this.setState({ listItems: listItems })
  131. }
  132. render() {
  133. return (
  134. <div className={s.root}>
  135. { this.state ?
  136. (<div className={s.container}>
  137. <Table
  138. headerItems={this.headers}
  139. listItems={this.state ? this.state.listItems : null}
  140. customClassName={s.table}
  141. show={this.state !== null}
  142. />
  143. </div>) : <LoadingIcon/>
  144. }
  145. </div>
  146. );
  147. }
  148. }
  149. export default withStyles(Tasks, s);