blob: 7ca0ae1e97083881affbab7a55389f64e27db4d2 [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 Scandolof2c3ed62016-12-15 14:32:50 -080026
Matteo Scandolo47860fe2017-02-02 12:05:55 -080027export interface IXosModelStoreService {
Matteo Scandolo1aee1982017-02-17 08:33:23 -080028 query(model: string, apiUrl?: string): Observable<any>;
Matteo Scandolo5d962a32017-08-01 18:16:14 -070029 get(model: string, id: string | number): Observable<any>;
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080030 search(modelName: string): any[];
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080031}
32
Matteo Scandolo1aee1982017-02-17 08:33:23 -080033export class XosModelStore implements IXosModelStoreService {
Matteo Scandoloba0d92e2017-03-02 16:47:46 -080034 static $inject = ['$log', 'WebSocket', 'StoreHelpers', 'ModelRest', 'XosDebouncer'];
Matteo Scandolof9dd4d02016-12-22 15:17:01 -080035 private _collections: any; // NOTE contains a map of {model: BehaviourSubject}
Matteo Scandoloba0d92e2017-03-02 16:47:46 -080036 private efficientNext: any; // NOTE debounce next
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080037 constructor(
Matteo Scandolo17bf8242017-01-23 17:30:39 -080038 private $log: ng.ILogService,
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080039 private webSocket: IWSEventService,
40 private storeHelpers: IStoreHelpersService,
Matteo Scandolobac22452017-01-03 16:35:32 -080041 private ModelRest: IXosResourceService,
Matteo Scandoloba0d92e2017-03-02 16:47:46 -080042 private XosDebouncer: IXosDebouncer
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080043 ) {
Matteo Scandolof9dd4d02016-12-22 15:17:01 -080044 this._collections = {};
Matteo Scandolo968e7f22017-03-03 11:49:18 -080045 this.efficientNext = this.XosDebouncer.debounce(this.next, 500, this, false);
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080046 }
47
Matteo Scandolo5d962a32017-08-01 18:16:14 -070048 public query(modelName: string, apiUrl?: string): Observable<any> {
Matteo Scandolof9dd4d02016-12-22 15:17:01 -080049 // if there isn't already an observable for that item
Matteo Scandolo042ea632017-03-01 19:02:34 -080050 // create a new one and .next() is called by this.loadInitialData once data are received
Matteo Scandolo1aee1982017-02-17 08:33:23 -080051 if (!this._collections[modelName]) {
52 this._collections[modelName] = new BehaviorSubject([]); // NOTE maybe this can be created when we get response from the resource
53 this.loadInitialData(modelName, apiUrl);
Matteo Scandolof9dd4d02016-12-22 15:17:01 -080054 }
Matteo Scandolo0f3692e2017-07-10 14:06:41 -070055 // 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 -080056 else {
Matteo Scandoloba0d92e2017-03-02 16:47:46 -080057 this.efficientNext(this._collections[modelName]);
Matteo Scandolo042ea632017-03-01 19:02:34 -080058 }
Matteo Scandolof9dd4d02016-12-22 15:17:01 -080059
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080060 this.webSocket.list()
Matteo Scandolo1aee1982017-02-17 08:33:23 -080061 .filter((e: IWSEvent) => e.model === modelName)
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080062 .subscribe(
63 (event: IWSEvent) => {
Matteo Scandolo1aee1982017-02-17 08:33:23 -080064 this.storeHelpers.updateCollection(event, this._collections[modelName]);
Matteo Scandoloa4a47112016-12-16 10:06:13 -080065 },
Matteo Scandolo38e94a82017-03-02 12:17:27 -080066 err => this.$log.error
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080067 );
Matteo Scandolof9dd4d02016-12-22 15:17:01 -080068
Matteo Scandolo1aee1982017-02-17 08:33:23 -080069 return this._collections[modelName].asObservable();
Matteo Scandolof2c3ed62016-12-15 14:32:50 -080070 }
71
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080072 public search(modelName: string): any[] {
Matteo Scandolo1aee1982017-02-17 08:33:23 -080073 try {
74 const res = _.reduce(Object.keys(this._collections), (results, k) => {
75 let partialRes;
76 // NOTE wrapped in a try catch as some subject may be errored, due to not available REST endpoint
77 try {
78 partialRes = _.filter(this._collections[k].value, i => {
79 if (i && i.humanReadableName) {
80 return i.humanReadableName.toLowerCase().indexOf(modelName) > -1;
81 }
82 else if (i && i.name) {
83 return i.name.toLowerCase().indexOf(modelName) > -1;
84 }
85 return false;
86 });
87 } catch (e) {
88 partialRes = [];
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080089 }
Matteo Scandolo1aee1982017-02-17 08:33:23 -080090 partialRes.map(m => {
Matteo Scandolo86bc26a2017-01-18 11:06:47 -080091 m.modelName = k;
92 return m;
93 });
Matteo Scandolo1aee1982017-02-17 08:33:23 -080094 return results.concat(partialRes);
95 }, []);
96 return res;
97 } catch (e) {
98 return [];
99 }
Matteo Scandolo86bc26a2017-01-18 11:06:47 -0800100 }
101
Matteo Scandolo5d962a32017-08-01 18:16:14 -0700102 public get(modelName: string, modelId: string | number): Observable<any> {
103 const subject = new BehaviorSubject([]);
104
105 const _findModel = (subject) => {
106 this._collections[modelName]
107 .subscribe((res) => {
108 const model = _.find(res, {id: modelId});
109 if (model) {
110 subject.next(model);
111 }
112 });
113 };
114
115 if (!this._collections[modelName]) {
116 // cache the models in that collection
117 this.query(modelName)
118 .subscribe((res) => {
119 _findModel(subject);
120 });
121 }
122 else {
123 _findModel(subject);
124 }
125
126 return subject.asObservable();
Matteo Scandolo04964232017-01-07 12:53:46 -0800127 }
128
Matteo Scandoloba0d92e2017-03-02 16:47:46 -0800129 private next(subject: BehaviorSubject<any>): void {
130 subject.next(subject.value);
131 }
132
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800133 private loadInitialData(model: string, apiUrl?: string) {
Matteo Scandolo5d962a32017-08-01 18:16:14 -0700134 // TODO provide always the apiUrl together with the query() params
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800135 if (!angular.isDefined(apiUrl)) {
136 // NOTE check what is the correct pattern to pluralize this
137 apiUrl = this.storeHelpers.urlFromCoreModel(model);
138 }
139 this.ModelRest.getResource(apiUrl).query().$promise
Matteo Scandolof2c3ed62016-12-15 14:32:50 -0800140 .then(
141 res => {
Matteo Scandolof9dd4d02016-12-22 15:17:01 -0800142 this._collections[model].next(res);
Matteo Scandolo17bf8242017-01-23 17:30:39 -0800143 })
144 .catch(
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800145 err => {
146 this._collections[model].error(err);
Matteo Scandolo1aee1982017-02-17 08:33:23 -0800147 }
Matteo Scandolof2c3ed62016-12-15 14:32:50 -0800148 );
149 }
150}