Skip to content

Commit

Permalink
[Usage Collection] [schema] Explicit "array" definition
Browse files Browse the repository at this point in the history
  • Loading branch information
afharo committed Sep 22, 2020
1 parent c03c7b3 commit 39a8515
Show file tree
Hide file tree
Showing 17 changed files with 206 additions and 93 deletions.
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 @@ -93,18 +96,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') {
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 @@ -131,7 +131,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": {
"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

0 comments on commit 39a8515

Please sign in to comment.