Skip to content

Commit

Permalink
preparing query_range API
Browse files Browse the repository at this point in the history
  • Loading branch information
matjaz99 committed Jan 21, 2022
1 parent d59b06e commit cc1c7e9
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
## 2.1.1-SNAPSHOT

## 2.1.0 / 2022-01-21

* [CHANGE] Journal view link removed from toolbar and moved to Statistics view.
Expand Down
5 changes: 3 additions & 2 deletions docs/Todo.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
- show targets by job
- configure labels for CID (if needed also from external_labels)
- store alarms in MongoDB

- function time_of_max
- downsample - from 1 min (ie 4 metrics) create 1 metric


https://www.primefaces.org/showcase/ui/data/timeline/basic.xhtml
Expand All @@ -20,7 +21,7 @@ https://prom.devops.iskratel.cloud/prometheus/api/v1/query?query=ALERTS
https://prom.devops.iskratel.cloud/prometheus/api/v1/alerts


https://www.primefaces.org/showcase/ui/data/datatable/columns.xhtml
https://www.primefaces.org/showcase



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
public class PQueryResult {

private Map<String, String> metric;
private Object[] value;
private List<Object[]> values;
private Object[] value; // timestamp-value pair
private List<Object[]> values; // timestamp-value pair

public Map<String, String> getMetric() {
return metric;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,34 @@ public PQueryMessage query(String query) throws PrometheusApiException {

}

/**
* Execute query_range
* @param query - the query
* @param start - start time in seconds (UNIX time)
* @param end - end time in seconds (UNIX time)
* @param step - eg. 5m
*/
public void queryRange(String query, long start, long end, String step) {


RequestBody formBody = new FormBody.Builder()
// add url encoded parameters
.add("query", query)
.add("start", String.valueOf(start))
.add("end", String.valueOf(end))
.add("step", step)
.build();

Request request = new Request.Builder()
.url(DAO.ALERTMONITOR_PROMETHEUS_SERVER + "/api/v1/query_range")
.addHeader("User-Agent", HTTP_CLIENT_USER_AGENT)
.addHeader("Content-Type", "application/x-www-form-urlencoded")
.post(formBody)
.build();

// TODO
}

public List<PAlert> alerts() throws PrometheusApiException {

Request request = new Request.Builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import javax.faces.bean.SessionScoped;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -103,18 +104,21 @@ public void executeQuery() {
}
}

// TODO this is a test method!
public void doMySpecialFunction() {
queryResult = null;
result = null;

PrometheusApi api = new PrometheusApi();
try {
// syntax: time_of_max(alertmonitor_active_alerts_count[24h])
query = query.replace("time_of_max(", "");
query = query.substring(0, query.length() - 1);
DAO.getLogger().info("QUERY: " + query);
//eg. time_of_max(alertmonitor_active_alerts_count[3h])

PQueryMessage msg = api.query(query);
String tempQuery = query.replace("time_of_max(", "");
tempQuery = tempQuery.substring(0, tempQuery.length() - 1);
DAO.getLogger().info("QUERY: " + tempQuery);

PQueryMessage msg = api.query(tempQuery);

if (msg.getErrorType() != null) {
result = msg.getErrorType() + ": " + msg.getError();
Expand All @@ -130,8 +134,21 @@ public void doMySpecialFunction() {
return;
}


for (PQueryResult r : queryResult) {
DAO.getLogger().debug("executeQuery: " + r.toString());
// find max value
double maxVal = 0;
for (Object[] oArray : r.getValues()) {
double d = Double.parseDouble(oArray[1].toString());
if (d > maxVal) maxVal = d;
}
// delete those which are less than max
for (Iterator<Object[]> it = r.getValues().iterator(); it.hasNext(); ) {
double d = Double.parseDouble(it.next()[1].toString());
if (d < maxVal) it.remove();
}
// fix the metric name
r.getMetric().put("__name__", "time_of_max(" + r.getMetric().get("__name__") + ")");
}

} catch (PrometheusApiException e) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/webapp/WEB-INF/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.1.0
2.1.1-SNAPSHOT

0 comments on commit cc1c7e9

Please sign in to comment.