Skip to content

Commit

Permalink
Improved logging functions
Browse files Browse the repository at this point in the history
  • Loading branch information
brendannee committed Oct 24, 2024
1 parent b263e69 commit 0990833
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 81 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Updated
- Improved logging functions

## [4.15.3] - 2024-10-17

### Added
Expand Down
24 changes: 14 additions & 10 deletions src/lib/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import untildify from 'untildify';
import * as models from '../models/models.ts';
import { openDb } from './db.ts';
import { prepDirectory, generateFolderName } from './file-utils.ts';
import { log as _log, logWarning as _logWarning } from './log-utils.ts';
import { log, logWarning } from './log-utils.ts';
import { setDefaultConfig } from './utils.ts';

import { Config, Model, SqlResults } from '../types/global_interfaces.ts';
Expand All @@ -37,8 +37,6 @@ const getAgencies = (db: Database.Database, config: Config) => {

export const exportGtfs = async (initialConfig: Config) => {
const config = setDefaultConfig(initialConfig);
const log = _log(config);
const logWarning = _logWarning(config);
const db = openDb(config);

// Get agency name for export folder from first line of agency.txt
Expand All @@ -50,12 +48,12 @@ export const exportGtfs = async (initialConfig: Config) => {
'No agencies found in SQLite. Be sure to first import data into SQLite using `gtfs-import` or `importGtfs(config);`',
);
} else if (agencyCount > 1) {
logWarning(
logWarning(config)(
'More than one agency is defined in config.json. Export will merge all into one GTFS file.',
);
}

log(
log(config)(
`Starting GTFS export for ${pluralize(
'agency',
agencyCount,
Expand Down Expand Up @@ -87,7 +85,7 @@ export const exportGtfs = async (initialConfig: Config) => {

if (!lines || lines.length === 0) {
if (!model.nonstandard) {
log(
log(config)(
`Skipping (no data) - ${model.filenameBase}.${model.filenameExtension}\r`,
);
}
Expand Down Expand Up @@ -137,18 +135,24 @@ export const exportGtfs = async (initialConfig: Config) => {
);
}

log(`Exporting - ${model.filenameBase}.${model.filenameExtension}\r`);
log(config)(
`Exporting - ${model.filenameBase}.${model.filenameExtension}\r`,
);

return `${model.filenameBase}.${model.filenameExtension}`;
},
);

if (compact(exportedFiles).length === 0) {
log('No GTFS data exported. Be sure to first import data into SQLite.');
log(config)(
'No GTFS data exported. Be sure to first import data into SQLite.',
);
return;
}

log(`Completed GTFS export to ${exportPath}`);
log(config)(`Completed GTFS export to ${exportPath}`);

log(`Completed GTFS export for ${pluralize('agency', agencyCount, true)}\n`);
log(config)(
`Completed GTFS export for ${pluralize('agency', agencyCount, true)}\n`,
);
};
46 changes: 15 additions & 31 deletions src/lib/import-gtfs-realtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ import mapSeries from 'promise-map-series';

import * as models from '../models/models.ts';
import { openDb } from './db.ts';
import {
log as _log,
logError as _logError,
logWarning as _logWarning,
} from './log-utils.ts';
import { log, logError, logWarning } from './log-utils.ts';
import {
convertLongTimeToDate,
setDefaultConfig,
Expand Down Expand Up @@ -120,10 +116,9 @@ async function fetchGtfsRealtimeData(
}

function removeExpiredRealtimeData(config: Config) {
const log = _log(config);
const db = openDb(config);

log(`Removing expired GTFS-Realtime data`);
log(config)(`Removing expired GTFS-Realtime data`);
db.prepare(
`DELETE FROM vehicle_positions WHERE expiration_timestamp <= strftime('%s','now')`,
).run();
Expand All @@ -139,7 +134,7 @@ function removeExpiredRealtimeData(config: Config) {
db.prepare(
`DELETE FROM service_alert_targets WHERE expiration_timestamp <= strftime('%s','now')`,
).run();
log(`Removed expired GTFS-Realtime data\r`, true);
log(config)(`Removed expired GTFS-Realtime data\r`, true);
}

function prepareRealtimeFieldValue(
Expand Down Expand Up @@ -355,15 +350,12 @@ export async function updateGtfsRealtimeData(task: GtfsRealtimeTask) {
export async function updateGtfsRealtime(initialConfig: Config) {
const config = setDefaultConfig(initialConfig);
validateConfigForImport(config);
const log = _log(config);
const logError = _logError(config);
const logWarning = _logWarning(config);

try {
openDb(config);

const agencyCount = config.agencies.length;
log(
log(config)(
`Starting GTFS-Realtime refresh for ${pluralize(
'agencies',
agencyCount,
Expand All @@ -384,42 +376,34 @@ export async function updateGtfsRealtime(initialConfig: Config) {
ignoreErrors: config.ignoreErrors,
sqlitePath: config.sqlitePath,
currentTimestamp: Math.floor(Date.now() / 1000),
log,
logWarning,
logError,
log: log(config),
logWarning: logWarning(config),
logError: logError(config),
};

await updateGtfsRealtimeData(task);
} catch (error: any) {
if (config.ignoreErrors) {
logError(error.message);
logError(config)(error.message);
} else {
throw error;
}
}
});

log(
log(config)(
`Completed GTFS-Realtime refresh for ${pluralize(
'agencies',
agencyCount,
true,
)}\n`,
);
} catch (error: any) {
handleDatabaseError(error, config, logError);
}
}

function handleDatabaseError(
error: any,
config: Config,
logError: (message: string) => void,
): void {
if (error?.code === 'SQLITE_CANTOPEN') {
logError(
`Unable to open sqlite database "${config.sqlitePath}" defined as \`sqlitePath\` config.json. Ensure the parent directory exists or remove \`sqlitePath\` from config.json.`,
);
if (error?.code === 'SQLITE_CANTOPEN') {
logError(config)(
`Unable to open sqlite database "${config.sqlitePath}" defined as \`sqlitePath\` config.json. Ensure the parent directory exists or remove \`sqlitePath\` from config.json.`,
);
}
throw error;
}
throw error;
}
57 changes: 17 additions & 40 deletions src/lib/import-gtfs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@ import { openDb } from './db.ts';
import { unzip } from './file-utils.ts';
import { isValidJSON } from './geojson-utils.ts';
import { updateGtfsRealtimeData } from './import-gtfs-realtime.ts';
import {
log as _log,
logError as _logError,
logWarning as _logWarning,
} from './log-utils.ts';
import { log, logError, logWarning } from './log-utils.ts';
import {
calculateSecondsFromMidnight,
setDefaultConfig,
Expand Down Expand Up @@ -504,15 +500,12 @@ export async function importGtfs(initialConfig: Config): Promise<void> {

const config = setDefaultConfig(initialConfig);
validateConfigForImport(config);
const log = _log(config);
const logError = _logError(config);
const logWarning = _logWarning(config);

try {
const db = openDb(config);
const agencyCount = config.agencies.length;

log(
log(config)(
`Starting GTFS import for ${pluralize('file', agencyCount, true)} using SQLite database at ${config.sqlitePath}`,
);

Expand All @@ -539,9 +532,9 @@ export async function importGtfs(initialConfig: Config): Promise<void> {
sqlitePath: config.sqlitePath,
prefix: agency.prefix,
currentTimestamp: Math.floor(Date.now() / 1000),
log,
logWarning,
logError,
log: log(config),
logWarning: logWarning(config),
logError: logError(config),
};

if (task.url) {
Expand All @@ -554,45 +547,29 @@ export async function importGtfs(initialConfig: Config): Promise<void> {

await rm(tempPath, { recursive: true });
} catch (error: any) {
handleImportError(error, config, logError);
if (config.ignoreErrors) {
logError(config)(error.message);
} else {
throw error;
}
}
});

log(`Creating DB indexes`);
log(config)(`Creating DB indexes`);
createGtfsIndexes(db);

const seconds = Math.round(timer.time() / 1000);
timer.stop();

log(
log(config)(
`Completed GTFS import for ${pluralize('agency', agencyCount, true)} in ${seconds} seconds\n`,
);
} catch (error: any) {
handleDatabaseError(error, config, logError);
}
}

function handleImportError(
error: any,
config: Config,
logError: (message: string) => void,
): void {
if (config.ignoreErrors) {
logError(error.message);
} else {
if (error?.code === 'SQLITE_CANTOPEN') {
logError(config)(
`Unable to open sqlite database "${config.sqlitePath}" defined as \`sqlitePath\` config.json. Ensure the parent directory exists or remove \`sqlitePath\` from config.json.`,
);
}
throw error;
}
}

function handleDatabaseError(
error: any,
config: Config,
logError: (message: string) => void,
): void {
if (error?.code === 'SQLITE_CANTOPEN') {
logError(
`Unable to open sqlite database "${config.sqlitePath}" defined as \`sqlitePath\` config.json. Ensure the parent directory exists or remove \`sqlitePath\` from config.json.`,
);
}
throw error;
}

0 comments on commit 0990833

Please sign in to comment.