mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
chore: Convert show-details telemetry to instantiationDecoratorToken
- Fix bug about throwing error on some calls to showDetails Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
parent
ba8f7f916d
commit
672ddc7b8d
@ -0,0 +1,40 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) OpenLens Authors. All rights reserved.
|
||||||
|
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||||
|
*/
|
||||||
|
import { getInjectable, createInstantiationTargetDecorator, instantiationDecoratorToken } from "@ogre-tools/injectable";
|
||||||
|
import { parseKubeApi } from "../../../common/k8s-api/kube-api-parse";
|
||||||
|
import showDetailsInjectable from "../../../renderer/components/kube-detail-params/show-details.injectable";
|
||||||
|
import emitTelemetryInjectable from "./emit-telemetry.injectable";
|
||||||
|
|
||||||
|
const telemetryDecoratorForShowDetailsInjectable = getInjectable({
|
||||||
|
id: "telemetry-decorator-for-show-details",
|
||||||
|
instantiate: (diForDecorator) => createInstantiationTargetDecorator({
|
||||||
|
target: showDetailsInjectable,
|
||||||
|
decorate: (instantiate) => (di) => {
|
||||||
|
const emitTelemetry = diForDecorator.inject(emitTelemetryInjectable);
|
||||||
|
const showDetails = instantiate(di);
|
||||||
|
|
||||||
|
return (...args) => {
|
||||||
|
emitTelemetry({
|
||||||
|
action: showDetailsInjectable.id,
|
||||||
|
params: {
|
||||||
|
kind: (() => {
|
||||||
|
try {
|
||||||
|
return parseKubeApi(args[0] || "").resource;
|
||||||
|
} catch {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
})(),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return showDetails(...args);
|
||||||
|
};
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
decorable: false,
|
||||||
|
injectionToken: instantiationDecoratorToken,
|
||||||
|
});
|
||||||
|
|
||||||
|
export default telemetryDecoratorForShowDetailsInjectable;
|
||||||
@ -5,13 +5,11 @@
|
|||||||
import { injectionDecoratorToken, getInjectable } from "@ogre-tools/injectable";
|
import { injectionDecoratorToken, getInjectable } from "@ogre-tools/injectable";
|
||||||
import assert from "assert";
|
import assert from "assert";
|
||||||
import emitTelemetryInjectable from "./emit-telemetry.injectable";
|
import emitTelemetryInjectable from "./emit-telemetry.injectable";
|
||||||
import type { WhiteListItem } from "./telemetry-white-list-for-functions.injectable";
|
|
||||||
import telemetryWhiteListForFunctionsInjectable from "./telemetry-white-list-for-functions.injectable";
|
import telemetryWhiteListForFunctionsInjectable from "./telemetry-white-list-for-functions.injectable";
|
||||||
import logErrorInjectable from "../../../common/log-error.injectable";
|
import { isFunction } from "@k8slens/utilities";
|
||||||
import { isFunction, isString } from "@k8slens/utilities";
|
|
||||||
|
|
||||||
const telemetryDecoratorInjectable = getInjectable({
|
const basicTelemetryDecoratorInjectable = getInjectable({
|
||||||
id: "telemetry-decorator",
|
id: "basic-telemetry-decorator",
|
||||||
|
|
||||||
instantiate: (diForDecorator) => ({
|
instantiate: (diForDecorator) => ({
|
||||||
decorate: (instantiateToBeDecorated) =>
|
decorate: (instantiateToBeDecorated) =>
|
||||||
@ -25,31 +23,11 @@ const telemetryDecoratorInjectable = getInjectable({
|
|||||||
assert(currentContext);
|
assert(currentContext);
|
||||||
|
|
||||||
const emitTelemetry = diForDecorator.inject(emitTelemetryInjectable);
|
const emitTelemetry = diForDecorator.inject(emitTelemetryInjectable);
|
||||||
const logError = diForDecorator.inject(logErrorInjectable);
|
|
||||||
const whiteList = diForDecorator.inject(telemetryWhiteListForFunctionsInjectable);
|
const whiteList = diForDecorator.inject(telemetryWhiteListForFunctionsInjectable);
|
||||||
|
|
||||||
const { isWhiteListed, getParams } = findWhiteListEntry(whiteList, currentContext.injectable.id);
|
if (whiteList.has(currentContext.injectable.id)) {
|
||||||
|
|
||||||
if (isWhiteListed) {
|
|
||||||
let params;
|
|
||||||
|
|
||||||
try {
|
|
||||||
params = getParams(...args);
|
|
||||||
} catch (e) {
|
|
||||||
params = {
|
|
||||||
error:
|
|
||||||
"Tried to produce params for telemetry, but getParams() threw an error",
|
|
||||||
};
|
|
||||||
|
|
||||||
logError(
|
|
||||||
`Tried to produce params for telemetry of "${currentContext.injectable.id}", but getParams() threw an error`,
|
|
||||||
e,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
emitTelemetry({
|
emitTelemetry({
|
||||||
action: currentContext.injectable.id,
|
action: currentContext.injectable.id,
|
||||||
params,
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -65,37 +43,4 @@ const telemetryDecoratorInjectable = getInjectable({
|
|||||||
injectionToken: injectionDecoratorToken,
|
injectionToken: injectionDecoratorToken,
|
||||||
});
|
});
|
||||||
|
|
||||||
type WhiteListEntry = {
|
export default basicTelemetryDecoratorInjectable;
|
||||||
isWhiteListed: true;
|
|
||||||
getParams: (...args: unknown[]) => Record<string, any> | undefined;
|
|
||||||
} | {
|
|
||||||
isWhiteListed: false;
|
|
||||||
getParams?: undefined;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
const findWhiteListEntry = (whiteList: WhiteListItem[], id: string): WhiteListEntry => {
|
|
||||||
for (const entry of whiteList) {
|
|
||||||
if (isString(entry)) {
|
|
||||||
if (entry === id) {
|
|
||||||
return {
|
|
||||||
isWhiteListed: true,
|
|
||||||
getParams: () => undefined,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (entry.id === id) {
|
|
||||||
return {
|
|
||||||
isWhiteListed: true,
|
|
||||||
getParams: entry.getParams,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
isWhiteListed: false,
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
export default telemetryDecoratorInjectable;
|
|
||||||
|
|||||||
@ -3,8 +3,6 @@
|
|||||||
* Licensed under MIT License. See LICENSE in root directory for more information.
|
* Licensed under MIT License. See LICENSE in root directory for more information.
|
||||||
*/
|
*/
|
||||||
import { getInjectable } from "@ogre-tools/injectable";
|
import { getInjectable } from "@ogre-tools/injectable";
|
||||||
import type { AppEvent } from "../../../common/app-event-bus/event-bus";
|
|
||||||
import { parseKubeApi } from "../../../common/k8s-api/kube-api-parse";
|
|
||||||
|
|
||||||
const navigateTo = [
|
const navigateTo = [
|
||||||
"navigate-to-preference-tab-id",
|
"navigate-to-preference-tab-id",
|
||||||
@ -92,32 +90,18 @@ const extensions = [
|
|||||||
|
|
||||||
const externalActions = ["open-link-in-browser"];
|
const externalActions = ["open-link-in-browser"];
|
||||||
|
|
||||||
const uiInteraction = [{
|
|
||||||
id: "show-details",
|
|
||||||
getParams: (selfLink: string) => {
|
|
||||||
return {
|
|
||||||
kind: selfLink ? parseKubeApi(selfLink).resource : "",
|
|
||||||
};
|
|
||||||
},
|
|
||||||
}];
|
|
||||||
|
|
||||||
const terminal = ["create-terminal-tab"];
|
const terminal = ["create-terminal-tab"];
|
||||||
|
|
||||||
export type WhiteListItem =
|
|
||||||
| string
|
|
||||||
| { id: string; getParams: (...args: any[]) => AppEvent["params"] };
|
|
||||||
|
|
||||||
const telemetryWhiteListForFunctionsInjectable = getInjectable({
|
const telemetryWhiteListForFunctionsInjectable = getInjectable({
|
||||||
id: "telemetry-white-list-for-functions",
|
id: "telemetry-white-list-for-functions",
|
||||||
instantiate: (): WhiteListItem[] => [
|
instantiate: () => new Set([
|
||||||
...navigateTo,
|
...navigateTo,
|
||||||
...helmInjectableIds,
|
...helmInjectableIds,
|
||||||
...kubeConfigActions,
|
...kubeConfigActions,
|
||||||
...extensions,
|
...extensions,
|
||||||
...externalActions,
|
...externalActions,
|
||||||
...uiInteraction,
|
|
||||||
...terminal,
|
...terminal,
|
||||||
],
|
]),
|
||||||
decorable: false,
|
decorable: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user