Skip to content

Commit

Permalink
Update models.py
Browse files Browse the repository at this point in the history
Adding support for M and G in memory
  • Loading branch information
joshua-seals authored Jan 22, 2025
1 parent 4d7c3f0 commit ef875d7
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions appstore/api/v1/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,23 @@ def _divide_memory(self, memory: str) -> str:
# Case for Gi (Gibibytes)
if memory.endswith("Gi"):
value = float(memory[:-2]) # Extract numeric value (removes 'Gi' suffix)
# Convert Gi to Mi by multiplying by 1024
value_in_mi = value * 1024
# Divide by 2
value_in_mi = value * 1024 # Convert Gi to Mi
divided_value = value_in_mi / 2
# Ensure the result is not less than 100Mi
if divided_value < 100:
divided_value = 100
return f"{int(divided_value)}Mi" # Return as Mi with no decimal
return f"{int(divided_value)}Mi"

# Case for G (Gigabytes)
elif memory.endswith("G"):
value = float(memory[:-1]) # Extract numeric value (removes 'G' suffix)
value_in_gi = value * 1024 # Convert G to Gi (G is 1024 times smaller than Gi)
value_in_mi = value_in_gi * 1024 # Convert Gi to Mi
divided_value = value_in_mi / 2
# Ensure the result is not less than 100Mi
if divided_value < 100:
divided_value = 100
return f"{int(divided_value)}Mi"

# Case for Mi (Mebibytes)
elif memory.endswith("Mi"):
Expand All @@ -132,11 +141,22 @@ def _divide_memory(self, memory: str) -> str:
# Ensure the result is not less than 100Mi
if divided_value < 100:
divided_value = 100
return f"{int(divided_value)}Mi" # Return as Mi with no decimal
return f"{int(divided_value)}Mi"

# Case for M (Megabytes)
elif memory.endswith("M"):
value = float(memory[:-1]) # Extract numeric value (removes 'M' suffix)
value_in_mi = value * 1024 # Convert M to Mi (M is 1024 times smaller than Mi)
divided_value = value_in_mi / 2
# Ensure the result is not less than 100Mi
if divided_value < 100:
divided_value = 100
return f"{int(divided_value)}Mi"

# Default case for unrecognized memory format
else:
return memory
return memory # Return as is if the format is not recognized


@dataclass
class InstanceSpec:
Expand Down

0 comments on commit ef875d7

Please sign in to comment.