Skip to content

Commit

Permalink
RAM usage
Browse files Browse the repository at this point in the history
  • Loading branch information
THWiseman committed Sep 19, 2024
1 parent ac5f731 commit 26e841d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,23 @@ class GcpCostCatalogService(serviceConfig: Config, globalConfig: Config, service
val costPerUnit: Money = pricingInfo.getPricingExpression.getTieredRates(0).getUnitPrice
val costPerCorePerHour: BigDecimal =
costPerUnit.getUnits + (costPerUnit.getNanos * 10e-9) // Same as above, but as a big decimal

println(s"Calculated ${coreCount} cpu cores to cost ${costPerCorePerHour * coreCount} per hour")
Success(costPerCorePerHour * coreCount)
}

private def calculateRamPricePerHour(ramSku: Sku, ramMbCount: Int): Try[BigDecimal] =
// TODO
Success(ramMbCount.toLong * 0.25)
private def calculateRamPricePerHour(ramSku: Sku, ramGbCount: Int): Try[BigDecimal] = {
val pricingInfo = getMostRecentPricingInfo(ramSku)
val usageUnit = pricingInfo.getPricingExpression.getUsageUnit
if (usageUnit != "GiBy.h") {
return Failure(new UnsupportedOperationException(s"Expected usage units of RAM to be 'GiBy.h'. Got ${usageUnit}"))
}
val costPerUnit: Money = pricingInfo.getPricingExpression.getTieredRates(0).getUnitPrice
val costPerGbHour: BigDecimal =
costPerUnit.getUnits + (costPerUnit.getNanos * 10e-9) // Same as above, but as a big decimal
println(s"Calculated ${ramGbCount} GB of ram to cost ${ramGbCount * costPerGbHour} per hour")
Success(costPerGbHour * ramGbCount)
}

private def getMostRecentPricingInfo(sku: Sku): PricingInfo = {
val mostRecentPricingInfoIndex = sku.getPricingInfoCount - 1
Expand All @@ -147,12 +158,13 @@ class GcpCostCatalogService(serviceConfig: Config, globalConfig: Config, service
val region = instantiatedVmInfo.region
val coreCount = MachineType.extractCoreCountFromMachineTypeString(instantiatedVmInfo.machineType)
val ramMbCount = MachineType.extractRamMbFromMachineTypeString(instantiatedVmInfo.machineType)
val ramGbCount = ramMbCount.getOrElse(0) / 1024

val cpuResourceGroup = Cpu // TODO: Investigate the situation in which the resource group is n1
val cpuKey =
CostCatalogKey(machineType, Option(usageType), Option(machineCustomization), Option(cpuResourceGroup), region)
val cpuSku = getSku(cpuKey)
if(cpuSku.isEmpty) {
if (cpuSku.isEmpty) {
println(s"Failed to find CPU Sku for ${cpuKey}")
} else {
println(s"Found CPU Sku ${cpuSku.get.catalogObject.getDescription} from key ${cpuKey}")
Expand All @@ -163,12 +175,12 @@ class GcpCostCatalogService(serviceConfig: Config, globalConfig: Config, service
val ramKey =
CostCatalogKey(machineType, Option(usageType), Option(machineCustomization), Option(ramResourceGroup), region)
val ramSku = getSku(ramKey)
if(ramSku.isEmpty) {
if (ramSku.isEmpty) {
println(s"Failed to find Ram Sku for ${ramKey}")
} else {
println(s"Found CPU Sku ${ramSku.get.catalogObject.getDescription} from key ${ramKey}")
}
val ramCost = ramSku.map(sku => calculateRamPricePerHour(sku.catalogObject, ramMbCount.get)) // TODO .get
val ramCost = ramSku.map(sku => calculateRamPricePerHour(sku.catalogObject, ramGbCount)) // TODO .get
Success(cpuCost.get.get + ramCost.get.get)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,16 @@ object MachineType {
Failure(new IllegalArgumentException(s"Could not extract core count from ${machineTypeString}"))
}
}

def extractRamMbFromMachineTypeString(machineTypeString: String): Try[Int] =
// TODO
Success(4096)
def extractRamMbFromMachineTypeString(machineTypeString: String): Try[Int] = {
// Regular expression to match the number after the second dash
val pattern: Pattern = Pattern.compile(".*?-.*?-(\\d+)")
val matcher: Matcher = pattern.matcher(machineTypeString);
if (matcher.find()) {
Success(matcher.group(1).toInt)
} else {
Failure(new IllegalArgumentException(s"Could not Ram MB count from ${machineTypeString}"))
}
}
}
sealed trait MachineType { def machineTypeName: String }
case object N1 extends MachineType { override val machineTypeName = "N1" }
Expand Down

0 comments on commit 26e841d

Please sign in to comment.