|
| 1 | +package utils |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "io/ioutil" |
| 8 | + "net/http" |
| 9 | + "strconv" |
| 10 | + "strings" |
| 11 | + "time" |
| 12 | + |
| 13 | + storkv1 "github.com/libopenstorage/stork/pkg/apis/stork/v1alpha1" |
| 14 | + "github.com/sirupsen/logrus" |
| 15 | +) |
| 16 | + |
| 17 | +const ( |
| 18 | + AetosStatsURL = "http://aetos.pwx.purestorage.com/dashboard/stats?stats_type=MigrationDemoFinal&limit=100" |
| 19 | +) |
| 20 | + |
| 21 | +var StorkVersion string |
| 22 | +var PortworxVersion string |
| 23 | + |
| 24 | +type StatsExportType struct { |
| 25 | + id OidType `json: "_id",omitempty` |
| 26 | + Name string `json: "name",omitempty` |
| 27 | + Product string `json: "product",omitempty` |
| 28 | + Version string `json: "version",omitempty` |
| 29 | + StatsType string `json: "statsType",omitempty` |
| 30 | + Data MigrationStatsType `json: "data",omitempty` |
| 31 | +} |
| 32 | + |
| 33 | +type MigrationStatsType struct { |
| 34 | + CreatedOn string `json: "created",omitempty` |
| 35 | + TotalNumberOfVolumes json.Number `json: "totalNumberOfVolumes",omitempty` |
| 36 | + NumOfMigratedVolumes json.Number `json: "numOfMigratedVolumes",omitempty` |
| 37 | + TotalNumberOfResources json.Number `json: "totalNumberOfResources",omitempty` |
| 38 | + NumOfMigratedResources json.Number `json: "numOfMigratedResources",omitempty` |
| 39 | + TotalBytesMigrated json.Number `json: "totalBytesMigrated",omitempty` |
| 40 | + ElapsedTimeForVolumeMigration string `json: "elapsedTimeForVolumeMigration",omitempty` |
| 41 | + ElapsedTimeForResourceMigration string `json: "elapsedTimeForResourceMigration",omitempty` |
| 42 | + Application string `json: "application",omitempty` |
| 43 | + StorkVersion string `json: "storkVersion",omitempty` |
| 44 | + PortworxVersion string `json: "portworxVersion",omitempty` |
| 45 | +} |
| 46 | + |
| 47 | +type OidType struct { |
| 48 | + oid string `json: "$oid"` |
| 49 | +} |
| 50 | + |
| 51 | +type AllStats []StatsExportType |
| 52 | + |
| 53 | +func GetMigrationStatsFromAetos(url string) ([]StatsExportType, error) { |
| 54 | + client := http.Client{Timeout: time.Second * 3} |
| 55 | + req, err := http.NewRequest("GET", url, nil) |
| 56 | + if err != nil { |
| 57 | + return nil, fmt.Errorf("error querying Aetos for stats: %v", err) |
| 58 | + } |
| 59 | + |
| 60 | + req.Header.Add("Content-Type", "application/json") |
| 61 | + |
| 62 | + q := req.URL.Query() |
| 63 | + q.Add("format", "json") |
| 64 | + req.URL.RawQuery = q.Encode() |
| 65 | + resp, err := client.Do(req) |
| 66 | + if err != nil { |
| 67 | + return nil, fmt.Errorf("error querying Aetos metadata: %v", err) |
| 68 | + } |
| 69 | + |
| 70 | + if resp.StatusCode != 200 { |
| 71 | + return nil, fmt.Errorf("error querying Aetos metadata: Code %d returned for url %s", resp.StatusCode, req.URL) |
| 72 | + } |
| 73 | + body, err := ioutil.ReadAll(resp.Body) |
| 74 | + if err != nil { |
| 75 | + return nil, fmt.Errorf("error querying Aetos metadata: %v", err) |
| 76 | + } |
| 77 | + if len(body) == 0 { |
| 78 | + return nil, fmt.Errorf("error querying Aetos metadata: Empty response") |
| 79 | + } |
| 80 | + |
| 81 | + //fmt.Printf("\nBody of response: %v\n\n", string(body)) |
| 82 | + data := AllStats{} |
| 83 | + err = json.Unmarshal(body, &data) |
| 84 | + if err != nil { |
| 85 | + return nil, fmt.Errorf("error parsing Aetos metadata: %v", err) |
| 86 | + } |
| 87 | + |
| 88 | + defer func() { |
| 89 | + err := resp.Body.Close() |
| 90 | + if err != nil { |
| 91 | + logrus.Errorf("Error closing body when getting Aetos data: %v", err) |
| 92 | + } |
| 93 | + }() |
| 94 | + return data, nil |
| 95 | +} |
| 96 | + |
| 97 | +func WriteMigrationStatsToAetos(data StatsExportType) error { |
| 98 | + body, err := json.Marshal(data) |
| 99 | + if err != nil { |
| 100 | + return fmt.Errorf("failed to marshal: %v", err) |
| 101 | + } |
| 102 | + resp, err := http.Post("http://aetos.pwx.purestorage.com/dashboard/stats", "application/json", bytes.NewBuffer(body)) |
| 103 | + if err != nil { |
| 104 | + return fmt.Errorf("post request to Aetos failed: %v", err) |
| 105 | + } |
| 106 | + defer resp.Body.Close() |
| 107 | + if resp.StatusCode == http.StatusCreated || resp.StatusCode == http.StatusOK { |
| 108 | + body, err := ioutil.ReadAll(resp.Body) |
| 109 | + if err != nil { |
| 110 | + //Failed to read response. |
| 111 | + return fmt.Errorf("response from Aetos failed: %v", err) |
| 112 | + } |
| 113 | + |
| 114 | + jsonStr := string(body) |
| 115 | + logrus.Infof("Stats successfully pushed to DB. Response: ", jsonStr) |
| 116 | + } else { |
| 117 | + return fmt.Errorf("Get failed with Reponse status: %s", resp.Status) |
| 118 | + } |
| 119 | + |
| 120 | + return nil |
| 121 | +} |
| 122 | + |
| 123 | +func MockStat() StatsExportType { |
| 124 | + mockStat := StatsExportType{ |
| 125 | + Name: "stork_integration_test", |
| 126 | + Product: "stork", |
| 127 | + StatsType: "migration_stats_mock", |
| 128 | + Version: "v1alpha1", |
| 129 | + Data: MigrationStatsType{ |
| 130 | + TotalNumberOfVolumes: "1", |
| 131 | + NumOfMigratedVolumes: "1", |
| 132 | + TotalNumberOfResources: "5", |
| 133 | + NumOfMigratedResources: "1", |
| 134 | + TotalBytesMigrated: "12345", |
| 135 | + ElapsedTimeForVolumeMigration: "11.1111s", |
| 136 | + ElapsedTimeForResourceMigration: "12321.3453s", |
| 137 | + Application: "mock_app", |
| 138 | + StorkVersion: StorkVersion, |
| 139 | + PortworxVersion: PortworxVersion, |
| 140 | + }, |
| 141 | + } |
| 142 | + return mockStat |
| 143 | +} |
| 144 | + |
| 145 | +func NewStat() StatsExportType { |
| 146 | + newStat := StatsExportType{ |
| 147 | + Name: "stork_integration_test", |
| 148 | + Product: "stork", |
| 149 | + StatsType: "MigrationDemoFinal", |
| 150 | + Version: "v1alpha1", |
| 151 | + Data: MigrationStatsType{ |
| 152 | + TotalNumberOfVolumes: "", |
| 153 | + Application: "", |
| 154 | + NumOfMigratedVolumes: "", |
| 155 | + TotalNumberOfResources: "", |
| 156 | + NumOfMigratedResources: "", |
| 157 | + TotalBytesMigrated: "", |
| 158 | + ElapsedTimeForVolumeMigration: "", |
| 159 | + ElapsedTimeForResourceMigration: "", |
| 160 | + StorkVersion: StorkVersion, |
| 161 | + PortworxVersion: PortworxVersion, |
| 162 | + }, |
| 163 | + } |
| 164 | + return newStat |
| 165 | +} |
| 166 | + |
| 167 | +func GetExportableStatsFromMigrationObject(mig *storkv1.Migration) StatsExportType { |
| 168 | + exportStats := NewStat() |
| 169 | + |
| 170 | + exportStats.Data.Application = getResourceNamesFromMigration(mig) |
| 171 | + |
| 172 | + exportStats.Data.CreatedOn = (mig.GetCreationTimestamp()).Format("2006-01-02 15:04:05") |
| 173 | + |
| 174 | + exportStats.Data.TotalNumberOfVolumes = json.Number(strconv.FormatUint(mig.Status.Summary.TotalNumberOfVolumes, 10)) |
| 175 | + |
| 176 | + exportStats.Data.NumOfMigratedVolumes = json.Number(strconv.FormatUint(mig.Status.Summary.NumberOfMigratedVolumes, 10)) |
| 177 | + |
| 178 | + exportStats.Data.TotalNumberOfResources = json.Number(strconv.FormatUint(mig.Status.Summary.TotalNumberOfResources, 10)) |
| 179 | + |
| 180 | + exportStats.Data.NumOfMigratedResources = json.Number(strconv.FormatUint(mig.Status.Summary.NumberOfMigratedResources, 10)) |
| 181 | + |
| 182 | + exportStats.Data.TotalBytesMigrated = json.Number(strconv.FormatUint(mig.Status.Summary.TotalBytesMigrated, 10)) |
| 183 | + |
| 184 | + exportStats.Data.ElapsedTimeForVolumeMigration = mig.Status.Summary.ElapsedTimeForVolumeMigration |
| 185 | + |
| 186 | + exportStats.Data.ElapsedTimeForResourceMigration = mig.Status.Summary.ElapsedTimeForResourceMigration |
| 187 | + |
| 188 | + PrettyStruct(exportStats) |
| 189 | + |
| 190 | + return exportStats |
| 191 | + |
| 192 | +} |
| 193 | + |
| 194 | +func PrettyStruct(data interface{}) { |
| 195 | + val, err := json.MarshalIndent(data, "", " ") |
| 196 | + if err != nil { |
| 197 | + logrus.Panicf("Failed to prettify data") |
| 198 | + } |
| 199 | + fmt.Printf("Exportable migration data: %v", string(val)) |
| 200 | +} |
| 201 | + |
| 202 | +func getResourceNamesFromMigration(mig *storkv1.Migration) string { |
| 203 | + var resourceList []string |
| 204 | + for _, resource := range mig.Status.Resources { |
| 205 | + if resource.Kind == "Deployment" || resource.Kind == "StatefulSet" { |
| 206 | + resourceList = append(resourceList, resource.Name) |
| 207 | + } |
| 208 | + } |
| 209 | + if len(resourceList) > 1 { |
| 210 | + // return comma separated list of apps if there are multiple apps |
| 211 | + return strings.Join(resourceList, ",") |
| 212 | + } else if len(resourceList) == 1 { |
| 213 | + return resourceList[0] |
| 214 | + } |
| 215 | + |
| 216 | + logrus.Info("App name not found for pushing to DB.") |
| 217 | + return "" |
| 218 | +} |
0 commit comments