Matteo Scandolo | fb46ae6 | 2017-08-08 09:10:50 -0700 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2017-present Open Networking Foundation |
| 4 | |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 19 | /// <reference path="../../../../typings/index.d.ts"/> |
Matteo Scandolo | 86bc26a | 2017-01-18 11:06:47 -0800 | [diff] [blame] | 20 | import * as _ from 'lodash'; |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 21 | import {BehaviorSubject, Observable} from 'rxjs/Rx'; |
| 22 | import {IWSEvent, IWSEventService} from '../websocket/global'; |
| 23 | import {IXosResourceService} from '../rest/model.rest'; |
| 24 | import {IStoreHelpersService} from '../helpers/store.helpers'; |
Matteo Scandolo | ba0d92e | 2017-03-02 16:47:46 -0800 | [diff] [blame] | 25 | import {IXosDebouncer} from '../../core/services/helpers/debounce.helper'; |
Matteo Scandolo | 0e171f3 | 2017-09-26 17:21:41 -0700 | [diff] [blame] | 26 | import {IXosModeldefsCache} from '../helpers/modeldefs.service'; |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 27 | |
Matteo Scandolo | 47860fe | 2017-02-02 12:05:55 -0800 | [diff] [blame] | 28 | export interface IXosModelStoreService { |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 29 | query(model: string, apiUrl?: string): Observable<any>; |
Matteo Scandolo | 5d962a3 | 2017-08-01 18:16:14 -0700 | [diff] [blame] | 30 | get(model: string, id: string | number): Observable<any>; |
Matteo Scandolo | 86bc26a | 2017-01-18 11:06:47 -0800 | [diff] [blame] | 31 | search(modelName: string): any[]; |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 32 | } |
| 33 | |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 34 | export class XosModelStore implements IXosModelStoreService { |
Matteo Scandolo | 0e171f3 | 2017-09-26 17:21:41 -0700 | [diff] [blame] | 35 | static $inject = [ |
| 36 | '$log', |
| 37 | 'WebSocket', |
| 38 | 'StoreHelpers', |
| 39 | 'ModelRest', |
| 40 | 'XosDebouncer', |
| 41 | 'XosModeldefsCache' |
| 42 | ]; |
Matteo Scandolo | f9dd4d0 | 2016-12-22 15:17:01 -0800 | [diff] [blame] | 43 | private _collections: any; // NOTE contains a map of {model: BehaviourSubject} |
Matteo Scandolo | ba0d92e | 2017-03-02 16:47:46 -0800 | [diff] [blame] | 44 | private efficientNext: any; // NOTE debounce next |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 45 | constructor( |
Matteo Scandolo | 17bf824 | 2017-01-23 17:30:39 -0800 | [diff] [blame] | 46 | private $log: ng.ILogService, |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 47 | private webSocket: IWSEventService, |
| 48 | private storeHelpers: IStoreHelpersService, |
Matteo Scandolo | bac2245 | 2017-01-03 16:35:32 -0800 | [diff] [blame] | 49 | private ModelRest: IXosResourceService, |
Matteo Scandolo | 0e171f3 | 2017-09-26 17:21:41 -0700 | [diff] [blame] | 50 | private XosDebouncer: IXosDebouncer, |
| 51 | private XosModeldefsCache: IXosModeldefsCache |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 52 | ) { |
Matteo Scandolo | f9dd4d0 | 2016-12-22 15:17:01 -0800 | [diff] [blame] | 53 | this._collections = {}; |
Matteo Scandolo | 968e7f2 | 2017-03-03 11:49:18 -0800 | [diff] [blame] | 54 | this.efficientNext = this.XosDebouncer.debounce(this.next, 500, this, false); |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 55 | } |
| 56 | |
Matteo Scandolo | 5d962a3 | 2017-08-01 18:16:14 -0700 | [diff] [blame] | 57 | public query(modelName: string, apiUrl?: string): Observable<any> { |
Matteo Scandolo | 04a34a1 | 2017-09-12 10:37:48 -0700 | [diff] [blame] | 58 | this.$log.debug(`[XosModelStore] QUERY: ${modelName}`); |
Matteo Scandolo | f9dd4d0 | 2016-12-22 15:17:01 -0800 | [diff] [blame] | 59 | // if there isn't already an observable for that item |
Matteo Scandolo | 042ea63 | 2017-03-01 19:02:34 -0800 | [diff] [blame] | 60 | // create a new one and .next() is called by this.loadInitialData once data are received |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 61 | if (!this._collections[modelName]) { |
| 62 | this._collections[modelName] = new BehaviorSubject([]); // NOTE maybe this can be created when we get response from the resource |
| 63 | this.loadInitialData(modelName, apiUrl); |
Matteo Scandolo | f9dd4d0 | 2016-12-22 15:17:01 -0800 | [diff] [blame] | 64 | } |
Matteo Scandolo | 0f3692e | 2017-07-10 14:06:41 -0700 | [diff] [blame] | 65 | // else manually trigger the next with the last know value to trigger the subscribe method of who's requesting this data |
Matteo Scandolo | 042ea63 | 2017-03-01 19:02:34 -0800 | [diff] [blame] | 66 | else { |
Matteo Scandolo | 04a34a1 | 2017-09-12 10:37:48 -0700 | [diff] [blame] | 67 | this.$log.debug(`[XosModelStore] QUERY: Calling "next" on: ${modelName}`); |
Matteo Scandolo | ba0d92e | 2017-03-02 16:47:46 -0800 | [diff] [blame] | 68 | this.efficientNext(this._collections[modelName]); |
Matteo Scandolo | 042ea63 | 2017-03-01 19:02:34 -0800 | [diff] [blame] | 69 | } |
Matteo Scandolo | f9dd4d0 | 2016-12-22 15:17:01 -0800 | [diff] [blame] | 70 | |
Matteo Scandolo | 0e171f3 | 2017-09-26 17:21:41 -0700 | [diff] [blame] | 71 | // NOTE do we need to subscriber every time we query? |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 72 | this.webSocket.list() |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 73 | .filter((e: IWSEvent) => e.model === modelName) |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 74 | .subscribe( |
| 75 | (event: IWSEvent) => { |
Matteo Scandolo | fe0d410 | 2017-11-21 10:41:28 -0800 | [diff] [blame] | 76 | if (event.deleted) { |
| 77 | this.storeHelpers.removeItemFromCollection(event, this._collections[modelName]); |
| 78 | } |
| 79 | else { |
| 80 | this.storeHelpers.updateCollection(event, this._collections[modelName]); |
| 81 | } |
Matteo Scandolo | a4a4711 | 2016-12-16 10:06:13 -0800 | [diff] [blame] | 82 | }, |
Matteo Scandolo | 38e94a8 | 2017-03-02 12:17:27 -0800 | [diff] [blame] | 83 | err => this.$log.error |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 84 | ); |
Matteo Scandolo | f9dd4d0 | 2016-12-22 15:17:01 -0800 | [diff] [blame] | 85 | |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 86 | return this._collections[modelName].asObservable(); |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 87 | } |
| 88 | |
Matteo Scandolo | 86bc26a | 2017-01-18 11:06:47 -0800 | [diff] [blame] | 89 | public search(modelName: string): any[] { |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 90 | try { |
| 91 | const res = _.reduce(Object.keys(this._collections), (results, k) => { |
| 92 | let partialRes; |
| 93 | // NOTE wrapped in a try catch as some subject may be errored, due to not available REST endpoint |
| 94 | try { |
| 95 | partialRes = _.filter(this._collections[k].value, i => { |
| 96 | if (i && i.humanReadableName) { |
| 97 | return i.humanReadableName.toLowerCase().indexOf(modelName) > -1; |
| 98 | } |
| 99 | else if (i && i.name) { |
| 100 | return i.name.toLowerCase().indexOf(modelName) > -1; |
| 101 | } |
| 102 | return false; |
| 103 | }); |
| 104 | } catch (e) { |
| 105 | partialRes = []; |
Matteo Scandolo | 86bc26a | 2017-01-18 11:06:47 -0800 | [diff] [blame] | 106 | } |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 107 | partialRes.map(m => { |
Matteo Scandolo | 86bc26a | 2017-01-18 11:06:47 -0800 | [diff] [blame] | 108 | m.modelName = k; |
| 109 | return m; |
| 110 | }); |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 111 | return results.concat(partialRes); |
| 112 | }, []); |
| 113 | return res; |
| 114 | } catch (e) { |
| 115 | return []; |
| 116 | } |
Matteo Scandolo | 86bc26a | 2017-01-18 11:06:47 -0800 | [diff] [blame] | 117 | } |
| 118 | |
Matteo Scandolo | 5d962a3 | 2017-08-01 18:16:14 -0700 | [diff] [blame] | 119 | public get(modelName: string, modelId: string | number): Observable<any> { |
Matteo Scandolo | 04a34a1 | 2017-09-12 10:37:48 -0700 | [diff] [blame] | 120 | this.$log.debug(`[XosModelStore] GET: ${modelName} [${modelId}]`); |
| 121 | const subject = new BehaviorSubject({}); |
Matteo Scandolo | 5d962a3 | 2017-08-01 18:16:14 -0700 | [diff] [blame] | 122 | |
Matteo Scandolo | 04a34a1 | 2017-09-12 10:37:48 -0700 | [diff] [blame] | 123 | if (angular.isString(modelId)) { |
| 124 | modelId = parseInt(modelId, 10); |
| 125 | } |
Matteo Scandolo | 5d962a3 | 2017-08-01 18:16:14 -0700 | [diff] [blame] | 126 | |
Matteo Scandolo | 04a34a1 | 2017-09-12 10:37:48 -0700 | [diff] [blame] | 127 | this.query(modelName) |
| 128 | .subscribe((res) => { |
| 129 | const model = _.find(res, {id: modelId}); |
| 130 | if (model) { |
| 131 | this.$log.debug(`[XosModelStore] GET: Calling "next" on: ${modelName} [${modelId}]`); |
| 132 | subject.next(model); |
| 133 | } |
| 134 | }); |
Matteo Scandolo | 5d962a3 | 2017-08-01 18:16:14 -0700 | [diff] [blame] | 135 | |
| 136 | return subject.asObservable(); |
Matteo Scandolo | 0496423 | 2017-01-07 12:53:46 -0800 | [diff] [blame] | 137 | } |
| 138 | |
Matteo Scandolo | ba0d92e | 2017-03-02 16:47:46 -0800 | [diff] [blame] | 139 | private next(subject: BehaviorSubject<any>): void { |
| 140 | subject.next(subject.value); |
| 141 | } |
| 142 | |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 143 | private loadInitialData(model: string, apiUrl?: string) { |
Matteo Scandolo | 5d962a3 | 2017-08-01 18:16:14 -0700 | [diff] [blame] | 144 | // TODO provide always the apiUrl together with the query() params |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 145 | if (!angular.isDefined(apiUrl)) { |
Matteo Scandolo | 0e171f3 | 2017-09-26 17:21:41 -0700 | [diff] [blame] | 146 | apiUrl = this.XosModeldefsCache.getApiUrlFromModel(this.XosModeldefsCache.get(model)); |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 147 | } |
| 148 | this.ModelRest.getResource(apiUrl).query().$promise |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 149 | .then( |
| 150 | res => { |
Matteo Scandolo | f9dd4d0 | 2016-12-22 15:17:01 -0800 | [diff] [blame] | 151 | this._collections[model].next(res); |
Matteo Scandolo | 17bf824 | 2017-01-23 17:30:39 -0800 | [diff] [blame] | 152 | }) |
| 153 | .catch( |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 154 | err => { |
| 155 | this._collections[model].error(err); |
Matteo Scandolo | 1aee198 | 2017-02-17 08:33:23 -0800 | [diff] [blame] | 156 | } |
Matteo Scandolo | f2c3ed6 | 2016-12-15 14:32:50 -0800 | [diff] [blame] | 157 | ); |
| 158 | } |
| 159 | } |