Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (C) 2020 Graylog, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* .
*/
package org.graylog2.rest.resources.system;
import com.codahale.metrics.annotation.Timed;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.graylog2.database.MongoConnection;
import org.graylog2.shared.rest.resources.RestResource;
import org.graylog2.shared.security.RestPermissions;
import javax.inject.Inject;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;
@RequiresAuthentication
@Api(value = "System/Metrics/History", description = "Get history of metrics")
@Path("/system/metrics/{metricName}/history")
@Produces(MediaType.APPLICATION_JSON)
public class MetricsHistoryResource extends RestResource {
private final MongoConnection mongoConnection;
@Inject
public MetricsHistoryResource(MongoConnection mongoConnection) {
this.mongoConnection = mongoConnection;
}
enum MetricType {
GAUGE,
COUNTER,
HISTOGRAM,
METER,
TIMER
}
@GET
@Timed
@ApiOperation(value = "Get history of a single metric", notes = "The maximum retention time is currently only 5 minutes.")
public Map historicSingleMetric(
@ApiParam(name = "metricName", required = true)
@PathParam("metricName") String metricName,
@ApiParam(name = "after", value = "Only values for after this UTC timestamp (1970 epoch)")
@QueryParam("after") @DefaultValue("-1") long after
) {
checkPermission(RestPermissions.METRICS_READHISTORY, metricName);
BasicDBObject andQuery = new BasicDBObject();
final List obj = new ArrayList();
obj.add(new BasicDBObject("name", metricName));
if (after != -1) {
obj.add(new BasicDBObject("$gt", new BasicDBObject("$gt", new Date(after))));
}
andQuery.put("$and", obj);
final DBCollection dbCollection = mongoConnection.getDatabase().getCollection("graylog2_metrics");
try(DBCursor cursor = dbCollection.find(andQuery).sort(new BasicDBObject("timestamp", 1))) {
final Map metricsData = Maps.newHashMap();
metricsData.put("name", metricName);
final List