diff options
author | Øyvind Grønnesby <oyving@verizonmedia.com> | 2020-11-25 11:10:26 +0100 |
---|---|---|
committer | Øyvind Grønnesby <oyving@verizonmedia.com> | 2020-11-25 11:10:26 +0100 |
commit | dd29c756814446282767f70e47f7a2f779e3898c (patch) | |
tree | 318b6900d2f722298c7cd6665ce2ea45abe4b37b /controller-server/src/main | |
parent | 24ecd0adfd9f389f7d8a57008971b8514c38ff16 (diff) |
Export all bills/invoices as CSV
Diffstat (limited to 'controller-server/src/main')
-rw-r--r-- | controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/billing/BillingApiHandler.java | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/billing/BillingApiHandler.java b/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/billing/BillingApiHandler.java index f5b86ea846a..367f8e728bf 100644 --- a/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/billing/BillingApiHandler.java +++ b/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/billing/BillingApiHandler.java @@ -27,11 +27,14 @@ import com.yahoo.vespa.hosted.controller.api.integration.billing.BillingControll import com.yahoo.vespa.hosted.controller.api.integration.billing.PlanId; import com.yahoo.vespa.hosted.controller.tenant.Tenant; import com.yahoo.yolean.Exceptions; +import org.apache.commons.csv.CSVFormat; import javax.ws.rs.BadRequestException; import javax.ws.rs.ForbiddenException; import javax.ws.rs.NotFoundException; import java.io.IOException; +import java.io.OutputStream; +import java.io.OutputStreamWriter; import java.math.BigDecimal; import java.security.Principal; import java.time.LocalDate; @@ -44,6 +47,7 @@ import java.util.List; import java.util.Optional; import java.util.concurrent.Executor; import java.util.logging.Level; +import java.util.stream.Collectors; /** * @author andreer @@ -101,10 +105,28 @@ public class BillingApiHandler extends LoggingRequestHandler { if (path.matches("/billing/v1/tenant/{tenant}/billing")) return getBilling(path.get("tenant"), request.getProperty("until")); if (path.matches("/billing/v1/tenant/{tenant}/plan")) return getPlan(path.get("tenant")); if (path.matches("/billing/v1/billing")) return getBillingAllTenants(request.getProperty("until")); + if (path.matches("/billing/v1/invoice/export")) return getAllInvoices(); if (path.matches("/billing/v1/invoice/tenant/{tenant}/line-item")) return getLineItems(path.get("tenant")); return ErrorResponse.notFoundError("Nothing at " + path); } + private HttpResponse getAllInvoices() { + var invoices = billingController.getInvoices(); + var headers = new String[]{ "ID", "Tenant", "From", "To", "CpuHours", "MemoryHours", "DiskHours", "Cpu", "Memory", "Disk" }; + var rows = invoices.stream() + .map(invoice -> { + return new Object[] { + invoice.id().value(), invoice.tenant().value(), + invoice.getStartTime().format(DateTimeFormatter.ISO_LOCAL_DATE), + invoice.getEndTime().format(DateTimeFormatter.ISO_LOCAL_DATE), + invoice.sumCpuHours(), invoice.sumMemoryHours(), invoice.sumDiskHours(), + invoice.sumCpuCost(), invoice.sumMemoryCost(), invoice.sumDiskCost() + }; + }) + .collect(Collectors.toList()); + return new CsvResponse(headers, rows); + } + private HttpResponse handlePATCH(HttpRequest request, Path path, String userId) { if (path.matches("/billing/v1/tenant/{tenant}/instrument")) return patchActiveInstrument(request, path.get("tenant"), userId); if (path.matches("/billing/v1/tenant/{tenant}/plan")) return patchPlan(request, path.get("tenant")); @@ -462,4 +484,27 @@ public class BillingApiHandler extends LoggingRequestHandler { .count() > 0; } + private static class CsvResponse extends HttpResponse { + private final String[] header; + private final List<Object[]> rows; + + CsvResponse(String[] header, List<Object[]> rows) { + super(200); + this.header = header; + this.rows = rows; + } + + @Override + public void render(OutputStream outputStream) throws IOException { + var writer = new OutputStreamWriter(outputStream); + var printer = CSVFormat.DEFAULT.withRecordSeparator('\n').withHeader(this.header).print(writer); + for (var row : this.rows) printer.printRecord(row); + printer.flush(); + } + + @Override + public String getContentType() { + return "text/csv; encoding=utf-8"; + } + } } |