2019-03-21 05:46:49 +00:00
|
|
|
import _ from 'lodash-es';
|
2019-11-12 23:41:42 +00:00
|
|
|
import { ResourceControlViewModel } from 'Portainer/models/resourceControl/resourceControl';
|
2019-03-21 05:46:49 +00:00
|
|
|
|
|
|
|
export function createStatus(statusText) {
|
2018-06-11 13:13:19 +00:00
|
|
|
var status = _.toLower(statusText);
|
|
|
|
|
|
|
|
if (status.indexOf('paused') > -1) {
|
|
|
|
return 'paused';
|
|
|
|
} else if (status.indexOf('dead') > -1) {
|
|
|
|
return 'dead';
|
|
|
|
} else if (status.indexOf('created') > -1) {
|
|
|
|
return 'created';
|
|
|
|
} else if (status.indexOf('exited') > -1) {
|
|
|
|
return 'stopped';
|
|
|
|
} else if (status.indexOf('(healthy)') > -1) {
|
|
|
|
return 'healthy';
|
|
|
|
} else if (status.indexOf('(unhealthy)') > -1) {
|
|
|
|
return 'unhealthy';
|
|
|
|
} else if (status.indexOf('(health: starting)') > -1) {
|
|
|
|
return 'starting';
|
|
|
|
}
|
|
|
|
return 'running';
|
|
|
|
}
|
|
|
|
|
2019-03-21 05:46:49 +00:00
|
|
|
export function ContainerViewModel(data) {
|
2017-01-31 23:26:29 +00:00
|
|
|
this.Id = data.Id;
|
2018-06-11 13:13:19 +00:00
|
|
|
this.Status = createStatus(data.Status);
|
2017-01-31 23:26:29 +00:00
|
|
|
this.State = data.State;
|
2018-07-05 07:24:53 +00:00
|
|
|
this.Created = data.Created;
|
2017-01-31 23:26:29 +00:00
|
|
|
this.Names = data.Names;
|
|
|
|
// Unavailable in Docker < 1.10
|
|
|
|
if (data.NetworkSettings && !_.isEmpty(data.NetworkSettings.Networks)) {
|
|
|
|
this.IP = data.NetworkSettings.Networks[Object.keys(data.NetworkSettings.Networks)[0]].IPAddress;
|
|
|
|
}
|
2017-10-16 16:54:48 +00:00
|
|
|
this.NetworkSettings = data.NetworkSettings;
|
2017-01-31 23:26:29 +00:00
|
|
|
this.Image = data.Image;
|
2017-10-15 17:24:40 +00:00
|
|
|
this.ImageID = data.ImageID;
|
2017-01-31 23:26:29 +00:00
|
|
|
this.Command = data.Command;
|
|
|
|
this.Checked = false;
|
2017-03-12 16:24:15 +00:00
|
|
|
this.Labels = data.Labels;
|
2017-10-15 17:24:40 +00:00
|
|
|
if (this.Labels && this.Labels['com.docker.compose.project']) {
|
|
|
|
this.StackName = this.Labels['com.docker.compose.project'];
|
|
|
|
} else if (this.Labels && this.Labels['com.docker.stack.namespace']) {
|
|
|
|
this.StackName = this.Labels['com.docker.stack.namespace'];
|
|
|
|
}
|
2017-03-12 16:24:15 +00:00
|
|
|
this.Mounts = data.Mounts;
|
2017-07-24 14:29:28 +00:00
|
|
|
|
|
|
|
this.Ports = [];
|
|
|
|
if (data.Ports) {
|
|
|
|
for (var i = 0; i < data.Ports.length; ++i) {
|
|
|
|
var p = data.Ports[i];
|
|
|
|
if (p.PublicPort) {
|
|
|
|
this.Ports.push({ host: p.IP, private: p.PrivatePort, public: p.PublicPort });
|
|
|
|
}
|
2017-01-31 23:26:29 +00:00
|
|
|
}
|
|
|
|
}
|
2017-07-24 14:29:28 +00:00
|
|
|
|
2017-03-12 16:24:15 +00:00
|
|
|
if (data.Portainer) {
|
|
|
|
if (data.Portainer.ResourceControl) {
|
2017-05-23 18:56:10 +00:00
|
|
|
this.ResourceControl = new ResourceControlViewModel(data.Portainer.ResourceControl);
|
2017-03-12 16:24:15 +00:00
|
|
|
}
|
2018-05-06 07:15:57 +00:00
|
|
|
if (data.Portainer.Agent && data.Portainer.Agent.NodeName) {
|
|
|
|
this.NodeName = data.Portainer.Agent.NodeName;
|
|
|
|
}
|
2017-03-12 16:24:15 +00:00
|
|
|
}
|
2017-01-31 23:26:29 +00:00
|
|
|
}
|
2018-02-28 06:19:28 +00:00
|
|
|
|
2019-03-21 05:46:49 +00:00
|
|
|
export function ContainerStatsViewModel(data) {
|
2018-11-08 00:31:33 +00:00
|
|
|
this.read = data.read;
|
|
|
|
this.preread = data.preread;
|
2020-04-10 21:54:53 +00:00
|
|
|
if (data.memory_stats.privateworkingset !== undefined) {
|
|
|
|
// Windows
|
2018-11-08 00:31:33 +00:00
|
|
|
this.MemoryUsage = data.memory_stats.privateworkingset;
|
|
|
|
this.MemoryCache = 0;
|
|
|
|
this.NumProcs = data.num_procs;
|
|
|
|
this.isWindows = true;
|
2020-04-10 21:54:53 +00:00
|
|
|
} else {
|
|
|
|
// Linux
|
2018-11-23 08:44:34 +00:00
|
|
|
if (data.memory_stats.stats === undefined || data.memory_stats.usage === undefined) {
|
|
|
|
this.MemoryUsage = this.MemoryCache = 0;
|
|
|
|
} else {
|
|
|
|
this.MemoryUsage = data.memory_stats.usage - data.memory_stats.stats.cache;
|
|
|
|
this.MemoryCache = data.memory_stats.stats.cache;
|
|
|
|
}
|
2018-11-08 00:31:33 +00:00
|
|
|
}
|
2018-02-28 06:19:28 +00:00
|
|
|
this.PreviousCPUTotalUsage = data.precpu_stats.cpu_usage.total_usage;
|
|
|
|
this.PreviousCPUSystemUsage = data.precpu_stats.system_cpu_usage;
|
|
|
|
this.CurrentCPUTotalUsage = data.cpu_stats.cpu_usage.total_usage;
|
|
|
|
this.CurrentCPUSystemUsage = data.cpu_stats.system_cpu_usage;
|
|
|
|
if (data.cpu_stats.cpu_usage.percpu_usage) {
|
|
|
|
this.CPUCores = data.cpu_stats.cpu_usage.percpu_usage.length;
|
|
|
|
}
|
|
|
|
this.Networks = _.values(data.networks);
|
|
|
|
}
|
|
|
|
|
2019-03-21 05:46:49 +00:00
|
|
|
export function ContainerDetailsViewModel(data) {
|
2018-02-28 06:19:28 +00:00
|
|
|
this.Model = data;
|
|
|
|
this.Id = data.Id;
|
|
|
|
this.State = data.State;
|
|
|
|
this.Created = data.Created;
|
|
|
|
this.Name = data.Name;
|
|
|
|
this.NetworkSettings = data.NetworkSettings;
|
|
|
|
this.Args = data.Args;
|
|
|
|
this.Image = data.Image;
|
|
|
|
this.Config = data.Config;
|
|
|
|
this.HostConfig = data.HostConfig;
|
|
|
|
this.Mounts = data.Mounts;
|
2019-11-12 23:41:42 +00:00
|
|
|
if (data.Portainer && data.Portainer.ResourceControl) {
|
|
|
|
this.ResourceControl = new ResourceControlViewModel(data.Portainer.ResourceControl);
|
2018-02-28 06:19:28 +00:00
|
|
|
}
|
|
|
|
}
|