github.com/mutagen-io/mutagen@v0.18.0-rc1/pkg/api/models/synchronization/session.go (about)

     1  package synchronization
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	"github.com/mutagen-io/mutagen/pkg/synchronization"
     8  )
     9  
    10  // Session represents a synchronization session.
    11  type Session struct {
    12  	// Identifier is the unique session identifier.
    13  	Identifier string `json:"identifier"`
    14  	// Version is the session version.
    15  	Version synchronization.Version `json:"version"`
    16  	// CreationTime is the session creation timestamp.
    17  	CreationTime string `json:"creationTime"`
    18  	// CreatingVersion is the version of Mutagen that created the session.
    19  	CreatingVersion string `json:"creatingVersion"`
    20  	// Alpha stores the alpha endpoint's configuration and state.
    21  	Alpha Endpoint `json:"alpha"`
    22  	// Beta stores the beta endpoint's configuration and state.
    23  	Beta Endpoint `json:"beta"`
    24  	// Configuration is the session configuration.
    25  	Configuration
    26  	// Name is the session name.
    27  	Name string `json:"name,omitempty"`
    28  	// Label are the session labels.
    29  	Labels map[string]string `json:"labels,omitempty"`
    30  	// Paused indicates whether or not the session is paused.
    31  	Paused bool `json:"paused"`
    32  	// Status is the session status.
    33  	Status synchronization.Status `json:"status"`
    34  	// SessionState stores state fields relevant to running sessions. It is
    35  	// non-nil if and only if the session is unpaused.
    36  	*SessionState
    37  }
    38  
    39  // SessionState encodes fields relevant to unpaused sessions.
    40  type SessionState struct {
    41  	// LastError is the last synchronization error to occur.
    42  	LastError string `json:"lastError,omitempty"`
    43  	// SuccessfulCycles is the number of successful synchronization cycles to
    44  	// occur since successfully connecting to the endpoints.
    45  	SuccessfulCycles uint64 `json:"successfulCycles,omitempty"`
    46  	// Conflicts are the conflicts that identified during reconciliation. This
    47  	// list may be a truncated version of the full list if too many conflicts
    48  	// are encountered to report via the API.
    49  	Conflicts []Conflict `json:"conflicts,omitempty"`
    50  	// ExcludedConflicts is the number of conflicts that have been excluded from
    51  	// Conflicts due to truncation. This value can only be non-zero if conflicts
    52  	// is non-empty.
    53  	ExcludedConflicts uint64 `json:"excludedConflicts,omitempty"`
    54  }
    55  
    56  // loadFromInternal sets a session to match an internal Protocol Buffers session
    57  // state representation. The session state must be valid.
    58  func (s *Session) loadFromInternal(state *synchronization.State) {
    59  	// Propagate basic information.
    60  	s.Identifier = state.Session.Identifier
    61  	s.Version = state.Session.Version
    62  	s.CreationTime = state.Session.CreationTime.AsTime().Format(time.RFC3339Nano)
    63  	s.CreatingVersion = fmt.Sprintf("%d.%d.%d",
    64  		state.Session.CreatingVersionMajor,
    65  		state.Session.CreatingVersionMinor,
    66  		state.Session.CreatingVersionPatch,
    67  	)
    68  	s.Name = state.Session.Name
    69  	s.Labels = state.Session.Labels
    70  	s.Paused = state.Session.Paused
    71  	s.Status = state.Status
    72  
    73  	// Propagate endpoint information.
    74  	s.Alpha.loadFromInternal(
    75  		state.Session.Alpha,
    76  		state.Session.ConfigurationAlpha,
    77  		state.AlphaState,
    78  	)
    79  	s.Beta.loadFromInternal(
    80  		state.Session.Beta,
    81  		state.Session.ConfigurationBeta,
    82  		state.BetaState,
    83  	)
    84  
    85  	// Propagate configuration information.
    86  	s.Configuration.loadFromInternal(state.Session.Configuration)
    87  
    88  	// Propagate state information if the session isn't paused.
    89  	if state.Session.Paused {
    90  		s.SessionState = nil
    91  	} else {
    92  		s.SessionState = &SessionState{
    93  			LastError:         state.LastError,
    94  			SuccessfulCycles:  state.SuccessfulCycles,
    95  			Conflicts:         exportConflicts(state.Conflicts),
    96  			ExcludedConflicts: state.ExcludedConflicts,
    97  		}
    98  	}
    99  }
   100  
   101  // ExportSessions converts a slice of internal session state representations to
   102  // a slice of public session representations. It is guaranteed to return a
   103  // non-nil value, even in the case of an empty slice.
   104  func ExportSessions(states []*synchronization.State) []Session {
   105  	// Create the resulting slice.
   106  	count := len(states)
   107  	results := make([]Session, count)
   108  
   109  	// Propagate session information
   110  	for i := 0; i < count; i++ {
   111  		results[i].loadFromInternal(states[i])
   112  	}
   113  
   114  	// Done.
   115  	return results
   116  }