All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.graylog2.restclient.models.Index Maven / Gradle / Ivy

/**
 * 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 .
 */
package org.graylog2.restclient.models;

import com.google.inject.assistedinject.Assisted;
import com.google.inject.assistedinject.AssistedInject;
import org.graylog2.rest.models.system.indexer.responses.IndexRangeSummary;
import org.graylog2.restclient.lib.ApiClient;
import org.graylog2.restclient.models.api.responses.system.indices.IndexShardsResponse;
import org.graylog2.restclient.models.api.responses.system.indices.IndexSummaryResponse;
import org.graylog2.restclient.models.api.responses.system.indices.ShardDocumentsResponse;
import org.graylog2.restclient.models.api.responses.system.indices.ShardMeterResponse;
import org.graylog2.restclient.models.api.responses.system.indices.ShardRoutingResponse;
import org.graylog2.restroutes.generated.routes;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

public class Index {
    private static final Logger LOG = LoggerFactory.getLogger(Index.class);

    public interface Factory {
        Index fromRangeResponse(IndexRangeSummary ir);
    }

    private final ApiClient api;
    private final Range range;
    private final String name;

    private Info indexInfo;

    @AssistedInject
    public Index(ApiClient api, @Assisted IndexRangeSummary ir) {
        this.api = api;

        this.range = new Range(ir);
        this.name = ir.indexName();
    }

    public Range getRange() {
        return range;
    }

    public String getName() {
        return name;
    }

    public int getNumber() {
        try {
            return Integer.parseInt(getName().substring(getName().lastIndexOf("_") + 1));
        } catch (Exception e) {
            LOG.error("Could not get number of index [" + getName() + "].", e);
            return -1;
        }
    }

    public Info getInfo() {
        if (indexInfo == null) {
            loadIndexInfo();
        }

        return indexInfo;
    }

    private void loadIndexInfo() {
        try {
            this.indexInfo = new Info(api.path(routes.IndicesResource().single(getName()), IndexSummaryResponse.class).execute());
        } catch (Exception e) {
            LOG.error("Could not get index information for index [" + getName() + "]", e);
        }
    }

    public static class Info {

        private final int openSearchContexts;
        private final long storeSizeBytes;
        private final long segments;

        private final boolean isReopened;

        private final ShardDocumentsResponse documents;
        private final ShardMeter primaryShards;
        private final ShardMeter allShards;
        private final List shardRouting;

        public Info(IndexSummaryResponse i) {
            this.primaryShards = new ShardMeter(i.primaryShards);
            this.allShards = new ShardMeter(i.allShards);

            IndexShardsResponse primaries = i.primaryShards;
            this.openSearchContexts = primaries.openSearchContexts;
            this.storeSizeBytes = primaries.storeSizeBytes;
            this.segments = primaries.segments;
            this.documents = primaries.documents;

            this.shardRouting = i.routing;

            this.isReopened = i.isReopened;
        }

        public int getOpenSearchContexts() {
            return openSearchContexts;
        }

        public long getStoreSizeBytes() {
            return storeSizeBytes;
        }

        public long getSegments() {
            return segments;
        }

        public List getShardRouting() {
            return shardRouting;
        }

        public ShardDocumentsResponse getDocuments() {
            return documents;
        }

        public ShardMeter getPrimaryShards() {
            return primaryShards;
        }

        public ShardMeter getAllShards() {
            return allShards;
        }

        public boolean isReopened() {
            return isReopened;
        }

        public static class ShardMeter {

            private final ShardMeterResponse indexMeter;
            private final ShardMeterResponse flushMeter;
            private final ShardMeterResponse getMeter;
            private final ShardMeterResponse mergeMeter;
            private final ShardMeterResponse searchFetchMeter;
            private final ShardMeterResponse searchQueryMeter;
            private final ShardMeterResponse refreshMeter;

            public ShardMeter(IndexShardsResponse shards) {
                this.indexMeter = shards.index;
                this.flushMeter = shards.flush;
                this.getMeter = shards.get;
                this.mergeMeter = shards.merge;
                this.searchFetchMeter = shards.searchFetch;
                this.searchQueryMeter = shards.searchQuery;
                this.refreshMeter = shards.refresh;
            }

            public ShardMeterResponse getIndexMeter() {
                return indexMeter;
            }

            public ShardMeterResponse getFlushMeter() {
                return flushMeter;
            }

            public ShardMeterResponse getGetMeter() {
                return getMeter;
            }

            public ShardMeterResponse getMergeMeter() {
                return mergeMeter;
            }

            public ShardMeterResponse getSearchFetchMeter() {
                return searchFetchMeter;
            }

            public ShardMeterResponse getSearchQueryMeter() {
                return searchQueryMeter;
            }

            public ShardMeterResponse getRefreshMeter() {
                return refreshMeter;
            }

        }
    }

    public static class Range {

        private final DateTime begin;
        private final DateTime end;
        private final long calculationTookMs;
        private final DateTime calculatedAt;

        public Range(IndexRangeSummary ir) {
            this.begin = ir.begin();
            this.end = ir.end();
                this.calculationTookMs = ir.calculationTookMs();
                this.calculatedAt = ir.calculatedAt();
        }

        public DateTime getBegin() {
            return begin;
        }

        public DateTime getEnd() {
            return end;
        }

        public boolean isProvidesCalculationInfo() {
            return true;
        }

        public long getCalculationTookMs() {
            return calculationTookMs;
        }

        public DateTime getCalculatedAt() {
            return calculatedAt;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy