aboutsummaryrefslogtreecommitdiffstats
path: root/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/deployment/BadgeApiHandler.java
blob: 839dbf76faa65f0733882787d72ce7e2543b2d47 (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.restapi.deployment;

import com.yahoo.config.provision.ApplicationId;
import com.yahoo.container.jdisc.EmptyResponse;
import com.yahoo.container.jdisc.HttpRequest;
import com.yahoo.container.jdisc.HttpResponse;
import com.yahoo.container.jdisc.ThreadedHttpRequestHandler;
import com.yahoo.jdisc.http.HttpRequest.Method;
import com.yahoo.restapi.ErrorResponse;
import com.yahoo.restapi.Path;
import com.yahoo.text.Text;
import com.yahoo.vespa.hosted.controller.Controller;
import com.yahoo.vespa.hosted.controller.api.integration.deployment.JobId;
import com.yahoo.vespa.hosted.controller.api.integration.deployment.JobType;
import com.yahoo.vespa.hosted.controller.application.TenantAndApplicationId;
import com.yahoo.vespa.hosted.controller.deployment.DeploymentStatus;
import com.yahoo.vespa.hosted.controller.deployment.JobStatus;
import com.yahoo.vespa.hosted.controller.restapi.ErrorResponses;
import com.yahoo.yolean.Exceptions;

import java.io.IOException;
import java.io.OutputStream;
import java.time.Instant;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.logging.Logger;

import static java.nio.charset.StandardCharsets.UTF_8;

/**
 * This API serves redirects to a badge server.
 * 
 * @author jonmv
 */
@SuppressWarnings("unused") // Handler
public class BadgeApiHandler extends ThreadedHttpRequestHandler {

    private final static Logger log = Logger.getLogger(BadgeApiHandler.class.getName());

    private final Controller controller;
    private final Map<Key, Value> badgeCache = new ConcurrentHashMap<>();

    public BadgeApiHandler(Context parentCtx, Controller controller) {
        super(parentCtx);
        this.controller = controller;
    }

    @Override
    public HttpResponse handle(HttpRequest request) {
        Method method = request.getMethod();
        try {
            return switch (method) {
                case OPTIONS -> new SvgHttpResponse("") {{
                    headers().add("Allow", "GET, HEAD, OPTIONS");
                    headers().add("Access-Control-Allow-Origin", "*");
                    headers().add("Access-Control-Allow-Methods", "GET, HEAD, OPTIONS");
                }};
                case HEAD, GET -> get(request);
                default -> ErrorResponse.methodNotAllowed("Method '" + method + "' is unsupported");
            };
        } catch (IllegalArgumentException|IllegalStateException e) {
            return ErrorResponse.badRequest(Exceptions.toMessageString(e));
        } catch (RuntimeException e) {
            return ErrorResponses.logThrowing(request, log, e);
        }
    }

    private HttpResponse get(HttpRequest request) {
        Path path = new Path(request.getUri());
        if (path.matches("/badge/v1/{tenant}/{application}/{instance}")) return overviewBadge(path.get("tenant"), path.get("application"), path.get("instance"));
        if (path.matches("/badge/v1/{tenant}/{application}/{instance}/{jobName}")) return historyBadge(path.get("tenant"), path.get("application"), path.get("instance"), path.get("jobName"), request.getProperty("historyLength"));

        return ErrorResponse.notFoundError(Text.format("No '%s' handler at '%s'", request.getMethod(),
                                                         request.getUri().getPath()));
    }

    /** Returns a URI which points to an overview badge for the given application. */
    private HttpResponse overviewBadge(String tenant, String application, String instance) {
        ApplicationId id = ApplicationId.from(tenant, application, instance);
        return cachedResponse(new Key(id, null, 0),
                              controller.clock().instant(),
                              () -> {
                                  DeploymentStatus status = controller.jobController().deploymentStatus(controller.applications().requireApplication(TenantAndApplicationId.from(id)));
                                  Predicate<JobStatus> isDeclaredJob = job -> status.jobSteps().get(job.id()) != null && status.jobSteps().get(job.id()).isDeclared();
                                  return Badges.overviewBadge(id, status.jobs().instance(id.instance()).matching(isDeclaredJob));
                              });
    }

    /** Returns a URI which points to a history badge for the given application and job type. */
    private HttpResponse historyBadge(String tenant, String application, String instance, String jobName, String historyLength) {
        ApplicationId id = ApplicationId.from(tenant, application, instance);
        JobType type = JobType.fromJobName(jobName, controller.zoneRegistry());
        int length = historyLength == null ? 5 : Math.min(32, Math.max(0, Integer.parseInt(historyLength)));
        return cachedResponse(new Key(id, type, length),
                              controller.clock().instant(),
                              () -> Badges.historyBadge(id,
                                                        controller.jobController().jobStatus(new JobId(id, type)),
                                                        length)
        );
    }

    private HttpResponse cachedResponse(Key key, Instant now, Supplier<String> badge)  {
        return new SvgHttpResponse(badgeCache.compute(key, (__, value) -> {
            return value != null && value.expiry.isAfter(now) ? value : new Value(badge.get(), now);
        }).badgeSvg);
    }

    private static class SvgHttpResponse extends HttpResponse {
        private final String svg;
        SvgHttpResponse(String svg) { super(200); this.svg = svg; }
        @Override public void render(OutputStream outputStream) throws IOException {
            outputStream.write(svg.getBytes(UTF_8));
        }
        @Override public String getContentType() {
            return "image/svg+xml";
        }
    }


    private static class Key {

        private final ApplicationId id;
        private final JobType type;
        private final int historyLength;

        private Key(ApplicationId id, JobType type, int historyLength) {
            this.id = id;
            this.type = type;
            this.historyLength = historyLength;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Key key = (Key) o;
            return historyLength == key.historyLength && id.equals(key.id) && Objects.equals(type, key.type);
        }

        @Override
        public int hashCode() {
            return Objects.hash(id, type, historyLength);
        }

    }

    private static class Value {

        private final String badgeSvg;
        private final Instant expiry;

        private Value(String badgeSvg, Instant created) {
            this.badgeSvg = badgeSvg;
            this.expiry = created.plusSeconds(60);
        }

    }

}