ice-frontend-react-mobx
Version:
ICE Frontend REACT+MobX
152 lines (125 loc) • 4.16 kB
JavaScript
const debug = require('debug')('ice:models:zone'); // eslint-disable-line no-unused-vars
import { observable, computed } from 'mobx';
export default class Zone {
_id = null;
name = null;
type = null;
allocatedPower = 0;
racks = [];
groups = [];
breakerLimits = [];
iceLimits = [];
metrics = new ZoneMetrics();
store = null;
constructor (store) {
this.store = store;
}
get id () {
return this._id;
}
get groupIds () {
return this.groups.map((group) => (group.id));
}
get rackIds () {
return this.racks.map((rack) => (rack.id));
}
get devices () {
let groupDevices = this.groups.reduce((devices, group) => {
let gDevices = group.devices ? group.devices.slice() : [];
return devices.concat(gDevices);
}, []);
let rackDevices = this.racks.reduce((devices, rack) => {
let rpdus = rack.rpdus ? rack.rpdus.slice() : [];
return devices.concat(rpdus);
}, []);
return groupDevices.concat(rackDevices);
}
get deviceIds () {
return this.devices.map((device) => (device.id));
}
get feeds () {
let hasSeen = {};
return this.devices.map((device) => {
if (device.feed && !hasSeen[device.feed.id]) {
hasSeen[device.feed.id] = true;
return device.feed;
}
}).filter((feed) => {
return !!feed;
});
}
get feedIds () {
return this.feeds.map((feed) => (feed.id));
}
get toJson () {
let zone = {
name: this.name,
zoneType: this.type,
allocatedPower: Number(this.allocatedPower),
rackIds: this.rackIds,
groups: this.groupIds,
breakerLimits: this.breakerLimits,
iceLimits: this.iceLimits
};
if (this.id) {
zone.id = this.id;
}
return zone;
}
updateFromJson (json) {
this._id = json.id || null;
this.name = json.name || null;
this.type = json.zoneType || null;
this.allocatedPower = json.allocatedPower || 0;
this.breakerLimits = json.breakerLimits || [];
this.iceLimits = json.iceLimits || [];
this.groups.clear();
if (json.groups) {
json.groups.forEach((groupId) => {
let group = this.store.groupStore.getById(groupId);
if (group) {
this.groups.push(group);
}
});
}
this.racks.clear();
if (json.rackIds) {
json.rackIds.forEach((rackId) => {
let rack = this.store.rackStore.getById(rackId);
if (rack) {
this.racks.push(rack);
}
});
}
}
save () {
return this.store.save(this);
}
delete () {
return this.store.delete(this.id);
}
}
export class ZoneMetrics {
available1nCapacityW = '';
available2nCapacityW = '';
availableCapacityW = '';
averageSocProp = '';
instantaneousInputPowerW = '';
maxInputPowerW = '';
maxOutputPowerW = '';
maxUtilizationProp = '';
phasePowerIn = [];
powerLimits = [];
updateFromJson (json) {
this.available1nCapacityW = (json.hasOwnProperty('available1nCapacityW')) ? json.available1nCapacityW : '';
this.available2nCapacityW = (json.hasOwnProperty('available2nCapacityW')) ? json.available2nCapacityW : '';
this.availableCapacityW = (json.hasOwnProperty('availableCapacityW')) ? json.availableCapacityW : '';
this.averageSocProp = (json.hasOwnProperty('averageSocProp')) ? json.averageSocProp : '';
this.instantaneousInputPowerW = (json.hasOwnProperty('instantaneousInputPowerW')) ? json.instantaneousInputPowerW : '';
this.maxInputPowerW = (json.hasOwnProperty('maxInputPowerW')) ? json.maxInputPowerW : '';
this.maxOutputPowerW = (json.hasOwnProperty('maxOutputPowerW')) ? json.maxOutputPowerW : '';
this.maxUtilizationProp = (json.hasOwnProperty('maxUtilizationProp')) ? json.maxUtilizationProp : '';
this.powerLimits.replace(json.powerLimits);
this.phasePowerIn.replace(json.phasePowerIn);
}
}