<commit_before>//
// Copyright (c) 2012-2017 Codenvy, S.A.
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// which accompanies this distribution, and is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// Contributors:
//   <NAME>. - initial API and implementation
//

package process

import (
	"time"
)

// Types of process events.
const (
	StartedEventType = "process_started"
	DiedEventType    = "process_died"
	StdoutEventType  = "process_stdout"
	StderrEventType  = "process_stderr"
)

// Event is a common interface for all the process events.
type Event interface {
	Type() string
}

// EventConsumer is a process events client.
type EventConsumer interface {
	Accept(event Event)
}

// StartedEvent published once when process is started.
type StartedEvent struct {
	Time        time.Time `json:"time"`
	Pid         uint64    `json:"pid"`
	NativePid   int       `json:"nativePid"`
	Name        string    `json:"name"`
	CommandLine string    `json:"commandLine"`
}

// Type returns StartedEventType.
func (se *StartedEvent) Type() string { return StartedEventType }

func newStartedEvent(mp MachineProcess) *StartedEvent {
	return &StartedEvent{
		Time:        time.Now(),
		Pid:         mp.Pid,
		NativePid:   mp.NativePid,
		Name:        mp.Name,
		CommandLine: mp.CommandLine,
	}
}

// DiedEvent published once after process death.
type DiedEvent struct {
	Time        time.Time `json:"time"`
	Pid         uint64    `json:"pid"`
	NativePid   int       `json:"nativePid"`
	Name        string    `json:"name"`
	CommandLine string    `json:"commandLine"`
	ExitCode    int       `json:"exitCode"`
}

// Type returns DiedEventType.
func (de *DiedEvent) Type() string { return DiedEventType }

func newDiedEvent(mp MachineProcess) *DiedEvent {
	return &DiedEvent{
		Time:        time.Now(),
		Pid:         mp.Pid,
		NativePid:   mp.NativePid,
		Name:        mp.Name,
		CommandLine: mp.CommandLine,
		ExitCode:    mp.ExitCode,
	}
}

// OutputEvent is published each time when process writes to stdout or stderr.
type OutputEvent struct {
	Time time.Time `json:"time"`
	Pid  uint64    `json:"pid"`
	Text string    `json:"text"`

	_type string
}

// Type returns one of StdoutEventType, StderrEventType.
func (se *OutputEvent) Type() string { return se._type }

func newStderrEvent(pid uint64, text string, when time.Time) Event {
	return &OutputEvent{
		Time:  when,
		Pid:   pid,
		Text:  text,
		_type: StderrEventType,
	}
}

func newStdoutEvent(pid uint64, text string, when time.Time) Event {
	return &OutputEvent{
		Time:  when,
		Pid:   pid,
		Text:  text,
		_type: StdoutEventType,
	}
}
<commit_msg>Add ability to use func as process EventConsumer<commit_after>//
// Copyright (c) 2012-2017 Codenvy, S.A.
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// which accompanies this distribution, and is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// Contributors:
//   <NAME>. - initial API and implementation
//

package process

import (
	"time"
)

// Types of process events.
const (
	StartedEventType = "process_started"
	DiedEventType    = "process_died"
	StdoutEventType  = "process_stdout"
	StderrEventType  = "process_stderr"
)

// Event is a common interface for all the process events.
type Event interface {
	Type() string
}

// EventConsumer is a process events client.
type EventConsumer interface {
	Accept(event Event)
}

// EventConsumerFunc is a function which is also consumer,
// allows to use anonymous functions as process event consumers.
type EventConsumerFunc func(event Event)

func (f EventConsumerFunc) Accept(event Event) { f(event) }

// StartedEvent published once when process is started.
type StartedEvent struct {
	Time        time.Time `json:"time"`
	Pid         uint64    `json:"pid"`
	NativePid   int       `json:"nativePid"`
	Name        string    `json:"name"`
	CommandLine string    `json:"commandLine"`
}

// Type returns StartedEventType.
func (se *StartedEvent) Type() string { return StartedEventType }

func newStartedEvent(mp MachineProcess) *StartedEvent {
	return &StartedEvent{
		Time:        time.Now(),
		Pid:         mp.Pid,
		NativePid:   mp.NativePid,
		Name:        mp.Name,
		CommandLine: mp.CommandLine,
	}
}

// DiedEvent published once after process death.
type DiedEvent struct {
	Time        time.Time `json:"time"`
	Pid         uint64    `json:"pid"`
	NativePid   int       `json:"nativePid"`
	Name        string    `json:"name"`
	CommandLine string    `json:"commandLine"`
	ExitCode    int       `json:"exitCode"`
}

// Type returns DiedEventType.
func (de *DiedEvent) Type() string { return DiedEventType }

func newDiedEvent(mp MachineProcess) *DiedEvent {
	return &DiedEvent{
		Time:        time.Now(),
		Pid:         mp.Pid,
		NativePid:   mp.NativePid,
		Name:        mp.Name,
		CommandLine: mp.CommandLine,
		ExitCode:    mp.ExitCode,
	}
}

// OutputEvent is published each time when process writes to stdout or stderr.
type OutputEvent struct {
	Time time.Time `json:"time"`
	Pid  uint64    `json:"pid"`
	Text string    `json:"text"`

	_type string
}

// Type returns one of StdoutEventType, StderrEventType.
func (se *OutputEvent) Type() string { return se._type }

func newStderrEvent(pid uint64, text string, when time.Time) Event {
	return &OutputEvent{
		Time:  when,
		Pid:   pid,
		Text:  text,
		_type: StderrEventType,
	}
}

func newStdoutEvent(pid uint64, text string, when time.Time) Event {
	return &OutputEvent{
		Time:  when,
		Pid:   pid,
		Text:  text,
		_type: StdoutEventType,
	}
}

{'fraction_non_alphanumeric': 0.10470836261419536, 'fraction_numerical': 0.007730147575544624, 'mean_word_length': 3.267616191904048, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '116571', 'n_tokens_mistral': 1917, 'n_tokens_neox': 1836, 'n_words': 566}