<commit_before>// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Contributor: <NAME> <EMAIL> [:zack]

package agents

import (
	"fmt"

	migdb "github.com/mozilla/mig/database"
)

type PersistHeartbeatPostgres struct {
	db *migdb.DB
}

func NewPersistHeartbeatPostgres(db *migdb.DB) PersistHeartbeatPostgres {
	return PersistHeartbeatPostgres{
		db: db,
	}
}

func (persist PersistHeartbeatPostgres) PersistHeartbeat(heartbeat Heartbeat) error {
	fmt.Printf("POST /heartbeat got heartbeat %v\n", heartbeat)

	agent := heartbeat.ToMigAgent()
	err := persist.db.InsertAgent(agent, nil)

	return err
}

// _dontrun invokes a goroutine that updates the agent table when a heartbeat
// message would have been handled by the scheduler. For now we're holding onto
// the code as a reference
func _dontrun() {
	go func() {
		// if an agent already exists in database, we update it, otherwise we insert it
		agent, err := ctx.DB.AgentByQueueAndPID(agt.QueueLoc, agt.PID)
		if err != nil {
			agt.DestructionTime = time.Date(9998, time.January, 11, 11, 11, 11, 11, time.UTC)
			agt.Status = mig.AgtStatusOnline
			// create a new agent, set starttime to now
			agt.StartTime = time.Now()
			err = ctx.DB.InsertAgent(agt, nil)
			if err != nil {
				ctx.Channels.Log <- mig.Log{Desc: fmt.Sprintf("Heartbeat DB insertion failed with error '%v' for agent '%s'", err, agt.Name)}.Err()
			}
		} else {
			// the agent exists in database. reuse the existing ID, and keep the status if it was
			// previously set to destroyed, otherwise set status to online<commit_msg>Update agent information in place when a heartbeat is received<commit_after>// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Contributor: <NAME> <EMAIL> [:zack]

package agents

import (
	"fmt"

	migdb "github.com/mozilla/mig/database"
)

type PersistHeartbeatPostgres struct {
	db *migdb.DB
}

func NewPersistHeartbeatPostgres(db *migdb.DB) PersistHeartbeatPostgres {
	return PersistHeartbeatPostgres{
		db: db,
	}
}

func (persist PersistHeartbeatPostgres) PersistHeartbeat(heartbeat Heartbeat) error {
	fmt.Printf("POST /heartbeat got heartbeat %v\n", heartbeat)

	agent := heartbeat.ToMigAgent()
	//err := persist.db.InsertAgent(agent, nil)
	agent, err := persist.db.AgentByQueueAndPID(agent.QueueLoc, agent.PID)
	if err != nil {
		return err
	}

	return persist.db.UpdateAgentHeartbeat(agent)
}

// _dontrun invokes a goroutine that updates the agent table when a heartbeat
// message would have been handled by the scheduler. For now we're holding onto
// the code as a reference
func _dontrun() {
	go func() {
		// if an agent already exists in database, we update it, otherwise we insert it
		agent, err := ctx.DB.AgentByQueueAndPID(agt.QueueLoc, agt.PID)
		if err != nil {
			agt.DestructionTime = time.Date(9998, time.January, 11, 11, 11, 11, 11, time.UTC)
			agt.Status = mig.AgtStatusOnline
			// create a new agent, set starttime to now
			agt.StartTime = time.Now()
			err = ctx.DB.InsertAgent(agt, nil)
			if err != nil {
				ctx.Channels.Log <- mig.Log{Desc: fmt.Sprintf("Heartbeat DB insertion failed with error '%v' for agent '%s'", err, agt.Name)}.Err()
			}
		} else {
			// the agent exists in database. reuse the existing ID, and keep the status if it was
			// previously set to destroyed, otherwise set status to online
{'fraction_non_alphanumeric': 0.1084970938278439, 'fraction_numerical': 0.00996401882092444, 'mean_word_length': 3.7180156657963446, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '403870', 'n_tokens_mistral': 1309, 'n_tokens_neox': 1163, 'n_words': 415}