-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added an endpoint to retrieve data #174
- Loading branch information
Showing
2 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
ui/src/main/java/org/moskito/control/ui/restapi/datarepository/DataEntityBean.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package org.moskito.control.ui.restapi.datarepository; | ||
|
||
import java.util.LinkedList; | ||
import java.util.List; | ||
|
||
public class DataEntityBean { | ||
private String name; | ||
private String value; | ||
private List<String> formulas = new LinkedList<>(); | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getValue() { | ||
return value; | ||
} | ||
|
||
public void setValue(String value) { | ||
this.value = value; | ||
} | ||
|
||
public List<String> getFormulas() { | ||
return formulas; | ||
} | ||
|
||
public void setFormulas(List<String> formulas) { | ||
this.formulas = formulas; | ||
} | ||
|
||
public void addFormula(String formula){ | ||
formulas.add(formula); | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
ui/src/main/java/org/moskito/control/ui/restapi/datarepository/DataRepositoryResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package org.moskito.control.ui.restapi.datarepository; | ||
|
||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.media.ArraySchema; | ||
import io.swagger.v3.oas.annotations.media.Content; | ||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
import io.swagger.v3.oas.annotations.servers.Server; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.Path; | ||
import jakarta.ws.rs.Produces; | ||
import jakarta.ws.rs.core.MediaType; | ||
import net.anotheria.util.StringUtils; | ||
import org.moskito.control.config.MoskitoControlConfiguration; | ||
import org.moskito.control.config.datarepository.RetrieverInstanceConfig; | ||
import org.moskito.control.config.datarepository.VariableMapping; | ||
import org.moskito.control.data.DataRepository; | ||
import org.moskito.control.ui.restapi.ReplyObject; | ||
import org.moskito.control.ui.restapi.control.ThresholdBean; | ||
|
||
import java.util.*; | ||
|
||
@Path("datarepository") | ||
@Produces(MediaType.APPLICATION_JSON) | ||
@Server(url = "/api/v2") | ||
@Tag(name = "Data Repository API", description = "API for data repository access.") | ||
/** | ||
* This class is responsible for handling methods for datarepository inspection. | ||
*/ | ||
|
||
public class DataRepositoryResource { | ||
@Path("store") | ||
@GET | ||
@Operation(summary = "Returns all formulas and values for the connected component", | ||
description = "Returns all calculated formulas, retrievable value and current value of each entity." | ||
) | ||
@ApiResponse(description = "Data Entities as list named 'data'", | ||
content = @Content(array = @ArraySchema(schema = @Schema(implementation = DataEntityBean.class)) | ||
)) | ||
|
||
public ReplyObject getStore(){ | ||
MoskitoControlConfiguration config = MoskitoControlConfiguration.getConfiguration(); | ||
HashMap<String, DataEntityBean> beans = new HashMap<>(); | ||
|
||
//Set formulas for beans. | ||
for (String processingLine : config.getDataprocessing().getProcessing()) { | ||
String tokens[] = StringUtils.tokenize(processingLine, ' '); | ||
if (tokens.length > 1) { | ||
String variableName = tokens[1]; | ||
DataEntityBean bean = new DataEntityBean(); | ||
bean.setName(variableName); | ||
if (!beans.containsKey(variableName)) { | ||
beans.put(variableName, bean); | ||
} | ||
beans.get(variableName).addFormula(processingLine); | ||
} | ||
} | ||
|
||
Map<String, String> retrieverVariableNames = new HashMap<>(); | ||
|
||
for (RetrieverInstanceConfig retriever : config.getDataprocessing().getRetrievers()) { | ||
for (VariableMapping mapping : retriever.getMappings()) { | ||
retrieverVariableNames.put(mapping.getVariableName(), mapping.getExpression()); | ||
} | ||
} | ||
|
||
//Set directly set values. | ||
for (String key : DataRepository.getInstance().getData().keySet()) { | ||
if (!beans.containsKey(key)) { | ||
DataEntityBean bean = new DataEntityBean(); | ||
bean.setName(key); | ||
bean.addFormula(retrieverVariableNames.get(key)); | ||
} | ||
} | ||
|
||
//Now set all the values for prepared beans. | ||
DataRepository.getInstance().getData().forEach((key, value) -> { | ||
if (beans.containsKey(key)) { | ||
beans.get(key).setValue(value); | ||
} | ||
}); | ||
|
||
return ReplyObject.success("data", beans.values() ); | ||
|
||
} | ||
} |