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

org.elasticsearch.client.slm.SnapshotLifecycleStats Maven / Gradle / Ivy

There is a newer version: 8.0.0-alpha2
Show newest version
/*
 * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
 * or more contributor license agreements. Licensed under the Elastic License
 * 2.0 and the Server Side Public License, v 1; you may not use this file except
 * in compliance with, at your election, the Elastic License 2.0 or the Server
 * Side Public License, v 1.
 */

package org.elasticsearch.client.slm;

import org.elasticsearch.common.Strings;
import org.elasticsearch.core.TimeValue;
import org.elasticsearch.xcontent.ConstructingObjectParser;
import org.elasticsearch.xcontent.ParseField;
import org.elasticsearch.xcontent.ToXContentFragment;
import org.elasticsearch.xcontent.ToXContentObject;
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xcontent.XContentParser;

import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

public class SnapshotLifecycleStats implements ToXContentObject {

    private final long retentionRunCount;
    private final long retentionFailedCount;
    private final long retentionTimedOut;
    private final long retentionTimeMs;
    private final Map policyStats;

    public static final ParseField RETENTION_RUNS = new ParseField("retention_runs");
    public static final ParseField RETENTION_FAILED = new ParseField("retention_failed");
    public static final ParseField RETENTION_TIMED_OUT = new ParseField("retention_timed_out");
    public static final ParseField RETENTION_TIME = new ParseField("retention_deletion_time");
    public static final ParseField RETENTION_TIME_MILLIS = new ParseField("retention_deletion_time_millis");
    public static final ParseField POLICY_STATS = new ParseField("policy_stats");
    public static final ParseField TOTAL_TAKEN = new ParseField("total_snapshots_taken");
    public static final ParseField TOTAL_FAILED = new ParseField("total_snapshots_failed");
    public static final ParseField TOTAL_DELETIONS = new ParseField("total_snapshots_deleted");
    public static final ParseField TOTAL_DELETION_FAILURES = new ParseField("total_snapshot_deletion_failures");

    @SuppressWarnings("unchecked")
    private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>(
        "snapshot_policy_stats",
        true,
        a -> {
            long runs = (long) a[0];
            long failed = (long) a[1];
            long timedOut = (long) a[2];
            long timeMs = (long) a[3];
            Map policyStatsMap = ((List) a[4]).stream()
                .collect(Collectors.toMap(m -> m.policyId, Function.identity()));
            return new SnapshotLifecycleStats(runs, failed, timedOut, timeMs, policyStatsMap);
        }
    );

    static {
        PARSER.declareLong(ConstructingObjectParser.constructorArg(), RETENTION_RUNS);
        PARSER.declareLong(ConstructingObjectParser.constructorArg(), RETENTION_FAILED);
        PARSER.declareLong(ConstructingObjectParser.constructorArg(), RETENTION_TIMED_OUT);
        PARSER.declareLong(ConstructingObjectParser.constructorArg(), RETENTION_TIME_MILLIS);
        PARSER.declareObjectArray(ConstructingObjectParser.constructorArg(), SnapshotPolicyStats.PARSER, POLICY_STATS);
    }

    // Package visible for testing
    private SnapshotLifecycleStats(
        long retentionRuns,
        long retentionFailed,
        long retentionTimedOut,
        long retentionTimeMs,
        Map policyStats
    ) {
        this.retentionRunCount = retentionRuns;
        this.retentionFailedCount = retentionFailed;
        this.retentionTimedOut = retentionTimedOut;
        this.retentionTimeMs = retentionTimeMs;
        this.policyStats = policyStats;
    }

    public static SnapshotLifecycleStats parse(XContentParser parser) {
        return PARSER.apply(parser, null);
    }

    public long getRetentionRunCount() {
        return retentionRunCount;
    }

    public long getRetentionFailedCount() {
        return retentionFailedCount;
    }

    public long getRetentionTimedOut() {
        return retentionTimedOut;
    }

    public long getRetentionTimeMillis() {
        return retentionTimeMs;
    }

    /**
     * @return a map of per-policy stats for each SLM policy
     */
    public Map getMetrics() {
        return Collections.unmodifiableMap(this.policyStats);
    }

    @Override
    public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
        builder.startObject();
        builder.field(RETENTION_RUNS.getPreferredName(), this.retentionRunCount);
        builder.field(RETENTION_FAILED.getPreferredName(), this.retentionFailedCount);
        builder.field(RETENTION_TIMED_OUT.getPreferredName(), this.retentionTimedOut);
        TimeValue retentionTime = TimeValue.timeValueMillis(this.retentionTimeMs);
        builder.field(RETENTION_TIME.getPreferredName(), retentionTime);
        builder.field(RETENTION_TIME_MILLIS.getPreferredName(), retentionTime.millis());

