Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update device key #211

Merged
merged 22 commits into from
Aug 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
d0d9f80
Adding logic to handle validation message, TODO: save validation message
slevertbiot Aug 4, 2022
0192ed1
not allowing validation events to be handled, will implmenent at a la…
slevertbiot Aug 8, 2022
820d040
Removing uneeded const
slevertbiot Aug 8, 2022
3b4d47f
Fixing test, removing uneeded whitespace and moved logic to class
slevertbiot Aug 9, 2022
2920d3e
Fixing test, removing uneeded whitespace and moved logic to class
slevertbiot Aug 9, 2022
7cc16e1
Merge branch 'master' into handle_validation_message
slevertbiot Aug 9, 2022
5f96ab8
Moving model into a model folder and intnroducing Validation as a model
slevertbiot Aug 9, 2022
a33f3fe
implemented the main logic to populate validation in device document
slevertbiot Aug 10, 2022
8866a69
Adding test cases and sample messages
slevertbiot Aug 10, 2022
dd7d574
Removing old files
slevertbiot Aug 10, 2022
e65b003
Cleaning sonarlint issues
slevertbiot Aug 10, 2022
a4e595b
Removing duplicate catch block
slevertbiot Aug 10, 2022
41d88c6
Removing duplicate catch block
slevertbiot Aug 10, 2022
81fd99c
Fixing tests
slevertbiot Aug 10, 2022
6e38b20
Addressing code review comments
slevertbiot Aug 11, 2022
1ac3deb
Fixing test
slevertbiot Aug 11, 2022
1a92695
Updating readme
slevertbiot Aug 11, 2022
dd82170
Adding docs to classes
slevertbiot Aug 11, 2022
6cff779
Forcing husky to run
slevertbiot Aug 11, 2022
1d68835
initial commit to update device key
slevertbiot Aug 11, 2022
797e5a8
Resolving merge conflicts
slevertbiot Aug 11, 2022
91716fd
fixing husky
slevertbiot Aug 12, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions udmif/event-handler/.husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

cd udmif/event-handler
npx lint-staged
8 changes: 4 additions & 4 deletions udmif/event-handler/src/DeviceDao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ import { DeviceKey } from './model/DeviceKey';
const options = { upsert: true };

export interface DeviceDao {
upsert(filterQuery: any, updateQuery: any);
get(filterQuery: any);
upsert(filterQuery: any, updateQuery: any): Promise<void>;
get(filterQuery: any): Promise<Device>;
}

