From 50396ca269f4c9b4bac590a7eb4c9f7b4581433f Mon Sep 17 00:00:00 2001 From: Lauri Nevala Date: Thu, 19 Nov 2020 15:36:51 +0200 Subject: [PATCH] React only prometeheus preference changes to re-initialise prometheus connection Signed-off-by: Lauri Nevala --- src/common/cluster-store.ts | 11 +++++++---- src/main/cluster.ts | 13 ++++++++++--- src/main/context-handler.ts | 4 ++-- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/common/cluster-store.ts b/src/common/cluster-store.ts index 4835d66124..370d00e89c 100644 --- a/src/common/cluster-store.ts +++ b/src/common/cluster-store.ts @@ -52,9 +52,15 @@ export interface ClusterModel { kubeConfig?: string; // yaml } -export interface ClusterPreferences { +export interface ClusterPreferences extends ClusterPrometheusPreferences{ terminalCWD?: string; clusterName?: string; + iconOrder?: number; + icon?: string; + httpsProxy?: string; +} + +export interface ClusterPrometheusPreferences { prometheus?: { namespace: string; service: string; @@ -64,9 +70,6 @@ export interface ClusterPreferences { prometheusProvider?: { type: string; }; - iconOrder?: number; - icon?: string; - httpsProxy?: string; } export class ClusterStore extends BaseStore { diff --git a/src/main/cluster.ts b/src/main/cluster.ts index 7d8b499c4e..6378b55c9a 100644 --- a/src/main/cluster.ts +++ b/src/main/cluster.ts @@ -1,8 +1,8 @@ import { ipcMain } from "electron" -import type { ClusterId, ClusterMetadata, ClusterModel, ClusterPreferences } from "../common/cluster-store" +import type { ClusterId, ClusterMetadata, ClusterModel, ClusterPreferences, ClusterPrometheusPreferences } from "../common/cluster-store" import type { IMetricsReqParams } from "../renderer/api/endpoints/metrics.api"; import type { WorkspaceId } from "../common/workspace-store"; -import { action, computed, observable, reaction, toJS, when } from "mobx"; +import { action, comparer, computed, observable, reaction, toJS, when } from "mobx"; import { apiKubePrefix } from "../common/vars"; import { broadcastMessage } from "../common/ipc"; import { ContextHandler } from "./context-handler" @@ -90,6 +90,13 @@ export class Cluster implements ClusterModel, ClusterState { return this.preferences.clusterName || this.contextName } + @computed get prometheusPreferences(): ClusterPrometheusPreferences { + const { prometheus, prometheusProvider } = this.preferences; + return toJS({ prometheus, prometheusProvider }, { + recurseEverything: true, + }) + } + get version(): string { return String(this.metadata?.version) || "" } @@ -139,7 +146,7 @@ export class Cluster implements ClusterModel, ClusterState { if (ipcMain) { this.eventDisposers.push( reaction(() => this.getState(), () => this.pushState()), - reaction(() => this.preferences, () => this.contextHandler.setupPrometheus(this.preferences)), + reaction(() => this.prometheusPreferences, (prefs) => this.contextHandler.setupPrometheus(prefs), { equals: comparer.structural, }), () => { clearInterval(refreshTimer) clearInterval(refreshMetadataTimer) diff --git a/src/main/context-handler.ts b/src/main/context-handler.ts index 42ede41fac..d25e1e6030 100644 --- a/src/main/context-handler.ts +++ b/src/main/context-handler.ts @@ -1,5 +1,5 @@ import type { PrometheusProvider, PrometheusService } from "./prometheus/provider-registry" -import type { ClusterPreferences } from "../common/cluster-store"; +import type { ClusterPrometheusPreferences } from "../common/cluster-store"; import type { Cluster } from "./cluster" import type httpProxy from "http-proxy" import url, { UrlWithStringQuery } from "url"; @@ -22,7 +22,7 @@ export class ContextHandler { this.setupPrometheus(cluster.preferences); } - public setupPrometheus(preferences: ClusterPreferences = {}) { + public setupPrometheus(preferences: ClusterPrometheusPreferences = {}) { this.prometheusProvider = preferences.prometheusProvider?.type; this.prometheusPath = null; if (preferences.prometheus) {