aboutsummaryrefslogtreecommitdiffstats
path: root/configserver/src/main/java/com/yahoo/vespa/config/server/http/v2/response/ReindexingResponse.java
blob: 47619536ae6399cc32fb23fbae81ee174321c703 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.config.server.http.v2.response;

import com.yahoo.jdisc.Response;
import com.yahoo.slime.Cursor;
import com.yahoo.vespa.config.server.application.ApplicationReindexing;
import com.yahoo.vespa.config.server.application.ClusterReindexing;
import com.yahoo.vespa.config.server.application.ClusterReindexing.State;
import com.yahoo.vespa.config.server.http.JSONResponse;

import java.time.Instant;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

public class ReindexingResponse extends JSONResponse {
    public ReindexingResponse(Map<String, Set<String>> documentTypes, ApplicationReindexing reindexing,
                       Map<String, ClusterReindexing> clusters) {
        super(Response.Status.OK);
        object.setBool("enabled", reindexing.enabled());
        Cursor clustersObject = object.setObject("clusters");
        documentTypes.forEach((cluster, types) -> {
            Cursor clusterObject = clustersObject.setObject(cluster);
            Cursor pendingObject = clusterObject.setObject("pending");
            Cursor readyObject = clusterObject.setObject("ready");

            for (String type : types) {
                Cursor statusObject = readyObject.setObject(type);
                Instant readyAt = Instant.EPOCH;
                State state = null;
                if (reindexing.clusters().containsKey(cluster)) {
                    if (reindexing.clusters().get(cluster).pending().containsKey(type)) {
                        pendingObject.setLong(type, reindexing.clusters().get(cluster).pending().get(type));
                        state = State.PENDING;
                    }

                    if (reindexing.clusters().get(cluster).ready().containsKey(type)) {
                        ApplicationReindexing.Status readyStatus = reindexing.clusters().get(cluster).ready().get(type);
                        readyAt = readyStatus.ready();
                        statusObject.setLong("readyMillis", readyStatus.ready().toEpochMilli());
                        statusObject.setDouble("speed", readyStatus.speed());
                        statusObject.setString("cause", readyStatus.cause());
                    }
                }
                if (clusters.containsKey(cluster))
                    if (clusters.get(cluster).documentTypeStatus().containsKey(type)) {
                        ClusterReindexing.Status status = clusters.get(cluster).documentTypeStatus().get(type);
                        statusObject.setLong("startedMillis", status.startedAt().toEpochMilli());
                        status.endedAt().ifPresent(endedAt -> statusObject.setLong("endedMillis", endedAt.toEpochMilli()));
                        if (status.startedAt().isAfter(readyAt) && status.state().isPresent()) state = status.state().get();
                        status.message().ifPresent(message -> statusObject.setString("message", message));
                        status.progress().ifPresent(progress -> statusObject.setDouble("progress", progress));
                    }
                if (readyAt != Instant.EPOCH && state == null) state = State.PENDING;
                if (state != null) statusObject.setString("state", state.asString());
            }
        });
    }

}