export class DefaultDeviceDao implements DeviceDao {
constructor(private collection: Collection<Device>) { }
constructor(private collection: Collection<Device>) {}

/**
* Updates a device document if it is found using the device key, else it will insert a new device document
* @param {DeviceKey} deviceKey
* @param {Device} deviceDocument
* @param {Device} deviceDocument
*/
async upsert(deviceKey: DeviceKey, deviceDocument: Device): Promise<void> {
// we're using upsert which will allow document updates if it already exists and a document cretion if it does not
Expand Down
31 changes: 7 additions & 24 deletions udmif/event-handler/src/DeviceDocumentFactory.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import { DeviceBuilder, Device } from './model/Device';
import {
isPointsetSubType,
isSystemSubType,
isValidationSubType,
} from './MessageUtils';
import { isPointsetSubType, isSystemSubType, isValidationSubType } from './MessageUtils';
import { UdmiMessage } from './model/UdmiMessage';
import { PointBuilder, Point } from './model/Point';
import { Validation, ValidationBuilder } from './model/Validation';
Expand All @@ -16,9 +12,7 @@ export class DeviceDocumentFactory {

export function createDeviceDocument(udmiMessage: UdmiMessage, existingPoints: Point[]): Device {
const builder: DeviceBuilder = new DeviceBuilder();
builder
.id(udmiMessage.attributes.deviceNumId)
.name(udmiMessage.attributes.deviceId);
builder.site(udmiMessage.attributes.deviceRegistryId).name(udmiMessage.attributes.deviceId);

if (isSystemSubType(udmiMessage)) {
return buildDeviceDocumentFromSystem(udmiMessage, builder);
Expand All @@ -32,11 +26,7 @@ export function createDeviceDocument(udmiMessage: UdmiMessage, existingPoints: P
/**
* https://faucetsdn.github.io/udmi/gencode/docs/event_system.html describes the incoming schema for an event system message
*/
function buildDeviceDocumentFromSystem(
udmiMessage: UdmiMessage,
builder: DeviceBuilder
): Device {

function buildDeviceDocumentFromSystem(udmiMessage: UdmiMessage, builder: DeviceBuilder): Device {
return builder
.lastPayload(udmiMessage.data.timestamp)
.operational(udmiMessage.data.operational)
Expand All @@ -45,18 +35,14 @@ function buildDeviceDocumentFromSystem(
.model(udmiMessage.data.hardware?.model)
.firmware(udmiMessage.data.software?.firmware)
.section(udmiMessage.data.location?.section)
.site(udmiMessage.data.location?.site)
.id(udmiMessage.attributes.deviceNumId)
.build();
}

/**
* https://faucetsdn.github.io/udmi/gencode/docs/event_validation.html describes the incoming schema for an event validation message
*/
function buildDeviceDocumentFromValidation(
udmiMessage: UdmiMessage,
builder: DeviceBuilder
): Device {

function buildDeviceDocumentFromValidation(udmiMessage: UdmiMessage, builder: DeviceBuilder): Device {
const validation: Validation = new ValidationBuilder()
.timestamp(udmiMessage.data.timestamp)
.category(udmiMessage.data.status.category)
Expand All @@ -65,8 +51,7 @@ function buildDeviceDocumentFromValidation(
.errors(udmiMessage.data.errors)
.build();

return builder.validation(validation)
.build();
return builder.validation(validation).build();
}

/**
Expand All @@ -80,7 +65,7 @@ function buildDeviceDocumentFromPointset(
const points: Point[] = [];

for (let pointCode in udmiMessage.data.points) {
const existingPoint = existingPoints.find(candidatePoint => candidatePoint.name === pointCode);
const existingPoint = existingPoints.find((candidatePoint) => candidatePoint.name === pointCode);
const point: Point = buildPoint(udmiMessage, existingPoint, pointCode);
points.push(point);
}
Expand All @@ -89,7 +74,6 @@ function buildDeviceDocumentFromPointset(
}

function buildPoint(udmiMessage: UdmiMessage, existingPoint: Point, pointCode: string): Point {

const pointValue = udmiMessage.data.points[pointCode];

// we get the value from either the message or the existing point
Expand All @@ -107,4 +91,3 @@ function buildPoint(udmiMessage: UdmiMessage, existingPoint: Point, pointCode: s
.metaCode(pointCode)
.build();
}

12 changes: 6 additions & 6 deletions udmif/event-handler/src/InvalidMessageError.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export class InvalidMessageError extends Error {
constructor(msg: string) {
super(msg);
constructor(msg: string) {
super(msg);

// Set the prototype explicitly.
Object.setPrototypeOf(this, InvalidMessageError.prototype);
}
}
// Set the prototype explicitly.
Object.setPrototypeOf(this, InvalidMessageError.prototype);
}
}
6 changes: 3 additions & 3 deletions udmif/event-handler/src/UdmiMessageHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ export default class UdmiMessageHandler {
throw new InvalidMessageError('An invalid device id was submitted');
}

if (!message.attributes.deviceNumId) {
throw new InvalidMessageError('An invalid device num id was submitted');
if (!message.attributes.deviceRegistryId) {
throw new InvalidMessageError('An invalid site was submitted');
}

return { name: message.attributes.deviceId, id: message.attributes.deviceNumId };
return { name: message.attributes.deviceId, site: message.attributes.deviceRegistryId };
}

private messageCanBeHandled(message: UdmiMessage): boolean {
Expand Down
17 changes: 8 additions & 9 deletions udmif/event-handler/src/__tests__/DeviceDao.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ afterAll(async () => {
beforeEach(async () => {
// clean the collection before each test
await deviceCollection.deleteMany({});
})
});

describe('DeviceDao.upsert', () => {
test('upsert calls the updateOne method on the provided collection', () => {
// arrange
const name: string = 'name';
const id: string = 'id';
const deviceKey: DeviceKey = { name, id };
const deviceDocument: Device = { name, id };
const site: string = 'site-1';
const deviceKey: DeviceKey = { name, site };
const deviceDocument: Device = { name, site };
const deviceDao: DeviceDao = new DefaultDeviceDao(deviceCollection);
const updateOneSpy = jest.spyOn(deviceCollection, 'updateOne').mockImplementation(jest.fn());

Expand All @@ -44,17 +44,16 @@ describe('DeviceDao.upsert', () => {
});

describe('DeviceDao.get', () => {

test('get method is called and returns the matching document', async () => {
// arrange
const findOneSpy = jest.spyOn(deviceCollection, 'findOne');

const name: string = 'name';
const id: string = 'id';
const deviceKey: DeviceKey = { name, id };
const site: string = 'id';
const deviceKey: DeviceKey = { name, site };
const deviceDao: DeviceDao = new DefaultDeviceDao(deviceCollection);

const insertedDeviceDocument: Device = { name, id, points: [], serialNumber: 'randomSerialId' };
const insertedDeviceDocument: Device = { name, site, points: [], serialNumber: 'randomSerialId' };
deviceCollection.insertOne(insertedDeviceDocument);

// act
Expand All @@ -64,4 +63,4 @@ describe('DeviceDao.get', () => {
expect(retrievedDeviceDocument).toEqual(insertedDeviceDocument);
expect(findOneSpy).toHaveBeenCalledWith(deviceKey);
});
});
});
Loading