49
49
50
50
import java .io .IOException ;
51
51
import java .util .ArrayList ;
52
+ import java .util .Arrays ;
52
53
import java .util .Collection ;
53
54
import java .util .Collections ;
54
55
import java .util .HashMap ;
58
59
import java .util .Map ;
59
60
import java .util .Objects ;
60
61
import java .util .Set ;
62
+ import java .util .stream .Collectors ;
61
63
import java .util .stream .Stream ;
62
64
63
65
import static org .elasticsearch .repositories .ProjectRepo .PROJECT_REPO_SERIALIZER ;
@@ -178,6 +180,28 @@ public List<Entry> forRepo(ProjectId projectId, String repository) {
178
180
return forRepo (new ProjectRepo (projectId , repository ));
179
181
}
180
182
183
+ /**
184
+ * Get a summary how many shards are in each {@link ShardState} for this repository
185
+ *
186
+ * @param projectId The project ID
187
+ * @param repository The repository name
188
+ * @return A map of each shard state to the count of shards in that state for all in-progress snapshots
189
+ */
190
+ public Map <ShardState , Integer > shardStateSummaryForRepository (ProjectId projectId , String repository ) {
191
+ return entries .getOrDefault (new ProjectRepo (projectId , repository ), ByRepo .EMPTY ).shardStateSummary ;
192
+ }
193
+
194
+ /**
195
+ * Get a summary how many snapshots are in each {@link State} for this repository
196
+ *
197
+ * @param projectId The project ID
198
+ * @param repository The repository name
199
+ * @return A map of each snapshot state to the count of in-progress snapshots in that state
200
+ */
201
+ public Map <State , Integer > snapshotStateSummaryForRepository (ProjectId projectId , String repository ) {
202
+ return entries .getOrDefault (new ProjectRepo (projectId , repository ), ByRepo .EMPTY ).snapshotStateSummary ;
203
+ }
204
+
181
205
/**
182
206
* Returns the list of snapshots in the specified repository.
183
207
*/
@@ -1875,7 +1899,9 @@ public void writeTo(StreamOutput out) throws IOException {
1875
1899
*
1876
1900
* @param entries all snapshots executing for a single repository
1877
1901
*/
1878
- private record ByRepo (List <Entry > entries ) implements Diffable <ByRepo > {
1902
+ private record ByRepo (List <Entry > entries , Map <State , Integer > snapshotStateSummary , Map <ShardState , Integer > shardStateSummary )
1903
+ implements
1904
+ Diffable <ByRepo > {
1879
1905
1880
1906
static final ByRepo EMPTY = new ByRepo (List .of ());
1881
1907
private static final DiffableUtils .NonDiffableValueSerializer <String , Integer > INT_DIFF_VALUE_SERIALIZER =
@@ -1892,7 +1918,27 @@ public Integer read(StreamInput in, String key) throws IOException {
1892
1918
};
1893
1919
1894
1920
private ByRepo (List <Entry > entries ) {
1895
- this .entries = List .copyOf (entries );
1921
+ this (List .copyOf (entries ), calculateStateSummaries (entries ));
1922
+ }
1923
+
1924
+ private ByRepo (List <Entry > entries , Tuple <Map <State , Integer >, Map <ShardState , Integer >> stateSummaries ) {
1925
+ this (entries , stateSummaries .v1 (), stateSummaries .v2 ());
1926
+ }
1927
+
1928
+ private static Tuple <Map <State , Integer >, Map <ShardState , Integer >> calculateStateSummaries (List <Entry > entries ) {
1929
+ final int [] snapshotCounts = new int [State .values ().length ];
1930
+ final int [] shardCounts = new int [ShardState .values ().length ];
1931
+ for (Entry entry : entries ) {
1932
+ snapshotCounts [entry .state ().ordinal ()]++;
1933
+ for (ShardSnapshotStatus shardSnapshotStatus : entry .shards ().values ()) {
1934
+ shardCounts [shardSnapshotStatus .state ().ordinal ()]++;
1935
+ }
1936
+ }
1937
+ final Map <State , Integer > snapshotStates = Arrays .stream (State .values ())
1938
+ .collect (Collectors .toUnmodifiableMap (state -> state , state -> snapshotCounts [state .ordinal ()]));
1939
+ final Map <ShardState , Integer > shardStates = Arrays .stream (ShardState .values ())
1940
+ .collect (Collectors .toUnmodifiableMap (shardState -> shardState , state -> shardCounts [state .ordinal ()]));
1941
+ return Tuple .tuple (snapshotStates , shardStates );
1896
1942
}
1897
1943
1898
1944
@ Override
0 commit comments