Skip to content

Commit

Permalink
app logs
Browse files Browse the repository at this point in the history
  • Loading branch information
K0K0V0K committed Nov 24, 2024
1 parent cc10dde commit d3dd170
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 17 deletions.
31 changes: 26 additions & 5 deletions src/main/java/koko/yayu/component/ComponentGenerator.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package koko.yayu.component;

import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

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

import org.apache.commons.lang3.StringUtils;
Expand All @@ -16,7 +20,7 @@ public class ComponentGenerator {
private String title;
private String color;
private final Map<String, String> displayNames = new LinkedHashMap<>();
private final Map<String, Function<Object, String>> propertyMappers = new HashMap<>();
private final Map<String, TemplateMethodModelEx> propertyMappers = new HashMap<>();

public static ComponentGenerator create() {
return new ComponentGenerator();
Expand Down Expand Up @@ -47,11 +51,14 @@ public ComponentGenerator addField(String propertyName, String displayName) {
public ComponentGenerator addField(
String propertyName,
String displayName,
Function<Object, String> propertyMapper
String propertyMapper
) {
displayNames.put(propertyName, displayName);
if (propertyMapper != null) {
propertyMappers.put(propertyName, propertyMapper);
propertyMappers.put(
propertyName,
(TemplateMethodModelEx) FreeMarkerConfig.methods.get(propertyMapper)
);
}
return this;
}
Expand All @@ -61,7 +68,7 @@ public String generate(JSONObject data) {
re += String.format("<div class=\"panel-heading\">%s</div>\n", title);
for (Map.Entry<String, String> field : displayNames.entrySet()) {
re += String.format("<a class=\"panel-block\">%s: %s</a>\n",
field.getValue(), data.query(field.getKey()));
field.getValue(), getValue(data, field.getKey()));
}
re += "</article>\n";
return re;
Expand All @@ -79,11 +86,25 @@ public String generate(List<JSONObject> data) {
for (JSONObject line : data) {
re += "<tr>\n";
for (Map.Entry<String, String> field : displayNames.entrySet()) {
re += String.format("<td>%s</td>\n", line.query(field.getKey()));
re += String.format("<td>%s</td>\n", getValue(line, field.getKey()));
}
re += "</tr>\n";
}
re += "</tbody></table></a></article>\n";
return re;
}

private String getValue(JSONObject data, String key) {
Object re = data.query(key);
TemplateMethodModelEx mapper = propertyMappers.get(key);
if (mapper != null) {
try {
re = mapper.exec(Collections.singletonList(re));
} catch (TemplateModelException e) {
throw new RuntimeException(e);
//TODO
}
}
return String.valueOf(re);
}
}
21 changes: 11 additions & 10 deletions src/main/java/koko/yayu/config/FreeMarkerConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.HashMap;
import java.util.Map;

import freemarker.template.TemplateMethodModelEx;
import koko.yayu.freemarker.AppStateMethod;
import koko.yayu.freemarker.DetailsMethod;
import koko.yayu.freemarker.LinkTagMethod;
Expand All @@ -16,20 +17,20 @@
@Configuration
public class FreeMarkerConfig {

public static final Map<String, Object> methods = Map.of(
"time", new TimeMethod(),
"progress", new ProgressMethod(),
"tableHead", new TableHeadMethod(),
"linkTag", new LinkTagMethod(),
"details", new DetailsMethod(),
"appState", new AppStateMethod()
);

@Bean
public FreeMarkerConfigurer freeMarkerConfigurer() {
FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
configurer.setTemplateLoaderPath("classpath:/templates");

Map<String, Object> variables = new HashMap<>();
variables.put("time", new TimeMethod());
variables.put("progress", new ProgressMethod());
variables.put("tableHead", new TableHeadMethod());
variables.put("linkTag", new LinkTagMethod());
variables.put("details", new DetailsMethod());
variables.put("appState", new AppStateMethod());
configurer.setFreemarkerVariables(variables);

configurer.setFreemarkerVariables(methods);
return configurer;
}
}
4 changes: 2 additions & 2 deletions src/main/java/koko/yayu/controller/AppController.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public String appDetails(Model model, @PathVariable String appId) {
.addField( "/user")
.addField( "/state")
.addField( "/finalStatus")
.addField( "/startedTime")
.addField( "/finishedTime")
.addField( "/startedTime", "Start", "time")
.addField( "/finishedTime", "End", "time")
.generate(resp)
);

Expand Down

0 comments on commit d3dd170

Please sign in to comment.