<commit_before>// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package state

import (
	"github.com/juju/errors"
	"github.com/juju/names"
	"gopkg.in/juju/charm.v5"

	"github.com/juju/juju/process"
)

// TODO(ericsnow) Track juju-level status in the status collection.

// UnitProcesses exposes high-level interaction with workload processes
// for a unit.
type UnitProcesses interface {
	// Register registers a workload process in state.
	Register(info process.Info) error
	// SetStatus sets the raw status of a workload process.
	SetStatus(id string, status process.Status) error
	// List builds the list of workload processes registered for
	// the given unit and IDs. If no IDs are provided then all
	// registered processes for the unit are returned.
	List(ids ...string) ([]process.Info, error)
	// Unregister marks the identified process as unregistered.
	Unregister(id string) error
}

// ProcessDefiniitions provides the state functionality related to
// workload process definitions.
type ProcessDefinitions interface {
	// EnsureDefined adds the definitions to state if they aren't there
	// already. If they are there then it verfies that the existing
	// definitions match the provided ones.
	EnsureDefined(definitions ...charm.Process) error
}

// TODO(ericsnow) Use a more generic component registration mechanism?

type newUnitProcessesFunc func(persist Persistence, unit names.UnitTag, charm names.CharmTag) (UnitProcesses, error)

type newProcessDefinitionsFunc func(persist Persistence, charm names.CharmTag) (ProcessDefinitions, error)

var (
	newUnitProcesses      newUnitProcessesFunc
	newProcessDefinitions newProcessDefinitionsFunc
)

// SetProcessesComponent registers the functions that provide the state
// functionality related to workload processes.
func SetProcessesComponent(upFunc newUnitProcessesFunc, pdFunc newProcessDefinitionsFunc) {
	newUnitProcesses = upFunc
	newProcessDefinitions = pdFunc
}

type unitProcesses struct {
	UnitProcesses
	charm *Charm
	st    *State
}

// UnitProcesses exposes interaction with workload processes in state
// for a the given unit.
func (st *State) UnitProcesses(unit *Unit) (UnitProcesses, error) {
	if newUnitProcesses == nil {
		return nil, errors.Errorf("unit processes not supported")
	}

	// TODO(ericsnow) unit.charm is sometimes wrong...
	charm, err := unit.charm()
	if err != nil {
		return nil, errors.Trace(err)
	}
	// TODO(ericsnow) Do we really need the charm tag?
	charmTag := charm.Tag().(names.CharmTag)<commit_msg>Drop a couple TODO comments.<commit_after>// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package state

import (
	"github.com/juju/errors"
	"github.com/juju/names"
	"gopkg.in/juju/charm.v5"

	"github.com/juju/juju/process"
)

// TODO(ericsnow) Track juju-level status in the status collection.

// UnitProcesses exposes high-level interaction with workload processes
// for a unit.
type UnitProcesses interface {
	// Register registers a workload process in state.
	Register(info process.Info) error
	// SetStatus sets the raw status of a workload process.
	SetStatus(id string, status process.Status) error
	// List builds the list of workload processes registered for
	// the given unit and IDs. If no IDs are provided then all
	// registered processes for the unit are returned.
	List(ids ...string) ([]process.Info, error)
	// Unregister marks the identified process as unregistered.
	Unregister(id string) error
}

// ProcessDefiniitions provides the state functionality related to
// workload process definitions.
type ProcessDefinitions interface {
	// EnsureDefined adds the definitions to state if they aren't there
	// already. If they are there then it verfies that the existing
	// definitions match the provided ones.
	EnsureDefined(definitions ...charm.Process) error
}

// TODO(ericsnow) Use a more generic component registration mechanism?

type newUnitProcessesFunc func(persist Persistence, unit names.UnitTag, charm names.CharmTag) (UnitProcesses, error)

type newProcessDefinitionsFunc func(persist Persistence, charm names.CharmTag) (ProcessDefinitions, error)

var (
	newUnitProcesses      newUnitProcessesFunc
	newProcessDefinitions newProcessDefinitionsFunc
)

// SetProcessesComponent registers the functions that provide the state
// functionality related to workload processes.
func SetProcessesComponent(upFunc newUnitProcessesFunc, pdFunc newProcessDefinitionsFunc) {
	newUnitProcesses = upFunc
	newProcessDefinitions = pdFunc
}

type unitProcesses struct {
	UnitProcesses
	charm *Charm
	st    *State
}

// UnitProcesses exposes interaction with workload processes in state
// for a the given unit.
func (st *State) UnitProcesses(unit *Unit) (UnitProcesses, error) {
	if newUnitProcesses == nil {
		return nil, errors.Errorf("unit processes not supported")
	}

	charm, err := unit.charm()
	if err != nil {
		return nil, errors.Trace(err)
	}
	charmTag := charm.Tag().(names.CharmTag)
{'fraction_non_alphanumeric': 0.08031968031968031, 'fraction_numerical': 0.0023976023976023976, 'mean_word_length': 4.48302300109529, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '6467610', 'n_tokens_mistral': 1463, 'n_tokens_neox': 1399, 'n_words': 518}