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

feat: add tooltip model by asset model #341

Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
e4400f1
add tooltip models
afondard May 3, 2024
ce03778
fix delete asset model
afondard May 6, 2024
e98b942
feat: unit fields for metadata & measures tooltip models
afondard May 6, 2024
9f24c19
remove unnecessary comments
afondard May 7, 2024
af768f9
add doc & tests
afondard May 7, 2024
23d4259
review comments
afondard May 15, 2024
5ad8fba
fix tests
afondard May 15, 2024
06817a6
fix tests
afondard May 15, 2024
cd26e8e
fix tests
afondard May 15, 2024
685527c
delete test models
afondard May 16, 2024
5ade43c
Feat tooltipModels add locales, static & suffix
afondard May 27, 2024
5fb76ce
rebase on 2-dev
afondard May 3, 2024
ba5b174
fix delete asset model
afondard May 6, 2024
855d5f3
feat: unit fields for metadata & measures tooltip models
afondard May 6, 2024
df0371f
remove unnecessary comments
afondard May 7, 2024
b98e297
add doc & tests
afondard May 7, 2024
076f131
review comments
afondard May 15, 2024
1857fff
fix tests
afondard May 15, 2024
5e14baa
fix tests
afondard May 15, 2024
37d16c7
fix tests
afondard May 15, 2024
39583af
rebase on 2-dev
afondard May 16, 2024
23d7efd
Feat tooltipModels add locales, static & suffix
afondard May 27, 2024
a1cdadd
merge conflicts
afondard May 27, 2024
f8f1d23
rebase on 2-dev
afondard May 27, 2024
a543df0
add update conflicts check
afondard May 28, 2024
9832784
doc for updateAsset model function
afondard May 28, 2024
ab3fd9f
review comments
afondard May 15, 2024
219e341
fix tests
afondard May 15, 2024
881b2fb
fix tests
afondard May 15, 2024
c80d6e9
fix tests
afondard May 15, 2024
f008e15
rebase on 2-dev
afondard May 27, 2024
b34d19c
add update conflicts check
afondard May 28, 2024
7b37c2a
doc for updateAsset model function
afondard May 28, 2024
50cb223
Merge branch 'KZLPRD-306-ETQ-utilisateur-JVP-d-finir-mes-tooltips-par…
afondard May 28, 2024
bc05189
merge conflicts
afondard May 28, 2024
0e33402
Merge remote-tracking branch 'origin/2-dev' into KZLPRD-306-ETQ-utili…
afondard Jul 2, 2024
6f1a698
changed from put to patch
afondard Jul 2, 2024
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
52 changes: 41 additions & 11 deletions doc/2/controllers/models/write-asset/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,49 @@ Method: POST
};
*/
},
"metadataGroups"; {
/*
Metadata groups list and details.
{
[groupName: string]: {
locales: {
[locale: string]: {
groupFriendlyName: string;
description: string;
"metadataGroups": {
/*
Metadata groups list and details.
{
[groupName: string]: {
locales: {
[locale: string]: {
groupFriendlyName: string;
description: string;
};
};
};
};
};
*/
*/
},
"tooltipModels": {
/*
Tooltip models for an asset model.
[key: string]: {
tooltipLabel: string;
content: [
{
category: "metadata";
label?: string;
afondard marked this conversation as resolved.
Show resolved Hide resolved
metadataPath: string;
unit?: string;
afondard marked this conversation as resolved.
Show resolved Hide resolved
},
{
category: "measure";
label?: string;
measureSlot: string;
measureValuePath: string;
unit?: string;
},
{
category: "standard";
afondard marked this conversation as resolved.
Show resolved Hide resolved
label?: string;
type: "link" | "image" | "text" | "title";
value: string;
}
];
};
*/
},
"measures": [
// Array of measure definition with type and name
Expand All @@ -84,6 +113,7 @@ Method: POST
- `defaultValues`: Default values for the metadata
- `metadataDetails`: Metadata group and translations
- `metadataGroups`: Groups list with translations for group name
- `tooltipModels`: Tooltip model list, containing each labels and tooltip content to display
- `measures`: Array of measure definition. Each item define a `type` and `name` properties for the measure.

---
Expand Down
72 changes: 71 additions & 1 deletion lib/modules/model/ModelService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { BadRequestError, Inflector, NotFoundError } from "kuzzle";
import {
BadRequestError,
Inflector,
KuzzleRequest,
NotFoundError,
} from "kuzzle";
import { ask, onAsk } from "kuzzle-plugin-commons";
import { JSONObject, KDocument } from "kuzzle-sdk";

Expand All @@ -18,6 +23,7 @@ import {
MetadataDetails,
MetadataGroups,
MetadataMappings,
TooltipModels,
} from "./types/ModelContent";
import {
AskModelAssetGet,
Expand Down Expand Up @@ -67,6 +73,7 @@ export class ModelService extends BaseService {
metadataDetails: MetadataDetails,
metadataGroups: MetadataGroups,
measures: AssetModelContent["asset"]["measures"],
tooltipModels: TooltipModels,
): Promise<KDocument<AssetModelContent>> {
if (Inflector.pascalCase(model) !== model) {
throw new BadRequestError(`Asset model "${model}" must be PascalCase.`);
Expand All @@ -80,6 +87,7 @@ export class ModelService extends BaseService {
metadataGroups,
metadataMappings,
model,
tooltipModels,
},
engineGroup,
type: "asset",
Expand Down Expand Up @@ -377,4 +385,66 @@ export class ModelService extends BaseService {

return result.hits[0];
}

/**
* Update an asset model
*/
async updateAsset(
_id: string,
engineGroup: string,
model: string,
metadataMappings: MetadataMappings,
defaultMetadata: JSONObject,
metadataDetails: MetadataDetails,
metadataGroups: MetadataGroups,
measures: AssetModelContent["asset"]["measures"],
tooltipModels: TooltipModels,
request: KuzzleRequest,
): Promise<KDocument<AssetModelContent>> {
if (Inflector.pascalCase(model) !== model) {
throw new BadRequestError(`Asset model "${model}" must be PascalCase.`);
}

this.checkDefaultValues(metadataMappings, defaultMetadata);

// The field must be deleted if an element of the table is to be deleted
await this.sdk.document.deleteFields(
this.config.adminIndex,
InternalCollection.MODELS,
_id,
["asset.tooltipModels"],
{ source: true },
);

const existingAsset = await this.getAsset(engineGroup, model);
const measuresUpdated =
measures.length === 0 ? existingAsset._source.asset.measures : measures;

const assetModel = {
_id,
_source: {
asset: {
defaultMetadata,
measures: measuresUpdated,
metadataDetails,
metadataGroups,
metadataMappings,
model,
tooltipModels,
},
},
};

const endDocument = await this.updateDocument<AssetModelContent>(
request,
assetModel,
{
collection: InternalCollection.MODELS,
engineId: this.config.adminIndex,
},
{ source: true },
);

return endDocument;
}
}
36 changes: 36 additions & 0 deletions lib/modules/model/ModelsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
ApiModelWriteAssetResult,
ApiModelWriteDeviceResult,
ApiModelWriteMeasureResult,
ApiModelUpdateAssetResult,
ApiModelDeleteAssetResult,
ApiModelDeleteDeviceResult,
ApiModelDeleteMeasureResult,
Expand Down Expand Up @@ -64,6 +65,10 @@ export class ModelsController {
handler: this.listMeasures.bind(this),
http: [{ path: "device-manager/models/measures", verb: "get" }],
},
updateAsset: {
handler: this.updateAsset.bind(this),
http: [{ path: "device-manager/models/assets", verb: "put" }],
},
writeAsset: {
handler: this.writeAsset.bind(this),
http: [{ path: "device-manager/models/assets", verb: "post" }],
Expand Down Expand Up @@ -113,6 +118,7 @@ export class ModelsController {
const measures = request.getBodyArray("measures", []);
const metadataDetails = request.getBodyObject("metadataDetails", {});
const metadataGroups = request.getBodyObject("metadataGroups", {});
const tooltipModels = request.getBodyObject("tooltipModels", {});

const assetModel = await this.modelService.writeAsset(
engineGroup,
Expand All @@ -122,6 +128,7 @@ export class ModelsController {
metadataDetails,
metadataGroups,
measures,
tooltipModels,
);

return assetModel;
Expand Down Expand Up @@ -215,4 +222,33 @@ export class ModelsController {
total: models.length,
};
}

async updateAsset(
request: KuzzleRequest,
): Promise<ApiModelUpdateAssetResult> {
const _id = request.getId();
const engineGroup = request.getString("engineGroup");
const model = request.getString("model");
const metadataMappings = request.getBodyObject("metadataMappings", {});
const defaultValues = request.getBodyObject("defaultValues", {});
const measures = request.getBodyArray("measures", []);
const metadataDetails = request.getBodyObject("metadataDetails", {});
const metadataGroups = request.getBodyObject("metadataGroups", {});
const tooltipModels = request.getBodyObject("tooltipModels", {});

const updatedAssetModel = await this.modelService.updateAsset(
_id,
engineGroup,
model,
metadataMappings,
defaultValues,
metadataDetails,
metadataGroups,
measures,
tooltipModels,
request,
);

return updatedAssetModel;
}
}
4 changes: 4 additions & 0 deletions lib/modules/model/ModelsRegister.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
MetadataGroups,
MetadataMappings,
ModelContent,
TooltipModels,
} from "./types/ModelContent";
import { ModelSerializer } from "./ModelSerializer";
import { JSONObject } from "kuzzle-sdk";
Expand Down Expand Up @@ -59,6 +60,7 @@ export class ModelsRegister {
* @param defaultMetadata - The default metadata values for the model, defaults to an empty object.
* @param metadataDetails - Optional detailed metadata descriptions and localizations.
* @param metadataGroups - Optional groups for organizing metadata, with localizations.
* @param tooltipModels - Optional model list for tooltip, containing labels and tooltip content.
* @throws PluginImplementationError if the model name is not in PascalCase.
*/
registerAsset(
Expand All @@ -69,6 +71,7 @@ export class ModelsRegister {
defaultMetadata: JSONObject = {},
metadataDetails: MetadataDetails = {},
metadataGroups: MetadataGroups = {},
tooltipModels: TooltipModels = {},
) {
if (Inflector.pascalCase(model) !== model) {
throw new PluginImplementationError(
Expand All @@ -85,6 +88,7 @@ export class ModelsRegister {
metadataGroups,
metadataMappings,
model,
tooltipModels,
},
engineGroup,
type: "asset",
Expand Down
4 changes: 4 additions & 0 deletions lib/modules/model/collections/modelsMappings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ export const modelsMappings: CollectionMappings = {
name: { type: "keyword" },
},
},
tooltipModels: {
dynamic: "false",
properties: {},
},
},
},

Expand Down
20 changes: 20 additions & 0 deletions lib/modules/model/types/ModelApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
MetadataDetails,
MetadataGroups,
MetadataMappings,
TooltipModels,
} from "./ModelContent";

interface ModelsControllerRequest {
Expand Down Expand Up @@ -43,6 +44,7 @@ export interface ApiModelWriteAssetRequest extends ModelsControllerRequest {
metadataMappings?: MetadataMappings;
defaultValues?: JSONObject;
measures?: AssetModelContent["asset"]["measures"];
tooltipModels?: TooltipModels;
};
}
export type ApiModelWriteAssetResult = KDocument<AssetModelContent>;
Expand Down Expand Up @@ -71,6 +73,24 @@ export interface ApiModelWriteMeasureRequest extends ModelsControllerRequest {
}
export type ApiModelWriteMeasureResult = KDocument<MeasureModelContent>;

export interface ApiModelUpdateAssetRequest extends ModelsControllerRequest {
action: "updateAsset";

_id: string;
engineGroup: string;
model: string;

body: {
metadataDetails?: MetadataDetails;
metadataGroups?: MetadataGroups;
metadataMappings?: MetadataMappings;
defaultValues?: JSONObject;
measures?: AssetModelContent["asset"]["measures"];
tooltipModels?: TooltipModels;
};
}
export type ApiModelUpdateAssetResult = KDocument<AssetModelContent>;

export interface ApiModelDeleteAssetRequest extends ModelsControllerRequest {
action: "deleteAsset";

Expand Down
Loading
Loading