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

refactor: add BaseService #319

Merged
merged 2 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
21 changes: 4 additions & 17 deletions lib/modules/asset/AssetHistoryService.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,17 @@
import { PluginContext } from "kuzzle";
import { mCreateRequest } from "kuzzle-sdk";

import {
DeviceManagerConfiguration,
DeviceManagerPlugin,
InternalCollection,
} from "../plugin";
import { onAsk } from "../shared";
import { DeviceManagerPlugin, InternalCollection } from "../plugin";
import { onAsk, BaseService } from "../shared";

import { AskAssetHistoryAdd } from "./types/AssetEvents";
import {
AssetHistoryContent,
AssetHistoryEvent,
} from "./types/AssetHistoryContent";

export class AssetHistoryService {
private context: PluginContext;
private config: DeviceManagerConfiguration;

private get sdk() {
return this.context.accessors.sdk;
}

export class AssetHistoryService extends BaseService {
constructor(plugin: DeviceManagerPlugin) {
this.context = plugin.context;
this.config = plugin.config;
super(plugin);

onAsk<AskAssetHistoryAdd<AssetHistoryEvent>>(
"ask:device-manager:asset:history:add",
Expand Down
32 changes: 6 additions & 26 deletions lib/modules/asset/AssetService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Backend, BadRequestError, PluginContext, User } from "kuzzle";
import { BadRequestError, User } from "kuzzle";
import {
BaseRequest,
DocumentSearchResult,
Expand All @@ -19,7 +19,6 @@ import {
import { AskModelAssetGet, AssetModelContent } from "../model";
import {
AskEngineList,
DeviceManagerConfiguration,
DeviceManagerPlugin,
InternalCollection,
} from "../plugin";
Expand All @@ -30,6 +29,7 @@ import {
flattenObject,
lock,
onAsk,
BaseService,
} from "../shared";

import { AssetHistoryService } from "./AssetHistoryService";
Expand All @@ -46,35 +46,15 @@ import {
} from "./types/AssetHistoryContent";
import { ApiAssetMigrateTenantResult } from "./types/AssetApi";

export class AssetService {
private context: PluginContext;
private config: DeviceManagerConfiguration;
export class AssetService extends BaseService {
private assetHistoryService: AssetHistoryService;

private get sdk() {
return this.context.accessors.sdk;
}

private get app(): Backend {
return global.app;
}

private get impersonatedSdk() {
return (user: User) => {
if (user?._id) {
return this.sdk.as(user, { checkRights: false });
}

return this.sdk;
};
}

constructor(
plugin: DeviceManagerPlugin,
assetHistoryService: AssetHistoryService
) {
this.context = plugin.context;
this.config = plugin.config;
super(plugin);

this.assetHistoryService = assetHistoryService;

this.registerAskEvents();
Expand Down Expand Up @@ -327,7 +307,7 @@ export class AssetService {
errors = errors.concat(...assets.errors);

if (assets.successes.length === 0) {
this.context.log.error("No assets found to migrate");
this.app.log.error("No assets found to migrate");
return { errors, successes };
}

Expand Down
34 changes: 9 additions & 25 deletions lib/modules/decoder/PayloadService.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,21 @@
import { Backend, BadRequestError, KuzzleRequest, PluginContext } from "kuzzle";
import { BadRequestError, KuzzleRequest } from "kuzzle";
import { JSONObject, KDocument } from "kuzzle-sdk";
import { v4 as uuidv4 } from "uuid";

import {
DeviceManagerPlugin,
DeviceManagerConfiguration,
InternalCollection,
} from "../plugin";
import { DeviceManagerPlugin, InternalCollection } from "../plugin";
import { DeviceContent, DeviceSerializer } from "../device";
import { AskMeasureIngest, DecodedMeasurement } from "../measure";
import { ask, onAsk } from "../shared";
import { BaseService, ask, onAsk } from "../shared";

import { DecodedPayload } from "./DecodedPayload";
import { Decoder } from "./Decoder";
import { AskPayloadReceiveFormated } from "./types/PayloadEvents";
import { DecodingState } from "./DecodingState";
import { SkipError } from "./SkipError";

export class PayloadService {
private config: DeviceManagerConfiguration;
private context: PluginContext;

private get sdk() {
return this.context.accessors.sdk;
}

private get app(): Backend {
return global.app;
}

export class PayloadService extends BaseService {
constructor(plugin: DeviceManagerPlugin) {
this.config = plugin.config as any;
this.context = plugin.context;
super(plugin);

onAsk<AskPayloadReceiveFormated>(
"ask:device-manager:payload:receive-formated",
Expand Down Expand Up @@ -162,7 +146,7 @@ export class PayloadService {
uuid
);
} catch (error) {
this.context.log.error(
this.app.log.error(
`Cannot save the payload from "${deviceModel}": ${error}`
);
}
Expand Down Expand Up @@ -203,7 +187,7 @@ export class PayloadService {
});
devices.push(...newDevices);
} else {
this.context.log.info(
this.app.log.info(
`Skipping new devices "${errors.join(
", "
)}". Auto-provisioning is disabled.`
Expand Down Expand Up @@ -249,7 +233,7 @@ export class PayloadService {
);

for (const error of errors) {
this.context.log.error(
this.app.log.error(
`Cannot create device "${error.document._id}": ${error.reason}`
);
}
Expand Down Expand Up @@ -281,7 +265,7 @@ export class PayloadService {
filter.push({ term: { deviceModel } });
}

const deleted = await this.context.accessors.sdk.bulk.deleteByQuery(
const deleted = await this.sdk.bulk.deleteByQuery(
this.config.adminIndex,
"payloads",
{ query: { bool: { filter } } }
Expand Down
33 changes: 6 additions & 27 deletions lib/modules/device/DeviceService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Backend, BadRequestError, Plugin, PluginContext, User } from "kuzzle";
import { BadRequestError, User } from "kuzzle";
import { JSONObject, KDocument, KHit, SearchResult } from "kuzzle-sdk";

import {
Expand All @@ -7,8 +7,8 @@ import {
AssetHistoryEventLink,
AssetHistoryEventUnlink,
} from "./../asset";
import { InternalCollection, DeviceManagerConfiguration } from "../plugin";
import { Metadata, lock, ask, onAsk } from "../shared";
import { InternalCollection, DeviceManagerPlugin } from "../plugin";
import { Metadata, lock, ask, onAsk, BaseService } from "../shared";
import {
AskModelAssetGet,
AskModelDeviceGet,
Expand All @@ -32,31 +32,10 @@ import { AskPayloadReceiveFormated } from "../decoder/types/PayloadEvents";

type MeasureName = { asset: string; device: string; type: string };

export class DeviceService {
private config: DeviceManagerConfiguration;
private context: PluginContext;
export class DeviceService extends BaseService {
constructor(plugin: DeviceManagerPlugin) {
super(plugin);

private get sdk() {
return this.context.accessors.sdk;
}

private get app(): Backend {
return global.app;
}

private get impersonatedSdk() {
return (user: User) => {
if (user?._id) {
return this.sdk.as(user, { checkRights: false });
}

return this.sdk;
};
}

constructor(plugin: Plugin) {
this.config = plugin.config as any;
this.context = plugin.context;
this.registerAskEvents();
}

Expand Down
38 changes: 12 additions & 26 deletions lib/modules/measure/MeasureService.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import {
Backend,
BadRequestError,
JSONObject,
KDocument,
PluginContext,
} from "kuzzle";
import { BadRequestError, JSONObject, KDocument } from "kuzzle";
import _ from "lodash";

import {
Expand All @@ -16,12 +10,16 @@ import {
AssetSerializer,
} from "../asset";
import { DeviceContent } from "../device";
import { DeviceManagerPlugin, InternalCollection } from "../plugin";
import {
DeviceManagerConfiguration,
DeviceManagerPlugin,
InternalCollection,
} from "../plugin";
import { ask, keepStack, lock, Metadata, objectDiff, onAsk } from "../shared";
ask,
BaseService,
keepStack,
lock,
Metadata,
objectDiff,
onAsk,
} from "../shared";

import { DecodedMeasurement, MeasureContent } from "./types/MeasureContent";
import {
Expand All @@ -34,21 +32,9 @@ import {
TenantEventMeasureProcessBefore,
} from "./types/MeasureEvents";

export class MeasureService {
private config: DeviceManagerConfiguration;
private context: PluginContext;

private get sdk() {
return this.context.accessors.sdk;
}

private get app(): Backend {
return global.app;
}

export class MeasureService extends BaseService {
constructor(plugin: DeviceManagerPlugin) {
this.config = plugin.config as any;
this.context = plugin.context;
super(plugin);

onAsk<AskMeasureIngest>(
"device-manager:measures:ingest",
Expand Down
22 changes: 4 additions & 18 deletions lib/modules/model/ModelService.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
import {
BadRequestError,
Inflector,
NotFoundError,
PluginContext,
} from "kuzzle";
import { BadRequestError, Inflector, NotFoundError } from "kuzzle";
import { JSONObject, KDocument } from "kuzzle-sdk";

import {
AskEngineUpdateAll,
DeviceManagerConfiguration,
DeviceManagerPlugin,
InternalCollection,
} from "../plugin";
import { ask, onAsk } from "../shared/utils/ask";

import { AskAssetRefreshModel } from "../asset";
import { flattenObject } from "../shared/utils/flattenObject";
import { BaseService, flattenObject } from "../shared";
import { ModelSerializer } from "./ModelSerializer";
import {
AssetModelContent,
Expand All @@ -28,17 +22,9 @@ import {
AskModelMeasureGet,
} from "./types/ModelEvents";

export class ModelService {
private config: DeviceManagerConfiguration;
private context: PluginContext;

private get sdk() {
return this.context.accessors.sdk;
}

export class ModelService extends BaseService {
constructor(plugin: DeviceManagerPlugin) {
this.config = plugin.config as any;
this.context = plugin.context;
super(plugin);

this.registerAskEvents();
}
Expand Down
29 changes: 29 additions & 0 deletions lib/modules/shared/services/BaseService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Backend, EmbeddedSDK, User } from "kuzzle";

import { DeviceManagerPlugin, DeviceManagerConfiguration } from "../../plugin";

export abstract class BaseService {
constructor(private plugin: DeviceManagerPlugin) {}

protected get app(): Backend {
return global.app;
}

protected get sdk(): EmbeddedSDK {
return this.plugin.context.accessors.sdk;
}

protected get config(): DeviceManagerConfiguration {
return this.plugin.config;
}

protected get impersonatedSdk() {
return (user: User) => {
if (user?._id) {
return this.sdk.as(user, { checkRights: false });
}

return this.sdk;
};
}
}
1 change: 1 addition & 0 deletions lib/modules/shared/services/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./AbstractExporter";
export * from "./BaseService";
export * from "./DigitalTwinExporter";
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 @@ -91,9 +91,9 @@ describe("DevicesController:exportMeasures", () => {
* Here we specify a measuredAt, because of a side effect in the decoder.
* Sometimes the acceleration measurement would be registered earlier than the temperature's one.
*
* The +1sec is to ensure that those two measures will always remain the last ones.
* The +10sec is to ensure that those two measures will always remain the last ones.
*/
measuredAt: Date.now() + 1000,
measuredAt: Date.now() + 10000,
},
]);
await sdk.collection.refresh("engine-ayse", "measures");
Expand Down
Loading