Skip to content

Commit

Permalink
fix: allow all units for bitrate
Browse files Browse the repository at this point in the history
ie, in case of download speed in MB/s
  • Loading branch information
sharevb committed Jan 1, 2025
1 parent ea71d16 commit 746fe85
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest';
import { amountTransferable, neededRate, transferTimeSeconds } from './data-transfer-rate-converter.service';
import { amountTransferable, transferSpeedRate, transferTimeSeconds } from './data-transfer-rate-converter.service';

describe('data-transfer-converter', () => {
describe('transferTimeSeconds', () => {
Expand All @@ -12,16 +12,24 @@ describe('data-transfer-converter', () => {
})).toBe(80);
});
});
describe('neededRate', () => {
it('compute neededRate', () => {
expect(neededRate({
describe('transferSpeedRate', () => {
it('compute transferSpeedRate', () => {
expect(transferSpeedRate({
dataSize: 100,
dataSizeUnit: 'MB',
hours: 0,
minutes: 1,
seconds: 20,
bitRateUnit: 'Mb',
})).toBe(10);
expect(transferSpeedRate({
dataSize: 100,
dataSizeUnit: 'MB',
hours: 0,
minutes: 1,
seconds: 20,
bitRateUnit: 'MB',
})).toBe(1.25);
});
});
describe('amountTransferable', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type AllSupportedUnits, type BitsUnits, convertStorageAndRateUnits } from '../data-storage-unit-converter/data-storage-unit-converter.service';
import { type AllSupportedUnits, convertStorageAndRateUnits } from '../data-storage-unit-converter/data-storage-unit-converter.service';

export function transferTimeSeconds({
dataSize,
Expand All @@ -9,14 +9,14 @@ export function transferTimeSeconds({
dataSize: number
dataSizeUnit: AllSupportedUnits
bitRate: number
bitRateUnit: BitsUnits
bitRateUnit: AllSupportedUnits
}): number {
const dataSizeInBytes = convertStorageAndRateUnits({ value: dataSize, fromUnit: dataSizeUnit, toUnit: 'B' });
const bitRateInBytes = convertStorageAndRateUnits({ value: bitRate, fromUnit: bitRateUnit, toUnit: 'B' });
return bitRateInBytes > 0 ? dataSizeInBytes / bitRateInBytes : 0;
}

export function neededRate({
export function transferSpeedRate({
dataSize,
dataSizeUnit,
hours,
Expand All @@ -29,7 +29,7 @@ export function neededRate({
hours: number
minutes: number
seconds: number
bitRateUnit: BitsUnits
bitRateUnit: AllSupportedUnits
}): number {
const dataSizeInBits = convertStorageAndRateUnits({ value: dataSize, fromUnit: dataSizeUnit, toUnit: 'b' });
const timeInSeconds = hours * 3600 + minutes * 60 + seconds;
Expand All @@ -45,7 +45,7 @@ export function amountTransferable({
dataSizeUnit,
}: {
bitRate: number
bitRateUnit: BitsUnits
bitRateUnit: AllSupportedUnits
hours: number
minutes: number
seconds: number
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script setup lang="ts">
import { formatDuration, intervalToDuration } from 'date-fns';
import { type AllSupportedUnits, type BitsUnits, displayStorageAndRateUnits } from '../data-storage-unit-converter/data-storage-unit-converter.service';
import { amountTransferable, neededRate, transferTimeSeconds } from './data-transfer-rate-converter.service';
import { type AllSupportedUnits, displayStorageAndRateUnits } from '../data-storage-unit-converter/data-storage-unit-converter.service';
import { amountTransferable, transferSpeedRate, transferTimeSeconds } from './data-transfer-rate-converter.service';
const allStorateUnits = [
{ value: 'B', label: 'Bytes (B)' },
Expand Down Expand Up @@ -35,6 +35,8 @@ const allBitsUnits = [
{ value: 'Yb', label: 'Yottabits (Ybit)' },
];
const allRateUnits = [...allBitsUnits, ...allStorateUnits];
function convertToTimeDisplay(seconds: number) {
if (seconds === 0) {
return '0';
Expand All @@ -59,15 +61,15 @@ const transferTimeOutput = computed(() => {
dataSize: Number(transferTimeInput.value.dataSize),
dataSizeUnit: transferTimeInput.value.dataSizeUnit as AllSupportedUnits,
bitRate: Number(transferTimeInput.value.bitRate),
bitRateUnit: transferTimeInput.value.bitRateUnit as BitsUnits,
bitRateUnit: transferTimeInput.value.bitRateUnit as AllSupportedUnits,
}));
}
catch (e: any) {
return e.toString();
}
});
const neededRateInput = ref<{
const transferSpeedRateInput = ref<{
dataSize: string
dataSizeUnit: string
hours: number
Expand All @@ -82,18 +84,18 @@ const neededRateInput = ref<{
seconds: 0,
bitRateUnit: 'Mb',
});
const neededRateOutput = computed(() => {
const transferSpeedRateOutput = computed(() => {
try {
return displayStorageAndRateUnits({
unit: neededRateInput.value.bitRateUnit as BitsUnits,
unit: transferSpeedRateInput.value.bitRateUnit as AllSupportedUnits,
appendUnit: true,
value: neededRate({
dataSize: Number(neededRateInput.value.dataSize),
dataSizeUnit: neededRateInput.value.dataSizeUnit as AllSupportedUnits,
hours: neededRateInput.value.hours,
minutes: neededRateInput.value.minutes,
seconds: neededRateInput.value.seconds,
bitRateUnit: neededRateInput.value.bitRateUnit as BitsUnits,
value: transferSpeedRate({
dataSize: Number(transferSpeedRateInput.value.dataSize),
dataSizeUnit: transferSpeedRateInput.value.dataSizeUnit as AllSupportedUnits,
hours: transferSpeedRateInput.value.hours,
minutes: transferSpeedRateInput.value.minutes,
seconds: transferSpeedRateInput.value.seconds,
bitRateUnit: transferSpeedRateInput.value.bitRateUnit as AllSupportedUnits,
}),
});
}
Expand Down Expand Up @@ -124,7 +126,7 @@ const amountTransferableOutput = computed(() => {
appendUnit: true,
value: amountTransferable({
bitRate: Number(amountTransferableInput.value.bitRate),
bitRateUnit: amountTransferableInput.value.bitRateUnit as BitsUnits,
bitRateUnit: amountTransferableInput.value.bitRateUnit as AllSupportedUnits,
hours: amountTransferableInput.value.hours,
minutes: amountTransferableInput.value.minutes,
seconds: amountTransferableInput.value.seconds,
Expand Down Expand Up @@ -157,7 +159,7 @@ const amountTransferableOutput = computed(() => {
<c-select
v-model:value="transferTimeInput.bitRateUnit"
searchable
:options="allBitsUnits"
:options="allRateUnits"
placeholder="Select a bit rate unit"
ml-1
/>
Expand All @@ -171,35 +173,35 @@ const amountTransferableOutput = computed(() => {
placeholder="Transfer time will be here..."
/>
</c-card>
<c-card title="Needed Bit Rate" mb-2>
<c-card title="Transfer Bit Rate/Speed" mb-2>
<n-form-item label="Data Size:" label-placement="left">
<n-input v-model:value="neededRateInput.dataSize" placeholder="Data Size..." :min="0" w-full />
<n-input v-model:value="transferSpeedRateInput.dataSize" placeholder="Data Size..." :min="0" w-full />
<c-select
v-model:value="neededRateInput.dataSizeUnit"
v-model:value="transferSpeedRateInput.dataSizeUnit"
:options="allStorateUnits"
placeholder="Select a storage unit"
ml-1
/>
</n-form-item>

<n-form-item label="Duration (h/m/s):" label-placement="left">
<n-input-number v-model:value="neededRateInput.hours" mr-1 placeholder="Hours" :min="0" w-full />
<n-input-number v-model:value="neededRateInput.minutes" mr-1 placeholder="Minutes" :min="0" w-full />
<n-input-number v-model:value="neededRateInput.seconds" mr-1 placeholder="Seconds" :min="0" w-full />
<n-input-number v-model:value="transferSpeedRateInput.hours" mr-1 placeholder="Hours" :min="0" w-full />
<n-input-number v-model:value="transferSpeedRateInput.minutes" mr-1 placeholder="Minutes" :min="0" w-full />
<n-input-number v-model:value="transferSpeedRateInput.seconds" mr-1 placeholder="Seconds" :min="0" w-full />
</n-form-item>

<n-divider />

<div flex items-baseline gap-2>
<InputCopyable
label="Needed Bit Rate:"
label="Transfer Bit Rate/Speed:"
label-position="left"
:value="neededRateOutput"
placeholder="Needed Bit Rate will be here..."
:value="transferSpeedRateOutput"
placeholder="Bit Rate will be here..."
/>
<c-select
v-model:value="transferTimeInput.bitRateUnit"
:options="allBitsUnits"
v-model:value="transferSpeedRateInput.bitRateUnit"
:options="allRateUnits"
placeholder="Select a bit rate unit"
ml-1
/>
Expand All @@ -210,7 +212,7 @@ const amountTransferableOutput = computed(() => {
<n-input v-model:value="amountTransferableInput.bitRate" placeholder="Bit Rate..." :min="0" w-full />
<c-select
v-model:value="amountTransferableInput.bitRateUnit"
:options="allBitsUnits"
:options="allRateUnits"
placeholder="Select a bit rate unit"
ml-1
/>
Expand Down

0 comments on commit 746fe85

Please sign in to comment.