|
5 | 5 | */
|
6 | 6 | package org.elasticsearch.xpack.core.indexlifecycle;
|
7 | 7 |
|
| 8 | +import org.elasticsearch.common.ParseField; |
| 9 | +import org.elasticsearch.common.Strings; |
8 | 10 | import org.elasticsearch.common.io.stream.StreamInput;
|
| 11 | +import org.elasticsearch.common.io.stream.StreamOutput; |
| 12 | +import org.elasticsearch.common.io.stream.Writeable; |
| 13 | +import org.elasticsearch.common.unit.TimeValue; |
| 14 | +import org.elasticsearch.common.xcontent.ToXContentObject; |
| 15 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
9 | 16 | import org.elasticsearch.xpack.core.XPackFeatureSet;
|
10 | 17 | import org.elasticsearch.xpack.core.XPackField;
|
11 | 18 |
|
12 | 19 | import java.io.IOException;
|
| 20 | +import java.util.Arrays; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.Objects; |
13 | 24 |
|
14 | 25 | public class IndexLifecycleFeatureSetUsage extends XPackFeatureSet.Usage {
|
15 | 26 |
|
| 27 | + private List<PolicyStats> policyStats; |
| 28 | + |
16 | 29 | public IndexLifecycleFeatureSetUsage(StreamInput input) throws IOException {
|
17 | 30 | super(input);
|
| 31 | + if (input.readBoolean()) { |
| 32 | + policyStats = input.readList(PolicyStats::new); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + public void writeTo(StreamOutput out) throws IOException { |
| 38 | + super.writeTo(out); |
| 39 | + boolean hasPolicyStats = policyStats != null; |
| 40 | + out.writeBoolean(hasPolicyStats); |
| 41 | + if (hasPolicyStats) { |
| 42 | + out.writeList(policyStats); |
| 43 | + } |
18 | 44 | }
|
19 | 45 |
|
20 | 46 | public IndexLifecycleFeatureSetUsage(boolean available, boolean enabled) {
|
| 47 | + this(available, enabled, null); |
| 48 | + } |
| 49 | + |
| 50 | + public IndexLifecycleFeatureSetUsage(boolean available, boolean enabled, List<PolicyStats> policyStats) { |
21 | 51 | super(XPackField.INDEX_LIFECYCLE, available, enabled);
|
| 52 | + this.policyStats = policyStats; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + protected void innerXContent(XContentBuilder builder, Params params) throws IOException { |
| 57 | + if (policyStats != null) { |
| 58 | + builder.field("policy_count", policyStats.size()); |
| 59 | + builder.field("policy_stats", policyStats); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + public List<PolicyStats> getPolicyStats() { |
| 64 | + return policyStats; |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public int hashCode() { |
| 69 | + return Objects.hash(available, enabled, policyStats); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public boolean equals(Object obj) { |
| 74 | + if (obj == null) { |
| 75 | + return false; |
| 76 | + } |
| 77 | + if (getClass() != obj.getClass()) { |
| 78 | + return false; |
| 79 | + } |
| 80 | + IndexLifecycleFeatureSetUsage other = (IndexLifecycleFeatureSetUsage) obj; |
| 81 | + return Objects.equals(available, other.available) && |
| 82 | + Objects.equals(enabled, other.enabled) && |
| 83 | + Objects.equals(policyStats, other.policyStats); |
| 84 | + } |
| 85 | + |
| 86 | + public static final class PolicyStats implements ToXContentObject, Writeable { |
| 87 | + |
| 88 | + public static final ParseField INDICES_MANAGED_FIELD = new ParseField("indices_managed"); |
| 89 | + |
| 90 | + private final Map<String, PhaseStats> phaseStats; |
| 91 | + private final int indicesManaged; |
| 92 | + |
| 93 | + public PolicyStats(Map<String, PhaseStats> phaseStats, int numberIndicesManaged) { |
| 94 | + this.phaseStats = phaseStats; |
| 95 | + this.indicesManaged = numberIndicesManaged; |
| 96 | + } |
| 97 | + |
| 98 | + public PolicyStats(StreamInput in) throws IOException { |
| 99 | + this.phaseStats = in.readMap(StreamInput::readString, PhaseStats::new); |
| 100 | + this.indicesManaged = in.readVInt(); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public void writeTo(StreamOutput out) throws IOException { |
| 105 | + out.writeMap(phaseStats, StreamOutput::writeString, (o, p) -> p.writeTo(o)); |
| 106 | + out.writeVInt(indicesManaged); |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { |
| 111 | + builder.startObject(); |
| 112 | + builder.field(LifecyclePolicy.PHASES_FIELD.getPreferredName(), phaseStats); |
| 113 | + builder.field(INDICES_MANAGED_FIELD.getPreferredName(), indicesManaged); |
| 114 | + builder.endObject(); |
| 115 | + return builder; |
| 116 | + } |
| 117 | + |
| 118 | + public Map<String, PhaseStats> getPhaseStats() { |
| 119 | + return phaseStats; |
| 120 | + } |
| 121 | + |
| 122 | + public int getIndicesManaged() { |
| 123 | + return indicesManaged; |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public int hashCode() { |
| 128 | + return Objects.hash(phaseStats, indicesManaged); |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public boolean equals(Object obj) { |
| 133 | + if (obj == null) { |
| 134 | + return false; |
| 135 | + } |
| 136 | + if (getClass() != obj.getClass()) { |
| 137 | + return false; |
| 138 | + } |
| 139 | + PolicyStats other = (PolicyStats) obj; |
| 140 | + return Objects.equals(phaseStats, other.phaseStats) && |
| 141 | + Objects.equals(indicesManaged, other.indicesManaged); |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + public String toString() { |
| 146 | + return Strings.toString(this); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + public static final class PhaseStats implements ToXContentObject, Writeable { |
| 151 | + private final String[] actionNames; |
| 152 | + private final TimeValue minimumAge; |
| 153 | + |
| 154 | + public PhaseStats(TimeValue after, String[] actionNames) { |
| 155 | + this.actionNames = actionNames; |
| 156 | + this.minimumAge = after; |
| 157 | + } |
| 158 | + |
| 159 | + public PhaseStats(StreamInput in) throws IOException { |
| 160 | + actionNames = in.readStringArray(); |
| 161 | + minimumAge = in.readTimeValue(); |
| 162 | + } |
| 163 | + |
| 164 | + @Override |
| 165 | + public void writeTo(StreamOutput out) throws IOException { |
| 166 | + out.writeStringArray(actionNames); |
| 167 | + out.writeTimeValue(minimumAge); |
| 168 | + } |
| 169 | + |
| 170 | + @Override |
| 171 | + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { |
| 172 | + builder.startObject(); |
| 173 | + builder.field(Phase.MINIMUM_AGE.getPreferredName(), minimumAge.getMillis()); |
| 174 | + builder.field(Phase.ACTIONS_FIELD.getPreferredName(), actionNames); |
| 175 | + builder.endObject(); |
| 176 | + return builder; |
| 177 | + } |
| 178 | + |
| 179 | + public String[] getActionNames() { |
| 180 | + return actionNames; |
| 181 | + } |
| 182 | + |
| 183 | + public TimeValue getAfter() { |
| 184 | + return minimumAge; |
| 185 | + } |
| 186 | + |
| 187 | + @Override |
| 188 | + public int hashCode() { |
| 189 | + return Objects.hash(Arrays.hashCode(actionNames), minimumAge); |
| 190 | + } |
| 191 | + |
| 192 | + @Override |
| 193 | + public boolean equals(Object obj) { |
| 194 | + if (obj == null) { |
| 195 | + return false; |
| 196 | + } |
| 197 | + if (getClass() != obj.getClass()) { |
| 198 | + return false; |
| 199 | + } |
| 200 | + PhaseStats other = (PhaseStats) obj; |
| 201 | + return Objects.equals(minimumAge, other.minimumAge) && |
| 202 | + Objects.deepEquals(actionNames, other.actionNames); |
| 203 | + } |
22 | 204 | }
|
23 | 205 | }
|
0 commit comments