Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

♻️ refactor: refactor format utils #3034

Merged
merged 2 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import useSWR from 'swr';

import { ollamaService } from '@/services/ollama';
import { useChatStore } from '@/store/chat';
import { formatSize } from '@/utils/format';

import { ErrorActionContainer, FormAction } from '../../style';
import { formatSize, useDownloadMonitor } from './useDownloadMonitor';
import { useDownloadMonitor } from './useDownloadMonitor';

interface OllamaModelFormProps {
id: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,6 @@
import { useEffect, useRef, useState } from 'react';

import { formatTime } from '@/utils/speed';

export const formatSize = (bytes: number): string => {
const kbSize = bytes / 1024;
if (kbSize < 1024) {
return `${kbSize.toFixed(1)} KB`;
} else if (kbSize < 1_048_576) {
const mbSize = kbSize / 1024;
return `${mbSize.toFixed(1)} MB`;
} else {
const gbSize = kbSize / 1_048_576;
return `${gbSize.toFixed(1)} GB`;
}
};

const formatSpeed = (speed: number): string => {
return `${formatSize(speed)}/s`;
};
import { formatSpeed, formatTime } from '@/utils/format';

export const useDownloadMonitor = (totalSize: number, completedSize: number) => {
const [downloadSpeed, setDownloadSpeed] = useState<string>('0 KB/s');
Expand Down
4 changes: 2 additions & 2 deletions src/features/DataImporter/FileUploading.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import React, { memo } from 'react';
import { useTranslation } from 'react-i18next';
import { Flexbox } from 'react-layout-kit';

import { formatSpeed, formatTime } from '@/utils/speed';
import { formatSpeed, formatTime } from '@/utils/format';

import DataLoading from './Loading';

Expand Down Expand Up @@ -40,7 +40,7 @@ export const FileUploading = memo<FileUploadingProps>(({ progress = 0, speed = 0
{t('importModal.uploading.restTime')}: {restTime ? formatTime(restTime) : '-'}
</span>
<span>
{t('importModal.uploading.speed')}: {formatSpeed(speed)}
{t('importModal.uploading.speed')}: {formatSpeed(speed * 1024)}
</span>
</Flexbox>
</Flexbox>
Expand Down
50 changes: 0 additions & 50 deletions src/hooks/useDownloadSpeed.ts

This file was deleted.

75 changes: 75 additions & 0 deletions src/utils/format.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { describe, expect, it } from 'vitest';

import { formatSize, formatSpeed, formatTime } from './format';

describe('formatSize', () => {
it('should format bytes to KB correctly', () => {
expect(formatSize(1024)).toBe('1.0 KB');
expect(formatSize(2048)).toBe('2.0 KB');
expect(formatSize(1536)).toBe('1.5 KB');
});

it('should format bytes to MB correctly', () => {
expect(formatSize(1048576)).toBe('1.0 MB');
expect(formatSize(2097152)).toBe('2.0 MB');
expect(formatSize(1572864)).toBe('1.5 MB');
});

it('should format bytes to GB correctly', () => {
expect(formatSize(1073741824)).toBe('1.0 GB');
expect(formatSize(2147483648)).toBe('2.0 GB');
expect(formatSize(1610612736)).toBe('1.5 GB');
});

it('should handle edge cases', () => {
expect(formatSize(0)).toBe('0.0 KB');
expect(formatSize(1023)).toBe('1.0 KB');
expect(formatSize(1073741823)).toBe('1024.0 MB');
});
});

describe('formatSpeed', () => {
it('should format speed in KB/s correctly', () => {
expect(formatSpeed(10 * 1024)).toBe('10.00 KB/s');
expect(formatSpeed(999.99 * 1024)).toBe('999.99 KB/s');
});

it('should format speed in MB/s correctly', () => {
expect(formatSpeed(1024 * 1024)).toBe('1.00 MB/s');
expect(formatSpeed(10240 * 1024)).toBe('10.00 MB/s');
});

it('should format speed in GB/s correctly', () => {
expect(formatSpeed(1048576 * 1024)).toBe('1.00 GB/s');
expect(formatSpeed(10485760 * 1024)).toBe('10.00 GB/s');
});

it('should handle edge cases', () => {
expect(formatSpeed(0)).toBe('0.00 Byte/s');
expect(formatSpeed(1000 * 1024)).toBe('1000.00 KB/s');
expect(formatSpeed(1000.01 * 1024)).toBe('0.98 MB/s');
});
});

describe('formatTime', () => {
it('should format time in seconds correctly', () => {
expect(formatTime(30)).toBe('30.0 s');
expect(formatTime(59.9)).toBe('59.9 s');
});

it('should format time in minutes correctly', () => {
expect(formatTime(60)).toBe('1.0 min');
expect(formatTime(3599)).toBe('60.0 min');
});

it('should format time in hours correctly', () => {
expect(formatTime(3600)).toBe('1.00 h');
expect(formatTime(7200)).toBe('2.00 h');
});

it('should handle edge cases', () => {
expect(formatTime(0)).toBe('0.0 s');
expect(formatTime(59.99)).toBe('60.0 s');
expect(formatTime(3599.99)).toBe('60.0 min');
});
});
48 changes: 48 additions & 0 deletions src/utils/format.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
export const formatSize = (bytes: number, fractionDigits = 1): string => {
const kbSize = bytes / 1024;
if (kbSize < 1024) {
return `${kbSize.toFixed(fractionDigits)} KB`;
} else if (kbSize < 1_048_576) {
const mbSize = kbSize / 1024;
return `${mbSize.toFixed(fractionDigits)} MB`;
} else {
const gbSize = kbSize / 1_048_576;
return `${gbSize.toFixed(fractionDigits)} GB`;
}
};

/**
* format speed from Byte number to string like KB/s, MB/s or GB/s
*/
export const formatSpeed = (byte: number, fractionDigits = 2) => {
let word = '';

// Byte
if (byte <= 1000) {
word = byte.toFixed(fractionDigits) + ' Byte/s';
}
// KB
else if (byte / 1024 <= 1000) {
word = (byte / 1024).toFixed(fractionDigits) + ' KB/s';
}
// MB
else if (byte / 1024 / 1024 <= 1000) {
word = (byte / 1024 / 1024).toFixed(fractionDigits) + ' MB/s';
}
// GB
else {
word = (byte / 1024 / 1024 / 1024).toFixed(fractionDigits) + ' GB/s';
}

return word;
};

export const formatTime = (timeInSeconds: number): string => {
if (timeInSeconds < 60) {
return `${timeInSeconds.toFixed(1)} s`;
} else if (timeInSeconds < 3600) {
return `${(timeInSeconds / 60).toFixed(1)} min`;
} else {
return `${(timeInSeconds / 3600).toFixed(2)} h`;
}
};
32 changes: 0 additions & 32 deletions src/utils/speed.ts

This file was deleted.

Loading