node-api/stats.go
Adam Štrauch 60f99d52b0
Various fixes
* UI: abbility to start container in exited state
* Fix all Where() method usages
* Container state is updated every list and get operation
2020-07-23 23:50:20 +02:00

81 lines
1.4 KiB
Go

package main
import (
"log"
"github.com/rosti-cz/node-api/apps"
"github.com/rosti-cz/node-api/docker"
)
// Updates only container's state
func updateContainerState(app *apps.App) error {
container := docker.Container{
App: app,
}
state, err := container.Status()
if err != nil {
return err
}
err = apps.UpdateContainerState(
app.Name,
state,
)
return err
}
// Updates info about all containers
func updateContainerStats(app *apps.App) error {
container := docker.Container{
App: app,
}
state, err := container.GetState()
if err != nil {
return err
}
err = apps.UpdateState(
app.Name,
state.State,
state.CPUUsage,
state.MemoryUsage,
state.DiskUsageBytes,
state.DiskUsageInodes,
)
return err
}
// gatherContainersStats gathers information about containers and saves it into the database
func gatherContainersStats() error {
appList, err := apps.List()
if err != nil {
return err
}
for _, app := range *appList {
err := updateContainerStats(&app)
if err != nil {
log.Println("STATS ERROR:", err.Error())
}
}
return nil
}
// gatherContainersStates refreshes all container's state
func gatherContainersStates() error {
appList, err := apps.List()
if err != nil {
return err
}
for _, app := range *appList {
err := updateContainerState(&app)
if err != nil {
log.Println("STATE ERROR:", err.Error())
}
}
return nil
}