github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/core/storage/boltdb/migrations/session-to-history.go (about)

     1  /*
     2   * Copyright (C) 2018 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 migrations
    19  
    20  import (
    21  	"math/big"
    22  	"time"
    23  
    24  	"github.com/asdine/storm/v3"
    25  	consumer_session "github.com/mysteriumnetwork/node/consumer/session"
    26  	"github.com/mysteriumnetwork/node/core/connection/connectionstate"
    27  	"github.com/mysteriumnetwork/node/identity"
    28  	node_session "github.com/mysteriumnetwork/node/session"
    29  	"github.com/rs/zerolog/log"
    30  )
    31  
    32  // Status represents list of possible session statuses
    33  type Status int
    34  
    35  // Session holds structure for saving session history
    36  type Session struct {
    37  	SessionID       node_session.ID `storm:"id"`
    38  	ProviderID      identity.Identity
    39  	ServiceType     string
    40  	ProviderCountry string
    41  	Started         time.Time
    42  	Status          Status
    43  	Updated         time.Time
    44  	DataStats       connectionstate.Statistics // is updated on disconnect event
    45  }
    46  
    47  // ToSessionHistory converts the session struct to a session history struct
    48  func (s Session) ToSessionHistory() consumer_session.History {
    49  	status := ""
    50  	if s.Status == 0 {
    51  		status = "New"
    52  	} else if s.Status == 1 {
    53  		status = "Completed"
    54  	}
    55  
    56  	return consumer_session.History{
    57  		SessionID:       s.SessionID,
    58  		ProviderID:      s.ProviderID,
    59  		ServiceType:     s.ServiceType,
    60  		ProviderCountry: s.ProviderCountry,
    61  		Started:         s.Started,
    62  		Status:          status,
    63  		Updated:         s.Updated,
    64  		DataSent:        s.DataStats.BytesSent,
    65  		DataReceived:    s.DataStats.BytesReceived,
    66  		Tokens:          new(big.Int),
    67  	}
    68  }
    69  
    70  // MigrateSessionToHistory runs the session to session history migration
    71  func MigrateSessionToHistory(db *storm.DB) error {
    72  	res := []Session{}
    73  	tx, err := db.Begin(true)
    74  	if err != nil {
    75  		return err
    76  	}
    77  
    78  	err = tx.All(&res)
    79  	if err != nil {
    80  		return err
    81  	}
    82  
    83  	historyBucket := tx.From("session-history")
    84  	for i := range res {
    85  		sh := res[i].ToSessionHistory()
    86  		err := historyBucket.Save(&sh)
    87  		if err != nil {
    88  			rollbackError := tx.Rollback()
    89  			if rollbackError != nil {
    90  				log.Error().Err(err).Stack().Msg("Migrate session to history rollback failed!")
    91  			}
    92  			return err
    93  		}
    94  	}
    95  	return tx.Commit()
    96  }