github.com/choria-io/go-choria@v0.28.1-0.20240416190746-b3bf9c7d5a45/client/rpcutilclient/action_daemon_stats.go (about)

     1  // generated code; DO NOT EDIT
     2  //
     3  // Client for Choria RPC Agent 'rpcutil' Version 0.28.0 generated using Choria version 0.28.0
     4  
     5  package rpcutilclient
     6  
     7  import (
     8  	"context"
     9  	"encoding/json"
    10  	"fmt"
    11  	"io"
    12  	"sync"
    13  
    14  	"github.com/choria-io/go-choria/protocol"
    15  	rpcclient "github.com/choria-io/go-choria/providers/agent/mcorpc/client"
    16  	"github.com/choria-io/go-choria/providers/agent/mcorpc/ddl/agent"
    17  	"github.com/choria-io/go-choria/providers/agent/mcorpc/replyfmt"
    18  )
    19  
    20  // DaemonStatsRequester performs a RPC request to rpcutil#daemon_stats
    21  type DaemonStatsRequester struct {
    22  	r    *requester
    23  	outc chan *DaemonStatsOutput
    24  }
    25  
    26  // DaemonStatsOutput is the output from the daemon_stats action
    27  type DaemonStatsOutput struct {
    28  	details *ResultDetails
    29  	reply   map[string]any
    30  }
    31  
    32  // DaemonStatsResult is the result from a daemon_stats action
    33  type DaemonStatsResult struct {
    34  	ddl        *agent.DDL
    35  	stats      *rpcclient.Stats
    36  	outputs    []*DaemonStatsOutput
    37  	rpcreplies []*replyfmt.RPCReply
    38  	mu         sync.Mutex
    39  }
    40  
    41  func (d *DaemonStatsResult) RenderResults(w io.Writer, format RenderFormat, displayMode DisplayMode, verbose bool, silent bool, colorize bool, log Log) error {
    42  	d.mu.Lock()
    43  	defer d.mu.Unlock()
    44  
    45  	if d.stats == nil {
    46  		return fmt.Errorf("result stats is not set, result was not completed")
    47  	}
    48  
    49  	results := &replyfmt.RPCResults{
    50  		Agent:   d.stats.Agent(),
    51  		Action:  d.stats.Action(),
    52  		Replies: d.rpcreplies,
    53  		Stats:   d.stats,
    54  	}
    55  
    56  	addl, err := d.ddl.ActionInterface(d.stats.Action())
    57  	if err != nil {
    58  		return err
    59  	}
    60  
    61  	switch format {
    62  	case JSONFormat:
    63  		return results.RenderJSON(w, addl)
    64  	case TableFormat:
    65  		return results.RenderTable(w, addl)
    66  	case TXTFooter:
    67  		results.RenderTXTFooter(w, verbose)
    68  		return nil
    69  	default:
    70  		return results.RenderTXT(w, addl, verbose, silent, replyfmt.DisplayMode(displayMode), colorize, log)
    71  	}
    72  }
    73  
    74  // Stats is the rpc request stats
    75  func (d *DaemonStatsResult) Stats() Stats {
    76  	return d.stats
    77  }
    78  
    79  // ResultDetails is the details about the request
    80  func (d *DaemonStatsOutput) ResultDetails() *ResultDetails {
    81  	return d.details
    82  }
    83  
    84  // HashMap is the raw output data
    85  func (d *DaemonStatsOutput) HashMap() map[string]any {
    86  	return d.reply
    87  }
    88  
    89  // JSON is the JSON representation of the output data
    90  func (d *DaemonStatsOutput) JSON() ([]byte, error) {
    91  	return json.Marshal(d.reply)
    92  }
    93  
    94  // ParseDaemonStatsOutput parses the result value from the DaemonStats action into target
    95  func (d *DaemonStatsOutput) ParseDaemonStatsOutput(target any) error {
    96  	j, err := d.JSON()
    97  	if err != nil {
    98  		return fmt.Errorf("could not access payload: %s", err)
    99  	}
   100  
   101  	err = json.Unmarshal(j, target)
   102  	if err != nil {
   103  		return fmt.Errorf("could not unmarshal JSON payload: %s", err)
   104  	}
   105  
   106  	return nil
   107  }
   108  
   109  // Do performs the request
   110  func (d *DaemonStatsRequester) Do(ctx context.Context) (*DaemonStatsResult, error) {
   111  	dres := &DaemonStatsResult{ddl: d.r.client.ddl}
   112  
   113  	addl, err := dres.ddl.ActionInterface(d.r.action)
   114  	if err != nil {
   115  		return nil, err
   116  	}
   117  
   118  	handler := func(pr protocol.Reply, r *rpcclient.RPCReply) {
   119  		// filtered by expr filter
   120  		if r == nil {
   121  			return
   122  		}
   123  
   124  		output := &DaemonStatsOutput{
   125  			reply: make(map[string]any),
   126  			details: &ResultDetails{
   127  				sender:  pr.SenderID(),
   128  				code:    int(r.Statuscode),
   129  				message: r.Statusmsg,
   130  				ts:      pr.Time(),
   131  			},
   132  		}
   133  
   134  		addl.SetOutputDefaults(output.reply)
   135  
   136  		err := json.Unmarshal(r.Data, &output.reply)
   137  		if err != nil {
   138  			d.r.client.errorf("Could not decode reply from %s: %s", pr.SenderID(), err)
   139  		}
   140  
   141  		// caller wants a channel so we dont return a resultset too (lots of memory etc)
   142  		// this is unused now, no support for setting a channel
   143  		if d.outc != nil {
   144  			d.outc <- output
   145  			return
   146  		}
   147  
   148  		// else prepare our result set
   149  		dres.mu.Lock()
   150  		dres.outputs = append(dres.outputs, output)
   151  		dres.rpcreplies = append(dres.rpcreplies, &replyfmt.RPCReply{
   152  			Sender:   pr.SenderID(),
   153  			RPCReply: r,
   154  		})
   155  		dres.mu.Unlock()
   156  	}
   157  
   158  	res, err := d.r.do(ctx, handler)
   159  	if err != nil {
   160  		return nil, err
   161  	}
   162  
   163  	dres.stats = res
   164  
   165  	return dres, nil
   166  }
   167  
   168  // AllOutputs provide access to all outputs
   169  func (d *DaemonStatsResult) AllOutputs() []*DaemonStatsOutput {
   170  	return d.outputs
   171  }
   172  
   173  // EachOutput iterates over all results received
   174  func (d *DaemonStatsResult) EachOutput(h func(r *DaemonStatsOutput)) {
   175  	for _, resp := range d.outputs {
   176  		h(resp)
   177  	}
   178  }
   179  
   180  // Agents is the value of the agents output
   181  //
   182  // Description: List of agents loaded
   183  func (d *DaemonStatsOutput) Agents() []any {
   184  	val := d.reply["agents"]
   185  
   186  	return val.([]any)
   187  
   188  }
   189  
   190  // Configfile is the value of the configfile output
   191  //
   192  // Description: Config file used to start the daemon
   193  func (d *DaemonStatsOutput) Configfile() string {
   194  	val := d.reply["configfile"]
   195  
   196  	return val.(string)
   197  
   198  }
   199  
   200  // Events is the value of the events output
   201  //
   202  // Description: The number of lifecycle events that was published
   203  func (d *DaemonStatsOutput) Events() int64 {
   204  	val := d.reply["events"]
   205  
   206  	return val.(int64)
   207  
   208  }
   209  
   210  // Filtered is the value of the filtered output
   211  //
   212  // Description: Count of message that didn't pass filter checks
   213  func (d *DaemonStatsOutput) Filtered() int64 {
   214  	val := d.reply["filtered"]
   215  
   216  	return val.(int64)
   217  
   218  }
   219  
   220  // Passed is the value of the passed output
   221  //
   222  // Description: Count of messages that passed filter checks
   223  func (d *DaemonStatsOutput) Passed() int64 {
   224  	val := d.reply["passed"]
   225  
   226  	return val.(int64)
   227  
   228  }
   229  
   230  // Pid is the value of the pid output
   231  //
   232  // Description: Process ID of the Choria Server
   233  func (d *DaemonStatsOutput) Pid() int64 {
   234  	val := d.reply["pid"]
   235  
   236  	return val.(int64)
   237  
   238  }
   239  
   240  // Replies is the value of the replies output
   241  //
   242  // Description: Count of replies sent back to clients
   243  func (d *DaemonStatsOutput) Replies() int64 {
   244  	val := d.reply["replies"]
   245  
   246  	return val.(int64)
   247  
   248  }
   249  
   250  // Starttime is the value of the starttime output
   251  //
   252  // Description: Time the Choria Server started in unix seconds
   253  func (d *DaemonStatsOutput) Starttime() int64 {
   254  	val := d.reply["starttime"]
   255  
   256  	return val.(int64)
   257  
   258  }
   259  
   260  // Threads is the value of the threads output
   261  //
   262  // Description: List of threads active in the Choria Server
   263  func (d *DaemonStatsOutput) Threads() []any {
   264  	val := d.reply["threads"]
   265  
   266  	return val.([]any)
   267  
   268  }
   269  
   270  // Times is the value of the times output
   271  //
   272  // Description: Processor time consumed by the Choria Server
   273  func (d *DaemonStatsOutput) Times() map[string]any {
   274  	val := d.reply["times"]
   275  
   276  	return val.(map[string]any)
   277  
   278  }
   279  
   280  // Total is the value of the total output
   281  //
   282  // Description: Count of messages received by the Choria Server
   283  func (d *DaemonStatsOutput) Total() int64 {
   284  	val := d.reply["total"]
   285  
   286  	return val.(int64)
   287  
   288  }
   289  
   290  // Ttlexpired is the value of the ttlexpired output
   291  //
   292  // Description: Count of messages that did pass TTL checks
   293  func (d *DaemonStatsOutput) Ttlexpired() int64 {
   294  	val := d.reply["ttlexpired"]
   295  
   296  	return val.(int64)
   297  
   298  }
   299  
   300  // Unvalidated is the value of the unvalidated output
   301  //
   302  // Description: Count of messages that failed security validation
   303  func (d *DaemonStatsOutput) Unvalidated() int64 {
   304  	val := d.reply["unvalidated"]
   305  
   306  	return val.(int64)
   307  
   308  }
   309  
   310  // Validated is the value of the validated output
   311  //
   312  // Description: Count of messages that passed security validation
   313  func (d *DaemonStatsOutput) Validated() int64 {
   314  	val := d.reply["validated"]
   315  
   316  	return val.(int64)
   317  
   318  }
   319  
   320  // Version is the value of the version output
   321  //
   322  // Description: Choria Server Version
   323  func (d *DaemonStatsOutput) Version() string {
   324  	val := d.reply["version"]
   325  
   326  	return val.(string)
   327  
   328  }