Skip to content

Commit

Permalink
Merge branch 'master' into chore/rest-graphql-cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
elasticmachine authored Sep 24, 2020
2 parents 1d218e2 + 3618cef commit 16323e1
Show file tree
Hide file tree
Showing 28 changed files with 869 additions and 85 deletions.
4 changes: 1 addition & 3 deletions .telemetryrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@
"src/plugins/kibana_react/",
"src/plugins/testbed/",
"src/plugins/kibana_utils/",
"src/plugins/kibana_usage_collection/server/collectors/kibana/kibana_usage_collector.ts",
"src/plugins/kibana_usage_collection/server/collectors/management/telemetry_management_collector.ts",
"src/plugins/kibana_usage_collection/server/collectors/ui_metric/telemetry_ui_metric_collector.ts",
"src/plugins/telemetry/server/collectors/usage/telemetry_usage_collector.ts"
"src/plugins/kibana_usage_collection/server/collectors/ui_metric/telemetry_ui_metric_collector.ts"
]
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,9 @@ export const parsedWorkingCollector: ParsedUsageCollection = [
type: 'StringKeyword',
},
my_index_signature_prop: {
'': {
'@@INDEX@@': {
kind: SyntaxKind.NumberKeyword,
type: 'NumberKeyword',
},
'@@INDEX@@': {
kind: SyntaxKind.NumberKeyword,
type: 'NumberKeyword',
},
},
my_objects: {
Expand Down

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

26 changes: 24 additions & 2 deletions packages/kbn-telemetry-tools/src/tools/serializer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ export function loadFixtureProgram(fixtureName: string) {
}

describe('getDescriptor', () => {
const usageInterfaces = new Map<string, ts.InterfaceDeclaration>();
const usageInterfaces = new Map<string, ts.InterfaceDeclaration | ts.TypeAliasDeclaration>();
let tsProgram: ts.Program;
beforeAll(() => {
const { program, sourceFile } = loadFixtureProgram('constants');
tsProgram = program;
for (const node of traverseNodes(sourceFile)) {
if (ts.isInterfaceDeclaration(node)) {
if (ts.isInterfaceDeclaration(node) || ts.isTypeAliasDeclaration(node)) {
const interfaceName = node.name.getText();
usageInterfaces.set(interfaceName, node);
}
Expand Down Expand Up @@ -102,4 +102,26 @@ describe('getDescriptor', () => {
'Mapping does not support conflicting union types.'
);
});

it('serializes TypeAliasDeclaration', () => {
const usageInterface = usageInterfaces.get('TypeAliasWithUnion')!;
const descriptor = getDescriptor(usageInterface, tsProgram);
expect(descriptor).toEqual({
locale: { kind: ts.SyntaxKind.StringKeyword, type: 'StringKeyword' },
prop1: { kind: ts.SyntaxKind.StringKeyword, type: 'StringKeyword' },
prop2: { kind: ts.SyntaxKind.StringKeyword, type: 'StringKeyword' },
prop3: { kind: ts.SyntaxKind.StringKeyword, type: 'StringKeyword' },
prop4: { kind: ts.SyntaxKind.StringLiteral, type: 'StringLiteral' },
prop5: { kind: ts.SyntaxKind.FirstLiteralToken, type: 'FirstLiteralToken' },
});
});

it('serializes Record entries', () => {
const usageInterface = usageInterfaces.get('TypeAliasWithRecord')!;
const descriptor = getDescriptor(usageInterface, tsProgram);
expect(descriptor).toEqual({
locale: { kind: ts.SyntaxKind.StringKeyword, type: 'StringKeyword' },
'@@INDEX@@': { kind: ts.SyntaxKind.NumberKeyword, type: 'NumberKeyword' },
});
});
});
27 changes: 24 additions & 3 deletions packages/kbn-telemetry-tools/src/tools/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,13 @@ export function getDescriptor(node: ts.Node, program: ts.Program): Descriptor |
}
if (ts.isTypeLiteralNode(node) || ts.isInterfaceDeclaration(node)) {
return node.members.reduce((acc, m) => {
acc[m.name?.getText() || ''] = getDescriptor(m, program);
return acc;
}, {} as any);
const key = m.name?.getText();
if (key) {
return { ...acc, [key]: getDescriptor(m, program) };
} else {
return { ...acc, ...getDescriptor(m, program) };
}
}, {});
}

// If it's defined as signature { [key: string]: OtherInterface }
Expand Down Expand Up @@ -114,6 +118,10 @@ export function getDescriptor(node: ts.Node, program: ts.Program): Descriptor |
if (symbolName === 'Date') {
return { kind: TelemetryKinds.Date, type: 'Date' };
}
// Support `Record<string, SOMETHING>`
if (symbolName === 'Record' && node.typeArguments![0].kind === ts.SyntaxKind.StringKeyword) {
return { '@@INDEX@@': getDescriptor(node.typeArguments![1], program) };
}
const declaration = (symbol?.getDeclarations() || [])[0];
if (declaration) {
return getDescriptor(declaration, program);
Expand Down Expand Up @@ -157,6 +165,19 @@ export function getDescriptor(node: ts.Node, program: ts.Program): Descriptor |
return uniqueKinds[0];
}

// Support `type MyUsageType = SomethingElse`
if (ts.isTypeAliasDeclaration(node)) {
return getDescriptor(node.type, program);
}

// Support `&` unions
if (ts.isIntersectionTypeNode(node)) {
return node.types.reduce(
(acc, unionNode) => ({ ...acc, ...getDescriptor(unionNode, program) }),
{}
);
}

switch (node.kind) {
case ts.SyntaxKind.NumberKeyword:
case ts.SyntaxKind.BooleanKeyword:
Expand Down
2 changes: 1 addition & 1 deletion packages/kbn-telemetry-tools/src/tools/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ export function difference(actual: any, expected: any) {
function (result, value, key) {
if (key && /@@INDEX@@/.test(`${key}`)) {
// The type definition is an Index Signature, fuzzy searching for similar keys
const regexp = new RegExp(`${key}`.replace(/@@INDEX@@/g, '(.+)?'));
const regexp = new RegExp(`^${key}`.replace(/@@INDEX@@/g, '(.+)?'));
const keysInBase = Object.keys(base)
.map((k) => {
const match = k.match(regexp);
Expand Down
4 changes: 4 additions & 0 deletions src/fixtures/telemetry_collectors/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,7 @@ export const externallyDefinedSchema: MakeSchemaFrom<{ locale: string }> = {
type: 'keyword',
},
};

export type TypeAliasWithUnion = Usage & WithUnion;

export type TypeAliasWithRecord = Usage & Record<string, number>;
30 changes: 11 additions & 19 deletions src/plugins/discover/public/application/angular/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,26 +45,18 @@ const k7Breadcrumbs = ($route) => {
};

getAngularModule().config(($routeProvider) => {
$routeProvider
// deprecated route, kept for compatibility
// should be removed in the future
.when('/context/:indexPatternId/:type/:id*', {
redirectTo: '/context/:indexPatternId/:id',
})
.when('/context/:indexPatternId/:id*', {
controller: ContextAppRouteController,
k7Breadcrumbs,
controllerAs: 'contextAppRoute',
resolve: {
indexPattern: ($route, Promise) => {
const indexPattern = getServices().indexPatterns.get(
$route.current.params.indexPatternId
);
return Promise.props({ ip: indexPattern });
},
$routeProvider.when('/context/:indexPatternId/:id*', {
controller: ContextAppRouteController,
k7Breadcrumbs,
controllerAs: 'contextAppRoute',
resolve: {
indexPattern: ($route, Promise) => {
const indexPattern = getServices().indexPatterns.get($route.current.params.indexPatternId);
return Promise.props({ ip: indexPattern });
},
template: contextAppRouteTemplate,
});
},
template: contextAppRouteTemplate,
});
});

function ContextAppRouteController($routeParams, $scope, $route) {
Expand Down
8 changes: 8 additions & 0 deletions src/plugins/discover/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,14 @@ export class DiscoverPlugin
return `#${path}`;
});
plugins.urlForwarding.forwardApp('context', 'discover', (path) => {
const urlParts = path.split('/');
// take care of urls containing legacy url, those split in the following way
// ["", "context", indexPatternId, _type, id + params]
if (urlParts[4]) {
// remove _type part
const newPath = [...urlParts.slice(0, 3), ...urlParts.slice(4)].join('/');
return `#${newPath}`;
}
return `#${path}`;
});
plugins.urlForwarding.forwardApp('discover', 'discover', (path) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,12 @@ const TYPES = [
];

export interface KibanaSavedObjectCounts {
[pluginName: string]: {
total: number;
};
dashboard: { total: number };
visualization: { total: number };
search: { total: number };
index_pattern: { total: number };
graph_workspace: { total: number };
timelion_sheet: { total: number };
}

export async function getSavedObjectsCounts(
Expand Down Expand Up @@ -71,7 +74,7 @@ export async function getSavedObjectsCounts(
// Initialise the object with all zeros for all the types
const allZeros: KibanaSavedObjectCounts = TYPES.reduce(
(acc, type) => ({ ...acc, [snakeCase(type)]: { total: 0 } }),
{}
{} as KibanaSavedObjectCounts
);

// Add the doc_count from each bucket
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,28 @@ import { take } from 'rxjs/operators';
import { SharedGlobalConfig } from 'kibana/server';
import { UsageCollectionSetup } from 'src/plugins/usage_collection/server';
import { KIBANA_STATS_TYPE } from '../../../common/constants';
import { getSavedObjectsCounts } from './get_saved_object_counts';
import { getSavedObjectsCounts, KibanaSavedObjectCounts } from './get_saved_object_counts';

interface KibanaUsage extends KibanaSavedObjectCounts {
index: string;
}

export function getKibanaUsageCollector(
usageCollection: UsageCollectionSetup,
legacyConfig$: Observable<SharedGlobalConfig>
) {
return usageCollection.makeUsageCollector({
return usageCollection.makeUsageCollector<KibanaUsage, { usage: KibanaUsage }>({
type: 'kibana',
isReady: () => true,
schema: {
index: { type: 'keyword' },
dashboard: { total: { type: 'long' } },
visualization: { total: { type: 'long' } },
search: { total: { type: 'long' } },
index_pattern: { total: { type: 'long' } },
graph_workspace: { total: { type: 'long' } },
timelion_sheet: { total: { type: 'long' } },
},
async fetch(callCluster) {
const {
kibana: { index },
Expand Down
Loading

0 comments on commit 16323e1

Please sign in to comment.