Cleanup and rename
This commit is contained in:
@@ -170,22 +170,41 @@ function segmentId(): string {
|
||||
return new Date().toISOString().replace(/[.:]/g, '-');
|
||||
}
|
||||
|
||||
function partitionPathFromSegmentId(id: string): string {
|
||||
const match = /^(\d{4})-(\d{2})-(\d{2})T/.exec(id);
|
||||
if (match) {
|
||||
const [, year, month, day] = match;
|
||||
return path.join(`year=${year}`, `month=${month}`, `day=${day}`);
|
||||
}
|
||||
|
||||
const now = new Date();
|
||||
const year = now.getUTCFullYear().toString();
|
||||
const month = String(now.getUTCMonth() + 1).padStart(2, '0');
|
||||
const day = String(now.getUTCDate()).padStart(2, '0');
|
||||
return path.join(`year=${year}`, `month=${month}`, `day=${day}`);
|
||||
}
|
||||
|
||||
function buildPartitionedFilePath(prefix: string, id: string): string {
|
||||
return path.join(DATA_DIR, partitionPathFromSegmentId(id), `${prefix}-${id}.parquet`);
|
||||
}
|
||||
|
||||
function buildVehicleFile(id: string): string {
|
||||
return path.join(DATA_DIR, `vehicle_positions-${id}.parquet`);
|
||||
return buildPartitionedFilePath('vehicle_positions', id);
|
||||
}
|
||||
|
||||
function buildArrivalFile(id: string): string {
|
||||
return path.join(DATA_DIR, `arrival_records-${id}.parquet`);
|
||||
return buildPartitionedFilePath('arrival_records', id);
|
||||
}
|
||||
|
||||
function buildSnapshotFile(id: string): string {
|
||||
return path.join(DATA_DIR, `vehicle_snapshots-${id}.parquet`);
|
||||
return buildPartitionedFilePath('vehicle_snapshots', id);
|
||||
}
|
||||
|
||||
function createFileWriter(filename: string): Writer {
|
||||
const writer = new ByteWriter() as unknown as Writer & { index: number };
|
||||
const chunkSize = 1_000_000;
|
||||
|
||||
fsSync.mkdirSync(path.dirname(filename), { recursive: true });
|
||||
fsSync.writeFileSync(filename, '', { flag: 'w' });
|
||||
|
||||
const flush = () => {
|
||||
@@ -299,7 +318,12 @@ async function uploadFileToObjectStorage(filePath: string): Promise<boolean> {
|
||||
}
|
||||
|
||||
const keyPrefix = OBJECT_STORAGE_PREFIX ? `${OBJECT_STORAGE_PREFIX}/` : '';
|
||||
const key = `${keyPrefix}${path.basename(filePath)}`;
|
||||
const relativePath = path.relative(DATA_DIR, filePath);
|
||||
const normalizedRelativePath =
|
||||
!relativePath.startsWith('..') && !path.isAbsolute(relativePath)
|
||||
? relativePath.split(path.sep).join('/')
|
||||
: path.basename(filePath);
|
||||
const key = `${keyPrefix}${normalizedRelativePath}`;
|
||||
|
||||
const body = await fs.readFile(filePath);
|
||||
|
||||
@@ -448,10 +472,14 @@ async function ensureInitialized(): Promise<void> {
|
||||
console.log(`[OK] Hyparquet storage ready at ${DATA_DIR}, roll=${ROLL_INTERVAL_MS / 60000}m${storageInfo}`);
|
||||
}
|
||||
|
||||
export async function initDatabase(): Promise<void> {
|
||||
export async function initStorage(): Promise<void> {
|
||||
await ensureInitialized();
|
||||
}
|
||||
|
||||
export async function initDatabase(): Promise<void> {
|
||||
await initStorage();
|
||||
}
|
||||
|
||||
export async function logVehiclePosition(position: VehiclePosition): Promise<void> {
|
||||
await logVehiclePositions([position]);
|
||||
}
|
||||
@@ -566,7 +594,7 @@ export async function cleanupOldData(_daysToKeep: number = 90): Promise<void> {
|
||||
console.log('cleanupOldData skipped: parquet segment mode');
|
||||
}
|
||||
|
||||
export async function getDatabaseStats() {
|
||||
export async function getStorageStats() {
|
||||
await ensureInitialized();
|
||||
|
||||
const [vehicleInfo, arrivalInfo, snapshotInfo] = await Promise.all([
|
||||
@@ -581,9 +609,9 @@ export async function getDatabaseStats() {
|
||||
vehicleSnapshots: writes.vehicleSnapshots,
|
||||
oldestRecord: null,
|
||||
newestRecord: null,
|
||||
dbType: 'hyparquet(rolling-write)',
|
||||
storageType: 'hyparquet(rolling-write)',
|
||||
host: OBJECT_STORAGE_ENABLED ? 'object-storage+local' : 'local-filesystem',
|
||||
database: DATA_DIR,
|
||||
storagePath: DATA_DIR,
|
||||
rolling: {
|
||||
minutes: ROLL_INTERVAL_MS / 60000,
|
||||
currentSegmentId,
|
||||
@@ -616,7 +644,11 @@ export async function getDatabaseStats() {
|
||||
};
|
||||
}
|
||||
|
||||
export async function closeDatabase(): Promise<void> {
|
||||
export async function getDatabaseStats() {
|
||||
return getStorageStats();
|
||||
}
|
||||
|
||||
export async function closeStorage(): Promise<void> {
|
||||
if (!initialized) {
|
||||
return;
|
||||
}
|
||||
@@ -630,3 +662,7 @@ export async function closeDatabase(): Promise<void> {
|
||||
await enqueueRotation('shutdown', false);
|
||||
initialized = false;
|
||||
}
|
||||
|
||||
export async function closeDatabase(): Promise<void> {
|
||||
await closeStorage();
|
||||
}
|
||||
Reference in New Issue
Block a user