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IAC9B,OAAO,CAAC,qBAAqB,CAAS;IAE/B,WAAW,EAAE,UAAU,CAAC,eAAe,EAAE,0BAA0B,CAAC,CAAC;IACrE,mBAAmB,EAAE,UAAU,CAAC,CAAC,eAAe,EAAE,qBAAqB,CAAC,EAAE,0BAA0B,CAAC,CAAC;IACtG,YAAY,EAAE,UAAU,CAAC,gBAAgB,EAAE,0BAA0B,CAAC,CAAC;IACvE,oBAAoB,EAAE,UAAU,CAAC,CAAC,gBAAgB,EAAE,sBAAsB,CAAC,EAAE,0BAA0B,CAAC,CAAC;IACzG,YAAY,EAAE,UAAU,CAAC,gBAAgB,EAAE,0BAA0B,CAAC,CAAC;gBAE1D,UAAU,EAAE,UAAU,EAAE,OAAO,CAAC,EAAE,2BAA2B;IA8CjF;;OAEG;IACI,KAAK,IAAI,IAAI;IAkBpB;;;;;;;OAOG;IACH,OAAO,CAAC,qBAAqB;IAiC7B,OAAO,CAAC,6BAA6B;IAuBrC,OAAO,CAAC,sBAAsB;IAgB9B,OAAO,CAAC,8BAA8B;IAoBtC,OAAO,CAAC,sBAAsB;CASjC","file":"inventory-query-caches.d.ts","sourcesContent":["import { Observable, zip } from 'rxjs';\r\nimport { map, retry, share } from 'rxjs/operators';\r\nimport { AppContext } from '../data/app-context';\r\nimport { QueryCache } from '../data/query-cache';\r\nimport { ClusterInventory } from '../shared/cluster-inventory/cluster-inventory';\r\nimport { ClusterInventoryCache } from '../shared/cluster-inventory/cluster-inventory-cache';\r\nimport { GatewayInventory } from '../shared/gateway-inventory/gateway-inventory';\r\nimport { GatewayInventoryCache } from '../shared/gateway-inventory/gateway-inventory-cache';\r\nimport { GatewayInventoryDetail } from '../shared/gateway-inventory/gateway-inventory-detail';\r\nimport { GatewayInventoryDetailCache } from '../shared/gateway-inventory/gateway-inventory-detail-cache';\r\nimport { ServerInventory } from '../shared/server-inventory/server-inventory';\r\nimport { ServerInventoryCache } from '../shared/server-inventory/server-inventory-cache';\r\nimport { ServerInventoryDetail } from '../shared/server-inventory/server-inventory-detail';\r\nimport { ServerInventoryDetailCache } from '../shared/server-inventory/server-inventory-detail-cache';\r\nimport { SharedCacheOptions } from './shared-cache';\r\n\r\nexport interface InventoryQueryCachesParams {\r\n    name?: string;\r\n    retry?: number;\r\n}\r\n\r\nexport interface InventoryQueryCachesOptions {\r\n    serverCache?: {\r\n        expiration?: number\r\n    };\r\n    serverCombinedCache?: {\r\n        expiration?: number\r\n    };\r\n    gatewayCache?: {\r\n        expiration?: number\r\n    };\r\n    gatewayCombinedCache?: {\r\n        expiration?: number\r\n    };\r\n    clusterCache?: {\r\n        expiration?: number\r\n    };\r\n}\r\n\r\nexport class InventoryQueryCaches {\r\n    private serverInventoryCache: ServerInventoryCache;\r\n    private serverInventoryDetailCache: ServerInventoryDetailCache;\r\n    private gatewayInventoryCache: GatewayInventoryCache;\r\n    private gatewayInventoryDetailCache: GatewayInventoryDetailCache;\r\n    private clusterInventoryCache: ClusterInventoryCache;\r\n\r\n    private serverInventorySharedContext = {\r\n        name: <string>null,\r\n        server: false,\r\n        detail: false,\r\n        shared: <Observable<ServerInventory>>null\r\n    };\r\n\r\n    private serverInventoryCacheNodeName: string;\r\n    private gatewayCached = false;\r\n    private gatewayCombinedCached = false;\r\n\r\n    public serverCache: QueryCache<ServerInventory, InventoryQueryCachesParams>;\r\n    public serverCombinedCache: QueryCache<[ServerInventory, ServerInventoryDetail], InventoryQueryCachesParams>;\r\n    public gatewayCache: QueryCache<GatewayInventory, InventoryQueryCachesParams>;\r\n    public gatewayCombinedCache: QueryCache<[GatewayInventory, GatewayInventoryDetail], InventoryQueryCachesParams>;\r\n    public clusterCache: QueryCache<ClusterInventory, InventoryQueryCachesParams>;\r\n\r\n    constructor(private appContext: AppContext, options?: InventoryQueryCachesOptions) {\r\n        let cacheOptions: SharedCacheOptions;\r\n        if (options.serverCache) {\r\n            cacheOptions = options.serverCache.expiration ? { expiration: options.serverCache.expiration } : null;\r\n            this.serverInventoryCache = new ServerInventoryCache(this.appContext, cacheOptions);\r\n            this.serverCache = new QueryCache<ServerInventory, InventoryQueryCachesParams>(\r\n                params => this.createServerInventory(params),\r\n                params => params.name);\r\n        }\r\n\r\n        if (options.serverCache && options.serverCombinedCache) {\r\n            cacheOptions = options.serverCombinedCache.expiration ? { expiration: options.serverCombinedCache.expiration } : null;\r\n            this.serverInventoryDetailCache = new ServerInventoryDetailCache(this.appContext, cacheOptions);\r\n            this.serverCombinedCache = new QueryCache<[ServerInventory, ServerInventoryDetail], InventoryQueryCachesParams>(\r\n                params => this.createServerInventoryCombined(params),\r\n                params => params.name,\r\n                () => this.serverInventoryCacheNodeName = null);\r\n        }\r\n\r\n        if (options.gatewayCache) {\r\n            cacheOptions = options.gatewayCache.expiration ? { expiration: options.gatewayCache.expiration } : null;\r\n            this.gatewayInventoryCache = new GatewayInventoryCache(this.appContext, cacheOptions);\r\n            this.gatewayCache = new QueryCache<GatewayInventory, InventoryQueryCachesParams>(\r\n                params => this.createGatewayInventory(params),\r\n                () => 'gateway',\r\n                () => this.gatewayCached = false);\r\n        }\r\n\r\n        if (options.gatewayCache && options.gatewayCombinedCache) {\r\n            cacheOptions = options.gatewayCombinedCache.expiration ? { expiration: options.gatewayCombinedCache.expiration } : null;\r\n            this.gatewayInventoryDetailCache = new GatewayInventoryDetailCache(this.appContext, cacheOptions);\r\n            this.gatewayCombinedCache = new QueryCache<[GatewayInventory, GatewayInventoryDetail], InventoryQueryCachesParams>(\r\n                params => this.createGatewayInventoryCombined(params),\r\n                () => 'gateway',\r\n                () => this.gatewayCombinedCached = false);\r\n        }\r\n\r\n        if (options.clusterCache) {\r\n            cacheOptions = options.clusterCache.expiration ? { expiration: options.clusterCache.expiration } : null;\r\n            this.clusterInventoryCache = new ClusterInventoryCache(this.appContext, cacheOptions);\r\n            this.clusterCache = new QueryCache<ClusterInventory, InventoryQueryCachesParams>(\r\n                params => this.createClusterInventory(params),\r\n                params => params.name);\r\n        }\r\n    }\r\n\r\n    /**\r\n     * Clear all session storage.\r\n     */\r\n    public clear(): void {\r\n        if (this.serverInventoryCache) {\r\n            this.serverInventoryCache.clear();\r\n        }\r\n        if (this.serverInventoryDetailCache) {\r\n            this.serverInventoryDetailCache.clear();\r\n        }\r\n        if (this.gatewayInventoryCache) {\r\n            this.gatewayInventoryCache.clear();\r\n        }\r\n        if (this.gatewayInventoryDetailCache) {\r\n            this.gatewayInventoryDetailCache.clear();\r\n        }\r\n        if (this.clusterInventoryCache) {\r\n            this.clusterInventoryCache.clear();\r\n        }\r\n    }\r\n\r\n    /**\r\n     * Create server inventory query.\r\n     * - It shares the query with ServerInventory and ServerInventoryCombined. Keep track the request and re-create if asked twice.\r\n     *\r\n     * @param params the inventory query parameters.\r\n     * @param detail the state of server query or detail query.\r\n     * @return Observable<ServerInventory> Shared ServerInventory observable.\r\n     */\r\n    private createServerInventory(params: InventoryQueryCachesParams, detail?: boolean): Observable<ServerInventory> {\r\n        // check\r\n        // - if not created yet.\r\n        // - if requested server name is changed.\r\n        // - if server query second time.\r\n        // - if detail query second time.\r\n        const createNew = !this.serverInventorySharedContext.shared\r\n            || params.name !== this.serverInventorySharedContext.name\r\n            || (this.serverInventorySharedContext.server && !detail)\r\n            || (this.serverInventorySharedContext.detail && detail);\r\n\r\n        if (!createNew) {\r\n            if (detail) {\r\n                this.serverInventorySharedContext.detail = true;\r\n            } else {\r\n                this.serverInventorySharedContext.server = true;\r\n            }\r\n\r\n            // return shared pre-created one.\r\n            return this.serverInventorySharedContext.shared;\r\n        }\r\n\r\n        // creates new shared observable.\r\n        let observable = this.serverInventoryCache.query({ name: params.name })\r\n            .pipe(map(inventory => inventory.instance));\r\n        if (params && params.retry) {\r\n            observable = observable.pipe(retry(params.retry));\r\n        }\r\n\r\n        this.serverInventorySharedContext = { name: params.name, server: !detail, detail: !!detail, shared: observable.pipe(share()) };\r\n        return this.serverInventorySharedContext.shared;\r\n    }\r\n\r\n    private createServerInventoryCombined(params: InventoryQueryCachesParams): Observable<[ServerInventory, ServerInventoryDetail]> {\r\n        let observable: Observable<[ServerInventory, ServerInventoryDetail]>;\r\n        if (!this.serverInventoryCacheNodeName || this.serverInventoryCacheNodeName !== params.name) {\r\n            this.serverInventoryCacheNodeName = params.name;\r\n            observable = zip(\r\n                this.createServerInventory(params, true),\r\n                this.serverInventoryDetailCache.query({ name: params.name })\r\n                    .pipe(map(inventory => inventory.instance)));\r\n        } else {\r\n            observable = zip(\r\n                this.serverInventoryCache.refresh({ name: params.name })\r\n                    .pipe(map(inventory => inventory.instance)),\r\n                this.serverInventoryDetailCache.refresh({ name: params.name })\r\n                    .pipe(map(inventory => inventory.instance)));\r\n        }\r\n\r\n        if (params && params.retry) {\r\n            return observable.pipe(retry(params.retry));\r\n        }\r\n\r\n        return observable;\r\n    }\r\n\r\n    private createGatewayInventory(params: InventoryQueryCachesParams): Observable<GatewayInventory> {\r\n        let observable: Observable<GatewayInventory>;\r\n        if (!this.gatewayCached) {\r\n            this.gatewayCached = true;\r\n            observable = this.gatewayInventoryCache.query({}).pipe(map(inventory => inventory.instance));\r\n        } else {\r\n            observable = this.gatewayInventoryCache.refresh({}).pipe(map(inventory => inventory.instance));\r\n        }\r\n\r\n        if (params && params.retry) {\r\n            return observable.pipe(retry(params.retry));\r\n        }\r\n\r\n        return observable;\r\n    }\r\n\r\n    private createGatewayInventoryCombined(params: InventoryQueryCachesParams): Observable<[GatewayInventory, GatewayInventoryDetail]> {\r\n        let observable: Observable<[GatewayInventory, GatewayInventoryDetail]>;\r\n        if (!this.gatewayCombinedCached) {\r\n            this.gatewayCombinedCached = true;\r\n            observable = zip(\r\n                this.gatewayInventoryCache.query({}).pipe(map(inventory => inventory.instance)),\r\n                this.gatewayInventoryDetailCache.query({}).pipe(map(inventory => inventory.instance)));\r\n        } else {\r\n            observable = zip(\r\n                this.gatewayInventoryCache.refresh({}).pipe(map(inventory => inventory.instance)),\r\n                this.gatewayInventoryDetailCache.refresh({}).pipe(map(inventory => inventory.instance)));\r\n        }\r\n\r\n        if (params && params.retry) {\r\n            return observable.pipe(retry(params.retry));\r\n        }\r\n\r\n        return observable;\r\n    }\r\n\r\n    private createClusterInventory(params: InventoryQueryCachesParams): Observable<ClusterInventory> {\r\n        const observable = this.clusterInventoryCache.query({ name: params.name })\r\n            .pipe(map(inventory => inventory.instance));\r\n        if (params && params.retry) {\r\n            return observable.pipe(retry(params.retry));\r\n        }\r\n\r\n        return observable;\r\n    }\r\n}\r\n"]}