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

import com.yahoo.config.provision.SystemName;
import com.yahoo.config.provision.TenantName;
import com.yahoo.vespa.hosted.controller.Controller;
import com.yahoo.vespa.hosted.controller.LockedTenant;
import com.yahoo.vespa.hosted.controller.api.integration.billing.BillingController;
import com.yahoo.vespa.hosted.controller.api.integration.billing.BillingReporter;
import com.yahoo.vespa.hosted.controller.api.integration.billing.Plan;
import com.yahoo.vespa.hosted.controller.api.integration.billing.PlanRegistry;
import com.yahoo.vespa.hosted.controller.tenant.CloudTenant;
import com.yahoo.vespa.hosted.controller.tenant.Tenant;

import java.time.Duration;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

public class BillingReportMaintainer extends ControllerMaintainer {

    private final BillingReporter reporter;
    private final BillingController billing;
    private final PlanRegistry plans;

    public BillingReportMaintainer(Controller controller, Duration interval) {
        super(controller, interval, null, Set.of(SystemName.PublicCd));
        this.reporter = controller.serviceRegistry().billingReporter();
        this.billing = controller.serviceRegistry().billingController();
        this.plans = controller.serviceRegistry().planRegistry();
    }

    @Override
    protected double maintain() {
        maintainTenants();
        return 0.0;
    }

    private void maintainTenants() {
        var tenants = cloudTenants();
        var tenantNames = List.copyOf(tenants.keySet());
        var billableTenants = billableTenants(tenantNames);

        billableTenants.forEach(tenant -> {
            controller().tenants().lockIfPresent(tenant, LockedTenant.Cloud.class, locked -> {
                var ref = reporter.maintainTenant(locked.get());
                if (locked.get().billingReference().isEmpty() || ! locked.get().billingReference().get().equals(ref)) {
                    controller().tenants().store(locked.with(ref));
                }
            });
        });
    }

    private Map<TenantName, CloudTenant> cloudTenants() {
        return controller().tenants().asList()
                .stream()
                .filter(CloudTenant.class::isInstance)
                .map(CloudTenant.class::cast)
                .collect(Collectors.toMap(
                        Tenant::name,
                        Function.identity()));
    }

    private List<Plan> billablePlans() {
        return plans.all().stream()
                .filter(Plan::isBilled)
                .toList();
    }

    private List<TenantName> billableTenants(List<TenantName> tenants) {
        return billablePlans().stream()
                .flatMap(p -> billing.tenantsWithPlan(tenants, p.id()).stream())
                .toList();
    }
}