<commit_before>// Package agent runs readers, writers, and HTTP server.
package agent

import (
	"encoding/json"
	"fmt"
	"os"
	"strings"
	"time"

	"github.com/Sirupsen/logrus"
	"github.com/resourced/resourced-master/wstrafficker"
	"github.com/resourced/resourced/Godeps/_workspace/src/github.com/satori/go.uuid"
	resourced_config "github.com/resourced/resourced/config"
	"github.com/resourced/resourced/executors"
	"github.com/resourced/resourced/host"
	"github.com/resourced/resourced/libtime"
	"github.com/resourced/resourced/readers"
	"github.com/resourced/resourced/storage"
	"github.com/resourced/resourced/writers"
)

// New is the constructor for Agent struct.
func New() (*Agent, error) {
	agent := &Agent{}

	agent.ID = uuid.NewV4().String()

	err := agent.setConfigs()
	if err != nil {
		return nil, err
	}

	err = agent.setTags()
	if err != nil {
		return nil, err
	}

	err = agent.setAccessTokens()
	if err != nil {
		return nil, err
	}

	err = agent.setStorages()
	if err != nil {
		return nil, err
	}

	return agent, err
}

// Agent struct carries most of the functionality of ResourceD.
// It collects information through readers and serve them up as HTTP+JSON.
type Agent struct {
	ID               string
	Tags             map[string]string
	AccessTokens     []string
	Configs          *resourced_config.Configs
	GeneralConfig    resourced_config.GeneralConfig
	MetadataStorages *storage.MetadataStorages
	DbPath           string
	Db               *storage.Storage
	WSTrafficker     *wstrafficker.WSTrafficker
}

func (a *Agent) IsTLS() bool {
	if a.GeneralConfig.HTTPS.CertFile != "" && a.GeneralConfig.HTTPS.KeyFile != "" {
		return true
	}
	return false
}

// pathWithPrefix prepends the short version of config.Kind to path.
func (a *Agent) pathWithPrefix(config resourced_config.Config) string {
	if config.Kind == "reader" {
		return a.pathWithKindPrefix("r", config)
	} else if config.Kind == "writer" {
		return a.pathWithKindPrefix("w", config)
	} else if config.Kind == "executor" {
		return a.pathWithKindPrefix("x", config)
	}
	return config.Path
}

// pathWithKindPrefix is common function called by pathWithReaderPrefix or pathWithWriterPrefix
func (a *Agent) pathWithKindPrefix(kind string, input interface{}) string {
	prefix := "/" + kind

	switch v := input.(type) {
	case resourced_config.Config:<commit_msg>Remove wstrafficker<commit_after>// Package agent runs readers, writers, and HTTP server.
package agent

import (
	"encoding/json"
	"fmt"
	"os"
	"strings"
	"time"

	"github.com/Sirupsen/logrus"
	"github.com/resourced/resourced/Godeps/_workspace/src/github.com/satori/go.uuid"
	resourced_config "github.com/resourced/resourced/config"
	"github.com/resourced/resourced/executors"
	"github.com/resourced/resourced/host"
	"github.com/resourced/resourced/libtime"
	"github.com/resourced/resourced/readers"
	"github.com/resourced/resourced/storage"
	"github.com/resourced/resourced/writers"
)

// New is the constructor for Agent struct.
func New() (*Agent, error) {
	agent := &Agent{}

	agent.ID = uuid.NewV4().String()

	err := agent.setConfigs()
	if err != nil {
		return nil, err
	}

	err = agent.setTags()
	if err != nil {
		return nil, err
	}

	err = agent.setAccessTokens()
	if err != nil {
		return nil, err
	}

	err = agent.setStorages()
	if err != nil {
		return nil, err
	}

	return agent, err
}

// Agent struct carries most of the functionality of ResourceD.
// It collects information through readers and serve them up as HTTP+JSON.
type Agent struct {
	ID               string
	Tags             map[string]string
	AccessTokens     []string
	Configs          *resourced_config.Configs
	GeneralConfig    resourced_config.GeneralConfig
	MetadataStorages *storage.MetadataStorages
	DbPath           string
	Db               *storage.Storage
}

func (a *Agent) IsTLS() bool {
	if a.GeneralConfig.HTTPS.CertFile != "" && a.GeneralConfig.HTTPS.KeyFile != "" {
		return true
	}
	return false
}

// pathWithPrefix prepends the short version of config.Kind to path.
func (a *Agent) pathWithPrefix(config resourced_config.Config) string {
	if config.Kind == "reader" {
		return a.pathWithKindPrefix("r", config)
	} else if config.Kind == "writer" {
		return a.pathWithKindPrefix("w", config)
	} else if config.Kind == "executor" {
		return a.pathWithKindPrefix("x", config)
	}
	return config.Path
}

// pathWithKindPrefix is common function called by pathWithReaderPrefix or pathWithWriterPrefix
func (a *Agent) pathWithKindPrefix(kind string, input interface{}) string {
	prefix := "/" + kind

	switch v := input.(type) {
	case resourced_config.Config:
{'fraction_non_alphanumeric': 0.11730141458106638, 'fraction_numerical': 0.0004352557127312296, 'mean_word_length': 3.7430340557275543, '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': '4574696', 'n_tokens_mistral': 1592, 'n_tokens_neox': 1515, 'n_words': 381}