aboutsummaryrefslogtreecommitdiffstats
path: root/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/deployment/RunLog.java
blob: f388a148ffb3401f467795dd0bbaf39fc7a83b9e (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.deployment;

import com.google.common.collect.ImmutableMap;
import com.yahoo.vespa.hosted.controller.api.integration.LogEntry;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.OptionalLong;

/**
 * Immutable class which contains the log of a deployment job run.
 *
 * @author jonmv
 */
public class RunLog {

    private static final RunLog empty = RunLog.of(Collections.emptyMap());

    private final Map<Step, List<LogEntry>> log;
    private final OptionalLong lastId;

    private RunLog(OptionalLong lastId, Map<Step, List<LogEntry>> log) {
        this.log = log;
        this.lastId = lastId;
    }

    /** Creates a RunLog which contains a deep copy of the given log. */
    public static RunLog of(Map<Step, List<LogEntry>> log) {
        ImmutableMap.Builder<Step, List<LogEntry>> builder = ImmutableMap.builder();
        log.forEach((step, entries) -> {
            if ( ! entries.isEmpty())
                builder.put(step, List.copyOf(entries));
        });
        OptionalLong lastId = log.values().stream()
                                 .flatMap(List::stream)
                                 .mapToLong(LogEntry::id)
                                 .max();
        return new RunLog(lastId, builder.build());
    }

    /** Returns an empty RunLog. */
    public static RunLog empty() {
        return empty;
    }

    /** Returns the log entries for the given step, if any are recorded. */
    public List<LogEntry> get(Step step) {
        return log.getOrDefault(step, Collections.emptyList());
    }

    /** Returns the id of the last log entry in this, if it has any. */
    public OptionalLong lastId() {
        return lastId;
    }

}