Browse Source

优化综合运控接口

pull/4/head
songjc 2 years ago
parent
commit
305ceda830
3 changed files with 30 additions and 67 deletions
  1. +0
    -16
      api/unifyops/client.go
  2. +22
    -43
      api/unifyops/unifyops.go
  3. +8
    -8
      api/unifyops/unifyops_test.go

+ 0
- 16
api/unifyops/client.go View File

@@ -28,19 +28,3 @@ func NewClient(baseURL string) *Client {
baseURL: baseURL,
}
}

// func (c *Client) SendRequest(path string, method string, body []byte) (*http.Response, error) {
// url := c.BaseURL + path
// req, err := http.NewRequest(method, url, bytes.NewBuffer(body))
// if err != nil {
// return nil, err
// }

// client := &http.Client{}
// resp, err := client.Do(req)
// if err != nil {
// return nil, err
// }

// return resp, nil
// }

+ 22
- 43
api/unifyops/unifyops.go View File

@@ -17,7 +17,7 @@ type SlwNode struct {
SlwRegionID int64 `json:"slwRegionID"`
}

func (c *Client) getSlwNodeInfo() (*[]SlwNode, error) {
func (c *Client) GetSlwNodeInfo() (*[]SlwNode, error) {
url, err := url.JoinPath(c.baseURL, "/cmdb/resApi/getSlwNodeInfo")
if err != nil {
return nil, err
@@ -40,6 +40,7 @@ func (c *Client) getSlwNodeInfo() (*[]SlwNode, error) {

return nil, codeResp.ToError()
}

return nil, fmt.Errorf("unknow response content type: %s", contType)
}

@@ -47,18 +48,18 @@ type Node struct {
NodeId int64 `json:"nodeId"`
}

type CalResourceData struct {
type ResourceData[T any] struct {
Name string `json:"name"`
Total calDetailType `json:"total"`
Available calDetailType `json:"available"`
Total DetailType[T] `json:"total"`
Available DetailType[T] `json:"available"`
}

type calDetailType struct {
Unit string
Value int64
type DetailType[T any] struct {
Unit string `json:"unit"`
Value T `json:"value"`
}

func (c *Client) getCPUData(node Node) (*CalResourceData, error) {
func (c *Client) GetCPUData(node Node) (*ResourceData[int64], error) {
//TODO 整合成一个接口,参数增加一个resourceType,根据type调用不同的接口
url, err := url.JoinPath(c.baseURL, "/cmdb/resApi/getCPUData")
if err != nil {
@@ -74,7 +75,7 @@ func (c *Client) getCPUData(node Node) (*CalResourceData, error) {
contType := resp.Header.Get("Content-Type")
if strings.Contains(contType, myhttp.ContentTypeJSON) {

var codeResp response[CalResourceData]
var codeResp response[ResourceData[int64]]
if err := serder.JSONToObjectStream(resp.Body, &codeResp); err != nil {
return nil, fmt.Errorf("parsing response: %w", err)
}
@@ -89,7 +90,7 @@ func (c *Client) getCPUData(node Node) (*CalResourceData, error) {
return nil, fmt.Errorf("unknow response content type: %s", contType)
}

func (c *Client) getNPUData(node Node) (*CalResourceData, error) {
func (c *Client) GetNPUData(node Node) (*ResourceData[int64], error) {
url, err := url.JoinPath(c.baseURL, "/cmdb/resApi/getNPUData")
if err != nil {
return nil, err
@@ -104,7 +105,7 @@ func (c *Client) getNPUData(node Node) (*CalResourceData, error) {
contType := resp.Header.Get("Content-Type")
if strings.Contains(contType, myhttp.ContentTypeJSON) {

var codeResp response[CalResourceData]
var codeResp response[ResourceData[int64]]
if err := serder.JSONToObjectStream(resp.Body, &codeResp); err != nil {
return nil, fmt.Errorf("parsing response: %w", err)
}
@@ -119,7 +120,7 @@ func (c *Client) getNPUData(node Node) (*CalResourceData, error) {
return nil, fmt.Errorf("unknow response content type: %s", contType)
}

func (c *Client) getGPUData(node Node) (*CalResourceData, error) {
func (c *Client) GetGPUData(node Node) (*ResourceData[int64], error) {
url, err := url.JoinPath(c.baseURL, "/cmdb/resApi/getGPUData")
if err != nil {
return nil, err
@@ -134,7 +135,7 @@ func (c *Client) getGPUData(node Node) (*CalResourceData, error) {
contType := resp.Header.Get("Content-Type")
if strings.Contains(contType, myhttp.ContentTypeJSON) {

var codeResp response[CalResourceData]
var codeResp response[ResourceData[int64]]
if err := serder.JSONToObjectStream(resp.Body, &codeResp); err != nil {
return nil, fmt.Errorf("parsing response: %w", err)
}
@@ -149,7 +150,7 @@ func (c *Client) getGPUData(node Node) (*CalResourceData, error) {
return nil, fmt.Errorf("unknow response content type: %s", contType)
}

func (c *Client) getMLUData(node Node) (*CalResourceData, error) {
func (c *Client) GetMLUData(node Node) (*ResourceData[int64], error) {
url, err := url.JoinPath(c.baseURL, "/cmdb/resApi/getMLUData")
if err != nil {
return nil, err
@@ -164,7 +165,7 @@ func (c *Client) getMLUData(node Node) (*CalResourceData, error) {
contType := resp.Header.Get("Content-Type")
if strings.Contains(contType, myhttp.ContentTypeJSON) {

var codeResp response[CalResourceData]
var codeResp response[ResourceData[int64]]
if err := serder.JSONToObjectStream(resp.Body, &codeResp); err != nil {
return nil, fmt.Errorf("parsing response: %w", err)
}
@@ -179,18 +180,7 @@ func (c *Client) getMLUData(node Node) (*CalResourceData, error) {
return nil, fmt.Errorf("unknow response content type: %s", contType)
}

type StrResourceData struct {
Name string `json:"name"`
Total strDetailType `json:"total"`
Available strDetailType `json:"available"`
}

type strDetailType struct {
Unit string
Value float64
}

func (c *Client) getStorageData(node Node) (*StrResourceData, error) {
func (c *Client) GetStorageData(node Node) (*ResourceData[float64], error) {
url, err := url.JoinPath(c.baseURL, "/cmdb/resApi/getStorageData")
if err != nil {
return nil, err
@@ -205,7 +195,7 @@ func (c *Client) getStorageData(node Node) (*StrResourceData, error) {
contType := resp.Header.Get("Content-Type")
if strings.Contains(contType, myhttp.ContentTypeJSON) {

var codeResp response[StrResourceData]
var codeResp response[ResourceData[float64]]
if err := serder.JSONToObjectStream(resp.Body, &codeResp); err != nil {
return nil, fmt.Errorf("parsing response: %w", err)
}
@@ -220,18 +210,7 @@ func (c *Client) getStorageData(node Node) (*StrResourceData, error) {
return nil, fmt.Errorf("unknow response content type: %s", contType)
}

type TotalResourceData struct {
Name string `json:"name"`
Total totalDetailType `json:"total"`
Available totalDetailType `json:"available"`
}

type totalDetailType struct {
Unit string
Value interface{}
}

func (c *Client) getMemoryData(node Node) (*StrResourceData, error) {
func (c *Client) GetMemoryData(node Node) (*ResourceData[float64], error) {
url, err := url.JoinPath(c.baseURL, "/cmdb/resApi/getMemoryData")
if err != nil {
return nil, err
@@ -246,7 +225,7 @@ func (c *Client) getMemoryData(node Node) (*StrResourceData, error) {
contType := resp.Header.Get("Content-Type")
if strings.Contains(contType, myhttp.ContentTypeJSON) {

var codeResp response[StrResourceData]
var codeResp response[ResourceData[float64]]
if err := serder.JSONToObjectStream(resp.Body, &codeResp); err != nil {
return nil, fmt.Errorf("parsing response: %w", err)
}
@@ -261,7 +240,7 @@ func (c *Client) getMemoryData(node Node) (*StrResourceData, error) {
return nil, fmt.Errorf("unknow response content type: %s", contType)
}

func (c *Client) getIndicatorData(node Node) (*[]TotalResourceData, error) {
func (c *Client) GetIndicatorData(node Node) (*[]ResourceData[any], error) {
url, err := url.JoinPath(c.baseURL, "/cmdb/resApi/getIndicatorData")
if err != nil {
return nil, err
@@ -276,7 +255,7 @@ func (c *Client) getIndicatorData(node Node) (*[]TotalResourceData, error) {
contType := resp.Header.Get("Content-Type")
if strings.Contains(contType, myhttp.ContentTypeJSON) {

var codeResp response[[]TotalResourceData]
var codeResp response[[]ResourceData[any]]
if err := serder.JSONToObjectStream(resp.Body, &codeResp); err != nil {
return nil, fmt.Errorf("parsing response: %w", err)
}


+ 8
- 8
api/unifyops/unifyops_test.go View File

@@ -11,47 +11,47 @@ func Test_Unify_Ops(t *testing.T) {
Convey("测试获取SlwNode信息", t, func() {
cli := NewClient("http://101.201.215.165:6000")

slwNodeInfo, err := cli.getSlwNodeInfo()
slwNodeInfo, err := cli.GetSlwNodeInfo()
So(err, ShouldBeNil)

sNodes := *slwNodeInfo
cpuData, err := cli.getCPUData(Node{
cpuData, err := cli.GetCPUData(Node{
NodeId: sNodes[0].ID,
})
So(err, ShouldBeNil)
fmt.Printf("cpuData: %v\n", cpuData)

gpuData, err := cli.getGPUData(Node{
gpuData, err := cli.GetGPUData(Node{
NodeId: sNodes[0].ID,
})
So(err, ShouldBeNil)
fmt.Printf("gpuData: %v\n", gpuData)

npuData, err := cli.getNPUData(Node{
npuData, err := cli.GetNPUData(Node{
NodeId: sNodes[0].ID,
})
So(err, ShouldBeNil)
fmt.Printf("npuData: %v\n", npuData)

mluData, err := cli.getMLUData(Node{
mluData, err := cli.GetMLUData(Node{
NodeId: sNodes[0].ID,
})
So(err, ShouldBeNil)
fmt.Printf("mluData: %v\n", mluData)

storageData, err := cli.getStorageData(Node{
storageData, err := cli.GetStorageData(Node{
NodeId: sNodes[0].ID,
})
So(err, ShouldBeNil)
fmt.Printf("storageData: %v\n", storageData)

memoryData, err := cli.getMemoryData(Node{
memoryData, err := cli.GetMemoryData(Node{
NodeId: sNodes[0].ID,
})
So(err, ShouldBeNil)
fmt.Printf("memoryData: %v\n", memoryData)

indicatorData, err := cli.getIndicatorData(Node{
indicatorData, err := cli.GetIndicatorData(Node{
NodeId: sNodes[0].ID,
})
So(err, ShouldBeNil)


Loading…
Cancel
Save