github.com/minio/console@v1.4.1/api/admin_profiling.go (about)

     1  // This file is part of MinIO Console Server
     2  // Copyright (c) 2021 MinIO, Inc.
     3  //
     4  // This program is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Affero 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 Affero General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Affero General Public License
    15  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package api
    18  
    19  import (
    20  	"context"
    21  	"io"
    22  	"net/http"
    23  
    24  	"github.com/minio/console/models"
    25  	"github.com/minio/madmin-go/v3"
    26  	"github.com/minio/websocket"
    27  )
    28  
    29  var items []*models.StartProfilingItem
    30  
    31  type profileOptions struct {
    32  	Types string
    33  }
    34  
    35  func getProfileOptionsFromReq(req *http.Request) (*profileOptions, error) {
    36  	pOptions := profileOptions{}
    37  	pOptions.Types = req.FormValue("types")
    38  	return &pOptions, nil
    39  }
    40  
    41  func startProfiling(ctx context.Context, conn WSConn, client MinioAdmin, pOpts *profileOptions) error {
    42  	profilingResults, err := client.startProfiling(ctx, madmin.ProfilerType(pOpts.Types))
    43  	if err != nil {
    44  		return err
    45  	}
    46  	items = []*models.StartProfilingItem{}
    47  	for _, result := range profilingResults {
    48  		items = append(items, &models.StartProfilingItem{
    49  			Success:  result.Success,
    50  			Error:    result.Error,
    51  			NodeName: result.NodeName,
    52  		})
    53  	}
    54  	zippedData, err := client.stopProfiling(ctx)
    55  	if err != nil {
    56  		return err
    57  	}
    58  	message, err := io.ReadAll(zippedData)
    59  	if err != nil {
    60  		return err
    61  	}
    62  	return conn.writeMessage(websocket.BinaryMessage, message)
    63  }