github.com/altipla-consulting/ravendb-go-client@v0.1.3/get_index_statistics_operation.go (about)

     1  package ravendb
     2  
     3  import (
     4  	"net/http"
     5  )
     6  
     7  var _ IMaintenanceOperation = &GetIndexStatisticsOperation{}
     8  
     9  type GetIndexStatisticsOperation struct {
    10  	indexName string
    11  
    12  	Command *GetIndexStatisticsCommand
    13  }
    14  
    15  func NewGetIndexStatisticsOperation(indexName string) *GetIndexStatisticsOperation {
    16  	panicIf(indexName == "", "Index name connot be empty")
    17  	return &GetIndexStatisticsOperation{
    18  		indexName: indexName,
    19  	}
    20  }
    21  
    22  func (o *GetIndexStatisticsOperation) GetCommand(conventions *DocumentConventions) (RavenCommand, error) {
    23  	var err error
    24  	o.Command, err = NewGetIndexStatisticsCommand(o.indexName)
    25  	if err != nil {
    26  		return nil, err
    27  	}
    28  	return o.Command, nil
    29  }
    30  
    31  var (
    32  	_ RavenCommand = &GetIndexStatisticsCommand{}
    33  )
    34  
    35  type GetIndexStatisticsCommand struct {
    36  	RavenCommandBase
    37  
    38  	_indexName string
    39  
    40  	Result *IndexStats
    41  }
    42  
    43  func NewGetIndexStatisticsCommand(indexName string) (*GetIndexStatisticsCommand, error) {
    44  	if indexName == "" {
    45  		return nil, newIllegalArgumentError("Index name connot be empty")
    46  	}
    47  
    48  	res := &GetIndexStatisticsCommand{
    49  		RavenCommandBase: NewRavenCommandBase(),
    50  
    51  		_indexName: indexName,
    52  	}
    53  	res.IsReadRequest = true
    54  	return res, nil
    55  }
    56  
    57  func (c *GetIndexStatisticsCommand) createRequest(node *ServerNode) (*http.Request, error) {
    58  	url := node.URL + "/databases/" + node.Database + "/indexes/stats?name=" + urlUtilsEscapeDataString(c._indexName)
    59  
    60  	return newHttpGet(url)
    61  }
    62  
    63  func (c *GetIndexStatisticsCommand) setResponse(response []byte, fromCache bool) error {
    64  	if response == nil {
    65  		return throwInvalidResponse()
    66  	}
    67  
    68  	var res struct {
    69  		Results []*IndexStats `json:"Results"`
    70  	}
    71  
    72  	err := jsonUnmarshal(response, &res)
    73  	if err != nil {
    74  		return err
    75  	}
    76  	if len(res.Results) == 0 {
    77  		return throwInvalidResponse()
    78  	}
    79  	c.Result = res.Results[0]
    80  	return nil
    81  }