Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show stats for each index set on the index sets overview page #3322

Merged
merged 1 commit into from Jan 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,49 @@
/**
* This file is part of Graylog.
*
* Graylog is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Graylog 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
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Graylog. If not, see <http://www.gnu.org/licenses/>.
*/
package org.graylog2.indexer;

import org.elasticsearch.action.admin.indices.stats.IndexStats;
import org.graylog2.indexer.indices.Indices;
import org.graylog2.rest.resources.system.indexer.responses.IndexSetStats;

import javax.inject.Inject;
import java.util.Map;
import java.util.Set;

public class IndexSetStatsCreator {
private final Indices indices;

@Inject
public IndexSetStatsCreator(final Indices indices) {
this.indices = indices;
}

public IndexSetStats getForIndexSet(final IndexSet indexSet) {
final Map<String, IndexStats> docCounts = indices.getAllDocCounts(indexSet);
final Set<String> closedIndices = indices.getClosedIndices(indexSet);
final long documents = docCounts.values()
.stream()
.mapToLong(indexStats -> indexStats.getPrimaries().getDocs().getCount())
.sum();
final long size = docCounts.values()
.stream()
.mapToLong(indexStats -> indexStats.getPrimaries().getStore().sizeInBytes())
.sum();

return IndexSetStats.create(docCounts.size() + closedIndices.size(), documents, size);
}
}
Expand Up @@ -29,6 +29,7 @@
import org.graylog2.audit.jersey.AuditEvent;
import org.graylog2.indexer.IndexSet;
import org.graylog2.indexer.IndexSetRegistry;
import org.graylog2.indexer.IndexSetStatsCreator;
import org.graylog2.indexer.IndexSetValidator;
import org.graylog2.indexer.indexset.DefaultIndexSetConfig;
import org.graylog2.indexer.indexset.IndexSetConfig;
Expand All @@ -37,6 +38,7 @@
import org.graylog2.plugin.cluster.ClusterConfigService;
import org.graylog2.rest.resources.system.indexer.requests.IndexSetUpdateRequest;
import org.graylog2.rest.resources.system.indexer.responses.IndexSetResponse;
import org.graylog2.rest.resources.system.indexer.responses.IndexSetStats;
import org.graylog2.rest.resources.system.indexer.responses.IndexSetSummary;
import org.graylog2.shared.rest.resources.RestResource;
import org.graylog2.shared.security.RestPermissions;
Expand All @@ -63,7 +65,9 @@
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
Expand All @@ -81,6 +85,7 @@ public class IndexSetsResource extends RestResource {
private final IndexSetRegistry indexSetRegistry;
private final IndexSetValidator indexSetValidator;
private final IndexSetCleanupJob.Factory indexSetCleanupJobFactory;
private final IndexSetStatsCreator indexSetStatsCreator;
private final ClusterConfigService clusterConfigService;
private final SystemJobManager systemJobManager;

Expand All @@ -89,12 +94,14 @@ public IndexSetsResource(final IndexSetService indexSetService,
final IndexSetRegistry indexSetRegistry,
final IndexSetValidator indexSetValidator,
final IndexSetCleanupJob.Factory indexSetCleanupJobFactory,
final IndexSetStatsCreator indexSetStatsCreator,
final ClusterConfigService clusterConfigService,
final SystemJobManager systemJobManager) {
this.indexSetService = requireNonNull(indexSetService);
this.indexSetRegistry = indexSetRegistry;
this.indexSetValidator = indexSetValidator;
this.indexSetCleanupJobFactory = requireNonNull(indexSetCleanupJobFactory);
this.indexSetStatsCreator = indexSetStatsCreator;
this.clusterConfigService = clusterConfigService;
this.systemJobManager = systemJobManager;
}
Expand All @@ -108,7 +115,9 @@ public IndexSetsResource(final IndexSetService indexSetService,
public IndexSetResponse list(@ApiParam(name = "skip", value = "The number of elements to skip (offset).", required = true)
@QueryParam("skip") @DefaultValue("0") int skip,
@ApiParam(name = "limit", value = "The maximum number of elements to return.", required = true)
@QueryParam("limit") @DefaultValue("0") int limit) {
@QueryParam("limit") @DefaultValue("0") int limit,
@ApiParam(name = "stats", value = "Include index set stats.")
@QueryParam("stats") @DefaultValue("false") boolean computeStats) {
final IndexSetConfig defaultIndexSet = indexSetService.getDefault();

List<IndexSetSummary> indexSets;
Expand All @@ -133,7 +142,15 @@ public IndexSetResponse list(@ApiParam(name = "skip", value = "The number of ele
count = indexSets.size();
}

return IndexSetResponse.create(count, indexSets);
final Map<String, IndexSetStats> stats;
if (computeStats) {
stats = indexSetRegistry.getAllIndexSets().stream()
.collect(Collectors.toMap(indexSet -> indexSet.getConfig().id(), indexSetStatsCreator::getForIndexSet));
} else {
stats = Collections.emptyMap();
}

return IndexSetResponse.create(count, indexSets, stats);
}

@GET
Expand Down
Expand Up @@ -23,7 +23,7 @@
import org.graylog.autovalue.WithBeanGetter;

import java.util.List;
import java.util.Set;
import java.util.Map;

@JsonAutoDetect
@AutoValue
Expand All @@ -35,9 +35,13 @@ public abstract class IndexSetResponse {
@JsonProperty("index_sets")
public abstract List<IndexSetSummary> indexSets();

@JsonProperty("stats")
public abstract Map<String, IndexSetStats> stats();

@JsonCreator
public static IndexSetResponse create(@JsonProperty("total") int total,
@JsonProperty("index_sets") List<IndexSetSummary> ranges) {
return new AutoValue_IndexSetResponse(total, ranges);
@JsonProperty("index_sets") List<IndexSetSummary> ranges,
@JsonProperty("stats") Map<String, IndexSetStats> stats) {
return new AutoValue_IndexSetResponse(total, ranges, stats);
}
}
@@ -0,0 +1,46 @@
/**
* This file is part of Graylog.
*
* Graylog is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Graylog 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
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Graylog. If not, see <http://www.gnu.org/licenses/>.
*/
package org.graylog2.rest.resources.system.indexer.responses;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.auto.value.AutoValue;

@JsonAutoDetect
@AutoValue
public abstract class IndexSetStats {
private static final String FIELD_INDICES = "indices";
private static final String FIELD_DOCUMENTS = "documents";
private static final String FIELD_SIZE = "size";

@JsonProperty(FIELD_INDICES)
public abstract long indices();

@JsonProperty(FIELD_DOCUMENTS)
public abstract long documents();

@JsonProperty(FIELD_SIZE)
public abstract long size();

@JsonCreator
public static IndexSetStats create(@JsonProperty(FIELD_INDICES) long indices,
@JsonProperty(FIELD_DOCUMENTS) long documents,
@JsonProperty(FIELD_SIZE) long size) {
return new AutoValue_IndexSetStats(indices, documents, size);
}
}