github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/tequilapi/endpoints/session_connectivity.go (about)

     1  /*
     2   * Copyright (C) 2019 The "MysteriumNetwork/node" Authors.
     3   *
     4   * This program is free software: you can redistribute it and/or modify
     5   * it under the terms of the GNU General Public License as published by
     6   * the Free Software Foundation, either version 3 of the License, or
     7   * (at your option) any later version.
     8   *
     9   * This program is distributed in the hope that it will be useful,
    10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12   * GNU General Public License for more details.
    13   *
    14   * You should have received a copy of the GNU General Public License
    15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16   */
    17  
    18  package endpoints
    19  
    20  import (
    21  	"time"
    22  
    23  	"github.com/gin-gonic/gin"
    24  
    25  	"github.com/mysteriumnetwork/node/session/connectivity"
    26  	"github.com/mysteriumnetwork/node/tequilapi/utils"
    27  )
    28  
    29  // swagger:model ConnectivityStatus
    30  type sessionConnectivityStatusCollection struct {
    31  	Entries []*sessionConnectivityStatus `json:"entries"`
    32  }
    33  
    34  type sessionConnectivityStatus struct {
    35  	PeerAddress  string    `json:"peer_address"`
    36  	SessionID    string    `json:"session_id"`
    37  	Code         uint32    `json:"code"`
    38  	Message      string    `json:"message"`
    39  	CreatedAtUTC time.Time `json:"created_at_utc"`
    40  }
    41  
    42  type sessionConnectivityEndpoint struct {
    43  	statusStorage connectivity.StatusStorage
    44  }
    45  
    46  // swagger:operation GET /sessions-connectivity-status ConnectivityStatus
    47  //
    48  //	---
    49  //	summary: Returns session connectivity status
    50  //	description: Returns list of session connectivity status
    51  //	responses:
    52  //	  200:
    53  //	    description: List of connectivity statuses
    54  //	    schema:
    55  //	      "$ref": "#/definitions/ConnectivityStatus"
    56  func (e *sessionConnectivityEndpoint) List(c *gin.Context) {
    57  	r := sessionConnectivityStatusCollection{
    58  		Entries: []*sessionConnectivityStatus{},
    59  	}
    60  
    61  	for _, entry := range e.statusStorage.GetAllStatusEntries() {
    62  		r.Entries = append(r.Entries, &sessionConnectivityStatus{
    63  			PeerAddress:  entry.PeerID.Address,
    64  			SessionID:    entry.SessionID,
    65  			Code:         uint32(entry.StatusCode),
    66  			Message:      entry.Message,
    67  			CreatedAtUTC: entry.CreatedAtUTC,
    68  		})
    69  	}
    70  
    71  	utils.WriteAsJSON(r, c.Writer)
    72  }
    73  
    74  // AddRoutesForConnectivityStatus attaches connectivity statuses endpoints to router.
    75  func AddRoutesForConnectivityStatus(statusStorage connectivity.StatusStorage) func(*gin.Engine) error {
    76  	e := &sessionConnectivityEndpoint{
    77  		statusStorage: statusStorage,
    78  	}
    79  	return func(g *gin.Engine) error {
    80  		g.GET("/sessions-connectivity-status", e.List)
    81  		return nil
    82  	}
    83  }