Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
Signed-off-by: Stepan Kiryakov <[email protected]>
  • Loading branch information
Stepan-Kirjakov committed Dec 19, 2024
1 parent 63df3fa commit d312bae
Show file tree
Hide file tree
Showing 15 changed files with 257 additions and 40 deletions.
3 changes: 3 additions & 0 deletions common/src/import-export/policy-label.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export class PolicyLabelImportExport {
const config: IPolicyLabelConfig = {
imports: PolicyLabelImportExport.validateImports(data?.imports),
children: PolicyLabelImportExport.validateChildren(data?.children),
schemaId: PolicyLabelImportExport.validateString(data?.schemaId),
}
return config;
}
Expand Down Expand Up @@ -153,6 +154,7 @@ export class PolicyLabelImportExport {
title: PolicyLabelImportExport.validateString(data.title),
tag: PolicyLabelImportExport.validateTag(data.tag),
rule: PolicyLabelImportExport.validateString(data.rule) as any,
schemaId: PolicyLabelImportExport.validateString(data.schemaId),
children: PolicyLabelImportExport.validateChildren(data.children),
};
return child;
Expand All @@ -166,6 +168,7 @@ export class PolicyLabelImportExport {
tag: PolicyLabelImportExport.validateTag(data.tag),
description: PolicyLabelImportExport.validateString(data.description),
owner: PolicyLabelImportExport.validateString(data.owner),
schemaId: PolicyLabelImportExport.validateString(data.schemaId),
messageId: PolicyLabelImportExport.validateString(data.messageId),
config: PolicyLabelImportExport.validateConfig(data.config),
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@

<div class="node-container" *ngFor="let current of steps">
<div *ngIf="current.type === 'validate'" class="node-header">
<span class="node-status" [attr.status]="current.item.status"></span>
<span *ngIf="current.prefix" class="node-prefix">{{current.prefix}}</span>
<span>{{current.title}}</span>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,24 @@
margin-bottom: 4px;
}

.node-status {
width: 8px;
height: 8px;
overflow: hidden;
border-radius: 50%;
background: #AAB7C4;
display: inline-block;
margin: 3px 12px 3px 0px;

&[status="true"] {
background: #19BE47;
}

&[status="false"] {
background: #FF432A;
}
}

