Skip to content

Commit

Permalink
feat(usage-statistics): add cloud-templates to diagram open
Browse files Browse the repository at this point in the history
Closes #2786
  • Loading branch information
Niklas Kiefer committed Feb 28, 2022
1 parent 2cf327f commit fe0c0a7
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import { getMetrics } from '../../../util';

import { getEngineProfile as parseEngineProfile } from '../../../util/parse';

import { getCloudTemplates, getPlatformTemplates } from '../../../util/elementTemplates';

import { ENGINES } from '../../../util/Engines';

const HTTP_STATUS_PAYLOAD_TOO_BIG = 413;
Expand Down Expand Up @@ -57,11 +59,7 @@ export default class DiagramOpenEventHandler extends BaseEventHandler {
type
} = tab;

if (type === types.BPMN) {
return await this.onCamundaDiagramOpened(file);
} else { // <cloud-bpmn>, maybe more in the future
return await this.onBpmnDiagramOpened(file, type);
}
return await this.onBpmnDiagramOpened(file, type);
});

subscribe('dmn.modeler.created', () => {
Expand Down Expand Up @@ -192,28 +190,19 @@ export default class DiagramOpenEventHandler extends BaseEventHandler {
onBpmnDiagramOpened = async (file, type, context = {}) => {

const diagramMetrics = await this.generateMetrics(file, type);

const engineProfile = await this.getEngineProfile(file, type);
const elementTemplates = await this.getElementTemplates(file, type);

return await this.onDiagramOpened(types.BPMN, {
diagramMetrics,
engineProfile,
elementTemplates,
...context
});

}

onCamundaDiagramOpened = async (file) => {

const elementTemplates = await this.getElementTemplates(file);

return await this.onBpmnDiagramOpened(file, types.BPMN, {
elementTemplates
});

}

getElementTemplates = async (file) => {
getElementTemplates = async (file, type) => {

const config = this._config;

Expand All @@ -223,7 +212,9 @@ export default class DiagramOpenEventHandler extends BaseEventHandler {
return [];
}

return elementTemplates.map((elementTemplate) => {
const elementTemplateFilter = getElementTemplatesFilter(type);

return elementTemplateFilter(elementTemplates).map((elementTemplate) => {
const { appliesTo, properties } = elementTemplate;

const propertyCounts = properties.map((property) => {
Expand Down Expand Up @@ -264,3 +255,11 @@ function getDefaultExecutionPlatform(type) {

return ENGINES.PLATFORM;
}

function getElementTemplatesFilter(type) {
if (type === 'cloud-bpmn') {
return getCloudTemplates;
}

return getPlatformTemplates;
}
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ describe('<DiagramOpenEventHandler>', () => {
});
});


it('should not send with broken file contents: form', async () => {

// given
Expand Down Expand Up @@ -255,7 +256,7 @@ describe('<DiagramOpenEventHandler>', () => {

describe('element templates', () => {

it('should send element templates', async () => {
async function expectTemplatesSent(elementTemplates, type, expected) {

// given
const subscribe = sinon.spy();
Expand All @@ -267,12 +268,12 @@ describe('<DiagramOpenEventHandler>', () => {
const config = { get: (key, file) => {
configSpy(key, file);

return mockElementTemplates();
return elementTemplates;
} };

const tab = createTab({
file: { path: 'testPath' },
type: 'bpmn'
type
});

// when
Expand All @@ -286,11 +287,81 @@ describe('<DiagramOpenEventHandler>', () => {

const configArgs = configSpy.getCall(0).args;

const elementTemplates = onSend.getCall(0).args[0].elementTemplates;
const sentElementTemplates = onSend.getCall(0).args[0].elementTemplates;

// then
expect(configArgs).to.eql([ 'bpmn.elementTemplates', { path: 'testPath' } ]);
expect(elementTemplates).to.eql([
expect(sentElementTemplates).to.eql(expected);
}


it('should send element templates - platform', async () => {

// given
const elementTemplates = mockPlatformElementTemplates();

const type = 'bpmn';

const expected = [
{
appliesTo: [ 'bpmn:ServiceTask' ],
properties: {
'camunda:asyncBefore': 1,
'camunda:class': 1,
'camunda:inputParameter': 3,
'camunda:outputParameter': 1
}
}
];

// then
await expectTemplatesSent(
elementTemplates,
type,
expected
);
});


it('should send element templates - cloud', async () => {

// given
const elementTemplates = mockCloudElementTemplates();

const type = 'cloud-bpmn';

const expected = [
{
appliesTo: [ 'bpmn:ServiceTask' ],
properties: {
'zeebe:input': 3,
'zeebe:output': 1,
'zeebe:taskDefinition:type': 1,
'zeebe:taskHeader': 1
}
}
];

// then
await expectTemplatesSent(
elementTemplates,
type,
expected
);
});


it('should ONLY send platform element templates (mixture)', async () => {

// given
const elementTemplates = [
...mockPlatformElementTemplates(),
...mockCloudElementTemplates()
];

const type = 'bpmn';

const expected = [
{
appliesTo: [ 'bpmn:ServiceTask' ],
properties: {
Expand All @@ -300,7 +371,45 @@ describe('<DiagramOpenEventHandler>', () => {
'camunda:outputParameter': 1
}
}
]);
];

// then
await expectTemplatesSent(
elementTemplates,
type,
expected
);
});


it('should ONLY send cloud element templates (mixture)', async () => {

// given
const elementTemplates = [
...mockPlatformElementTemplates(),
...mockCloudElementTemplates()
];

const type = 'cloud-bpmn';

const expected = [
{
appliesTo: [ 'bpmn:ServiceTask' ],
properties: {
'zeebe:input': 3,
'zeebe:output': 1,
'zeebe:taskDefinition:type': 1,
'zeebe:taskHeader': 1
}
}
];

// then
await expectTemplatesSent(
elementTemplates,
type,
expected
);
});


Expand All @@ -309,7 +418,7 @@ describe('<DiagramOpenEventHandler>', () => {
// given
const subscribe = sinon.spy();

const config = { get: () => mockElementTemplates() };
const config = { get: () => mockPlatformElementTemplates() };

const onSendSpy = sinon.spy();

Expand Down Expand Up @@ -1604,7 +1713,7 @@ describe('<DiagramOpenEventHandler>', () => {

// helpers //////

function mockElementTemplates() {
function mockPlatformElementTemplates() {
return [
{
appliesTo: [ 'bpmn:ServiceTask' ],
Expand All @@ -1620,6 +1729,23 @@ function mockElementTemplates() {
];
}

function mockCloudElementTemplates() {
return [
{
$schema: 'https://example.com/@camunda/zeebe-element-templates-json-schema/resources/schema.json',
appliesTo: [ 'bpmn:ServiceTask' ],
properties: [
{ binding: { type: 'zeebe:taskDefinition:type' } },
{ binding: { name: 'sender', type: 'zeebe:input' } },
{ binding: { name: 'receivers', type: 'zeebe:input' } },
{ binding: { name: 'messageBody', type: 'zeebe:input' } },
{ binding: { source: 'result', type: 'zeebe:output' } },
{ binding: { key: 'header', type: 'zeebe:taskHeader' } }
]
}
];
}

function createTab(overrides = {}) {
return {
id: 42,
Expand Down

0 comments on commit fe0c0a7

Please sign in to comment.