gitlab.com/SkynetLabs/skyd@v1.6.9/cmd/skyc/json.go (about)

     1  package main
     2  
     3  // json.go implements commands which provide json output rather than human
     4  // output.
     5  
     6  import (
     7  	"encoding/json"
     8  	"fmt"
     9  
    10  	"github.com/spf13/cobra"
    11  
    12  	"gitlab.com/SkynetLabs/skyd/skymodules"
    13  )
    14  
    15  var (
    16  	jsonCmd = &cobra.Command{
    17  		Use:   "json",
    18  		Short: "provide a json dump of siad's current status",
    19  		Long:  "queries a large number of endpoints in the siad api and produces a json dump with all of the information",
    20  		Run:   wrap(jsoncmd),
    21  	}
    22  )
    23  
    24  // jsoncmd queries a large number of endpoints in the siad api and aggregates
    25  // them together to produce a single dump of information.
    26  //
    27  // If this ever gets split into multiple subcommands, the current implementation
    28  // would specifically be 'json renter' as the focus of the current implementation
    29  // is on pulling together a large amount of renter information.
    30  func jsoncmd() {
    31  	var rs skymodules.RenterStats
    32  
    33  	// Grab any alerts.
    34  	alerts, err := httpClient.DaemonAlertsGet()
    35  	if err != nil {
    36  		die("Could not fetch alerts:", err)
    37  	}
    38  	rs.Alerts = alerts.CriticalAlerts
    39  
    40  	// Grab the contract statistics.
    41  	rc, err := httpClient.RenterDisabledContractsGet()
    42  	if err != nil {
    43  		die("Could not fetch contract status:", err)
    44  	}
    45  
    46  	// Grab the statistics on the various classes of contracts.
    47  	activeSize, activeSpent, activeRemaining, activeFees := contractStats(rc.ActiveContracts)
    48  	passiveSize, passiveSpent, passiveRemaining, passiveFees := contractStats(rc.PassiveContracts)
    49  	_, refreshedSpent, refreshedRemaining, refreshedFees := contractStats(rc.RefreshedContracts)
    50  	disabledSize, disabledSpent, disabledRemaining, disabledFees := contractStats(rc.DisabledContracts)
    51  	// Sum up the appropriate totals.
    52  	rs.ActiveContractData = activeSize
    53  	rs.PassiveContractData = passiveSize
    54  	rs.WastedContractData = disabledSize
    55  	spentToHost := activeSpent.Add(passiveSpent).Add(refreshedSpent).Add(disabledSpent)
    56  	spentToFees := activeFees.Add(passiveFees).Add(refreshedFees).Add(disabledFees)
    57  	rs.TotalContractSpentFunds = spentToHost.Add(spentToFees)
    58  	rs.TotalContractSpentFees = spentToFees
    59  	rs.TotalContractRemainingFunds = activeRemaining.Add(passiveRemaining).Add(refreshedRemaining).Add(disabledRemaining)
    60  
    61  	// Get the number of files on the system.
    62  	rf, err := httpClient.RenterDirRootGet(skymodules.RootSiaPath())
    63  	if err != nil {
    64  		die("Could not get the renter root dir:", err)
    65  	}
    66  	rs.AggregateLastHealthCheckTime = rf.Directories[0].AggregateLastHealthCheckTime
    67  	rs.TotalRepairSize = rf.Directories[0].AggregateRepairSize
    68  	rs.TotalSiafiles = rf.Directories[0].AggregateNumFiles
    69  	rs.TotalSiadirs = rf.Directories[0].AggregateNumSubDirs
    70  	rs.TotalSize = rf.Directories[0].AggregateSize
    71  	rs.TotalStuckChunks = rf.Directories[0].AggregateNumStuckChunks
    72  	rs.TotalStuckSize = rf.Directories[0].AggregateStuckSize
    73  
    74  	// Get information on the allowance.
    75  	rg, err := httpClient.RenterGet()
    76  	if err != nil {
    77  		die("Could not get the renter status:", err)
    78  	}
    79  	rs.Allowance = rg.Settings.Allowance
    80  	_, _, rs.AllowanceUnspentUnallocated = rg.FinancialMetrics.SpendingBreakdown()
    81  
    82  	// Get the wallet balance.
    83  	wg, err := httpClient.WalletGet()
    84  	if err != nil {
    85  		die("Could not get the wallet balance:", err)
    86  	}
    87  	rs.WalletFunds = wg.ConfirmedSiacoinBalance.Add(wg.UnconfirmedIncomingSiacoins).Sub(wg.UnconfirmedOutgoingSiacoins)
    88  
    89  	// Get information on the memory.
    90  	if rg.MemoryStatus.Available > 0 {
    91  		rs.HasRenterMemory = true
    92  	}
    93  	if rg.MemoryStatus.PriorityAvailable > 0 {
    94  		rs.HasPriorityRenterMemory = true
    95  	}
    96  
    97  	// Convert the rs to marshalled json.
    98  	json, err := json.MarshalIndent(rs, "", "\t")
    99  	if err != nil {
   100  		die("Could not marshal the json output:", err)
   101  	}
   102  	fmt.Println(string(json))
   103  }