Skip to content

Commit

Permalink
fix: improve measure export to be usable with multiple measures in on…
Browse files Browse the repository at this point in the history
…e measure
  • Loading branch information
Juiced66 committed Feb 19, 2024
1 parent 6082008 commit 459e5c9
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 19 deletions.
49 changes: 33 additions & 16 deletions lib/modules/measure/MeasureExporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,15 @@ export class MeasureExporter extends AbstractExporter<MeasureExportParams> {
return super.prepareExport(engineId, user, exportParams);
}

protected formatHit(columns: Column[], hit: KHit<MeasureContent>) {
return columns.map(({ header: measureName, isMeasure, path }) => {
protected formatHit(
columns: Array<Column & { shouldCheckName: boolean; name: string }>,
hit: KHit<MeasureContent>,
) {
return columns.map(({ name, shouldCheckName, path }) => {
if (
isMeasure &&
shouldCheckName &&
this.target === InternalCollection.ASSETS &&
hit._source.asset?.measureName !== measureName
hit._source.asset?.measureName !== name
) {
return null;
}
Expand Down Expand Up @@ -169,9 +172,28 @@ export class MeasureExporter extends AbstractExporter<MeasureExportParams> {
const measureColumns = await this.generateMeasureColumns(
modelDocument[targetModel].measures,
);
// sometimes we have multiple measures with same type
// detect them and add them a flag that will be used later
const foundDuplicateType = [];

for (const col of measureColumns) {
if (!foundDuplicateType.includes(col.path)) {
const pathFilteredColumns = measureColumns.filter(
(c) => col.path === c.path,
);
if (pathFilteredColumns.length > 1) {
foundDuplicateType.push(col.path);
}
}
}

for (const col of measureColumns) {
if (foundDuplicateType.includes(col.path)) {
col.shouldCheckName = true;
}
}
const columns: Column[] = [
{ header: "Payload Id", path: "_id" },
{ header: "Measure Id", path: "_id" },
{ header: "Measured At", path: "_source.measuredAt" },
{ header: "Measure Type", path: "_source.type" },
{ header: "Device Id", path: "_source.origin._id" },
Expand All @@ -189,7 +211,7 @@ export class MeasureExporter extends AbstractExporter<MeasureExportParams> {

private async generateMeasureColumns(
documentMeasures: NamedMeasures,
): Promise<Array<Column & { isMeasure: true }>> {
): Promise<Array<Column & { shouldCheckName: boolean; name: string }>> {
/**
* @example
* {
Expand All @@ -203,7 +225,8 @@ export class MeasureExporter extends AbstractExporter<MeasureExportParams> {
* }
*/
const mappingsByMeasureType: Record<string, string[]> = {};
const measures: Array<Column & { isMeasure: true }> = [];
const measures: Array<Column & { shouldCheckName: boolean; name: string }> =
[];

for (const { name, type } of documentMeasures) {
if (!(type in mappingsByMeasureType)) {
Expand All @@ -226,18 +249,12 @@ export class MeasureExporter extends AbstractExporter<MeasureExportParams> {
*/
const path = measure.replace(".type", "").replace(".properties", "");

/**
* All of this is here to avoid a long header name like 'accelerationSensor.acceleration.x' (we don't need the 'acceleration' part)
*
* @example
* 'temperatureInt', 'accelerationSensor.x', 'accelerationSensor.accuracy'
*/
const header = `${name}.${path}`.replace(`${name}.${type}`, name);

const header = `${name}.${path}`;
measures.push({
header,
isMeasure: true,
name,
path: `_source.values.${path}`,
shouldCheckName: false,
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ describe("AssetsController:exportMeasures", () => {

expect(csv).toHaveLength(5);
expect(csv[0]).toBe(
"Payload Id,Measured At,Measure Type,Device Id,Device Model,Asset Id,Asset Model,temperatureExt,temperatureInt,position,position.accuracy,position.altitude,temperatureWeather\n",
"Measure Id,Measured At,Measure Type,Device Id,Device Model,Asset Id,Asset Model,temperatureExt.temperature,temperatureInt.temperature,position.position,position.accuracy,position.altitude,temperatureWeather.temperature\n",
);
const [
payloadId,
Expand Down
4 changes: 2 additions & 2 deletions tests/scenario/modules/devices/action-export-measures.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ describe("DevicesController:exportMeasures", () => {

expect(csv).toHaveLength(26);
expect(csv[0]).toBe(
"Payload Id,Measured At,Measure Type,Device Id,Device Model,Asset Id,Asset Model,temperature,accelerationSensor.x,accelerationSensor.y,accelerationSensor.z,accelerationSensor.accuracy,battery\n",
"Measure Id,Measured At,Measure Type,Device Id,Device Model,Asset Id,Asset Model,temperature.temperature,accelerationSensor.acceleration.x,accelerationSensor.acceleration.y,accelerationSensor.acceleration.z,accelerationSensor.accuracy,battery.battery\n",
);

const [
Expand Down Expand Up @@ -208,7 +208,7 @@ describe("DevicesController:exportMeasures", () => {

expect(csv).toHaveLength(3);
expect(csv[0]).toBe(
"Payload Id,Measured At,Measure Type,Device Id,Device Model,Asset Id,Asset Model,temperature,accelerationSensor.x,accelerationSensor.y,accelerationSensor.z,accelerationSensor.accuracy,battery\n",
"Measure Id,Measured At,Measure Type,Device Id,Device Model,Asset Id,Asset Model,temperature.temperature,accelerationSensor.acceleration.x,accelerationSensor.acceleration.y,accelerationSensor.acceleration.z,accelerationSensor.accuracy,battery.battery\n",
);
});
});

0 comments on commit 459e5c9

Please sign in to comment.