Skip to content

Commit

Permalink
StatisticsGenerator
Browse files Browse the repository at this point in the history
  • Loading branch information
K0K0V0K committed Nov 24, 2024
1 parent 6fed10b commit e9f8eea
Show file tree
Hide file tree
Showing 14 changed files with 170 additions and 83 deletions.
49 changes: 49 additions & 0 deletions src/main/java/koko/yayu/component/StatisticsGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package koko.yayu.component;

import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import freemarker.template.TemplateMethodModelEx;
import freemarker.template.TemplateModelException;
import koko.yayu.config.FreeMarkerConfig;
import org.json.JSONObject;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.commons.text.WordUtils;

public class StatisticsGenerator {

private final String title;
private final int size;
List<Pair<String, Integer>> stats;

public StatisticsGenerator(String title, int size, String property, List<JSONObject> data) {
this.title = title;
this.size = size;
stats =
data.stream().collect(Collectors.groupingBy(
jsonObject -> jsonObject.getString(property)
)).entrySet().stream().map(
entry -> Pair.of(entry.getKey(), entry.getValue().size()))
.sorted(Map.Entry.comparingByValue())
.limit(size)
.toList();
}

public String generate() {
String re = "<article class=\"message\"><div class=\"message-header\"><p>"
+ String.format("TOP %s %s", size, title)
+ "</p></div><div class=\"message-body\"><table class=\"table is-striped is-fullwidth\">";
for (Pair<String, Integer> pair : stats) {
re += "<tr class=\"is-dark\"><td>" + pair.getKey() + "</td><td>" + pair.getValue() + "</td></tr>";
}
re += "</table></div></article>";
return re;
}
}
1 change: 0 additions & 1 deletion src/main/java/koko/yayu/config/GlobalModelAttributes.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package koko.yayu.config;

import koko.yayu.service.ActiveRMService;
import koko.yayu.service.RMApiService;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ModelAttribute;
Expand Down
23 changes: 16 additions & 7 deletions src/main/java/koko/yayu/controller/AppController.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
import java.util.stream.Collectors;

