Add units to AwsEstimate component props and format code

This commit is contained in:
Rendani Gangazhe
2023-06-02 10:08:17 -04:00
committed by Dannon Baker
parent d01495bac2
commit 3083f5e9d7
5 changed files with 64 additions and 65 deletions
@@ -7,48 +7,48 @@ const localVue = getLocalVue();
const oneGibibyeMemoryInMebibyte = 1024;
const oneHourInSeconds = 3600;
const testServerInstance = {
name: 'some-server-name',
cpuInfo: {
modelName: 'some-processor-name',
totalAvailableCores: 10,
tdp: 100
}
}
name: "some-server-name",
cpuInfo: {
modelName: "some-processor-name",
totalAvailableCores: 10,
tdp: 100,
},
};
describe("CarbonEmissions/CarbonEmissions.vue", () => {
it("correctly calculated carbon emissions.", async () => {
const wrapper = mount(CarbonEmissions, {
propsData: {
estimatedServerInstance: testServerInstance,
jobRuntimeInSeconds: oneHourInSeconds,
coresAllocated: 1,
memoryAllocatedInMebibyte: oneGibibyeMemoryInMebibyte,
estimatedServerInstance: testServerInstance,
jobRuntimeInSeconds: oneHourInSeconds,
coresAllocated: 1,
memoryAllocatedInMebibyte: oneGibibyeMemoryInMebibyte,
},
localVue,
});
const cpuEmissions = wrapper.find("#cpu-carbon-emissions").text();
const memoryEmissions = wrapper.find("#memory-carbon-emissions").text();
const cpuEnergyUsage = wrapper.find("#cpu-energy-usage").text();
const memoryEnergyUsage = wrapper.find("#memory-energy-usage").text();
const cpuEmissions = wrapper.find("#cpu-carbon-emissions").text();
const memoryEmissions = wrapper.find("#memory-carbon-emissions").text();
const cpuEnergyUsage = wrapper.find("#cpu-energy-usage").text();
const memoryEnergyUsage = wrapper.find("#memory-energy-usage").text();
expect(cpuEmissions).toMatch("8 g CO2e");
expect(memoryEmissions).toMatch("295 mg CO2e");
expect(cpuEnergyUsage).toMatch("17 mW⋅h");
expect(memoryEnergyUsage).toMatch("1 mW⋅h");
expect(cpuEmissions).toMatch("8 g CO2e");
expect(memoryEmissions).toMatch("295 mg CO2e");
expect(cpuEnergyUsage).toMatch("17 mW⋅h");
expect(memoryEnergyUsage).toMatch("1 mW⋅h");
});
it("does not render memory estimates when no value can be determined.", () => {
const wrapper = mount(CarbonEmissions, {
propsData: {
estimatedServerInstance: testServerInstance,
jobRuntime: 1,
coresAllocated: 1,
estimatedServerInstance: testServerInstance,
jobRuntime: 1,
coresAllocated: 1,
},
localVue,
});
expect(wrapper.find("#memory-carbon-emissions").exists()).toBe(false);
expect(wrapper.find("#memory-energy-usage").exists()).toBe(false);
expect(wrapper.find("#memory-carbon-emissions").exists()).toBe(false);
expect(wrapper.find("#memory-energy-usage").exists()).toBe(false);
});
});
@@ -14,10 +14,10 @@ export interface CarbonEmissionsProps {
estimatedServerInstance: {
name: string;
cpuInfo: {
modelName: string;
totalAvailableCores: number;
tdp: number;
}
modelName: string;
totalAvailableCores: number;
tdp: number;
};
};
jobRuntimeInSeconds: number;
coresAllocated: number;
@@ -18,9 +18,9 @@ describe("JobMetrics/AwsEstimate.vue", () => {
const wrapper = mount(AwsEstimate, {
propsData: {
jobId: "0",
jobRuntime: 0,
jobRuntimeInSeconds: 0,
coresAllocated: -999,
memoryAllocated: -999,
memoryAllocatedInMebibyte: -999,
},
localVue,
});
@@ -37,9 +37,9 @@ describe("JobMetrics/AwsEstimate.vue", () => {
localVue,
propsData: {
jobId: JOB_ID,
jobRuntime: Number(seconds),
jobRuntimeInSeconds: Number(seconds),
coresAllocated: Number(cores),
memoryAllocated: Number(memory),
memoryAllocatedInMebibyte: Number(memory),
},
});
@@ -3,21 +3,21 @@ import { computed } from "vue";
import ec2 from "./ec2.json";
export interface AwsEstimateProps {
jobRuntime: number;
jobRuntimeInSeconds: number;
coresAllocated: number;
memoryAllocated?: number;
memoryAllocatedInMebibyte?: number;
}
const props = defineProps<AwsEstimateProps>();
const computedAwsEstimate = computed(() => {
const { coresAllocated, jobRuntime, memoryAllocated } = props;
const { coresAllocated, jobRuntimeInSeconds, memoryAllocatedInMebibyte } = props;
if (coresAllocated <= 0 || jobRuntime <= 0) {
if (coresAllocated <= 0 || jobRuntimeInSeconds <= 0) {
return;
}
const adjustedMemoryAllocated = memoryAllocated ? memoryAllocated / 1024 : 0.5;
const adjustedMemoryAllocated = memoryAllocatedInMebibyte ? memoryAllocatedInMebibyte / 1024 : 0.5;
// Estimate EC2 instance. Data is already sorted
const ec2Instance = ec2.find((ec) => {
@@ -29,11 +29,11 @@ const computedAwsEstimate = computed(() => {
}
return {
seconds: jobRuntime,
vcpus: coresAllocated,
memory: adjustedMemoryAllocated,
price: ((jobRuntime * ec2Instance.price) / 3600).toFixed(2),
instance: ec2Instance
seconds: jobRuntimeInSeconds,
vcpus: coresAllocated,
memory: adjustedMemoryAllocated,
price: ((jobRuntimeInSeconds * ec2Instance.price) / 3600).toFixed(2),
instance: ec2Instance,
};
});
</script>
@@ -46,9 +46,8 @@ const computedAwsEstimate = computed(() => {
<br />
This job requested {{ computedAwsEstimate.vcpus }} core(s) and
{{ computedAwsEstimate.memory.toFixed(3) }} GiB of memory. Given this
information, the smallest EC2 machine we could find is:
This job requested {{ computedAwsEstimate.vcpus }} core(s) and {{ computedAwsEstimate.memory.toFixed(3) }} GiB
of memory. Given this information, the smallest EC2 machine we could find is:
<span id="aws-name">{{ computedAwsEstimate.instance.name }}</span>
(<span id="aws-mem">{{ computedAwsEstimate.instance.mem }}</span> GB /
+20 -20
View File
@@ -20,7 +20,7 @@ const props = withDefaults(defineProps<JobMetricsProps>(), {
datasetId: "",
datasetType: "hda",
includeTitle: true,
shouldShowAwsEstimate: false
shouldShowAwsEstimate: false,
});
const jobMetricsStore = useJobMetricsStore();
@@ -67,7 +67,7 @@ const pluginsSortedByPluginType = computed(() => {
return Object.keys(jobMetricsGroupedByPluginType.value).sort();
});
const jobRuntime = computed(() => {
const jobRuntimeInSeconds = computed(() => {
const runtime = getMetricByName("runtime_seconds");
return runtime ? parseInt(runtime) : undefined;
@@ -79,7 +79,7 @@ const coresAllocated = computed(() => {
return coreCount ? parseInt(coreCount) : undefined;
});
const memoryAllocated = computed(() => {
const memoryAllocatedInMebibyte = computed(() => {
const memoryUsage = getMetricByName("galaxy_memory_mb");
return memoryUsage ? parseInt(memoryUsage) : undefined;
@@ -91,17 +91,17 @@ const estimatedServerInstance = computed(() => {
return;
}
const memory = unref(memoryAllocated);
const memory = unref(memoryAllocatedInMebibyte);
const adjustedMemory = memory ? memory / 1024 : 0;
const serverInstance = ec2.find((ec) => {
if (adjustedMemory === 0) {
// Exclude memory from search criteria
return ec2.find((ec) => ec.vcpus >= cores);
}
if (adjustedMemory === 0) {
// Exclude memory from search criteria
return ec2.find((ec) => ec.vcpus >= cores);
}
// Search by all criteria
return ec.mem >= adjustedMemory && ec.vcpus >= cores
// Search by all criteria
return ec.mem >= adjustedMemory && ec.vcpus >= cores;
});
if (!serverInstance) {
@@ -116,10 +116,10 @@ const estimatedServerInstance = computed(() => {
return {
name: serverInstance.name,
cpuInfo: {
modelName: cpu.cpuModel,
totalAvailableCores: cpu.coreCount,
tdp: cpu.tdp
}
modelName: cpu.cpuModel,
totalAvailableCores: cpu.coreCount,
tdp: cpu.tdp,
},
};
});
</script>
@@ -144,17 +144,17 @@ const estimatedServerInstance = computed(() => {
</div>
<AwsEstimate
v-if="jobRuntime && coresAllocated"
v-if="jobRuntimeInSeconds && coresAllocated"
:should-show-aws-estimate="shouldShowAwsEstimate"
:job-runtime="jobRuntime"
:job-runtime-in-seconds="jobRuntimeInSeconds"
:cores-allocated="coresAllocated"
:memory-allocated="memoryAllocated" />
:memory-allocated-in-mebibyte="memoryAllocatedInMebibyte" />
<CarbonEmissions
v-if="estimatedServerInstance && jobRuntime && coresAllocated"
v-if="estimatedServerInstance && jobRuntimeInSeconds && coresAllocated"
:estimated-server-instance="estimatedServerInstance"
:job-runtime-in-seconds="jobRuntime"
:job-runtime-in-seconds="jobRuntimeInSeconds"
:cores-allocated="coresAllocated"
:memory-allocated-in-mebibyte="memoryAllocated" />
:memory-allocated-in-mebibyte="memoryAllocatedInMebibyte" />
</div>
</template>