@golemio/pid
Version:
Golemio PID Module
68 lines • 4.14 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CommonMessageProcessor = void 0;
const Di_1 = require("../../../../ioc/Di");
const vehicle_positions_1 = require("../../../../../schema-definitions/vehicle-positions");
const CoreToken_1 = require("@golemio/core/dist/helpers/ioc/CoreToken");
const config_1 = require("@golemio/core/dist/integration-engine/config");
const queueprocessors_1 = require("@golemio/core/dist/integration-engine/queueprocessors");
const golemio_errors_1 = require("@golemio/core/dist/shared/golemio-errors");
const ONE_HOUR_IN_MILLIS = 60 * 60 * 1000;
const MIN_HOURS_DIFF_TO_LOG = 22;
class CommonMessageProcessor {
constructor(runsRepository) {
this.runsRepository = runsRepository;
/**
* Process a transformed run message
*
* @param element - The run with message data (already filtered, with isNotPublic flag set)
* @param firstMessageCreatedAt - Timestamp of the first message in the batch
*/
this.processTransformedRun = async (element, firstMessageCreatedAt) => {
this.logElementIssues(element);
const record = await this.runsRepository.getRunRecordForUpdate(element.run);
// Read flag set by filter (avoids duplicate whitelist check)
const isNotPublic = element.isNotPublic === true ? true : undefined;
let outputMsg;
if (record) {
// Data quality check: Ensure scheduled vehicles have timestamps
// Not-public vehicles (replacement buses/trams) don't need scheduled timestamps
// Regular scheduled vehicles MUST have a timestamp or be able to get one from history
if (!element.run_message.actual_stop_timestamp_scheduled) {
// If NOT a not-public vehicle, try to get timestamp from last message
// If we can't get it, skip this message (data quality issue)
if (!isNotPublic) {
const lastRecordMessage = await this.runsRepository["runsMessagesRepository"].getLastMessage(record.id);
if (!lastRecordMessage || !lastRecordMessage.actual_stop_timestamp_scheduled) {
// Cannot process scheduled vehicle without timestamp
return;
}
element.run_message.actual_stop_timestamp_scheduled = lastRecordMessage.actual_stop_timestamp_scheduled;
}
// If IS a not-public vehicle, continue without timestamp (this is expected)
}
outputMsg = await this.runsRepository.updateAndAssociate(element, record.id);
}
else {
outputMsg = await this.runsRepository.createAndAssociate(element);
}
await queueprocessors_1.QueueManager.sendMessageToExchange(`${config_1.config.RABBIT_EXCHANGE_NAME}.${vehicle_positions_1.VehiclePositions.name.toLowerCase()}`, "updateRunsGTFSTripId", {
...outputMsg,
isNotPublic,
notPublicVehicleRouteType: element.notPublicVehicleRouteType,
}, { timestamp: firstMessageCreatedAt });
};
this.logger = Di_1.PidContainer.resolve(CoreToken_1.CoreToken.Logger);
}
logElementIssues(element) {
if (element.run_message.actual_stop_timestamp_scheduled instanceof Date &&
element.run_message.actual_stop_timestamp_scheduled.getTime() - Date.now() >=
MIN_HOURS_DIFF_TO_LOG * ONE_HOUR_IN_MILLIS) {
this.logger.error(new golemio_errors_1.GeneralError(`Message timestamp 'tjr' of value '${element.run_message.actual_stop_timestamp_scheduled.toISOString()}'` +
` is ${MIN_HOURS_DIFF_TO_LOG}+ hrs ahead (line ${element.run.line_short_name}, run` +
` ${element.run.run_number})`, this.constructor.name, undefined, undefined, "pid"));
}
}
}
exports.CommonMessageProcessor = CommonMessageProcessor;
//# sourceMappingURL=CommonMessageProcessor.js.map