code.vegaprotocol.io/vega@v0.79.0/datanode/admin/dehistory.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package admin
    17  
    18  import (
    19  	"context"
    20  	"fmt"
    21  	"net/http"
    22  
    23  	"code.vegaprotocol.io/vega/datanode/networkhistory/segment"
    24  )
    25  
    26  type NetworkHistoryService interface {
    27  	CopyHistorySegmentToFile(ctx context.Context, historySegmentID string, outFile string) error
    28  	FetchHistorySegment(ctx context.Context, historySegmentID string) (segment.Full, error)
    29  }
    30  
    31  type NetworkHistoryAdminService struct {
    32  	networkHistoryService NetworkHistoryService
    33  }
    34  
    35  type CopyHistorySegmentToFileArg struct {
    36  	HistorySegmentID string
    37  	OutFile          string
    38  }
    39  
    40  type CopyHistorySegmentToFileReply struct {
    41  	Reply string
    42  	Err   error
    43  }
    44  
    45  func NewNetworkHistoryAdminService(networkHistoryService NetworkHistoryService) *NetworkHistoryAdminService {
    46  	return &NetworkHistoryAdminService{
    47  		networkHistoryService: networkHistoryService,
    48  	}
    49  }
    50  
    51  func (d *NetworkHistoryAdminService) CopyHistorySegmentToFile(req *http.Request, args *CopyHistorySegmentToFileArg, reply *CopyHistorySegmentToFileReply) error {
    52  	err := d.networkHistoryService.CopyHistorySegmentToFile(req.Context(), args.HistorySegmentID, args.OutFile)
    53  	if err != nil {
    54  		reply.Err = fmt.Errorf("copy history segment %s to file %s failed - %w", args.HistorySegmentID, args.OutFile, err)
    55  		return err
    56  	}
    57  
    58  	reply.Reply = fmt.Sprintf("copied history segment %s to file %s", args.HistorySegmentID, args.OutFile)
    59  	return err
    60  }
    61  
    62  func (d *NetworkHistoryAdminService) FetchHistorySegment(req *http.Request, historySegmentID *string, reply *segment.Full) (err error) {
    63  	*reply, err = d.networkHistoryService.FetchHistorySegment(req.Context(), *historySegmentID)
    64  	return
    65  }