diff options
3 files changed, 34 insertions, 10 deletions
diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/client/Client.java b/src/main/java/pl/edu/mimuw/cloudatlas/client/Client.java index b63e4fd..ca9e7e0 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/client/Client.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/client/Client.java @@ -2,8 +2,10 @@ package pl.edu.mimuw.cloudatlas.client; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication +@EnableScheduling public class Client { public static void main(String[] args) { SpringApplication.run(Client.class, args); diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/client/ClientController.java b/src/main/java/pl/edu/mimuw/cloudatlas/client/ClientController.java index df32f50..510b721 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/client/ClientController.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/client/ClientController.java @@ -1,6 +1,7 @@ package pl.edu.mimuw.cloudatlas.client; import com.google.gson.Gson; +import org.springframework.scheduling.annotation.Scheduled; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.*; import org.springframework.stereotype.Controller; @@ -11,10 +12,7 @@ import java.net.InetAddress; import java.net.UnknownHostException; import java.rmi.registry.LocateRegistry; import java.rmi.registry.Registry; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; +import java.util.*; /* should enable reading attribute values stored by the agent @@ -31,6 +29,9 @@ plotting the attributes with numeric values as real-time graphs. public class ClientController { private Api api; + private Map<ValueTime, AttributesMap> attributes; + private String currentZoneName; + ClientController() { try { Registry registry = LocateRegistry.getRegistry("localhost"); @@ -39,6 +40,9 @@ public class ClientController { System.err.println("Client exception:"); e.printStackTrace(); } + + this.attributes = new LinkedHashMap<ValueTime, AttributesMap>(); + this.currentZoneName = "/"; } @GetMapping("/") @@ -224,23 +228,41 @@ public class ClientController { @GetMapping("/values") public String valuesPage(Model model) { model.addAttribute("availableZones", getAvailableZonesString()); + model.addAttribute("currentZone", "Current zone: " + this.currentZoneName); model.addAttribute("zoneName", new ContactsString()); return "attribChart"; } - @PostMapping("/values") - public String valuesPage(@ModelAttribute ContactsString zoneName, Model model) { - boolean success = true; + @Scheduled(fixedRate = 5000) + private void fetchAttributeData() { AttributesMap attribData; + ValueTime currentTime; try { - attribData = api.getZoneAttributeValues(zoneName.getString()); + if (!this.currentZoneName.isEmpty()) { + attribData = api.getZoneAttributeValues(this.currentZoneName); + currentTime = new ValueTime(System.currentTimeMillis()); + this.attributes.put(currentTime, attribData); + System.out.println(currentTime.toString() + ": " + attribData.toString()); + } } catch (Exception e) { - success = false; System.err.println("Client exception:"); e.printStackTrace(); } + Iterator<ValueTime> it = this.attributes.keySet().iterator(); + while (it.hasNext() && this.attributes.size() > 1000) { + it.next(); + it.remove(); + System.out.println(this.attributes.toString()); + } + } + + @PostMapping("/values") + public String valuesPage(@ModelAttribute ContactsString zoneName, Model model) { + this.currentZoneName = zoneName.getString(); + this.attributes.clear(); + model.addAttribute("currentZone", "Current zone: " + this.currentZoneName); model.addAttribute("availableZones", getAvailableZonesString()); return "attribChart"; } diff --git a/src/main/resources/pl/edu/mimuw/cloudatlas/client/templates/attribChart.html b/src/main/resources/pl/edu/mimuw/cloudatlas/client/templates/attribChart.html index 29721e8..2a9e1e0 100644 --- a/src/main/resources/pl/edu/mimuw/cloudatlas/client/templates/attribChart.html +++ b/src/main/resources/pl/edu/mimuw/cloudatlas/client/templates/attribChart.html @@ -34,6 +34,7 @@ <body> <div th:replace="fragments/navbar :: navbar"></div> +<h4 th:text="${currentZone}"></h4> <div id="zoneAttribForm"> <form action="#" th:action="@{/values}" th:object="${zoneName}" method="post"> <div class="form-group"> @@ -45,7 +46,6 @@ <button type="submit" class="btn btn-primary">Submit</button> </form> </div> - <div id="chart_div" style="width: 100%; height: 500px;"></div> <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script> |