Skip to content

Commit

Permalink
Locate another transformation function in transformations.ts, too
Browse files Browse the repository at this point in the history
transform underlines to dots

Signed-off-by: Christian Muehlbauer <[email protected]>
  • Loading branch information
chrizmc committed Nov 13, 2024
1 parent c078a05 commit 21236aa
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 28 deletions.
15 changes: 4 additions & 11 deletions cdsp/information-layer/handlers/src/HandlerBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
} from "../utils/data-types";
import { getDataPointsPath } from "../config/config";
import { logMessage, MessageType } from "../../utils/logger";
import { transformDataPointsWithUnderscores } from "../utils/transformations";


export abstract class HandlerBase {
// Default implementations of required functions
Expand Down Expand Up @@ -120,15 +122,6 @@ export abstract class HandlerBase {
};
}

/**
* Transforms a message node by replacing dots with underscores.
* @param node - The message node to transform.
* @returns - The transformed message node with dots replaced by underscores.
*/
protected transformDataPointsWithUnderscores(node: string): string {
return node.replace(/\./g, "_");
}

/**
* Reads and parses a data points file in either JSON, YML, or YAML format.
*/
Expand Down Expand Up @@ -188,7 +181,7 @@ export abstract class HandlerBase {
const supportedDataPoints = this.extractDataTypes(dataPointObj);
const result: { [key: string]: any } = {};
Object.entries(supportedDataPoints).forEach(([node, value]) => {
const underscored_node = this.transformDataPointsWithUnderscores(node);
const underscored_node = transformDataPointsWithUnderscores(node);
if (value !== null) {
result[underscored_node] = value;
}
Expand All @@ -210,7 +203,7 @@ export abstract class HandlerBase {
const nodes = message.node ? [message.node] : message.nodes || [];

const unknownFields = nodes.filter(({ name }) => {
const transformedName = this.transformDataPointsWithUnderscores(name);
const transformedName = transformDataPointsWithUnderscores(name);
return !dataPointsSchema.hasOwnProperty(transformedName);
});

Expand Down
9 changes: 5 additions & 4 deletions cdsp/information-layer/handlers/src/iotdb/src/IoTDBHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
import { createErrorMessage } from "../../../../utils/error-message-helper";
import { WebSocket, Message, STATUS_ERRORS, WebSocketWithId } from "../../../utils/data-types";
import { transformSessionDataSet } from "../utils/database-helper";
import { transformDataPointsWithUnderscores } from "../../../utils/transformations";

export class IoTDBHandler extends HandlerBase {
private client: any = null;
Expand Down Expand Up @@ -344,11 +345,11 @@ export class IoTDBHandler extends HandlerBase {

if (message.node) {
dataPoints.push(
this.transformDataPointsWithUnderscores(message.node.name)
transformDataPointsWithUnderscores(message.node.name)
);
} else if (message.nodes) {
dataPoints = message.nodes.map((node) =>
this.transformDataPointsWithUnderscores(node.name)
transformDataPointsWithUnderscores(node.name)
);
}
return dataPoints;
Expand All @@ -372,11 +373,11 @@ export class IoTDBHandler extends HandlerBase {
const data: Record<string, any> = { [dataPointId]: id };

if (message.node) {
data[this.transformDataPointsWithUnderscores(message.node.name)] =
data[transformDataPointsWithUnderscores(message.node.name)] =
message.node.value;
} else if (message.nodes) {
message.nodes.forEach((node) => {
data[this.transformDataPointsWithUnderscores(node.name)] = node.value;
data[transformDataPointsWithUnderscores(node.name)] = node.value;
});
}
return data;
Expand Down
12 changes: 2 additions & 10 deletions cdsp/information-layer/handlers/src/iotdb/utils/database-helper.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { SessionDataSet } from "./SessionDataSet";
import { IoTDBDataInterpreter } from "./IoTDBDataInterpreter";
import { transformDataPointsWithDots } from "../../../utils/transformations";

/**
* Transforms a session data set to a set with the latest values of a vehicle.
Expand Down Expand Up @@ -42,13 +43,4 @@ export function transformSessionDataSet(
name,
value,
}));
}

/**
* Transforms a database field name by replacing underscores with dots.
* @param field - The database filed to transform.
* @returns - The transformed to message node replacing underscores by dots.
*/
function transformDataPointsWithDots(field: string): string {
return field.replace(/\_/g, ".");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
} from "../../../../utils/logger";
import { createErrorMessage } from "../../../../utils/error-message-helper";
import { WebSocket, Message, STATUS_ERRORS } from "../../../utils/data-types";
import { transformDataPointsWithDots, transformDataPointsWithUnderscores } from "../../../utils/transformations";

// Define a type for changes
interface Changes {
Expand All @@ -32,7 +33,7 @@ function parseOnMediaElementChangeResponse(
mediaElement: any
) {
return changes.changedProperties.map((prop) => ({
name: prop,
name: transformDataPointsWithDots(prop),
value: mediaElement[prop],
}));
}
Expand Down Expand Up @@ -109,7 +110,7 @@ export class RealmDBHandler extends HandlerBase {

const transformAndAssign = (element: any, nodes: any[]) => {
nodes.forEach(({ name, value }) => {
const prop = this.transformDataPointsWithUnderscores(name);
const prop = transformDataPointsWithUnderscores(name);
element[prop] = value;
});
};
Expand Down Expand Up @@ -391,7 +392,7 @@ export class RealmDBHandler extends HandlerBase {
const data: { name: string; value: any }[] = [];
const nodes = message.node ? [message.node] : message.nodes;
nodes?.forEach((node: any) => {
const prop = this.transformDataPointsWithUnderscores(node.name);
const prop = transformDataPointsWithUnderscores(node.name);
data.push({
name: node.name,
value: queryResponseObj[prop],
Expand Down
18 changes: 18 additions & 0 deletions cdsp/information-layer/handlers/utils/transformations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Transforms a database field name by replacing underscores with dots.
* @param field - The database filed to transform.
* @returns - The transformed to message node replacing underscores by dots.
*/
export function transformDataPointsWithDots(field: string): string {
return field.replace(/\_/g, ".");
}

/**
* Transforms a message node by replacing dots with underscores.
* @param node - The message node to transform.
* @returns - The transformed message node with dots replaced by underscores.
*/
export function transformDataPointsWithUnderscores(node: string): string {
return node.replace(/\./g, "_");
}

0 comments on commit 21236aa

Please sign in to comment.