blob: 09bd715a09a296955c5a65e4811da9bdf2f79bc0 [file] [log] [blame]
Matteo Scandolofb46ae62017-08-08 09:10:50 -07001
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 Scandolof2c3ed62016-12-15 14:32:50 -080019/// <reference path="../../../../typings/index.d.ts"/>
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080020import * as _ from 'lodash';
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080021import {BehaviorSubject, Observable} from 'rxjs/Rx';
22import {IWSEvent, IWSEventService} from '../websocket/global';
23import {IXosResourceService} from '../rest/model.rest';
24import {IStoreHelpersService} from '../helpers/store.helpers';
Matteo Scandoloba0d92e2017-03-02 16:47:46 -080025import {IXosDebouncer} from '../../core/services/helpers/debounce.helper';
Matteo Scandolo0e171f32017-09-26 17:21:41 -070026import {IXosModeldefsCache} from '../helpers/modeldefs.service';
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080027
Matteo Scandolo47860fe2017-02-02 12:05:55 -080028export interface IXosModelStoreService {
Matteo Scandolo1aee1982017-02-17 08:33:23 -080029 query(model: string, apiUrl?: string): Observable<any>;
Matteo Scandolo5d962a32017-08-01 18:16:14 -070030 get(model: string, id: string | number): Observable<any>;
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080031 search(modelName: string): any[];
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080032}
33
Matteo Scandolo1aee1982017-02-17 08:33:23 -080034export class XosModelStore implements IXosModelStoreService {
Matteo Scandolo0e171f32017-09-26 17:21:41 -070035 static $inject = [
36 '$log',
37 'WebSocket',
38 'StoreHelpers',
39 'ModelRest',
40 'XosDebouncer',
41 'XosModeldefsCache'
42 ];
Matteo Scandolof9dd4d02016-12-22 15:17:01 -080043 private _collections: any; // NOTE contains a map of {model: BehaviourSubject}
Matteo Scandoloba0d92e2017-03-02 16:47:46 -080044 private efficientNext: any; // NOTE debounce next
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080045 constructor(
Matteo Scandolo17bf8242017-01-23 17:30:39 -080046 private $log: ng.ILogService,
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080047 private webSocket: IWSEventService,
48 private storeHelpers: IStoreHelpersService,
Matteo Scandolobac22452017-01-03 16:35:32 -080049 private ModelRest: IXosResourceService,
Matteo Scandolo0e171f32017-09-26 17:21:41 -070050 private XosDebouncer: IXosDebouncer,
51 private XosModeldefsCache: IXosModeldefsCache
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080052 ) {
Matteo Scandolof9dd4d02016-12-22 15:17:01 -080053 this._collections = {};
Matteo Scandolo968e7f22017-03-03 11:49:18 -080054 this.efficientNext = this.XosDebouncer.debounce(this.next, 500, this, false);
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080055 }
56
Matteo Scandolo5d962a32017-08-01 18:16:14 -070057 public query(modelName: string, apiUrl?: string): Observable<any> {
Matteo Scandolo04a34a12017-09-12 10:37:48 -070058 this.$log.debug(`[XosModelStore] QUERY: ${modelName}`);
Matteo Scandolof9dd4d02016-12-22 15:17:01 -080059 // if there isn't already an observable for that item
Matteo Scandolo042ea632017-03-01 19:02:34 -080060 // create a new one and .next() is called by this.loadInitialData once data are received
Matteo Scandolo1aee1982017-02-17 08:33:23 -080061 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 Scandolof9dd4d02016-12-22 15:17:01 -080064 }
Matteo Scandolo0f3692e2017-07-10 14:06:41 -070065 // else manually trigger the next with the last know value to trigger the subscribe method of who's requesting this data
Matteo Scandolo042ea632017-03-01 19:02:34 -080066 else {
Matteo Scandolo04a34a12017-09-12 10:37:48 -070067 this.$log.debug(`[XosModelStore] QUERY: Calling "next" on: ${modelName}`);
Matteo Scandoloba0d92e2017-03-02 16:47:46 -080068 this.efficientNext(this._collections[modelName]);
Matteo Scandolo042ea632017-03-01 19:02:34 -080069 }
Matteo Scandolof9dd4d02016-12-22 15:17:01 -080070
Matteo Scandolo0e171f32017-09-26 17:21:41 -070071 // NOTE do we need to subscriber every time we query?
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080072 this.webSocket.list()
Matteo Scandolo1aee1982017-02-17 08:33:23 -080073 .filter((e: IWSEvent) => e.model === modelName)
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080074 .subscribe(
75 (event: IWSEvent) => {
Matteo Scandolofe0d4102017-11-21 10:41:28 -080076 if (event.deleted) {
77 this.storeHelpers.removeItemFromCollection(event, this._collections[modelName]);
78 }
79 else {
80 this.storeHelpers.updateCollection(event, this._collections[modelName]);
81 }
Matteo Scandoloa4a47112016-12-16 10:06:13 -080082 },
Matteo Scandolo38e94a82017-03-02 12:17:27 -080083 err => this.$log.error
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080084 );
Matteo Scandolof9dd4d02016-12-22 15:17:01 -080085
Matteo Scandolo1aee1982017-02-17 08:33:23 -080086 return this._collections[modelName].asObservable();
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080087 }
88
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080089 public search(modelName: string): any[] {
Matteo Scandolo1aee1982017-02-17 08:33:23 -080090 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 Scandolo86bc26a2017-01-18 11:06:47 -0800106 }
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800107 partialRes.map(m => {
Matteo Scandolo86bc26a2017-01-18 11:06:47 -0800108 m.modelName = k;
109 return m;
110 });
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800111 return results.concat(partialRes);
112 }, []);
113 return res;
114 } catch (e) {
115 return [];
116 }
Matteo Scandolo86bc26a2017-01-18 11:06:47 -0800117 }
118
Matteo Scandolo5d962a32017-08-01 18:16:14 -0700119 public get(modelName: string, modelId: string | number): Observable<any> {
Matteo Scandolo04a34a12017-09-12 10:37:48 -0700120 this.$log.debug(`[XosModelStore] GET: ${modelName} [${modelId}]`);
121 const subject = new BehaviorSubject({});
Matteo Scandolo5d962a32017-08-01 18:16:14 -0700122
Matteo Scandolo04a34a12017-09-12 10:37:48 -0700123 if (angular.isString(modelId)) {
124 modelId = parseInt(modelId, 10);
125 }
Matteo Scandolo5d962a32017-08-01 18:16:14 -0700126
Matteo Scandolo04a34a12017-09-12 10:37:48 -0700127 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 Scandolo5d962a32017-08-01 18:16:14 -0700135
136 return subject.asObservable();
Matteo Scandolo04964232017-01-07 12:53:46 -0800137 }
138
Matteo Scandoloba0d92e2017-03-02 16:47:46 -0800139 private next(subject: BehaviorSubject<any>): void {
140 subject.next(subject.value);
141 }
142
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800143 private loadInitialData(model: string, apiUrl?: string) {
Matteo Scandolo5d962a32017-08-01 18:16:14 -0700144 // TODO provide always the apiUrl together with the query() params
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800145 if (!angular.isDefined(apiUrl)) {
Matteo Scandolo0e171f32017-09-26 17:21:41 -0700146 apiUrl = this.XosModeldefsCache.getApiUrlFromModel(this.XosModeldefsCache.get(model));
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800147 }
148 this.ModelRest.getResource(apiUrl).query().$promise
Matteo Scandolof2c3ed62016-12-15 14:32:50 -0800149 .then(
150 res => {
Matteo Scandolof9dd4d02016-12-22 15:17:01 -0800151 this._collections[model].next(res);
Matteo Scandolo17bf8242017-01-23 17:30:39 -0800152 })
153 .catch(
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800154 err => {
155 this._collections[model].error(err);
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800156 }
Matteo Scandolof2c3ed62016-12-15 14:32:50 -0800157 );
158 }
159}