        Map metrics = getMetrics();
        long totalTaken = metrics.values().stream().mapToLong(s -> s.snapshotsTaken).sum();
        long totalFailed = metrics.values().stream().mapToLong(s -> s.snapshotsFailed).sum();
        long totalDeleted = metrics.values().stream().mapToLong(s -> s.snapshotsDeleted).sum();
        long totalDeleteFailures = metrics.values().stream().mapToLong(s -> s.snapshotDeleteFailures).sum();
        builder.field(TOTAL_TAKEN.getPreferredName(), totalTaken);
        builder.field(TOTAL_FAILED.getPreferredName(), totalFailed);
        builder.field(TOTAL_DELETIONS.getPreferredName(), totalDeleted);
        builder.field(TOTAL_DELETION_FAILURES.getPreferredName(), totalDeleteFailures);
        builder.startObject(POLICY_STATS.getPreferredName());
        for (Map.Entry policy : metrics.entrySet()) {
            SnapshotPolicyStats perPolicyMetrics = policy.getValue();
            builder.startObject(perPolicyMetrics.policyId);
            perPolicyMetrics.toXContent(builder, params);
            builder.endObject();
        }
        builder.endObject();
        builder.endObject();
        return builder;
    }

    @Override
    public int hashCode() {
        return Objects.hash(retentionRunCount, retentionFailedCount, retentionTimedOut, retentionTimeMs, policyStats);
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (obj.getClass() != getClass()) {
            return false;
        }
        SnapshotLifecycleStats other = (SnapshotLifecycleStats) obj;
        return retentionRunCount == other.retentionRunCount
            && retentionFailedCount == other.retentionFailedCount
            && retentionTimedOut == other.retentionTimedOut
            && retentionTimeMs == other.retentionTimeMs
            && Objects.equals(policyStats, other.policyStats);
    }

    @Override
    public String toString() {
        return Strings.toString(this);
    }

    public static class SnapshotPolicyStats implements ToXContentFragment {
        private final String policyId;
        private final long snapshotsTaken;
        private final long snapshotsFailed;
        private final long snapshotsDeleted;
        private final long snapshotDeleteFailures;

        public static final ParseField POLICY_ID = new ParseField("policy");
        static final ParseField SNAPSHOTS_TAKEN = new ParseField("snapshots_taken");
        static final ParseField SNAPSHOTS_FAILED = new ParseField("snapshots_failed");
        static final ParseField SNAPSHOTS_DELETED = new ParseField("snapshots_deleted");
        static final ParseField SNAPSHOT_DELETION_FAILURES = new ParseField("snapshot_deletion_failures");

        private static final ConstructingObjectParser PARSER = new ConstructingObjectParser<>(
            "snapshot_policy_stats",
            true,
            a -> {
                String id = (String) a[0];
                long taken = (long) a[1];
                long failed = (long) a[2];
                long deleted = (long) a[3];
                long deleteFailed = (long) a[4];
                return new SnapshotPolicyStats(id, taken, failed, deleted, deleteFailed);
            }
        );

        static {
            PARSER.declareString(ConstructingObjectParser.constructorArg(), POLICY_ID);
            PARSER.declareLong(ConstructingObjectParser.constructorArg(), SNAPSHOTS_TAKEN);
            PARSER.declareLong(ConstructingObjectParser.constructorArg(), SNAPSHOTS_FAILED);
            PARSER.declareLong(ConstructingObjectParser.constructorArg(), SNAPSHOTS_DELETED);
            PARSER.declareLong(ConstructingObjectParser.constructorArg(), SNAPSHOT_DELETION_FAILURES);
        }

        public SnapshotPolicyStats(String policyId, long snapshotsTaken, long snapshotsFailed, long deleted, long failedDeletes) {
            this.policyId = policyId;
            this.snapshotsTaken = snapshotsTaken;
            this.snapshotsFailed = snapshotsFailed;
            this.snapshotsDeleted = deleted;
            this.snapshotDeleteFailures = failedDeletes;
        }

        public static SnapshotPolicyStats parse(XContentParser parser, String policyId) {
            return PARSER.apply(parser, null);
        }

        public String getPolicyId() {
            return policyId;
        }

        public long getSnapshotsTaken() {
            return snapshotsTaken;
        }

        public long getSnapshotsFailed() {
            return snapshotsFailed;
        }

        public long getSnapshotsDeleted() {
            return snapshotsDeleted;
        }

        public long getSnapshotDeleteFailures() {
            return snapshotDeleteFailures;
        }

        @Override
        public int hashCode() {
            return Objects.hash(policyId, snapshotsTaken, snapshotsFailed, snapshotsDeleted, snapshotDeleteFailures);
        }

        @Override
        public boolean equals(Object obj) {
            if (obj == null) {
                return false;
            }
            if (obj.getClass() != getClass()) {
                return false;
            }
            SnapshotPolicyStats other = (SnapshotPolicyStats) obj;
            return Objects.equals(policyId, other.policyId)
                && snapshotsTaken == other.snapshotsTaken
                && snapshotsFailed == other.snapshotsFailed
                && snapshotsDeleted == other.snapshotsDeleted
                && snapshotDeleteFailures == other.snapshotDeleteFailures;
        }

        @Override
        public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
            builder.field(SnapshotPolicyStats.SNAPSHOTS_TAKEN.getPreferredName(), snapshotsTaken);
            builder.field(SnapshotPolicyStats.SNAPSHOTS_FAILED.getPreferredName(), snapshotsFailed);
            builder.field(SnapshotPolicyStats.SNAPSHOTS_DELETED.getPreferredName(), snapshotsDeleted);
            builder.field(SnapshotPolicyStats.SNAPSHOT_DELETION_FAILURES.getPreferredName(), snapshotDeleteFailures);
            return builder;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy