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

[Usage Collection] [schema] Explicit "array" definition #78141

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,19 @@
}
},
"my_array": {
"properties": {
"total": {
"type": "number"
},
"type": {
"type": "boolean"
"type": "array",
"items": {
"properties": {
"total": {
"type": "number"
},
"type": {
"type": "boolean"
}
}
}
},
"my_str_array": { "type": "keyword" }
"my_str_array": { "type": "array", "items": { "type": "keyword" } }
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,15 @@ export const parsedWorkingCollector: ParsedUsageCollection = [
},
},
my_array: {
total: {
type: 'number',
type: 'array',
items: {
total: {
type: 'number',
},
type: { type: 'boolean' },
},
type: { type: 'boolean' },
},
my_str_array: { type: 'keyword' },
my_str_array: { type: 'array', items: { type: 'keyword' } },
},
},
fetch: {
Expand Down Expand Up @@ -91,18 +94,22 @@ export const parsedWorkingCollector: ParsedUsageCollection = [
},
},
my_array: {
total: {
kind: SyntaxKind.NumberKeyword,
type: 'NumberKeyword',
},
type: {
kind: SyntaxKind.BooleanKeyword,
type: 'BooleanKeyword',
items: {
total: {
kind: SyntaxKind.NumberKeyword,
type: 'NumberKeyword',
},
type: {
kind: SyntaxKind.BooleanKeyword,
type: 'BooleanKeyword',
},
},
},
my_str_array: {
kind: SyntaxKind.StringKeyword,
type: 'StringKeyword',
items: {
kind: SyntaxKind.StringKeyword,
type: 'StringKeyword',
},
},
},
},
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 16 additions & 2 deletions packages/kbn-telemetry-tools/src/tools/manage_schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export type AllowedSchemaTypes =
| 'date'
| 'float';

export function compatibleSchemaTypes(type: AllowedSchemaTypes) {
export function compatibleSchemaTypes(type: AllowedSchemaTypes | 'array') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why didnt we add array to the AllowedSchemaTypes type here instead of using union?

Copy link
Member Author

@afharo afharo Sep 24, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because I think TS would allow { type: 'array' } for non-array fields and we'll only catch it when running the check.

switch (type) {
case 'keyword':
case 'text':
Expand All @@ -40,6 +40,8 @@ export function compatibleSchemaTypes(type: AllowedSchemaTypes) {
case 'float':
case 'long':
return 'number';
case 'array':
return 'array';
default:
throw new Error(`Unknown schema type ${type}`);
}
Expand All @@ -66,10 +68,22 @@ export function isObjectMapping(entity: any) {
return false;
}

function isArrayMapping(entity: any): entity is { type: 'array'; items: object } {
return typeof entity === 'object' && entity.type === 'array' && typeof entity.items === 'object';
}

function getValueMapping(value: any) {
return isObjectMapping(value) ? transformToEsMapping(value) : value;
}

function transformToEsMapping(usageMappingValue: any) {
const fieldMapping: any = { properties: {} };
for (const [key, value] of Object.entries(usageMappingValue)) {
fieldMapping.properties[key] = isObjectMapping(value) ? transformToEsMapping(value) : value;
if (isArrayMapping(value)) {
fieldMapping.properties[key] = { ...value, items: getValueMapping(value.items) };
} else {
fieldMapping.properties[key] = getValueMapping(value);
}
}
return fieldMapping;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/kbn-telemetry-tools/src/tools/serializer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ describe('getDescriptor', () => {
expect(descriptor).toEqual({
prop1: { kind: TelemetryKinds.MomentDate, type: 'MomentDate' },
prop2: { kind: TelemetryKinds.MomentDate, type: 'MomentDate' },
prop3: { kind: TelemetryKinds.MomentDate, type: 'MomentDate' },
prop4: { kind: TelemetryKinds.Date, type: 'Date' },
prop3: { items: { kind: TelemetryKinds.MomentDate, type: 'MomentDate' } },
prop4: { items: { kind: TelemetryKinds.Date, type: 'Date' } },
});
});

Expand Down
2 changes: 1 addition & 1 deletion packages/kbn-telemetry-tools/src/tools/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export function getDescriptor(node: ts.Node, program: ts.Program): Descriptor |
}

if (ts.isArrayTypeNode(node)) {
return getDescriptor(node.elementType, program);
return { items: getDescriptor(node.elementType, program) };
}

if (ts.isLiteralTypeNode(node)) {
Expand Down
11 changes: 7 additions & 4 deletions src/fixtures/telemetry_collectors/working_collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,15 @@ export const myCollector = makeUsageCollector<Usage>({
type: { type: 'boolean' },
},
my_array: {
total: {
type: 'number',
type: 'array',
items: {
total: {
type: 'number',
},
type: { type: 'boolean' },
},
type: { type: 'boolean' },
},
my_str_array: { type: 'keyword' },
my_str_array: { type: 'array', items: { type: 'keyword' } },
my_index_signature_prop: {
count: { type: 'number' },
avg: { type: 'number' },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ export async function makeSampleDataUsageCollector(
fetch: fetchProvider(index),
isReady: () => true,
schema: {
installed: { type: 'keyword' },
installed: { type: 'array', items: { type: 'keyword' } },
last_install_date: { type: 'date' },
last_install_set: { type: 'keyword' },
last_uninstall_date: { type: 'date' },
last_uninstall_set: { type: 'keyword' },
uninstalled: { type: 'keyword' },
uninstalled: { type: 'array', items: { type: 'keyword' } },
},
});

Expand Down
10 changes: 8 additions & 2 deletions src/plugins/telemetry/schema/oss_plugins.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@
"sample-data": {
"properties": {
"installed": {
"type": "keyword"
"type": "array",
"items": {
"type": "keyword"
}
},
"last_install_date": {
"type": "date"
Expand All @@ -44,7 +47,10 @@
"type": "keyword"
},
"uninstalled": {
"type": "keyword"
"type": "array",
"items": {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about renaming items to properties?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to follow similarities to the JSON-schema specification (even though we are not fully using that).

I think items or an array make more sense to me. They are not properties or the array.

But happy to change it if you think properties is the right way to go.

"type": "keyword"
}
}
}
},
Expand Down
22 changes: 22 additions & 0 deletions src/plugins/usage_collection/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,14 @@ The `AllowedSchemaTypes` is the list of allowed schema types for the usage field
'keyword', 'text', 'number', 'boolean', 'long', 'date', 'float'
```

### Arrays

If any of your properties is an array, the schema definition must follow the convention below:

```
{ type: 'array', items: {...mySchemaDefinitionOfTheEntriesInTheArray} }
```

### Example

```ts
Expand All @@ -152,6 +160,8 @@ export const myCollector = makeUsageCollector<Usage>({
some_obj: {
total: 123,
},
some_array: ['value1', 'value2'],
some_array_of_obj: [{total: 123}],
};
},
schema: {
Expand All @@ -163,6 +173,18 @@ export const myCollector = makeUsageCollector<Usage>({
type: 'number',
},
},
some_array: {
type: 'array',
items: { type: 'keyword' }
},
some_array_of_obj: {
type: 'array',
items: {
total: {
type: 'number',
},
},
},
},
});
```
Expand Down
20 changes: 16 additions & 4 deletions src/plugins/usage_collection/server/collector/collector.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,10 @@ describe('collector', () => {
isReady: () => false,
fetch: () => ({ testPass: [{ name: 'a', value: 100 }] }),
schema: {
testPass: { name: { type: 'keyword' }, value: { type: 'long' } },
testPass: {
type: 'array',
items: { name: { type: 'keyword' }, value: { type: 'long' } },
},
},
});
expect(collector).toBeDefined();
Expand All @@ -166,7 +169,10 @@ describe('collector', () => {
fetch: () => ({ testPass: [{ name: 'a', value: 100 }], otherProp: 1 }),
// @ts-expect-error
schema: {
testPass: { name: { type: 'keyword' }, value: { type: 'long' } },
testPass: {
type: 'array',
items: { name: { type: 'keyword' }, value: { type: 'long' } },
},
},
});
expect(collector).toBeDefined();
Expand All @@ -185,7 +191,10 @@ describe('collector', () => {
},
// @ts-expect-error
schema: {
testPass: { name: { type: 'keyword' }, value: { type: 'long' } },
testPass: {
type: 'array',
items: { name: { type: 'keyword' }, value: { type: 'long' } },
},
},
});
expect(collector).toBeDefined();
Expand All @@ -203,7 +212,10 @@ describe('collector', () => {
return { otherProp: 1 };
},
schema: {
testPass: { name: { type: 'keyword' }, value: { type: 'long' } },
testPass: {
type: 'array',
items: { name: { type: 'keyword' }, value: { type: 'long' } },
},
otherProp: { type: 'long' },
},
});
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/usage_collection/server/collector/collector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export type RecursiveMakeSchemaFrom<U> = U extends object

export type MakeSchemaFrom<Base> = {
[Key in keyof Base]: Base[Key] extends Array<infer U>
? RecursiveMakeSchemaFrom<U>
? { type: 'array'; items: RecursiveMakeSchemaFrom<U> }
: RecursiveMakeSchemaFrom<Base[Key]>;
};

Expand Down
9 changes: 6 additions & 3 deletions x-pack/plugins/ingest_manager/server/collectors/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,12 @@ export function registerIngestManagerUsageCollector(
offline: { type: 'long' },
},
packages: {
name: { type: 'keyword' },
version: { type: 'keyword' },
enabled: { type: 'boolean' },
type: 'array',
items: {
name: { type: 'keyword' },
version: { type: 'keyword' },
enabled: { type: 'boolean' },
},
},
},
});
Expand Down
Loading