1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Refactoring

Signed-off-by: Lauri Nevala <lauri.nevala@gmail.com>
This commit is contained in:
Lauri Nevala 2020-04-29 08:28:27 +03:00
parent 40dcbdbf67
commit c6cd1a066a
8 changed files with 46 additions and 27 deletions

View File

@ -47,7 +47,9 @@ export type ClusterPreferences = {
service: string; service: string;
port: number; port: number;
}; };
prometheusSource?: string; prometheusProvider?: {
type: string;
};
icon?: string; icon?: string;
httpsProxy?: string; httpsProxy?: string;
} }

View File

@ -15,7 +15,7 @@ import { shellSync } from "./shell-sync"
import { getFreePort } from "./port" import { getFreePort } from "./port"
import { mangleProxyEnv } from "./proxy-env" import { mangleProxyEnv } from "./proxy-env"
import { findMainWebContents } from "./webcontents" import { findMainWebContents } from "./webcontents"
import { helmCli } from "./helm-cli" import "./prometheus/index"
mangleProxyEnv() mangleProxyEnv()
if (app.commandLine.getSwitchValue("proxy-server") !== "") { if (app.commandLine.getSwitchValue("proxy-server") !== "") {

View File

@ -0,0 +1,9 @@
import { PrometheusLens } from "./lens";
import { PrometheusHelm } from "./helm";
import { PrometheusOperator } from "./operator";
import { PrometheusProviderRegistry } from "./provider";
PrometheusProviderRegistry.registerProvider("lens", new PrometheusLens())
PrometheusProviderRegistry.registerProvider("helm", new PrometheusHelm())
PrometheusProviderRegistry.registerProvider("operator", new PrometheusOperator())

View File

@ -1,4 +1,5 @@
import { PrometheusProvider, PrometheusQuery, PrometheusQueryOpts } from "./provider"; import { PrometheusProvider, PrometheusQuery, PrometheusQueryOpts } from "./provider";
export class PrometheusLens implements PrometheusProvider { export class PrometheusLens implements PrometheusProvider {
rateAccuracy = "1m" rateAccuracy = "1m"

View File

@ -1,4 +1,5 @@
import { PrometheusProvider, PrometheusQuery, PrometheusQueryOpts } from "./provider"; import { PrometheusProvider, PrometheusQuery, PrometheusQueryOpts } from "./provider";
export class PrometheusOperator implements PrometheusProvider { export class PrometheusOperator implements PrometheusProvider {
rateAccuracy = "1m" rateAccuracy = "1m"

View File

@ -1,6 +1,4 @@
import { PrometheusHelm } from "./helm" import logger from "../logger";
import { PrometheusLens } from "./lens"
import { PrometheusOperator } from "./operator";
export type PrometheusQuery = { export type PrometheusQuery = {
[key: string]: string; [key: string]: string;
@ -14,16 +12,19 @@ export interface PrometheusProvider {
getQueries(opts: PrometheusQueryOpts): PrometheusQuery; getQueries(opts: PrometheusQueryOpts): PrometheusQuery;
} }
export class PrometheusProviderFactory { export class PrometheusProviderRegistry {
static createProvider(type: string): PrometheusProvider { private static prometheusProviders: {
if (type == "lens") { [key: string]: PrometheusProvider;
return new PrometheusLens() } = {}
} else if (type == "helm") {
return new PrometheusHelm() static getProvider(type: string): PrometheusProvider {
} else if (type == "operator") { if (!this.prometheusProviders[type]) {
return new PrometheusOperator()
} else {
throw "Unknown Prometheus provider"; throw "Unknown Prometheus provider";
} }
return this.prometheusProviders[type]
}
static registerProvider(key: string, provider: PrometheusProvider) {
this.prometheusProviders[key] = provider
} }
} }

View File

@ -1,7 +1,7 @@
import { LensApiRequest } from "../router" import { LensApiRequest } from "../router"
import { LensApi } from "../lens-api" import { LensApi } from "../lens-api"
import * as requestPromise from "request-promise-native" import * as requestPromise from "request-promise-native"
import { PrometheusProviderFactory, PrometheusProvider} from "../prometheus/provider" import { PrometheusProviderRegistry, PrometheusProvider} from "../prometheus/provider"
type MetricsQuery = string | string[] | { type MetricsQuery = string | string[] | {
[metricName: string]: string; [metricName: string]: string;
@ -23,12 +23,13 @@ class MetricsRoute extends LensApi {
queryParams[key] = value queryParams[key] = value
}) })
const prometheusInstallationSource = cluster.preferences.prometheusSource || "lens" const prometheusInstallationSource = cluster.preferences.prometheusProvider?.type || "lens"
let prometheusProvider: PrometheusProvider let prometheusProvider: PrometheusProvider
try { try {
prometheusProvider = PrometheusProviderFactory.createProvider(prometheusInstallationSource) prometheusProvider = PrometheusProviderRegistry.getProvider(prometheusInstallationSource)
} catch { } catch {
this.respondJson(response, {}) this.respondJson(response, {})
return
} }
// prometheus metrics loader // prometheus metrics loader
const attempts: { [query: string]: number } = {}; const attempts: { [query: string]: number } = {};

View File

@ -36,9 +36,9 @@
description="What query format is used to fetch metrics from Prometheus" description="What query format is used to fetch metrics from Prometheus"
> >
<b-form-select <b-form-select
v-model="prometheusSource" v-model="prometheusProvider"
:options="prometheusSources" :options="prometheusProviders"
@change="onPrometheusSourceSave" @change="onPrometheusProviderSave"
/> />
</b-form-group> </b-form-group>
</div> </div>
@ -81,8 +81,8 @@ export default {
terminalcwd: null terminalcwd: null
}, },
prometheusPath: "", prometheusPath: "",
prometheusSource: "", prometheusProvider: "",
prometheusSources: [ prometheusProviders: [
{ text: "Lens", value: "lens"}, { text: "Lens", value: "lens"},
{ text: "Helm", value: "helm"}, { text: "Helm", value: "helm"},
{ text: "Prometheus Operator", value: "operator"} { text: "Prometheus Operator", value: "operator"}
@ -102,7 +102,11 @@ export default {
} else { } else {
this.prometheusPath = "" this.prometheusPath = ""
} }
this.prometheusSource = this.cluster.preferences.prometheusSource || "lens" if (this.cluster.preferences.prometheusProvider) {
this.prometheusProvider = this.cluster.preferences.prometheusProvider.type
} else {
this.prometheusProvider = "lens"
}
}, },
parsePrometheusPath: function(path) { parsePrometheusPath: function(path) {
let parsed = path.split(/\/|:/) let parsed = path.split(/\/|:/)
@ -143,11 +147,11 @@ export default {
} }
this.$store.dispatch("storeCluster", this.cluster); this.$store.dispatch("storeCluster", this.cluster);
}, },
onPrometheusSourceSave: function() { onPrometheusProviderSave: function() {
if (this.prometheusSource === "") { if (this.prometheusProvider === "") {
this.cluster.preferences.prometheusSource = null; this.cluster.preferences.prometheusProvider = null;
} else { } else {
this.cluster.preferences.prometheusSource = this.prometheusSource this.cluster.preferences.prometheusProvider = { type: this.prometheusProvider }
} }
this.$store.dispatch("storeCluster", this.cluster); this.$store.dispatch("storeCluster", this.cluster);
}, },