Skip to content

Commit 4c61c63

Browse files
author
Rohit-PX
committed
move exports to package
Signed-off-by: Rohit-PX <rkulkarni@purestorage.com>
1 parent cf790e6 commit 4c61c63

1 file changed

Lines changed: 171 additions & 0 deletions

File tree

pkg/utils/export_stats.go

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
package utils
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"fmt"
7+
"io/ioutil"
8+
"net/http"
9+
"time"
10+
11+
storkv1 "github.com/libopenstorage/stork/pkg/apis/stork/v1alpha1"
12+
"github.com/sirupsen/logrus"
13+
)
14+
15+
const (
16+
aetosStatsURL = "http://aetos.pwx.purestorage.com/dashboard/stats?stats_type=migration_stats_new&limit=100"
17+
)
18+
19+
type StatsExportType struct {
20+
id OidType `json:"_id",omitempty`
21+
Name string `json:"name",omitempty`
22+
Product string `json:"product",omitempty`
23+
StatsType string `json:"statsType",omitempty`
24+
Version string `json:"version",omitempty`
25+
Data MigrationStatsType `json:"data",omitempty`
26+
}
27+
28+
type MigrationStatsType struct {
29+
TotalNumberOfVolumes json.Number `json:"totalNumberOfVolumes",omitempty`
30+
NumOfMigratedVolumes json.Number `json:"numOfMigratedVolumes",omitempty`
31+
TotalNumberOfResources json.Number `json:"totalNumberOfResources",omitempty`
32+
NumOfMigratedResources json.Number `json:"numOfMigratedResources",omitempty`
33+
TotalBytesMigrated json.Number `json:"totalBytesMigrated",omitempty`
34+
ElapsedTimeForVolumeMigration string `json:"elapsedTimeForVolumeMigration",omitempty`
35+
ElapsedTimeForResourceMigration string `json:"elapsedTimeForResourceMigration",omitempty`
36+
}
37+
38+
type OidType struct {
39+
oid string `json:"$oid"`
40+
}
41+
42+
func GetMigrationStatsFromAetos(url string) ([]StatsExportType, error) {
43+
client := http.Client{Timeout: time.Second * 3}
44+
req, err := http.NewRequest("GET", url, nil)
45+
if err != nil {
46+
return nil, fmt.Errorf("error querying Aetos for stats: %v", err)
47+
}
48+
49+
req.Header.Add("Content-Type", "application/json")
50+
51+
q := req.URL.Query()
52+
q.Add("format", "json")
53+
req.URL.RawQuery = q.Encode()
54+
resp, err := client.Do(req)
55+
if err != nil {
56+
return nil, fmt.Errorf("error querying Aetos metadata: %v", err)
57+
}
58+
59+
if resp.StatusCode != 200 {
60+
return nil, fmt.Errorf("error querying Aetos metadata: Code %d returned for url %s", resp.StatusCode, req.URL)
61+
}
62+
body, err := ioutil.ReadAll(resp.Body)
63+
if err != nil {
64+
return nil, fmt.Errorf("error querying Aetos metadata: %v", err)
65+
}
66+
if len(body) == 0 {
67+
return nil, fmt.Errorf("error querying Aetos metadata: Empty response")
68+
}
69+
70+
//fmt.Printf("\nBody of response: %v\n\n", string(body))
71+
data := []StatsExportType{}
72+
err = json.Unmarshal(body, &data)
73+
if err != nil {
74+
return nil, fmt.Errorf("error parsing Aetos metadata: %v", err)
75+
}
76+
77+
defer func() {
78+
err := resp.Body.Close()
79+
if err != nil {
80+
logrus.Errorf("Error closing body when getting Aetos data: %v", err)
81+
}
82+
}()
83+
return data, nil
84+
}
85+
86+
func WriteMigrationStatsToAetos() error {
87+
data := MockStat()
88+
body, err := json.Marshal(data)
89+
if err != nil {
90+
return fmt.Errorf("failed to marshal: %v", err)
91+
}
92+
resp, err := http.Post("http://aetos.pwx.purestorage.com/dashboard/stats", "application/json", bytes.NewBuffer(body))
93+
if err != nil {
94+
return fmt.Errorf("post request to Aetos failed: %v", err)
95+
}
96+
defer resp.Body.Close()
97+
if resp.StatusCode == http.StatusCreated || resp.StatusCode == http.StatusOK {
98+
body, err := ioutil.ReadAll(resp.Body)
99+
if err != nil {
100+
//Failed to read response.
101+
return fmt.Errorf("response from Aetos failed: %v", err)
102+
}
103+
104+
jsonStr := string(body)
105+
fmt.Println("Response: ", jsonStr)
106+
} else {
107+
fmt.Println("Get failed with error: ", resp.Status)
108+
}
109+
110+
return nil
111+
}
112+
113+
func MockStat() StatsExportType {
114+
mockStat := StatsExportType{
115+
Name: "stork_integration_test",
116+
Product: "stork",
117+
StatsType: "migration_stats_mock",
118+
Version: "v1alpha1",
119+
Data: MigrationStatsType{
120+
TotalNumberOfVolumes: "1",
121+
NumOfMigratedVolumes: "1",
122+
TotalNumberOfResources: "5",
123+
NumOfMigratedResources: "1",
124+
TotalBytesMigrated: "12345",
125+
ElapsedTimeForVolumeMigration: "11.1111s",
126+
ElapsedTimeForResourceMigration: "12321.3453s",
127+
},
128+
}
129+
return mockStat
130+
}
131+
132+
func NewStat() *StatsExportType {
133+
newStat := &StatsExportType{
134+
Name: "stork_integration_test",
135+
Product: "stork",
136+
StatsType: "migration_stats_mock",
137+
Version: "v1alpha1",
138+
Data: MigrationStatsType{
139+
TotalNumberOfVolumes: "",
140+
NumOfMigratedVolumes: "",
141+
TotalNumberOfResources: "",
142+
NumOfMigratedResources: "",
143+
TotalBytesMigrated: "",
144+
ElapsedTimeForVolumeMigration: "",
145+
ElapsedTimeForResourceMigration: "",
146+
},
147+
}
148+
return mockStat
149+
}
150+
151+
func GetExportableStatsFromMigrationObject(mig *storkv1.Migration) *StatsExportType {
152+
exportStats := NewStat()
153+
exportStats.Data.TotalNumberOfVolumes = mig.Status.Summary.TotalNumberOfVolumes
154+
exportStats.Data.NumOfMigratedVolumes = mig.Status.Summary.NumberOfMigratedVolumes
155+
exportStats.Data.TotalNumberOfResources = mig.Status.Summary.TotalNumberOfResources
156+
exportStats.Data.NumOfMigratedResources = mig.Status.Summary.NumberOfMigratedResources
157+
exportStats.Data.TotalBytesMigrated = mig.Status.Summary.TotalBytesMigrated
158+
exportStats.Data.ElapsedTimeForVolumeMigration = mig.Status.Summary.ElapsedTimeForVolumeMigration
159+
exportStats.Data.ElapsedTimeForResourceMigration = mig.Status.Summary.ElapsedTimeForResourceMigration
160+
161+
return exportStats
162+
163+
}
164+
165+
func PrettyStruct(data interface{}) (string, error) {
166+
val, err := json.MarshalIndent(data, "", " ")
167+
if err != nil {
168+
return "", err
169+
}
170+
return string(val), nil
171+
}

0 commit comments

Comments
 (0)