package com.hubspot.singularity;

import java.util.Map;

import com.google.common.base.Optional;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

public class SingularitySlaveUsage {

  public enum ResourceUsageType {
    CPU_USED, MEMORY_BYTES_USED
  }

  public static final long BYTES_PER_MEGABYTE = 1024L * 1024L;

  private final long memoryBytesUsed;
  private final long memoryMbReserved;
  private final int numTasks;
  private final long timestamp;
  private final double cpusUsed;
  private final double cpusReserved;
  private final Optional<Long> memoryMbTotal;
  private final Optional<Double> cpuTotal;
  private final Map<ResourceUsageType, Number> longRunningTasksUsage;

  @JsonCreator
  public SingularitySlaveUsage(@JsonProperty("memoryBytesUsed") long memoryBytesUsed,
                               @JsonProperty("memoryMbReserved") long memoryMbReserved,
                               @JsonProperty("timestamp") long timestamp,
                               @JsonProperty("cpusUsed") double cpusUsed,
                               @JsonProperty("cpusReserved") double cpusReserved,
                               @JsonProperty("numTasks") int numTasks,
                               @JsonProperty("memoryMbTotal") Optional<Long> memoryMbTotal,
                               @JsonProperty("cpuTotal") Optional<Double> cpuTotal,
                               @JsonProperty("longRunningTasksUsage") Map<ResourceUsageType, Number> longRunningTasksUsage) {
    this.memoryBytesUsed = memoryBytesUsed;
    this.memoryMbReserved = memoryMbReserved;
    this.timestamp = timestamp;
    this.cpusUsed = cpusUsed;
    this.cpusReserved = cpusReserved;
    this.numTasks = numTasks;
    this.memoryMbTotal = memoryMbTotal;
    this.cpuTotal = cpuTotal;
    this.longRunningTasksUsage = longRunningTasksUsage;
  }

  public long getMemoryBytesUsed() {
    return memoryBytesUsed;
  }

  public long getMemoryMbReserved() {
    return memoryMbReserved;
  }

  public long getTimestamp() {
    return timestamp;
  }

  public double getCpusUsed() {
    return cpusUsed;
  }

  public double getCpusReserved() {
    return cpusReserved;
  }

  public int getNumTasks() {
    return numTasks;
  }

  public Optional<Long> getMemoryBytesTotal() {
    return memoryMbTotal.isPresent() ? Optional.of(memoryMbTotal.get() * BYTES_PER_MEGABYTE) : Optional.absent();
  }

  public Optional<Long> getMemoryMbTotal() {
    return memoryMbTotal.isPresent() ? Optional.of(memoryMbTotal.get()) : Optional.absent();
  }

  public Optional<Double> getCpuTotal() {
    return cpuTotal;
  }

  public Map<ResourceUsageType, Number> getLongRunningTasksUsage() {
    return longRunningTasksUsage;
  }

  @Override
  public String toString() {
    return "SingularitySlaveUsage [memoryBytesUsed=" + memoryBytesUsed + ", numTasks=" + numTasks + ", timestamp=" + timestamp + ", cpusUsed=" + cpusUsed + "]";
  }
}

@@ -662,23 +662,24 @@
 ble%3E cpu
+s
 Total;%0A
-
   privat
@@ -1372,16 +1372,17 @@
 rty(%22cpu
+s
 Total%22) 
@@ -1397,24 +1397,25 @@
 %3CDouble%3E cpu
+s
 Total,%0A     
@@ -1807,16 +1807,17 @@
 this.cpu
+s
 Total = 
@@ -1815,24 +1815,25 @@
 sTotal = cpu
+s
 Total;%0A    t
@@ -2609,16 +2609,17 @@
 %3E getCpu
+s
 Total() 
@@ -2634,16 +2634,17 @@
 turn cpu
+s
 Total;%0A 
@@ -2872,16 +2872,186 @@
 ed + %22, 
+memoryMbReserved=%22 + memoryMbReserved + %22, memoryMbTotal=%22 + memoryMbTotal + %22, cpusUsed=%22 + cpusUsed + %22, cpusReserved%22 + cpusReserved + %22, cpusTotal=%22 + cpusTotal + %22, 
 numTasks
@@ -3096,35 +3096,8 @@
 tamp
- + %22, cpusUsed=%22 + cpusUsed
  + %22

add variables to output string