const asset = {
"id": "d6341699-44d9-40d4-96a7-a9e6399790f4",
"equipmentFamily": "19XRV",
"picControllerVersion": "PIC3",
}
const telemetryResponse = {
"findTelemetry": {
"data": [{
"id": "-",
"assetId": "9e54e15b-f7d8-4573-aa3a-aeed31ed1f40",
"timestamp": 1678968480000,
"brickClass": "Alarm",
"measureName": "al122",
"measureValue": true,
"createdAt": 1678982953477
},
{
"id": "-",
"assetId": "9e54e15b-f7d8-4573-aa3a-aeed31ed1f40",
"timestamp": 1678970700000,
"brickClass": "Alarm",
"measureName": "al160",
"measureValue": true,
"createdAt": 1678985127307
}
],
"limit": 1000
}
}
const alarmMetadataResponses = [{
getAlarmMetaData: {
data: [{
alarmCode: "239",
chillerModel: "19XR",
controllerType: "PIC2",
type: "Alarm",
level: 1,
category: "PROTECTIVE LIMIT",
resetType: "Manual",
},
{
alarmCode: "243",
chillerModel: "19XR",
controllerType: "PIC2",
type: "Alarm",
level: 3,
category: "POTENTIAL FREEZE- UP",
resetType: "Manual",
},
],
},
}, {
getAlarmMetaData: {
data: [{
alarmCode: "253",
chillerModel: "23XRV",
controllerType: "PIC3",
type: "Alarm",
level: 1,
category: "PROTECTIVE LIMIT",
resetType: "Manual",
}, ],
},
}, ];
const getMappedAlarms = () => {
const alarms = telemetryResponse?.findTelemetry?.data ?? [];
const assetAlarms = alarms.filter((alarm) => asset.id === alarm?.assetId && Boolean(alarm));
const alarmMetadataArray = alarmMetadataResponses
.reduce((acc, response) => {
return [
acc,
response?.getAlarmMetaData?.data ?? []).filter((metadata) => Boolean(metadata)) (
]
}, []);
return assetAlarms.map((alarm) => {
const telemetryAlarmCode = alarm.measureName.replace(/\D/g, "");
const isMetadataMatches = (alarmMetadata) => {
const {
chillerModel,
controllerType,
alarmCode
} = alarmMetadata ?? {};
if (!chillerModel && !controllerType && !alarmCode) {
return false;
}
return (
asset.equipmentFamily === chillerModel &&
asset.picControllerVersion === controllerType &&
telemetryAlarmCode === alarmCode
);
};
const metadata = alarmMetadataArray.find((data) => isMetadataMatches(data));
return getTransformedAlarm(alarm, metadata);
})
}
getMappedAlarms()
const asset = {
"id": "d6341699-44d9-40d4-96a7-a9e6399790f4",
"equipmentFamily": "19XRV",
"picControllerVersion": "PIC3",
}
const telemetryResponse = {
"findTelemetry": {
"data": [{
"id": "-",
"assetId": "9e54e15b-f7d8-4573-aa3a-aeed31ed1f40",
"timestamp": 1678968480000,
"brickClass": "Alarm",
"measureName": "al122",
"measureValue": true,
"createdAt": 1678982953477
},
{
"id": "-",
"assetId": "9e54e15b-f7d8-4573-aa3a-aeed31ed1f40",
"timestamp": 1678970700000,
"brickClass": "Alarm",
"measureName": "al160",
"measureValue": true,
"createdAt": 1678985127307
}
],
"limit": 1000
}
}
const alarmMetadataResponses = [{
getAlarmMetaData: {
data: [{
alarmCode: "239",
chillerModel: "19XR",
controllerType: "PIC2",
type: "Alarm",
level: 1,
category: "PROTECTIVE LIMIT",
resetType: "Manual",
},
{
alarmCode: "243",
chillerModel: "19XR",
controllerType: "PIC2",
type: "Alarm",
level: 3,
category: "POTENTIAL FREEZE- UP",
resetType: "Manual",
},
],
},
}, {
getAlarmMetaData: {
data: [{
alarmCode: "253",
chillerModel: "23XRV",
controllerType: "PIC3",
type: "Alarm",
level: 1,
category: "PROTECTIVE LIMIT",
resetType: "Manual",
}, ],
},
}, ];
const getMappedAlarms = () => {
const alarms = telemetryResponse?.findTelemetry?.data ?? [];
const assetAlarms = alarms.filter((alarm) => asset.id === alarm?.assetId && Boolean(alarm));
return assetAlarms.map((alarm) => {
const telemetryAlarmCode = alarm.measureName.replace(/\D/g, "");
const isMetadataMatches = (alarmMetadata) => {
const {
chillerModel,
controllerType,
alarmCode
} = alarmMetadata ?? {};
if (!chillerModel && !controllerType && !alarmCode) {
return false;
}
return (
asset.equipmentFamily === chillerModel &&
asset.picControllerVersion === controllerType &&
telemetryAlarmCode === alarmCode
);
};
const metadata = null;
alarmMetadataResponses.find((response) => {
const metadata = response?.getAlarmMetaData?.data?.find((data) => isMetadataMatches(data));
return metadata;
});
return getTransformedAlarm(alarm, metadata);
})
}
getMappedAlarms()
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
reduce + find | |
save on find |
Test name | Executions per second |
---|---|
reduce + find | 684420.8 Ops/sec |
save on find | 1176040.8 Ops/sec |
This is a complex data processing code snippet, and I'll do my best to break it down and provide an answer.
Code Overview
The code appears to be part of a larger application that processes telemetry data from various sources (e.g., sensors, equipment) and maps the data to alarm metadata. The getMappedAlarms
function is responsible for filtering and transforming the telemetry data based on certain conditions.
Key Functions and Variables
findTelemetry
: A function that returns a filtered array of telemetry data.filter((alarm) => asset.id === alarm?.assetId && Boolean(alarm))
: A filter function that selects alarms with an asset ID matching the current asset.id
.map((alarm) => { ... })
: A map function that transforms each alarm object.isMetadataMatches
: A function that checks if a given telemetry alarm matches a specific metadata pattern.getTransformedAlarm(alarm, metadata)
: A function that takes an alarm object and metadata (if available) to transform the alarm data.Key Data Structures
telemetryResponse
: An object containing the filtered telemetry data.asset.id
: The ID of a specific asset.alarm.measureName
: The name of a telemetry alarm.alarmMetadataResponses
: An array of objects containing alarm metadata responses.getAlarmMetaData
: A property within each alarmMetadataResponse
object that contains the alarm metadata data.Questions
Based on the provided code snippet, I have two questions to clarify:
findTelemetry
function? How does it filter and process the telemetry data?getTransformedAlarm
function? How does it transform the alarm data based on the metadata pattern?Please let me know if I can help with anything else!