<commit_before>// +build linux

package docker

import (
	"encoding/json"
	"github.com/resourced/resourced/libdocker"
	"github.com/resourced/resourced/readers"
	gopsutil_cpu "github.com/shirou/gopsutil/cpu"
	gopsutil_docker "github.com/shirou/gopsutil/docker"
	"strings"
)

func init() {
	readers.Register("DockerContainersCpu", NewDockerContainersCpu)
}

func NewDockerContainersCpu() readers.IReader {
	m := &DockerContainersCpu{}
	m.Data = make(map[string]*gopsutil_cpu.CPUTimesStat)
	return m
}

// DockerContainersCpu gathers docker containers CPU data.
// Data sources:
// * https://github.com/shirou/gopsutil/tree/master/cpu
// * https://github.com/shirou/gopsutil/blob/master/docker/docker_linux.go
type DockerContainersCpu struct {
	Data           map[string]*gopsutil_cpu.CPUTimesStat
	DockerHost     string
	CgroupBasePath string
}

// Run gathers cgroup CPU information from cgroup itself.
// If you use container via systemd.slice, you could use
// containerid = docker-<container id>.scope and base=/sys/fs/cgroup/cpuacct/system.slice/
func (m *DockerContainersCpu) Run() error {
	containers, err := libdocker.AllContainers(m.DockerHost)
	if err != nil {
		return nil
	}

	// Check if using systemd.
	useSystemd := false
	if strings.Contains(m.CgroupBasePath, "/system.slice") {
		useSystemd = true
	}

	for _, container := range containers {
		if container.ID != "" {
			containerDir := container.ID
			if useSystemd {
				containerDir = "docker-" + container.ID + ".scope"
			}

			data, err := gopsutil_docker.CgroupCPU(containerDir, m.CgroupBasePath)
			if err == nil {
				m.Data[container.Image+"-"+container.ID] = data
			}
		}
	}

	return nil
}

// ToJson serialize Data field to JSON.
func (m *DockerContainersCpu) ToJson() ([]byte, error) {
	return json.Marshal(m.Data)
}
<commit_msg>fix broken build because of dependency upgrade (gopsutil)<commit_after>// +build linux

package docker

import (
	"encoding/json"
	"github.com/resourced/resourced/libdocker"
	"github.com/resourced/resourced/readers"
	gopsutil_cpu "github.com/shirou/gopsutil/cpu"
	gopsutil_docker "github.com/shirou/gopsutil/docker"
	"strings"
)

func init() {
	readers.Register("DockerContainersCpu", NewDockerContainersCpu)
}

func NewDockerContainersCpu() readers.IReader {
	m := &DockerContainersCpu{}
	m.Data = make(map[string]*gopsutil_cpu.TimesStat)
	return m
}

// DockerContainersCpu gathers docker containers CPU data.
// Data sources:
// * https://github.com/shirou/gopsutil/tree/master/cpu
// * https://github.com/shirou/gopsutil/blob/master/docker/docker_linux.go
type DockerContainersCpu struct {
	Data           map[string]*gopsutil_cpu.TimesStat
	DockerHost     string
	CgroupBasePath string
}

// Run gathers cgroup CPU information from cgroup itself.
// If you use container via systemd.slice, you could use
// containerid = docker-<container id>.scope and base=/sys/fs/cgroup/cpuacct/system.slice/
func (m *DockerContainersCpu) Run() error {
	containers, err := libdocker.AllContainers(m.DockerHost)
	if err != nil {
		return nil
	}

	// Check if using systemd.
	useSystemd := false
	if strings.Contains(m.CgroupBasePath, "/system.slice") {
		useSystemd = true
	}

	for _, container := range containers {
		if container.ID != "" {
			containerDir := container.ID
			if useSystemd {
				containerDir = "docker-" + container.ID + ".scope"
			}

			data, err := gopsutil_docker.CgroupCPU(containerDir, m.CgroupBasePath)
			if err == nil {
				m.Data[container.Image+"-"+container.ID] = data
			}
		}
	}

	return nil
}

// ToJson serialize Data field to JSON.
func (m *DockerContainersCpu) ToJson() ([]byte, error) {
	return json.Marshal(m.Data)
}

{'fraction_non_alphanumeric': 0.12588943623426382, 'fraction_numerical': 0.0, 'mean_word_length': 4.020604395604396, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4026828', 'n_tokens_mistral': 1364, 'n_tokens_neox': 1272, 'n_words': 269}