From e1f6630b45fa63f41b8ffc5976d195e6995907c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magdalena=20Grodzi=C5=84ska?= Date: Thu, 21 Nov 2019 21:06:55 +0100 Subject: Add periodic attribute values fetch and storage --- .../pl/edu/mimuw/cloudatlas/client/Client.java | 2 ++ .../mimuw/cloudatlas/client/ClientController.java | 40 +++++++++++++++++----- .../cloudatlas/client/templates/attribChart.html | 2 +- 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 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(); + 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 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 @@
+

@@ -45,7 +46,6 @@
-
-- cgit v1.2.3