<commit_before>package client

import (
	"encoding/json"
	"net/http"
	"os"

	"fmt"

	"github.com/Gamebuildr/Dave/pkg/config"
	"github.com/Gamebuildr/Dave/pkg/gamebuildr_containers"
	"github.com/Gamebuildr/Dave/pkg/scaler"
	"github.com/Gamebuildr/Dave/pkg/watcher"
	"github.com/Gamebuildr/gamebuildr-lumberjack/pkg/logger"
	"github.com/Gamebuildr/gamebuildr-lumberjack/pkg/papertrail"
)

// GogetaSubsystem is a representation for identifying a ScalableSystem as gogeta
const GogetaSubsystem = "GOGETA"

// MrRobotSubsystem is a representation for identifying a ScalableSystem as mr robot
const MrRobotSubsystem = "MRROBOT"

// DaveClient scales microservices remotely
type DaveClient struct {
	Watcher      watcher.QueueMonitor
	Log          logger.Log
	DevMode      bool
	ClientScaler scaler.System
}

type basicEngineMessage struct {
	EngineName    string `json:"enginename"`
	EngineVersion string `json:"engineversion"`
}

const logFileName string = "dave_client_"

// Create a new DaveClient
func (client *DaveClient) Create() {
	logs := logger.SystemLogger{}
	if client.DevMode {
		fileLogger := logger.FileLogSave{
			LogFileName: logFileName,
			LogFileDir:  os.Getenv(config.LogEndpoint),
		}
		logs.LogSave = fileLogger
	} else {
		papertrailLog := papertrail.PapertrailLogSave{
			App: "Dave",
			URL: os.Getenv(config.LogEndpoint),
		}
		logs.LogSave = papertrailLog
	}

	// setup client
	clientWatcher := watcher.QueueWatcher{}
	clientWatcher.Setup()
	client.Watcher.Queue = &clientWatcher
	client.Log = logs
	client.ClientScaler = &scaler.HTTPScaler{
		Client: &http.Client{},
	}

	client.Log.Info("Dave Setup and Running")
}

// RunClient will read queue messages and send a response to an api endpoint to
// start up systems if the load is less than the max load
func (client *DaveClient) RunClient(subSystem string, queueURL string, maxLoad int) {
	message, err := client.Watcher.Queue.ReadNextMessage(queueURL)
	if err != nil {
		client.Log.Error(err.Error())
		return
	}
	if message.MessageID == "" {
		client.Log.Info(fmt.Sprintf("Found no messages for %v", subSystem))
		return
	}
	client.Log.Info(fmt.Sprintf("[%v] Found message for %v, %v", message.MessageID, subSystem, message.Message))
	if err := client.Watcher.Queue.DeleteMessage(message.MessageID, queueURL); err != nil {
		client.Log.Error(err.Error())
	}
	rawMessageData := []byte(message.Message)
	engineData := new(basicEngineMessage)
	if err := json.Unmarshal(rawMessageData, &engineData); err != nil {
		client.Log.Error(err.Error())
		return
	}

	err = client.setScalerURLs(subSystem, engineData.EngineName, engineData.EngineVersion)
	if err != nil {
		client.Log.Error(err.Error())
		return
	}
	load, err := client.ClientScaler.GetSystemLoad()
	if err != nil {
		client.Log.Error(err.Error())
		return<commit_msg>Remove log that would be better suited to telemetry<commit_after>package client

import (
	"encoding/json"
	"net/http"
	"os"

	"fmt"

	"github.com/Gamebuildr/Dave/pkg/config"
	"github.com/Gamebuildr/Dave/pkg/gamebuildr_containers"
	"github.com/Gamebuildr/Dave/pkg/scaler"
	"github.com/Gamebuildr/Dave/pkg/watcher"
	"github.com/Gamebuildr/gamebuildr-lumberjack/pkg/logger"
	"github.com/Gamebuildr/gamebuildr-lumberjack/pkg/papertrail"
)

// GogetaSubsystem is a representation for identifying a ScalableSystem as gogeta
const GogetaSubsystem = "GOGETA"

// MrRobotSubsystem is a representation for identifying a ScalableSystem as mr robot
const MrRobotSubsystem = "MRROBOT"

// DaveClient scales microservices remotely
type DaveClient struct {
	Watcher      watcher.QueueMonitor
	Log          logger.Log
	DevMode      bool
	ClientScaler scaler.System
}

type basicEngineMessage struct {
	EngineName    string `json:"enginename"`
	EngineVersion string `json:"engineversion"`
}

const logFileName string = "dave_client_"

// Create a new DaveClient
func (client *DaveClient) Create() {
	logs := logger.SystemLogger{}
	if client.DevMode {
		fileLogger := logger.FileLogSave{
			LogFileName: logFileName,
			LogFileDir:  os.Getenv(config.LogEndpoint),
		}
		logs.LogSave = fileLogger
	} else {
		papertrailLog := papertrail.PapertrailLogSave{
			App: "Dave",
			URL: os.Getenv(config.LogEndpoint),
		}
		logs.LogSave = papertrailLog
	}

	// setup client
	clientWatcher := watcher.QueueWatcher{}
	clientWatcher.Setup()
	client.Watcher.Queue = &clientWatcher
	client.Log = logs
	client.ClientScaler = &scaler.HTTPScaler{
		Client: &http.Client{},
	}

	client.Log.Info("Dave Setup and Running")
}

// RunClient will read queue messages and send a response to an api endpoint to
// start up systems if the load is less than the max load
func (client *DaveClient) RunClient(subSystem string, queueURL string, maxLoad int) {
	message, err := client.Watcher.Queue.ReadNextMessage(queueURL)
	if err != nil {
		client.Log.Error(err.Error())
		return
	}
	if message.MessageID == "" {
		return
	}
	client.Log.Info(fmt.Sprintf("[%v] Found message for %v, %v", message.MessageID, subSystem, message.Message))
	if err := client.Watcher.Queue.DeleteMessage(message.MessageID, queueURL); err != nil {
		client.Log.Error(err.Error())
	}
	rawMessageData := []byte(message.Message)
	engineData := new(basicEngineMessage)
	if err := json.Unmarshal(rawMessageData, &engineData); err != nil {
		client.Log.Error(err.Error())
		return
	}

	err = client.setScalerURLs(subSystem, engineData.EngineName, engineData.EngineVersion)
	if err != nil {
		client.Log.Error(err.Error())
		return
	}
	load, err := client.ClientScaler.GetSystemLoad()
	if err != nil {
		client.Log.Error(err.Error())
		return
{'fraction_non_alphanumeric': 0.11731943945382681, 'fraction_numerical': 0.0, 'mean_word_length': 4.490138067061144, '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': '7110341', 'n_tokens_mistral': 1935, 'n_tokens_neox': 1853, 'n_words': 423}