3 * Copyright 2016 RIFT.IO Inc
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 import React from 'react';
20 function debugDump(funcName, state, props) {
21 console.log("UpTime." + funcName + ": called");
22 console.log(" -> props = ", props);
23 console.log(" -> state = ", state);
28 * Accepts two properties:
29 * initialtime {number} number of milliseconds.
30 * run {boolean} determines whether the uptime ticker should run
32 export default class UpTime extends React.Component {
36 if (props.debugMode) {
37 console.log("UpTime.constructor called");
39 let initialTime = Math.floor(props.initialTime);
42 initialTime: initialTime,
43 time: this.handleConvert(this.calculateDuration({
44 intialTime: initialTime
46 noisySeconds: props.noisySeconds,
47 debugMode: props.debugMode
50 this.handleConvert = this.handleConvert.bind(this);
51 this.updateTime = this.updateTime.bind(this);
52 if (props.debugMode) {
53 debugDump("constructor", this.state, props);
57 componentWillReceiveProps(nextProps) {
58 if (this.state.debugMode) {
59 debugDump("componentWillReceiveProps", this.state, nextProps);
61 let initialTime = Math.floor(nextProps.initialTime);
62 if (initialTime > this.state.initialTime) {
63 initialTime = this.state.initialTime;
66 initialTime: initialTime,
67 time: this.handleConvert(this.calculateDuration({
68 intialTime: initialTime
73 calculateDuration(args={}) {
74 let initialTime = args.initialTime;
76 initialTime = this.state && this.state.initialTime
77 ? this.state.initialTime
80 let timeNow = args.timeNow ? args.timeNow : Date.now();
82 return Math.floor((timeNow/ 1000)) - initialTime;
88 handleConvert(input) {
95 if (input == "inactive" || typeof(input) === 'undefined') {
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);
110 var unitsRendered = [];
112 if (self.state.time.days > 0) {
113 ret += self.state.time.days + "d:";
114 unitsRendered.push("d");
117 if (self.state.time.hours > 0 || unitsRendered.length > 0) {
118 ret += self.state.time.hours + "h:";
119 unitsRendered.push("h");
122 if (self.state.time.minutes > 0 || unitsRendered.length > 0) {
123 ret += self.state.time.minutes + "m:";
124 unitsRendered.push("m");
127 if (self.props.noisySeconds || unitsRendered.length == 0
128 || unitsRendered.indexOf('m') == 0)
130 // console.log(" -> toString adding seconds: ", self.state.time.seconds);
131 ret += self.state.time.seconds + "s";
134 if (ret.endsWith(':')) {
135 ret = ret.slice(0,-1);
137 if (ret.length > 0) {
145 if (this.state.initialTime) {
147 time: this.handleConvert(this.calculateDuration())
152 componentDidMount() {
154 if (self.state.run) {
155 clearInterval(self.tick);
156 self.tick = setInterval(self.updateTime, 250);
160 componentWillUnmount() {
161 clearInterval(this.tick);
165 return (<span>{this.toString()}</span>);
168 UpTime.defaultProps = {
171 systemId: '' // System identifyer for uptime (e.g.: VM, NS, or process)