Skip to content

Commit

Permalink
Merge pull request #947 from SCADA-LTS/feature_nd/-#921_Improving_the…
Browse files Browse the repository at this point in the history
…_component_is_Alive

#921 correcting the isAlive component now uses the time on the server
  • Loading branch information
grzesiekb authored Jul 17, 2019
2 parents b1279ee + b3bbc43 commit 8eedc79
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 43 deletions.
116 changes: 73 additions & 43 deletions ScadaLTS-UI-1/src/components/IsAlive.vue
Original file line number Diff line number Diff line change
@@ -1,88 +1,118 @@
<template>
<div>
<alert v-if="bdanger" id="p_is_alive" type="danger"><b>Error</b> - time of the latest server response: {{timeFromServer | moment}} </alert>
<alert v-if="bwarning" id="p_is_alive" type="warning"><b>Warning</b> - time of the latest server response: {{timeFromServer | moment}}</alert>
<div>
<alert v-if="bdanger" id="p_is_alive" type="danger"><b>Error</b> - time of the latest server response:
{{timeFromServerEpoch | moment}}
</alert>
<alert v-if="bwarning" id="p_is_alive" type="warning"><b>Warning</b> - time of the latest server response:
{{timeFromServerEpoch | moment}}</alert>
<alert v-if="bsuccess" id="p_is_alive" type="success">{{label}}</alert>
<popover title="Is Alive" target="#p_is_alive" trigger="hover">
<template slot="popover">
<p class="p_is_now">Time from web browser: <b>{{timeInWeb | moment}}</b></p>
<p class="p_is_last_time_modyfication">Time of the latest server response: <b>{{timeFromServer | moment}}</b></p>
<p class="p_is_now">Time from web browser: <b>{{timeInWebEpoch | moment}}</b></p>
<p class="p_is_last_time_modyfication">Time of the latest server response: <b>{{timeFromServerEpoch |
moment}}</b></p>
<p>Refresh time: {{timeRefresh}} [ms]</p>
<p>Time before warning: {{timeWarning}} [ms]</p>
<p>Time before error: {{timeError}} [ms]</p>
<p>Time before warning: {{timeWarningEpoch}} [ms]</p>
<p>Time before error: {{timeErrorEpoch}} [ms]</p>
</template>
</popover>
</div>
</template>

<script>
import moment from "moment";
import moment from 'moment'
import httpClient from 'axios'
/**
* @author grzegorz.bylica@gmail.com
* @author grzegorz.bylica@gmail.com
*/
class ApiIsAlive {
time () {
return new Promise((resolve, reject) => {
try {
const apiIsAliveTime = `./api/is_alive/time`
httpClient.get(apiIsAliveTime, {timeout: 5000}).then(response => {
resolve(response)
}).catch(error => {
reject(error)
})
} catch (e) {
reject(e)
}
})
}
}
/**
* @author grzegorz.bylica@gmail.com
*/
export default {
props: ['plabel', 'ptimeWarning', 'ptimeError', 'ptimeRefresh'],
data() {
data () {
return {
label: this.plabel, // "Is alive"
timeWarning: this.ptimeWarning, // 5000 [ms]
timeError: this.ptimeError, // 10000 [ms]
timeRefresh: this.ptimeRefresh, // 1000 [ms]
timeInWeb: 0,
timeFromServer: 0,
label: this.plabel, // Is alive
timeWarningEpoch: this.ptimeWarning / 1000, // 5000 [ms]
timeErrorEpoch: this.ptimeError / 1000, // 10000 [ms]
timeRefresh: this.ptimeRefresh, // 1000 [ms]
timeInWebEpoch: 0,
timeFromServerEpoch: 0,
bdanger: false,
bwarning: false,
bsuccess: true
};
}
},
methods: {
getTime() {
this.timeInWeb = new Date();
return this.timeInWeb;
getTime () {
this.timeInWebEpoch = Date.now() / 1000
return this.timeInWebEpoch
},
getTimeFromServer() {
this.timeFromServer = lasTimeUpdate;
return this.timeFromServer;
},
checkTimeWarning() {
interpretationOfTheState () {
let isDanger =
this.getTime() - this.getTimeFromServer() > this.timeError;
this.getTime() - this.timeFromServerEpoch > this.timeErrorEpoch
if (isDanger) {
this.bwarning = false;
this.bsuccess = false;
this.bdanger = true;
this.bwarning = false
this.bsuccess = false
this.bdanger = true
} else {
let isWarning =
this.getTime() - this.getTimeFromServer() > this.timeWarning;
this.getTime() - this.timeFromServerEpoch > this.timeWarningEpoch
if (isWarning) {
this.bdanger = false;
this.bsuccess = false;
this.bwarning = true;
this.bdanger = false
this.bsuccess = false
this.bwarning = true
} else {
this.bdanger = false;
this.bwarning = false;
this.bsuccess = true;
this.bdanger = false
this.bwarning = false
this.bsuccess = true
}
}
},
checkTimeWarning () {
new ApiIsAlive().time().then(response => {
this.timeFromServerEpoch = response.data
this.interpretationOfTheState()
}).catch(er => {
this.interpretationOfTheState()
})
}
},
created() {
created () {
if (this.timeRefresh) {
setInterval(
function() {
this.checkTimeWarning();
function () {
this.checkTimeWarning()
}.bind(this),
this.timeRefresh
);
)
}
},
filters: {
moment: function(date) {
return moment(date).format(" hh:mm:ss");
moment: function (epoch) {
let date = new Date(epoch * 1000)
return moment(date).format(' hh:mm:ss')
}
}
};
}
</script>

<style>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.scada_lts.web.mvc.api.components.is_alive;

import com.serotonin.mango.Common;
import com.serotonin.mango.vo.User;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import java.time.Instant;


/**
* @autor [email protected] on 16.07.19
*/
@Controller
public class TimeFromServer {

private static final Log LOG = LogFactory.getLog(TimeFromServer.class);

@RequestMapping(value = "/api/is_alive/time", method = RequestMethod.GET)
public ResponseEntity<Long> time(HttpServletRequest request) {
LOG.info("/api/is_alive/time");

try {
User user = Common.getUser(request);

if (user != null) {
long unixTimestamp = Instant.now().getEpochSecond();
return new ResponseEntity<>(unixTimestamp, HttpStatus.OK);
}

return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);

} catch (Exception e) {
if (LOG.isTraceEnabled()) {
LOG.trace(e);
}
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}
}
}

0 comments on commit 8eedc79

Please sign in to comment.