github.com/choria-io/go-choria@v0.28.1-0.20240416190746-b3bf9c7d5a45/aagent/watchers/archivewatcher/state_notification.go (about)

     1  // Copyright (c) 2021, R.I. Pienaar and the Choria Project contributors
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  
     5  package archive
     6  
     7  import (
     8  	"encoding/json"
     9  	"fmt"
    10  
    11  	"github.com/choria-io/go-choria/aagent/watchers/event"
    12  	cloudevents "github.com/cloudevents/sdk-go/v2"
    13  )
    14  
    15  // StateNotification describes the current state of the watcher
    16  // described by io.choria.machine.watcher.exec.v1.state
    17  type StateNotification struct {
    18  	event.Event
    19  
    20  	Source          string `json:"source"`
    21  	Creates         string `json:"creates"`
    22  	PreviousOutcome string `json:"previous_outcome"`
    23  	PreviousRunTime int64  `json:"previous_run_time"`
    24  }
    25  
    26  // CloudEvent creates a CloudEvent from the state notification
    27  func (s *StateNotification) CloudEvent() cloudevents.Event {
    28  	return s.Event.CloudEvent(s)
    29  }
    30  
    31  // JSON creates a JSON representation of the notification
    32  func (s *StateNotification) JSON() ([]byte, error) {
    33  	return json.Marshal(s.CloudEvent())
    34  }
    35  
    36  // String is a string representation of the notification suitable for printing
    37  func (s *StateNotification) String() string {
    38  	return fmt.Sprintf("%s %s#%s source: %s, previous: %s ran: %.3fs", s.Identity, s.Machine, s.Name, s.Source, s.PreviousOutcome, float64(s.PreviousRunTime)/1000000000)
    39  }