stat.go raw

   1  package rest
   2  
   3  import (
   4  	"fmt"
   5  	"net/http"
   6  )
   7  
   8  const statsQPSEndpoint = "stats/qps"
   9  
  10  // StatsService handles 'stats/qps' endpoint.
  11  type StatsService service
  12  
  13  // GetQPS returns current queries per second (QPS) for the account.
  14  // The QPS number is lagged by approximately 30 seconds for statistics collection;
  15  // and the rate is computed over the preceding minute.
  16  func (s *StatsService) GetQPS() (float32, *http.Response, error) {
  17  	return s.getQPS(statsQPSEndpoint)
  18  }
  19  
  20  // GetZoneQPS returns current queries per second (QPS) for a specific zone.
  21  // The QPS number is lagged by approximately 30 seconds for statistics collection;
  22  // and the rate is computed over the preceding minute.
  23  func (s *StatsService) GetZoneQPS(zone string) (float32, *http.Response, error) {
  24  	path := fmt.Sprintf("%s/%s", statsQPSEndpoint, zone)
  25  	return s.getQPS(path)
  26  }
  27  
  28  // GetRecordQPS returns current queries per second (QPS) for a specific record.
  29  // The QPS number is lagged by approximately 30 seconds for statistics collection;
  30  // and the rate is computed over the preceding minute.
  31  func (s *StatsService) GetRecordQPS(zone, record, t string) (float32, *http.Response, error) {
  32  	path := fmt.Sprintf("%s/%s/%s/%s", statsQPSEndpoint, zone, record, t)
  33  	return s.getQPS(path)
  34  }
  35  
  36  func (s *StatsService) getQPS(path string) (float32, *http.Response, error) {
  37  	req, err := s.client.NewRequest("GET", path, nil)
  38  	if err != nil {
  39  		return 0, nil, err
  40  	}
  41  
  42  	var value struct {
  43  		/* by default unmartial will ignore any extra fields so we don't need these
  44  		Networks []struct {
  45  			Network int
  46  			Qps float32
  47  		}
  48  		*/
  49  		QPS float32
  50  	}
  51  	resp, err := s.client.Do(req, &value)
  52  
  53  	if err != nil {
  54  		switch err.(type) {
  55  		case *Error:
  56  			switch err.(*Error).Message {
  57  			case "zone not found":
  58  				return 0, nil, ErrZoneMissing
  59  			case "record not found":
  60  				return 0, nil, ErrRecordMissing
  61  			}
  62  		}
  63  		return 0, resp, err
  64  	}
  65  	return value.QPS, resp, nil
  66  }
  67