| Jeremy Mordkoff | e29efc3 | 2016-09-07 18:59:17 -0400 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2016 RIFT.IO Inc |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | * |
| 17 | */ |
| 18 | import React from 'react'; |
| 19 | |
| 20 | function debugDump(funcName, state, props) { |
| 21 | console.log("UpTime." + funcName + ": called"); |
| 22 | console.log(" -> props = ", props); |
| 23 | console.log(" -> state = ", state); |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * Uptime Compoeent |
| 28 | * Accepts two properties: |
| 29 | * initialtime {number} number of milliseconds. |
| 30 | * run {boolean} determines whether the uptime ticker should run |
| 31 | */ |
| 32 | export default class UpTime extends React.Component { |
| 33 | |
| 34 | constructor(props) { |
| 35 | super(props); |
| 36 | if (props.debugMode) { |
| 37 | console.log("UpTime.constructor called"); |
| 38 | } |
| 39 | let initialTime = Math.floor(props.initialTime); |
| 40 | this.state = { |
| 41 | run: props.run, |
| 42 | initialTime: initialTime, |
| 43 | time: this.handleConvert(this.calculateDuration({ |
| 44 | intialTime: initialTime |
| 45 | })), |
| 46 | noisySeconds: props.noisySeconds, |
| 47 | debugMode: props.debugMode |
| 48 | } |
| 49 | this.tick; |
| 50 | this.handleConvert = this.handleConvert.bind(this); |
| 51 | this.updateTime = this.updateTime.bind(this); |
| 52 | if (props.debugMode) { |
| 53 | debugDump("constructor", this.state, props); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | componentWillReceiveProps(nextProps) { |
| 58 | if (this.state.debugMode) { |
| 59 | debugDump("componentWillReceiveProps", this.state, nextProps); |
| 60 | } |
| 61 | let initialTime = Math.floor(nextProps.initialTime); |
| 62 | if (initialTime > this.state.initialTime) { |
| 63 | initialTime = this.state.initialTime; |
| 64 | } |
| 65 | this.state = { |
| 66 | initialTime: initialTime, |
| 67 | time: this.handleConvert(this.calculateDuration({ |
| 68 | intialTime: initialTime |
| 69 | })) |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | calculateDuration(args={}) { |
| 74 | let initialTime = args.initialTime; |
| 75 | if (!initialTime) { |
| 76 | initialTime = this.state && this.state.initialTime |
| 77 | ? this.state.initialTime |
| 78 | : 0; |
| 79 | } |
| 80 | let timeNow = args.timeNow ? args.timeNow : Date.now(); |
| 81 | if (initialTime) { |
| 82 | return Math.floor((timeNow/ 1000)) - initialTime; |
| 83 | } else { |
| 84 | return 0; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | handleConvert(input) { |
| 89 | var ret = { |
| 90 | days: 0, |
| 91 | hours: 0, |
| 92 | minutes: 0, |
| 93 | seconds: 0 |
| 94 | }; |
| 95 | if (input == "inactive" || typeof(input) === 'undefined') { |
| 96 | ret.seconds = -1; |
| 97 | } else if (input !== "" && input != "Expired") { |
| 98 | input = Math.floor(input); |
| 99 | ret.seconds = input % 60; |
| 100 | ret.minutes = Math.floor(input / 60) % 60; |
| 101 | ret.hours = Math.floor(input / 3600) % 24; |
| 102 | ret.days = Math.floor(input / 86400); |
| 103 | } |
| 104 | return ret; |
| 105 | } |
| 106 | |
| 107 | toString() { |
| 108 | var self = this; |
| 109 | var ret = ""; |
| 110 | var unitsRendered = []; |
| 111 | |
| 112 | if (self.state.time.days > 0) { |
| 113 | ret += self.state.time.days + "d:"; |
| 114 | unitsRendered.push("d"); |
| 115 | } |
| 116 | |
| 117 | if (self.state.time.hours > 0 || unitsRendered.length > 0) { |
| 118 | ret += self.state.time.hours + "h:"; |
| 119 | unitsRendered.push("h"); |
| 120 | } |
| 121 | |
| 122 | if (self.state.time.minutes > 0 || unitsRendered.length > 0) { |
| 123 | ret += self.state.time.minutes + "m:"; |
| 124 | unitsRendered.push("m"); |
| 125 | } |
| 126 | |
| 127 | if (self.props.noisySeconds || unitsRendered.length == 0 |
| 128 | || unitsRendered.indexOf('m') == 0) |
| 129 | { |
| 130 | // console.log(" -> toString adding seconds: ", self.state.time.seconds); |
| 131 | ret += self.state.time.seconds + "s"; |
| 132 | } |
| 133 | |
| 134 | if (ret.endsWith(':')) { |
| 135 | ret = ret.slice(0,-1); |
| 136 | } |
| 137 | if (ret.length > 0) { |
| 138 | return ret; |
| 139 | } else { |
| 140 | return "--"; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | updateTime() { |
| 145 | if (this.state.initialTime) { |
| 146 | this.setState({ |
| 147 | time: this.handleConvert(this.calculateDuration()) |
| 148 | }); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | componentDidMount() { |
| 153 | var self = this; |
| 154 | if (self.state.run) { |
| 155 | clearInterval(self.tick); |
| 156 | self.tick = setInterval(self.updateTime, 250); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | componentWillUnmount() { |
| 161 | clearInterval(this.tick); |
| 162 | } |
| 163 | |
| 164 | render() { |
| 165 | return (<span>{this.toString()}</span>); |
| 166 | } |
| 167 | } |
| 168 | UpTime.defaultProps = { |
| 169 | noisySeconds: false, |
| 170 | caller: '', |
| 171 | systemId: '' // System identifyer for uptime (e.g.: VM, NS, or process) |
| 172 | } |
| 173 | |