import koko.yayu.component.ComponentGenerator;
import koko.yayu.service.RMApiService;
import koko.yayu.component.StatisticsGenerator;
import koko.yayu.service.ApiService;
import koko.yayu.util.YayuUtil;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
Expand All @@ -18,27 +19,35 @@
@Controller
public class AppController {

private final RMApiService RMApiService;
private final ApiService apiService;

public AppController(RMApiService RMApiService) {
this.RMApiService = RMApiService;
public AppController(ApiService apiService) {
this.apiService = apiService;
}

@GetMapping("/")
public String index(Model model, @RequestParam(defaultValue = "") String order) {
List<JSONObject> resp = RMApiService.getApps();
List<JSONObject> resp = apiService.getApps();
YayuUtil.order(order, resp);
model.addAttribute("apps", resp);
Map<String, Long> counts = resp.stream().collect(Collectors.groupingBy(
jsonObject -> jsonObject.getString("state"),
Collectors.counting()));

model.addAttribute("topUser",
new StatisticsGenerator("User", 5, "user", resp).generate());
model.addAttribute("topQueue",
new StatisticsGenerator("Queue", 5, "queue", resp).generate());
model.addAttribute("topAppType",
new StatisticsGenerator("Application Type", 5, "applicationType", resp).generate());

model.addAttribute("counts", new TreeMap<>(counts));
return "index";
}

@GetMapping("/app/{appId}")
public String appDetails(Model model, @PathVariable String appId) {
JSONObject resp = RMApiService.getApp(appId);
JSONObject resp = apiService.getApp(appId);
model.addAttribute("props", resp);

model.addAttribute("info", ComponentGenerator.create()
Expand Down Expand Up @@ -71,7 +80,7 @@ public String appDetails(Model model, @PathVariable String appId) {
.generate(resp)
);
model.addAttribute("appId", appId);
List<JSONObject> attempts = RMApiService.getAttempts(appId);
List<JSONObject> attempts = apiService.getAttempts(appId);
model.addAttribute("attempts", attempts);
return "app-details";
}
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/koko/yayu/controller/ConfigController.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.util.Comparator;
import java.util.List;

import koko.yayu.service.RMApiService;
import koko.yayu.service.ApiService;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
Expand All @@ -12,15 +12,15 @@
@Controller
public class ConfigController {

private final RMApiService RMApiService;
private final ApiService apiService;

public ConfigController(RMApiService RMApiService) {
this.RMApiService = RMApiService;
public ConfigController(ApiService apiService) {
this.apiService = apiService;
}

@GetMapping("/config")
public String scheduler(Model model) {
List<JSONObject> resp = RMApiService.getConfig();
List<JSONObject> resp = apiService.getConfig();
resp.sort(Comparator.comparing(o -> o.getString("name")));
model.addAttribute("props",resp);
return "configs";
Expand Down
13 changes: 5 additions & 8 deletions src/main/java/koko/yayu/controller/LogController.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,22 @@
package koko.yayu.controller;

import koko.yayu.service.RMApiService;
import koko.yayu.util.YayuUtil;
import org.json.JSONObject;
import koko.yayu.service.ApiService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

@Controller
public class LogController {

private final RMApiService RMApiService;
private final ApiService apiService;

public LogController(RMApiService RMApiService) {
this.RMApiService = RMApiService;
public LogController(ApiService apiService) {
this.apiService = apiService;
}

@GetMapping("/logs")
public String scheduler(Model model) {
String resp = RMApiService.getLogs();
String resp = apiService.getLogs();
model.addAttribute("props", resp);
return "logs";
}
Expand Down
12 changes: 5 additions & 7 deletions src/main/java/koko/yayu/controller/LogDetailsController.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package koko.yayu.controller;

import koko.yayu.service.RMApiService;
import koko.yayu.service.ApiService;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
Expand All @@ -12,14 +10,14 @@
@RestController
public class LogDetailsController {

private final RMApiService RMApiService;
private final ApiService apiService;

public LogDetailsController(RMApiService RMApiService) {
this.RMApiService = RMApiService;
public LogDetailsController(ApiService apiService) {
this.apiService = apiService;
}

@GetMapping(value = "/logs/{fileName}", produces = MediaType.TEXT_PLAIN_VALUE)
public Flux<String> file(@PathVariable String fileName) {
return RMApiService.getLog(fileName);
return apiService.getLog(fileName);
}
}
12 changes: 6 additions & 6 deletions src/main/java/koko/yayu/controller/NodeController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.util.List;

import koko.yayu.service.RMApiService;
import koko.yayu.service.ApiService;
import koko.yayu.util.YayuUtil;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
Expand All @@ -14,23 +14,23 @@
@Controller
public class NodeController {

private final RMApiService RMApiService;
private final ApiService apiService;

public NodeController(RMApiService RMApiService) {
this.RMApiService = RMApiService;
public NodeController(ApiService apiService) {
this.apiService = apiService;
}

@GetMapping("/nodes")
public String nodes(Model model, @RequestParam(defaultValue = "") String order) {
List<JSONObject> resp = RMApiService.getNodes();
List<JSONObject> resp = apiService.getNodes();
YayuUtil.order(order, resp);
model.addAttribute("nodes", resp);
return "nodes";
}

@GetMapping("/node/{nodeId}")
public String nodeDetails(Model model, @PathVariable String nodeId) {
JSONObject resp = RMApiService.getNode(nodeId);
JSONObject resp = apiService.getNode(nodeId);
model.addAttribute("props", resp);
return "details";
}
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/koko/yayu/controller/QueueController.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import koko.yayu.service.RMApiService;
import koko.yayu.service.ApiService;
import koko.yayu.util.YayuUtil;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
Expand All @@ -19,15 +19,15 @@
@Controller
public class QueueController {

private final RMApiService RMApiService;
private final ApiService apiService;

public QueueController(RMApiService RMApiService) {
this.RMApiService = RMApiService;
public QueueController(ApiService apiService) {
this.apiService = apiService;
}

@GetMapping("/queues")
public String queues(Model model) {
JSONObject resp = RMApiService.getScheduler();
JSONObject resp = apiService.getScheduler();
List<JSONObject> topQueues = YayuUtil.mapJsonList(resp, "queues", "queue");
Map<Integer, List<JSONObject>> queues = flatten(topQueues)
.collect(Collectors.groupingBy(queue ->
Expand All @@ -40,7 +40,7 @@ public String queues(Model model) {

@GetMapping("/queue/{queuePath}")
public String queues(Model model, @PathVariable String queuePath) {
JSONObject resp = RMApiService.getScheduler();
JSONObject resp = apiService.getScheduler();
List<JSONObject> topQueues = YayuUtil.mapJsonList(resp, "queues", "queue");
JSONObject found = flatten(topQueues)
.filter(queue -> queuePath.equals(queue.getString("queuePath")))
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/koko/yayu/controller/SchedulerController.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package koko.yayu.controller;

import koko.yayu.component.ComponentGenerator;
import koko.yayu.service.RMApiService;
import koko.yayu.service.ApiService;
import koko.yayu.util.YayuUtil;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
Expand All @@ -11,15 +11,15 @@
@Controller
public class SchedulerController {

private final RMApiService RMApiService;
private final ApiService apiService;

public SchedulerController(RMApiService RMApiService) {
this.RMApiService = RMApiService;
public SchedulerController(ApiService apiService) {
this.apiService = apiService;
}

@GetMapping("/scheduler")
public String scheduler(Model model) {
JSONObject resp = RMApiService.getScheduler();
JSONObject resp = apiService.getScheduler();
model.addAttribute("props", resp);

model.addAttribute("info", ComponentGenerator.create()
Expand Down
11 changes: 4 additions & 7 deletions src/main/java/koko/yayu/service/ActiveRMService.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ public ActiveRMService(YayuConfig yayuConfig) {
System.err.println(rmUrls);
}

@Scheduled(fixedRate = 1000)
private void pollScheduler() {
public void refresh() {
for (URI rmUrl : rmUrls) {
try {
statuses.put(rmUrl, WebClient
Expand All @@ -49,7 +48,6 @@ private void pollScheduler() {
.map(jsonObject -> jsonObject.getJSONObject("clusterInfo"))
.block());
} catch (Exception e) {
System.err.println(e);
//throw new RuntimeException(e);
statuses.put(rmUrl, null);
}
Expand All @@ -58,10 +56,9 @@ private void pollScheduler() {

public URI getActive() {
return statuses.entrySet().stream()
.filter(
entry -> entry.getValue() != null
&& "ACTIVE".equals(entry.getValue().getString("haState"))
).findFirst().orElseThrow(RuntimeException::new).getKey();
.filter(entry -> entry.getValue() != null)
.filter(entry -> "ACTIVE".equals(entry.getValue().getString("haState")))
.findFirst().orElseThrow(RuntimeException::new).getKey();
}

public JSONObject getActiveStatus() {
Expand Down
Loading

0 comments on commit e9f8eea

Please sign in to comment.