aboutsummaryrefslogtreecommitdiffstats
path: root/metrics/src/main/java/ai/vespa/metrics/docs/UnitDocumentation.java
blob: 03f99a076b1ca86b47500b647136f356d5949a6d (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
package ai.vespa.metrics.docs;


import ai.vespa.metrics.Unit;

import java.io.FileWriter;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author olaa
 */
public class UnitDocumentation {

    protected static void writeUnitDocumentation(String path, Unit[] units) {
        var referenceBuilder = new StringBuilder();
        referenceBuilder.append(String.format("""
                        ---
                        # Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
                        title: "Metric Units Reference"
                        ---


                        <table class="table">
                          <thead>
                              <tr><th>Unit</th><th>Description</th></tr>
                          </thead>
                          <tbody>
                        %s    </tbody>
                        </table>
                        """, htmlRows(units)));

        try (FileWriter fileWriter = new FileWriter(path + "/unit-metrics-reference.html")) {
            fileWriter.write(referenceBuilder.toString());
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }

    private static String htmlRows(Unit[] units) {
        return Stream.of(units)
                .map(unit ->
                        String.format(
                                """
                                     <tr>
                                       <td>%s</td>
                                       <td>%s</td>
                                     </tr>
                                 """,
                                unit.fullName(),
                                unit.getDescription())
                ).collect(Collectors.joining());
    }
}