aboutsummaryrefslogtreecommitdiffstats
path: root/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/integration/billing/Bill.java
blob: 664669d8e555eb3f9ca9a0eefea5461ea3d87061 (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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.api.integration.billing;

import com.yahoo.config.provision.ApplicationId;
import com.yahoo.config.provision.NodeResources;
import com.yahoo.config.provision.TenantName;
import com.yahoo.config.provision.zone.ZoneId;

import java.math.BigDecimal;
import java.time.Clock;
import java.time.LocalDate;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.UUID;
import java.util.function.Function;


/**
 * An Bill is an identifier with a status (with history) and line items.  A line item is the meat and
 * potatoes of the content of the bill, and are a history of items.  Most line items are connected to
 * a given deployment in Vespa Cloud, but they can also be manually added to e.g. give a discount or represent
 * support.
 * <p>
 * All line items have a Plan associated with them - which was used to map from utilization to an actual price.
 * <p>
 * The bill has a status history, but only the latest status is exposed through this API.
 *
 * @author ogronnesby
 */
public class Bill {
    private static final BigDecimal SCALED_ZERO = new BigDecimal("0.00");

    private final Id id;
    private final TenantName tenant;
    private final List<LineItem> lineItems;
    private final StatusHistory statusHistory;
    private final ZonedDateTime startTime;
    private final ZonedDateTime endTime;

    public Bill(Id id, TenantName tenant, StatusHistory statusHistory, List<LineItem> lineItems, ZonedDateTime startTime, ZonedDateTime endTime) {
        this.id = id;
        this.tenant = tenant;
        this.lineItems = List.copyOf(lineItems);
        this.statusHistory = statusHistory;
        this.startTime = startTime;
        this.endTime = endTime;
    }

    public Id id() {
        return id;
    }

    public TenantName tenant() {
        return tenant;
    }

    public String status() {
        return statusHistory.current();
    }

    public StatusHistory statusHistory() {
        return statusHistory;
    }

    public List<LineItem> lineItems() {
        return lineItems;
    }

    public ZonedDateTime getStartTime() {
        return startTime;
    }

    public ZonedDateTime getEndTime() {
        return endTime;
    }

    public LocalDate getStartDate() {
        return startTime.toLocalDate();
    }

    public LocalDate getEndDate() {
        return endTime.minusDays(1).toLocalDate();
    }

    public BigDecimal sum() {
        return lineItems.stream().map(LineItem::amount).reduce(SCALED_ZERO, BigDecimal::add);
    }

    public BigDecimal sumCpuHours() {
        return sumResourceValues(LineItem::getCpuHours);
    }

    public BigDecimal sumMemoryHours() {
        return sumResourceValues(LineItem::getMemoryHours);
    }

    public BigDecimal sumDiskHours() {
        return sumResourceValues(LineItem::getDiskHours);
    }

    public BigDecimal sumCpuCost() {
        return sumResourceValues(LineItem::getCpuCost);
    }

    public BigDecimal sumMemoryCost() {
        return sumResourceValues(LineItem::getMemoryCost);
    }

    public BigDecimal sumDiskCost() {
        return sumResourceValues(LineItem::getDiskCost);
    }

    public BigDecimal sumGpuCost() {
        return sumResourceValues(LineItem::getGpuCost);
    }

    public BigDecimal sumAdditionalCost() {
        // anything that is not covered by the cost for resources is "additional" costs
        var resourceCosts = sumCpuCost().add(sumMemoryCost()).add(sumDiskCost()).add(sumGpuCost());
        return sum().subtract(resourceCosts);
    }

    private BigDecimal sumResourceValues(Function<LineItem, Optional<BigDecimal>> f) {
        return lineItems.stream().flatMap(li -> f.apply(li).stream()).reduce(SCALED_ZERO, BigDecimal::add);
    }

    public static final class Id {
        private final String value;

        public static Id of(String value) {
            Objects.requireNonNull(value);
            return new Id(value);
        }

        public static Id generate() {
            var id = UUID.randomUUID().toString();
            return new Id(id);
        }

        private Id(String value) {
            this.value = value;
        }

        public String value() {
            return value;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Id billId = (Id) o;
            return value.equals(billId.value);
        }

        @Override
        public int hashCode() {
            return Objects.hash(value);
        }

        @Override
        public String toString() {
            return "BillId{" +
                    "value='" + value + '\'' +
                    '}';
        }
    }

    /**
     * Represents a chargeable line on a bill.
     */
    public static class LineItem {
        private final String id;
        private final String description;
        private final BigDecimal amount;
        private final String plan;
        private final String agent;
        private final ZonedDateTime addedAt;
        private ZonedDateTime startedAt;
        private ZonedDateTime endedAt;
        private ApplicationId applicationId;
        private ZoneId zoneId;
        private BigDecimal cpuHours;
        private BigDecimal memoryHours;
        private BigDecimal diskHours;
        private BigDecimal gpuHours;
        private BigDecimal cpuCost;
        private BigDecimal memoryCost;
        private BigDecimal diskCost;
        private BigDecimal gpuCost;
        private NodeResources.Architecture architecture;
        private int majorVersion;

        public LineItem(String id, String description, BigDecimal amount, String plan, String agent, ZonedDateTime addedAt) {
            this.id = id;
            this.description = description;
            this.amount = amount;
            this.plan = plan;
            this.agent = agent;
            this.addedAt = addedAt;
        }

        public LineItem(String id, String description, BigDecimal amount, String plan, String agent, ZonedDateTime addedAt, ZonedDateTime startedAt, ZonedDateTime endedAt, ApplicationId applicationId, ZoneId zoneId,
                        BigDecimal cpuHours, BigDecimal memoryHours, BigDecimal diskHours, BigDecimal gpuHours, BigDecimal cpuCost, BigDecimal memoryCost, BigDecimal diskCost, BigDecimal gpuCost, NodeResources.Architecture architecture, int majorVersion) {
            this(id, description, amount, plan, agent, addedAt);
            this.startedAt = startedAt;
            this.endedAt = endedAt;

            if (applicationId == null && zoneId != null)
                throw new IllegalArgumentException("Must supply applicationId if zoneId is supplied");

            this.applicationId = applicationId;
            this.zoneId = zoneId;
            this.cpuHours = cpuHours;
            this.memoryHours = memoryHours;
            this.diskHours = diskHours;
            this.gpuHours = gpuHours;
            this.cpuCost = cpuCost;
            this.memoryCost = memoryCost;
            this.diskCost = diskCost;
            this.architecture = architecture;
            this.majorVersion = majorVersion;
            this.gpuCost = gpuCost;
        }

        /** The opaque ID of this */
        public String id() {
            return id;
        }

        /** The string description of this - used for display purposes */
        public String description() {
            return description;
        }

        /** The dollar amount of this */
        public BigDecimal amount() {
            return SCALED_ZERO.add(amount);
        }

        /** The plan used to calculate amount of this */
        public String plan() {
            return plan;
        }

        /** Who created this line item */
        public String agent() {
            return agent;
        }

        /** When was this line item added */
        public ZonedDateTime addedAt() {
            return addedAt;
        }

        /** What time period is this line item for - time start */
        public Optional<ZonedDateTime> startedAt() {
            return Optional.ofNullable(startedAt);
        }

        /** What time period is this line item for - time end */
        public Optional<ZonedDateTime> endedAt() {
            return Optional.ofNullable(endedAt);
        }

        /** Optionally - what application is this line item about */
        public Optional<ApplicationId> applicationId() {
            return Optional.ofNullable(applicationId);
        }

        /** Optionally - what zone deployment is this line item about */
        public Optional<ZoneId> zoneId() {
            return Optional.ofNullable(zoneId);
        }

        public Optional<BigDecimal> getCpuHours() {
            return Optional.ofNullable(cpuHours);
        }

        public Optional<BigDecimal> getMemoryHours() {
            return Optional.ofNullable(memoryHours);
        }

        public Optional<BigDecimal> getDiskHours() {
            return Optional.ofNullable(diskHours);
        }

        public Optional<BigDecimal> getGpuHours() {
            return Optional.ofNullable(gpuHours);
        }

        public Optional<BigDecimal> getCpuCost() {
            return Optional.ofNullable(cpuCost);
        }

        public Optional<BigDecimal> getMemoryCost() {
            return Optional.ofNullable(memoryCost);
        }

        public Optional<BigDecimal> getDiskCost() {
            return Optional.ofNullable(diskCost);
        }

        public Optional<BigDecimal> getGpuCost() {
            return Optional.ofNullable(gpuCost);
        }

        public Optional<NodeResources.Architecture> getArchitecture() {
            return Optional.ofNullable(architecture);
        }

        public int getMajorVersion() {
            return majorVersion;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            LineItem lineItem = (LineItem) o;
            return id.equals(lineItem.id) &&
                    description.equals(lineItem.description) &&
                    amount.equals(lineItem.amount) &&
                    plan.equals(lineItem.plan) &&
                    agent.equals(lineItem.agent) &&
                    addedAt.equals(lineItem.addedAt) &&
                    startedAt.equals(lineItem.startedAt) &&
                    endedAt.equals(lineItem.endedAt) &&
                    applicationId.equals(lineItem.applicationId) &&
                    zoneId.equals(lineItem.zoneId) &&
                    majorVersion == lineItem.majorVersion;
        }

        @Override
        public int hashCode() {
            return Objects.hash(id, description, amount, plan, agent, addedAt, startedAt, endedAt, applicationId, zoneId, majorVersion);
        }

        @Override
        public String toString() {
            return "LineItem{" +
                    "id='" + id + '\'' +
                    ", description='" + description + '\'' +
                    ", amount=" + amount +
                    ", plan='" + plan + '\'' +
                    ", agent='" + agent + '\'' +
                    ", addedAt=" + addedAt +
                    ", startedAt=" + startedAt +
                    ", endedAt=" + endedAt +
                    ", applicationId=" + applicationId +
                    ", zoneId=" + zoneId +
                    '}';
        }
    }

    public static class StatusHistory {
        SortedMap<ZonedDateTime, String> history;

        public StatusHistory(SortedMap<ZonedDateTime, String> history) {
            this.history = history;
        }

        public static StatusHistory open(Clock clock) {
            var now = clock.instant().atZone(ZoneOffset.UTC);
            return new StatusHistory(
                    new TreeMap<>(Map.of(now, "OPEN"))
            );
        }

        public String current() {
            return history.get(history.lastKey());
        }

        public SortedMap<ZonedDateTime, String> getHistory() {
            return history;
        }

    }

}