.node-body {
padding-top: 12px;
padding-bottom: 16px;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ export class PolicyLabelDocumentViewComponent implements OnInit {
this.steps = this.validator.getDocument();
this.validator.setData(this.relationships);
this.validator.setVp(this.document);
// this.steps.shift();
console.log( this.steps )

//
this.schemasMap = new Map<string, Schema>();
Expand Down
101 changes: 90 additions & 11 deletions guardian-service/src/api/helpers/policy-labels-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
Schema as SchemaCollection,
VcDocument as VcDocumentCollection,
VpDocument as VpDocumentCollection,
SchemaConverterUtils,
} from '@guardian/common';
import {
GenerateUUIDv4,
Expand All @@ -21,10 +22,13 @@ import {
NavItemType,
PolicyType,
Schema,
SchemaCategory,
SchemaHelper,
SchemaStatus,
TopicType
} from '@guardian/interfaces';
import { generateSchema as generateStatisticSchema } from './policy-statistics-helpers.js';
import { generateSchemaContext } from './schema-publish-helper.js';

export function publishLabelConfig(data?: IPolicyLabelConfig): IPolicyLabelConfig {
return data;
Expand Down Expand Up @@ -69,18 +73,29 @@ export async function generateSchema(
node: any,
schema: SchemaCollection
}[]> {
console.log('generateSchema')
const items = convertConfigToList([], config?.children);
console.log('items', items.length);
const nodes = items
.filter((e) => e.type === NavItemType.Statistic || e.type === NavItemType.Rules) as (IRulesItemConfig | IStatisticItemConfig)[];
console.log('nodes', nodes.length);
if (!config) {
return [];
}
const items = convertConfigToList([], config.children);
const schemas: any[] = [];
for (const node of nodes) {
const schema = await generateStatisticSchema(topicId, node.config, owner);
schemas.push({ node, schema });
const groupSchema = await generateGroupSchema(topicId, 'Group', owner);
schemas.push({ node: config, schema: groupSchema });
for (const node of items) {
if (node.type === NavItemType.Statistic) {
const schema = await generateStatisticSchema(topicId, node.config, owner, false);
schemas.push({ node, schema });
}
if (node.type === NavItemType.Rules) {
const schema = await generateStatisticSchema(topicId, node.config, owner, true);
schemas.push({ node, schema });
}
if (node.type === NavItemType.Group) {
schemas.push({ node, schema: groupSchema });
}
if (node.type === NavItemType.Label) {
schemas.push({ node, schema: groupSchema });
}
}
console.log('schemas', schemas.length);
return schemas;
}

Expand Down Expand Up @@ -186,4 +201,68 @@ export async function generateVcDocument(

const vcObject = await vcHelper.createVerifiableCredential(document, didDocument, null, null);
return vcObject;
}
}

export async function generateGroupSchema(topicId: string, type: string, owner: IOwner) {
const uuid = type;
const document: any = {
$id: `#${uuid}`,
$comment: `{ "term": "${uuid}", "@id": "#${uuid}" }`,
title: `${uuid}`,
description: `${uuid}`,
type: 'object',
properties: {
'@context': {
oneOf: [{
type: 'string'
}, {
type: 'array',
items: {
type: 'string'
}
}],
readOnly: true
},
type: {
oneOf: [{
type: 'string'
}, {
type: 'array',
items: {
type: 'string'
}
}],
readOnly: true
},
id: {
type: 'string',
readOnly: true
},
status: {
type: 'boolean',
readOnly: true
}
},
required: [],
additionalProperties: false
}
const newSchema: any = {};
newSchema.category = SchemaCategory.STATISTIC;
newSchema.readonly = true;
newSchema.system = false;
newSchema.uuid = uuid
newSchema.status = SchemaStatus.PUBLISHED;
newSchema.document = document;
newSchema.context = generateSchemaContext(newSchema);
newSchema.iri = `${uuid}`;
newSchema.codeVersion = SchemaConverterUtils.VERSION;
newSchema.documentURL = `schema:${uuid}`;
newSchema.contextURL = `schema:${uuid}`;
newSchema.topicId = topicId;
newSchema.creator = owner.creator;
newSchema.owner = owner.owner;
const schemaObject = DatabaseServer.createSchema(newSchema);
SchemaHelper.setVersion(schemaObject, '1.0.0', null);
SchemaHelper.updateIRI(schemaObject);
return schemaObject;
}
13 changes: 10 additions & 3 deletions guardian-service/src/api/helpers/policy-statistics-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,10 @@ export async function findRelationships(
}

export async function generateSchema(
topicId: string,
config: IStatisticConfig,
owner: IOwner
topicId: string,
config: IStatisticConfig,
owner: IOwner,
rules: boolean = false
) {
const uuid = GenerateUUIDv4();
const variables = config?.variables || [];
Expand Down Expand Up @@ -116,6 +117,12 @@ export async function generateSchema(
readOnly: false
}
}
if (rules) {
properties.status = {
type: 'boolean',
readOnly: true
}
}
const document: any = {
$id: `#${uuid}`,
$comment: `{ "term": "${uuid}", "@id": "#${uuid}" }`,
Expand Down
31 changes: 23 additions & 8 deletions guardian-service/src/api/helpers/schema-publish-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,23 +112,38 @@ export async function publishSchema(
}

/**
* Publish system schemas
* @param systemSchemas
* Publish schemas
* @param schemas
* @param messageServer
* @param user
* @param owner
* @param notifier
*/
export async function publishSchemas(
schemas: SchemaCollection[],
user: IOwner,
schemas: Iterable<SchemaCollection>,
owner: IOwner,
messageServer: MessageServer,
type: MessageAction
): Promise<SchemaCollection[]> {
): Promise<void> {
const tasks = [];
for (const schema of schemas) {
tasks.push(publishSchema(schema, user, messageServer, type));
tasks.push(publishSchema(schema, owner, messageServer, type));
}
await Promise.all(tasks);
}

/**
* Save schemas
* @param schemas
* @param messageServer
* @param owner
* @param notifier
*/
export async function saveSchemas(
schemas: Iterable<SchemaCollection>
): Promise<void> {
for (const schema of schemas) {
await DatabaseServer.createAndSaveSchema(schema);
}
return await Promise.all(tasks);
}

/**
Expand Down
31 changes: 24 additions & 7 deletions guardian-service/src/api/policy-labels.service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
import { ApiResponse } from './helpers/api-response.js';
import { BinaryMessageResponse, DatabaseServer, LabelDocumentMessage, LabelMessage, MessageAction, MessageError, MessageResponse, MessageServer, PinoLogger, PolicyImportExport, PolicyLabel, PolicyLabelImportExport, Users } from '@guardian/common';
import {
BinaryMessageResponse,
DatabaseServer,
LabelDocumentMessage,
LabelMessage,
MessageAction,
MessageError,
MessageResponse,
MessageServer,
PinoLogger,
PolicyImportExport,
PolicyLabel,
PolicyLabelImportExport,
Users,
Schema as SchemaCollection,
} from '@guardian/common';
import { EntityStatus, IOwner, LabelValidators, MessageAPI, PolicyType, Schema, SchemaStatus } from '@guardian/interfaces';
import { findRelationships, generateSchema, generateVpDocument, getOrCreateTopic, publishLabelConfig } from './helpers/policy-labels-helpers.js';
import { publishSchema } from './helpers/index.js';
import { publishSchemas, saveSchemas } from './helpers/index.js';

/**
* Connect to the message broker methods of working with policy labels.
Expand Down Expand Up @@ -155,9 +170,12 @@ export async function policyLabelsAPI(logger: PinoLogger): Promise<void> {
.concat(schemas, toolSchemas)
.filter((s) => s.status === SchemaStatus.PUBLISHED && s.entity !== 'EVC');

const documentsSchemas = await DatabaseServer.getSchemas({ topicId: item.topicId });

return new MessageResponse({
policy,
policySchemas: all,
documentsSchemas
});
} catch (error) {
await logger.error(error, ['GUARDIAN_SERVICE']);
Expand Down Expand Up @@ -265,13 +283,13 @@ export async function policyLabelsAPI(logger: PinoLogger): Promise<void> {
messageServer.setTopicObject(topic);

const schemas = await generateSchema(topic.topicId, item.config, owner);
const tasks = [];
const schemaList = new Set<SchemaCollection>();
for (const { schema } of schemas) {
tasks.push(publishSchema(schema, owner, messageServer, MessageAction.PublishSchema));
schemaList.add(schema);
}
await Promise.all(tasks);
await publishSchemas(schemaList, owner, messageServer, MessageAction.PublishSchema);
await saveSchemas(schemaList);
for (const { node, schema } of schemas) {
await DatabaseServer.createAndSaveSchema(schema);
node.schemaId = schema.iri;
}

Expand Down Expand Up @@ -614,7 +632,6 @@ export async function policyLabelsAPI(logger: PinoLogger): Promise<void> {
const status = validator.validate();

if (!status.valid) {
console.log(JSON.stringify(status, null, 4))
return new MessageError('Invalid item.');
}

Expand Down
4 changes: 2 additions & 2 deletions interfaces/src/interface/policy-label.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export interface IItemConfig {
name?: string;
description?: string;
owner?: string;
schemaId?:string;
}

export interface IGroupItemConfig extends IItemConfig {
Expand All @@ -36,13 +37,11 @@ export interface ILabelItemConfig extends IItemConfig {

export interface IRulesItemConfig extends IItemConfig {
type: NavItemType.Rules;
schemaId?:string;
config?: IStatisticConfig;
}

export interface IStatisticItemConfig extends IItemConfig {
type: NavItemType.Statistic;
schemaId?:string;
messageId?: string;
config?: IStatisticConfig;
}
Expand Down Expand Up @@ -73,6 +72,7 @@ export interface INavLabelImportConfig {
export type INavImportsConfig = INavStatisticImportConfig | INavLabelImportConfig

export interface IPolicyLabelConfig {
schemaId?:string;
imports?: INavImportsConfig[];
children?: INavItemConfig[];
}
Expand Down
Loading

0 comments on commit d312bae

Please sign in to comment.