Matteo Scandolo | 22f8e82 | 2017-09-28 16:35:20 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 3 | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | import * as _ from 'lodash'; |
| 18 | import './subscriber-dashboard.scss'; |
Matteo Scandolo | 9b0c1be | 2017-09-28 16:35:20 -0700 | [diff] [blame] | 19 | |
Matteo Scandolo | 9fbf845 | 2017-10-16 09:47:41 -0700 | [diff] [blame] | 20 | import {IRCordSubscriber, IRCordSubscriberDevice} from '../interfaces/rcord-subscriber.interface'; |
Matteo Scandolo | 22f8e82 | 2017-09-28 16:35:20 -0700 | [diff] [blame] | 21 | |
Matteo Scandolo | 9fbf845 | 2017-10-16 09:47:41 -0700 | [diff] [blame] | 22 | class RcordSubscriberDashboardCtrl { |
Matteo Scandolo | 22f8e82 | 2017-09-28 16:35:20 -0700 | [diff] [blame] | 23 | |
| 24 | static $inject = [ |
| 25 | 'toastr', |
| 26 | 'XosModelStore', |
| 27 | 'XosModeldefsCache' |
| 28 | ]; |
| 29 | |
| 30 | public levelOptions = []; |
Matteo Scandolo | 9fbf845 | 2017-10-16 09:47:41 -0700 | [diff] [blame] | 31 | public subscribers: IRCordSubscriber[] = []; |
Matteo Scandolo | 22f8e82 | 2017-09-28 16:35:20 -0700 | [diff] [blame] | 32 | public statusFieldOptions = []; |
| 33 | public slider = { |
| 34 | floor: 0, |
| 35 | ceil: 1000000000, |
| 36 | translate: (value) => { |
Matteo Scandolo | 9b0c1be | 2017-09-28 16:35:20 -0700 | [diff] [blame] | 37 | return `${Math.floor(value / 1000000)} MB`; |
Matteo Scandolo | 22f8e82 | 2017-09-28 16:35:20 -0700 | [diff] [blame] | 38 | } |
| 39 | }; |
Matteo Scandolo | 9fbf845 | 2017-10-16 09:47:41 -0700 | [diff] [blame] | 40 | public selectedSubscriber: IRCordSubscriber; |
Matteo Scandolo | 22f8e82 | 2017-09-28 16:35:20 -0700 | [diff] [blame] | 41 | |
| 42 | private subscriberDef; |
| 43 | |
| 44 | constructor ( |
| 45 | private toastr: any, |
| 46 | private XosModelStore: any, |
| 47 | private XosModeldefsCache: any |
| 48 | ) { |
| 49 | |
| 50 | } |
| 51 | |
| 52 | $onInit() { |
| 53 | this.XosModelStore.query('CordSubscriberRoot', '/rcord/cordsubscriberroots') |
| 54 | .subscribe( |
| 55 | res => { |
| 56 | this.subscribers = this.parseSubscribers(res); |
| 57 | } |
| 58 | ); |
| 59 | |
| 60 | this.levelOptions = [ |
| 61 | `G`, |
| 62 | `PG`, |
| 63 | `PG_13`, |
| 64 | `R`, |
| 65 | `X` |
| 66 | ]; |
| 67 | |
| 68 | this.subscriberDef = this.XosModeldefsCache.get('CordSubscriberRoot'); |
| 69 | this.statusFieldOptions = _.find(this.subscriberDef.fields, {name: 'status'}).options; |
| 70 | } |
| 71 | |
Matteo Scandolo | 9fbf845 | 2017-10-16 09:47:41 -0700 | [diff] [blame] | 72 | public addDevice(subscriber: IRCordSubscriber) { |
| 73 | if (angular.isUndefined(subscriber.service_specific_attribute.devices)) { |
| 74 | subscriber.service_specific_attribute.devices = []; |
| 75 | } |
Matteo Scandolo | 9b0c1be | 2017-09-28 16:35:20 -0700 | [diff] [blame] | 76 | |
Matteo Scandolo | 22f8e82 | 2017-09-28 16:35:20 -0700 | [diff] [blame] | 77 | subscriber.service_specific_attribute.devices.push({ |
| 78 | name: '', |
| 79 | mac: '', |
| 80 | level: 'PG_13' |
Matteo Scandolo | 9fbf845 | 2017-10-16 09:47:41 -0700 | [diff] [blame] | 81 | }); |
Matteo Scandolo | 22f8e82 | 2017-09-28 16:35:20 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Matteo Scandolo | 9fbf845 | 2017-10-16 09:47:41 -0700 | [diff] [blame] | 84 | public removeDevice(subscriber: IRCordSubscriber, device: IRCordSubscriberDevice) { |
| 85 | _.remove(subscriber.service_specific_attribute.devices, {name: device.name}); |
Matteo Scandolo | 22f8e82 | 2017-09-28 16:35:20 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Matteo Scandolo | 9fbf845 | 2017-10-16 09:47:41 -0700 | [diff] [blame] | 88 | public save(subscriber: IRCordSubscriber) { |
Matteo Scandolo | 22f8e82 | 2017-09-28 16:35:20 -0700 | [diff] [blame] | 89 | const item: any = angular.copy(subscriber); |
Matteo Scandolo | 9fbf845 | 2017-10-16 09:47:41 -0700 | [diff] [blame] | 90 | |
Matteo Scandolo | 9b0c1be | 2017-09-28 16:35:20 -0700 | [diff] [blame] | 91 | delete item.updated; |
| 92 | |
Matteo Scandolo | 9fbf845 | 2017-10-16 09:47:41 -0700 | [diff] [blame] | 93 | _.forEach(Object.keys(item), prop => { |
| 94 | if (prop.indexOf('-formatted') > -1 || prop.indexOf('_ptr') > -1) { |
| 95 | delete item[prop]; |
| 96 | } |
| 97 | }); |
| 98 | |
Matteo Scandolo | 22f8e82 | 2017-09-28 16:35:20 -0700 | [diff] [blame] | 99 | item.service_specific_attribute = JSON.stringify(item.service_specific_attribute); |
| 100 | item.$save() |
| 101 | .then(() => { |
| 102 | this.toastr.success(`Subscriber successfully saved`); |
Matteo Scandolo | 9fbf845 | 2017-10-16 09:47:41 -0700 | [diff] [blame] | 103 | }) |
| 104 | .catch(e => { |
| 105 | this.toastr.error(`Error while saving subscriber`); |
| 106 | console.error(e); |
Matteo Scandolo | 22f8e82 | 2017-09-28 16:35:20 -0700 | [diff] [blame] | 107 | }); |
| 108 | } |
| 109 | |
Matteo Scandolo | 9fbf845 | 2017-10-16 09:47:41 -0700 | [diff] [blame] | 110 | private parseSubscribers(subscribers: IRCordSubscriber) { |
Matteo Scandolo | 22f8e82 | 2017-09-28 16:35:20 -0700 | [diff] [blame] | 111 | return _.map(subscribers, (s) => { |
| 112 | if (angular.isString(s.service_specific_attribute)) { |
Matteo Scandolo | 9fbf845 | 2017-10-16 09:47:41 -0700 | [diff] [blame] | 113 | try { |
| 114 | s.service_specific_attribute = JSON.parse(s.service_specific_attribute); |
| 115 | } catch (e) { |
| 116 | s.service_specific_attribute = {}; |
| 117 | } |
Matteo Scandolo | 22f8e82 | 2017-09-28 16:35:20 -0700 | [diff] [blame] | 118 | } |
| 119 | return s; |
Matteo Scandolo | 9fbf845 | 2017-10-16 09:47:41 -0700 | [diff] [blame] | 120 | }); |
| 121 | } |
| 122 | |
| 123 | $onDestroy() { |
| 124 | this.selectedSubscriber.service_specific_attribute = JSON.stringify(this.selectedSubscriber.service_specific_attribute); |
Matteo Scandolo | 22f8e82 | 2017-09-28 16:35:20 -0700 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | |
| 128 | export const rcordSubscriberDashboard: angular.IComponentOptions = { |
| 129 | template: require('./subscriber-dashboard.html'), |
| 130 | controllerAs: 'vm', |
Matteo Scandolo | 9fbf845 | 2017-10-16 09:47:41 -0700 | [diff] [blame] | 131 | controller: RcordSubscriberDashboardCtrl |
Matteo Scandolo | 9b0c1be | 2017-09-28 16:35:20 -0700 | [diff] [blame] | 132 | } |