deployment.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  1. package controller
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "strconv"
  7. "strings"
  8. "time"
  9. "github.com/QuantumNous/new-api/common"
  10. "github.com/QuantumNous/new-api/pkg/ionet"
  11. "github.com/gin-gonic/gin"
  12. )
  13. func getIoAPIKey(c *gin.Context) (string, bool) {
  14. common.OptionMapRWMutex.RLock()
  15. enabled := common.OptionMap["model_deployment.ionet.enabled"] == "true"
  16. apiKey := common.OptionMap["model_deployment.ionet.api_key"]
  17. common.OptionMapRWMutex.RUnlock()
  18. if !enabled || strings.TrimSpace(apiKey) == "" {
  19. common.ApiErrorMsg(c, "io.net model deployment is not enabled or api key missing")
  20. return "", false
  21. }
  22. return apiKey, true
  23. }
  24. func GetModelDeploymentSettings(c *gin.Context) {
  25. common.OptionMapRWMutex.RLock()
  26. enabled := common.OptionMap["model_deployment.ionet.enabled"] == "true"
  27. hasAPIKey := strings.TrimSpace(common.OptionMap["model_deployment.ionet.api_key"]) != ""
  28. common.OptionMapRWMutex.RUnlock()
  29. common.ApiSuccess(c, gin.H{
  30. "provider": "io.net",
  31. "enabled": enabled,
  32. "configured": hasAPIKey,
  33. "can_connect": enabled && hasAPIKey,
  34. })
  35. }
  36. func getIoClient(c *gin.Context) (*ionet.Client, bool) {
  37. apiKey, ok := getIoAPIKey(c)
  38. if !ok {
  39. return nil, false
  40. }
  41. return ionet.NewClient(apiKey), true
  42. }
  43. func getIoEnterpriseClient(c *gin.Context) (*ionet.Client, bool) {
  44. apiKey, ok := getIoAPIKey(c)
  45. if !ok {
  46. return nil, false
  47. }
  48. return ionet.NewEnterpriseClient(apiKey), true
  49. }
  50. func TestIoNetConnection(c *gin.Context) {
  51. var req struct {
  52. APIKey string `json:"api_key"`
  53. }
  54. rawBody, err := c.GetRawData()
  55. if err != nil {
  56. common.ApiError(c, err)
  57. return
  58. }
  59. if len(bytes.TrimSpace(rawBody)) > 0 {
  60. if err := json.Unmarshal(rawBody, &req); err != nil {
  61. common.ApiErrorMsg(c, "invalid request payload")
  62. return
  63. }
  64. }
  65. apiKey := strings.TrimSpace(req.APIKey)
  66. if apiKey == "" {
  67. common.OptionMapRWMutex.RLock()
  68. storedKey := strings.TrimSpace(common.OptionMap["model_deployment.ionet.api_key"])
  69. common.OptionMapRWMutex.RUnlock()
  70. if storedKey == "" {
  71. common.ApiErrorMsg(c, "api_key is required")
  72. return
  73. }
  74. apiKey = storedKey
  75. }
  76. client := ionet.NewEnterpriseClient(apiKey)
  77. result, err := client.GetMaxGPUsPerContainer()
  78. if err != nil {
  79. if apiErr, ok := err.(*ionet.APIError); ok {
  80. message := strings.TrimSpace(apiErr.Message)
  81. if message == "" {
  82. message = "failed to validate api key"
  83. }
  84. common.ApiErrorMsg(c, message)
  85. return
  86. }
  87. common.ApiError(c, err)
  88. return
  89. }
  90. totalHardware := 0
  91. totalAvailable := 0
  92. if result != nil {
  93. totalHardware = len(result.Hardware)
  94. totalAvailable = result.Total
  95. if totalAvailable == 0 {
  96. for _, hw := range result.Hardware {
  97. totalAvailable += hw.Available
  98. }
  99. }
  100. }
  101. common.ApiSuccess(c, gin.H{
  102. "hardware_count": totalHardware,
  103. "total_available": totalAvailable,
  104. })
  105. }
  106. func requireDeploymentID(c *gin.Context) (string, bool) {
  107. deploymentID := strings.TrimSpace(c.Param("id"))
  108. if deploymentID == "" {
  109. common.ApiErrorMsg(c, "deployment ID is required")
  110. return "", false
  111. }
  112. return deploymentID, true
  113. }
  114. func requireContainerID(c *gin.Context) (string, bool) {
  115. containerID := strings.TrimSpace(c.Param("container_id"))
  116. if containerID == "" {
  117. common.ApiErrorMsg(c, "container ID is required")
  118. return "", false
  119. }
  120. return containerID, true
  121. }
  122. func mapIoNetDeployment(d ionet.Deployment) map[string]interface{} {
  123. var created int64
  124. if d.CreatedAt.IsZero() {
  125. created = time.Now().Unix()
  126. } else {
  127. created = d.CreatedAt.Unix()
  128. }
  129. timeRemainingHours := d.ComputeMinutesRemaining / 60
  130. timeRemainingMins := d.ComputeMinutesRemaining % 60
  131. var timeRemaining string
  132. if timeRemainingHours > 0 {
  133. timeRemaining = fmt.Sprintf("%d hour %d minutes", timeRemainingHours, timeRemainingMins)
  134. } else if timeRemainingMins > 0 {
  135. timeRemaining = fmt.Sprintf("%d minutes", timeRemainingMins)
  136. } else {
  137. timeRemaining = "completed"
  138. }
  139. hardwareInfo := fmt.Sprintf("%s %s x%d", d.BrandName, d.HardwareName, d.HardwareQuantity)
  140. return map[string]interface{}{
  141. "id": d.ID,
  142. "deployment_name": d.Name,
  143. "container_name": d.Name,
  144. "status": strings.ToLower(d.Status),
  145. "type": "Container",
  146. "time_remaining": timeRemaining,
  147. "time_remaining_minutes": d.ComputeMinutesRemaining,
  148. "hardware_info": hardwareInfo,
  149. "hardware_name": d.HardwareName,
  150. "brand_name": d.BrandName,
  151. "hardware_quantity": d.HardwareQuantity,
  152. "completed_percent": d.CompletedPercent,
  153. "compute_minutes_served": d.ComputeMinutesServed,
  154. "compute_minutes_remaining": d.ComputeMinutesRemaining,
  155. "created_at": created,
  156. "updated_at": created,
  157. "model_name": "",
  158. "model_version": "",
  159. "instance_count": d.HardwareQuantity,
  160. "resource_config": map[string]interface{}{
  161. "cpu": "",
  162. "memory": "",
  163. "gpu": strconv.Itoa(d.HardwareQuantity),
  164. },
  165. "description": "",
  166. "provider": "io.net",
  167. }
  168. }
  169. func computeStatusCounts(total int, deployments []ionet.Deployment) map[string]int64 {
  170. counts := map[string]int64{
  171. "all": int64(total),
  172. }
  173. for _, status := range []string{"running", "completed", "failed", "deployment requested", "termination requested", "destroyed"} {
  174. counts[status] = 0
  175. }
  176. for _, d := range deployments {
  177. status := strings.ToLower(strings.TrimSpace(d.Status))
  178. counts[status] = counts[status] + 1
  179. }
  180. return counts
  181. }
  182. func GetAllDeployments(c *gin.Context) {
  183. pageInfo := common.GetPageQuery(c)
  184. client, ok := getIoEnterpriseClient(c)
  185. if !ok {
  186. return
  187. }
  188. status := c.Query("status")
  189. opts := &ionet.ListDeploymentsOptions{
  190. Status: strings.ToLower(strings.TrimSpace(status)),
  191. Page: pageInfo.GetPage(),
  192. PageSize: pageInfo.GetPageSize(),
  193. SortBy: "created_at",
  194. SortOrder: "desc",
  195. }
  196. dl, err := client.ListDeployments(opts)
  197. if err != nil {
  198. common.ApiError(c, err)
  199. return
  200. }
  201. items := make([]map[string]interface{}, 0, len(dl.Deployments))
  202. for _, d := range dl.Deployments {
  203. items = append(items, mapIoNetDeployment(d))
  204. }
  205. data := gin.H{
  206. "page": pageInfo.GetPage(),
  207. "page_size": pageInfo.GetPageSize(),
  208. "total": dl.Total,
  209. "items": items,
  210. "status_counts": computeStatusCounts(dl.Total, dl.Deployments),
  211. }
  212. common.ApiSuccess(c, data)
  213. }
  214. func SearchDeployments(c *gin.Context) {
  215. pageInfo := common.GetPageQuery(c)
  216. client, ok := getIoEnterpriseClient(c)
  217. if !ok {
  218. return
  219. }
  220. status := strings.ToLower(strings.TrimSpace(c.Query("status")))
  221. keyword := strings.TrimSpace(c.Query("keyword"))
  222. dl, err := client.ListDeployments(&ionet.ListDeploymentsOptions{
  223. Status: status,
  224. Page: pageInfo.GetPage(),
  225. PageSize: pageInfo.GetPageSize(),
  226. SortBy: "created_at",
  227. SortOrder: "desc",
  228. })
  229. if err != nil {
  230. common.ApiError(c, err)
  231. return
  232. }
  233. filtered := make([]ionet.Deployment, 0, len(dl.Deployments))
  234. if keyword == "" {
  235. filtered = dl.Deployments
  236. } else {
  237. kw := strings.ToLower(keyword)
  238. for _, d := range dl.Deployments {
  239. if strings.Contains(strings.ToLower(d.Name), kw) {
  240. filtered = append(filtered, d)
  241. }
  242. }
  243. }
  244. items := make([]map[string]interface{}, 0, len(filtered))
  245. for _, d := range filtered {
  246. items = append(items, mapIoNetDeployment(d))
  247. }
  248. total := dl.Total
  249. if keyword != "" {
  250. total = len(filtered)
  251. }
  252. data := gin.H{
  253. "page": pageInfo.GetPage(),
  254. "page_size": pageInfo.GetPageSize(),
  255. "total": total,
  256. "items": items,
  257. }
  258. common.ApiSuccess(c, data)
  259. }
  260. func GetDeployment(c *gin.Context) {
  261. client, ok := getIoEnterpriseClient(c)
  262. if !ok {
  263. return
  264. }
  265. deploymentID, ok := requireDeploymentID(c)
  266. if !ok {
  267. return
  268. }
  269. details, err := client.GetDeployment(deploymentID)
  270. if err != nil {
  271. common.ApiError(c, err)
  272. return
  273. }
  274. data := map[string]interface{}{
  275. "id": details.ID,
  276. "deployment_name": details.ID,
  277. "model_name": "",
  278. "model_version": "",
  279. "status": strings.ToLower(details.Status),
  280. "instance_count": details.TotalContainers,
  281. "hardware_id": details.HardwareID,
  282. "resource_config": map[string]interface{}{
  283. "cpu": "",
  284. "memory": "",
  285. "gpu": strconv.Itoa(details.TotalGPUs),
  286. },
  287. "created_at": details.CreatedAt.Unix(),
  288. "updated_at": details.CreatedAt.Unix(),
  289. "description": "",
  290. "amount_paid": details.AmountPaid,
  291. "completed_percent": details.CompletedPercent,
  292. "gpus_per_container": details.GPUsPerContainer,
  293. "total_gpus": details.TotalGPUs,
  294. "total_containers": details.TotalContainers,
  295. "hardware_name": details.HardwareName,
  296. "brand_name": details.BrandName,
  297. "compute_minutes_served": details.ComputeMinutesServed,
  298. "compute_minutes_remaining": details.ComputeMinutesRemaining,
  299. "locations": details.Locations,
  300. "container_config": details.ContainerConfig,
  301. }
  302. common.ApiSuccess(c, data)
  303. }
  304. func UpdateDeploymentName(c *gin.Context) {
  305. client, ok := getIoEnterpriseClient(c)
  306. if !ok {
  307. return
  308. }
  309. deploymentID, ok := requireDeploymentID(c)
  310. if !ok {
  311. return
  312. }
  313. var req struct {
  314. Name string `json:"name" binding:"required"`
  315. }
  316. if err := c.ShouldBindJSON(&req); err != nil {
  317. common.ApiError(c, err)
  318. return
  319. }
  320. updateReq := &ionet.UpdateClusterNameRequest{
  321. Name: strings.TrimSpace(req.Name),
  322. }
  323. if updateReq.Name == "" {
  324. common.ApiErrorMsg(c, "deployment name cannot be empty")
  325. return
  326. }
  327. available, err := client.CheckClusterNameAvailability(updateReq.Name)
  328. if err != nil {
  329. common.ApiError(c, fmt.Errorf("failed to check name availability: %w", err))
  330. return
  331. }
  332. if !available {
  333. common.ApiErrorMsg(c, "deployment name is not available, please choose a different name")
  334. return
  335. }
  336. resp, err := client.UpdateClusterName(deploymentID, updateReq)
  337. if err != nil {
  338. common.ApiError(c, err)
  339. return
  340. }
  341. data := gin.H{
  342. "status": resp.Status,
  343. "message": resp.Message,
  344. "id": deploymentID,
  345. "name": updateReq.Name,
  346. }
  347. common.ApiSuccess(c, data)
  348. }
  349. func UpdateDeployment(c *gin.Context) {
  350. client, ok := getIoEnterpriseClient(c)
  351. if !ok {
  352. return
  353. }
  354. deploymentID, ok := requireDeploymentID(c)
  355. if !ok {
  356. return
  357. }
  358. var req ionet.UpdateDeploymentRequest
  359. if err := c.ShouldBindJSON(&req); err != nil {
  360. common.ApiError(c, err)
  361. return
  362. }
  363. resp, err := client.UpdateDeployment(deploymentID, &req)
  364. if err != nil {
  365. common.ApiError(c, err)
  366. return
  367. }
  368. data := gin.H{
  369. "status": resp.Status,
  370. "deployment_id": resp.DeploymentID,
  371. }
  372. common.ApiSuccess(c, data)
  373. }
  374. func ExtendDeployment(c *gin.Context) {
  375. client, ok := getIoEnterpriseClient(c)
  376. if !ok {
  377. return
  378. }
  379. deploymentID, ok := requireDeploymentID(c)
  380. if !ok {
  381. return
  382. }
  383. var req ionet.ExtendDurationRequest
  384. if err := c.ShouldBindJSON(&req); err != nil {
  385. common.ApiError(c, err)
  386. return
  387. }
  388. details, err := client.ExtendDeployment(deploymentID, &req)
  389. if err != nil {
  390. common.ApiError(c, err)
  391. return
  392. }
  393. data := mapIoNetDeployment(ionet.Deployment{
  394. ID: details.ID,
  395. Status: details.Status,
  396. Name: deploymentID,
  397. CompletedPercent: float64(details.CompletedPercent),
  398. HardwareQuantity: details.TotalGPUs,
  399. BrandName: details.BrandName,
  400. HardwareName: details.HardwareName,
  401. ComputeMinutesServed: details.ComputeMinutesServed,
  402. ComputeMinutesRemaining: details.ComputeMinutesRemaining,
  403. CreatedAt: details.CreatedAt,
  404. })
  405. common.ApiSuccess(c, data)
  406. }
  407. func DeleteDeployment(c *gin.Context) {
  408. client, ok := getIoEnterpriseClient(c)
  409. if !ok {
  410. return
  411. }
  412. deploymentID, ok := requireDeploymentID(c)
  413. if !ok {
  414. return
  415. }
  416. resp, err := client.DeleteDeployment(deploymentID)
  417. if err != nil {
  418. common.ApiError(c, err)
  419. return
  420. }
  421. data := gin.H{
  422. "status": resp.Status,
  423. "deployment_id": resp.DeploymentID,
  424. "message": "Deployment termination requested successfully",
  425. }
  426. common.ApiSuccess(c, data)
  427. }
  428. func CreateDeployment(c *gin.Context) {
  429. client, ok := getIoEnterpriseClient(c)
  430. if !ok {
  431. return
  432. }
  433. var req ionet.DeploymentRequest
  434. if err := c.ShouldBindJSON(&req); err != nil {
  435. common.ApiError(c, err)
  436. return
  437. }
  438. resp, err := client.DeployContainer(&req)
  439. if err != nil {
  440. common.ApiError(c, err)
  441. return
  442. }
  443. data := gin.H{
  444. "deployment_id": resp.DeploymentID,
  445. "status": resp.Status,
  446. "message": "Deployment created successfully",
  447. }
  448. common.ApiSuccess(c, data)
  449. }
  450. func GetHardwareTypes(c *gin.Context) {
  451. client, ok := getIoEnterpriseClient(c)
  452. if !ok {
  453. return
  454. }
  455. hardwareTypes, totalAvailable, err := client.ListHardwareTypes()
  456. if err != nil {
  457. common.ApiError(c, err)
  458. return
  459. }
  460. data := gin.H{
  461. "hardware_types": hardwareTypes,
  462. "total": len(hardwareTypes),
  463. "total_available": totalAvailable,
  464. }
  465. common.ApiSuccess(c, data)
  466. }
  467. func GetLocations(c *gin.Context) {
  468. client, ok := getIoClient(c)
  469. if !ok {
  470. return
  471. }
  472. locationsResp, err := client.ListLocations()
  473. if err != nil {
  474. common.ApiError(c, err)
  475. return
  476. }
  477. total := locationsResp.Total
  478. if total == 0 {
  479. total = len(locationsResp.Locations)
  480. }
  481. data := gin.H{
  482. "locations": locationsResp.Locations,
  483. "total": total,
  484. }
  485. common.ApiSuccess(c, data)
  486. }
  487. func GetAvailableReplicas(c *gin.Context) {
  488. client, ok := getIoEnterpriseClient(c)
  489. if !ok {
  490. return
  491. }
  492. hardwareIDStr := c.Query("hardware_id")
  493. gpuCountStr := c.Query("gpu_count")
  494. if hardwareIDStr == "" {
  495. common.ApiErrorMsg(c, "hardware_id parameter is required")
  496. return
  497. }
  498. hardwareID, err := strconv.Atoi(hardwareIDStr)
  499. if err != nil || hardwareID <= 0 {
  500. common.ApiErrorMsg(c, "invalid hardware_id parameter")
  501. return
  502. }
  503. gpuCount := 1
  504. if gpuCountStr != "" {
  505. if parsed, err := strconv.Atoi(gpuCountStr); err == nil && parsed > 0 {
  506. gpuCount = parsed
  507. }
  508. }
  509. replicas, err := client.GetAvailableReplicas(hardwareID, gpuCount)
  510. if err != nil {
  511. common.ApiError(c, err)
  512. return
  513. }
  514. common.ApiSuccess(c, replicas)
  515. }
  516. func GetPriceEstimation(c *gin.Context) {
  517. client, ok := getIoEnterpriseClient(c)
  518. if !ok {
  519. return
  520. }
  521. var req ionet.PriceEstimationRequest
  522. if err := c.ShouldBindJSON(&req); err != nil {
  523. common.ApiError(c, err)
  524. return
  525. }
  526. priceResp, err := client.GetPriceEstimation(&req)
  527. if err != nil {
  528. common.ApiError(c, err)
  529. return
  530. }
  531. common.ApiSuccess(c, priceResp)
  532. }
  533. func CheckClusterNameAvailability(c *gin.Context) {
  534. client, ok := getIoEnterpriseClient(c)
  535. if !ok {
  536. return
  537. }
  538. clusterName := strings.TrimSpace(c.Query("name"))
  539. if clusterName == "" {
  540. common.ApiErrorMsg(c, "name parameter is required")
  541. return
  542. }
  543. available, err := client.CheckClusterNameAvailability(clusterName)
  544. if err != nil {
  545. common.ApiError(c, err)
  546. return
  547. }
  548. data := gin.H{
  549. "available": available,
  550. "name": clusterName,
  551. }
  552. common.ApiSuccess(c, data)
  553. }
  554. func GetDeploymentLogs(c *gin.Context) {
  555. client, ok := getIoClient(c)
  556. if !ok {
  557. return
  558. }
  559. deploymentID, ok := requireDeploymentID(c)
  560. if !ok {
  561. return
  562. }
  563. containerID := c.Query("container_id")
  564. if containerID == "" {
  565. common.ApiErrorMsg(c, "container_id parameter is required")
  566. return
  567. }
  568. level := c.Query("level")
  569. stream := c.Query("stream")
  570. cursor := c.Query("cursor")
  571. limitStr := c.Query("limit")
  572. follow := c.Query("follow") == "true"
  573. var limit int = 100
  574. if limitStr != "" {
  575. if parsedLimit, err := strconv.Atoi(limitStr); err == nil && parsedLimit > 0 {
  576. limit = parsedLimit
  577. if limit > 1000 {
  578. limit = 1000
  579. }
  580. }
  581. }
  582. opts := &ionet.GetLogsOptions{
  583. Level: level,
  584. Stream: stream,
  585. Limit: limit,
  586. Cursor: cursor,
  587. Follow: follow,
  588. }
  589. if startTime := c.Query("start_time"); startTime != "" {
  590. if t, err := time.Parse(time.RFC3339, startTime); err == nil {
  591. opts.StartTime = &t
  592. }
  593. }
  594. if endTime := c.Query("end_time"); endTime != "" {
  595. if t, err := time.Parse(time.RFC3339, endTime); err == nil {
  596. opts.EndTime = &t
  597. }
  598. }
  599. rawLogs, err := client.GetContainerLogsRaw(deploymentID, containerID, opts)
  600. if err != nil {
  601. common.ApiError(c, err)
  602. return
  603. }
  604. common.ApiSuccess(c, rawLogs)
  605. }
  606. func ListDeploymentContainers(c *gin.Context) {
  607. client, ok := getIoEnterpriseClient(c)
  608. if !ok {
  609. return
  610. }
  611. deploymentID, ok := requireDeploymentID(c)
  612. if !ok {
  613. return
  614. }
  615. containers, err := client.ListContainers(deploymentID)
  616. if err != nil {
  617. common.ApiError(c, err)
  618. return
  619. }
  620. items := make([]map[string]interface{}, 0)
  621. if containers != nil {
  622. items = make([]map[string]interface{}, 0, len(containers.Workers))
  623. for _, ctr := range containers.Workers {
  624. events := make([]map[string]interface{}, 0, len(ctr.ContainerEvents))
  625. for _, event := range ctr.ContainerEvents {
  626. events = append(events, map[string]interface{}{
  627. "time": event.Time.Unix(),
  628. "message": event.Message,
  629. })
  630. }
  631. items = append(items, map[string]interface{}{
  632. "container_id": ctr.ContainerID,
  633. "device_id": ctr.DeviceID,
  634. "status": strings.ToLower(strings.TrimSpace(ctr.Status)),
  635. "hardware": ctr.Hardware,
  636. "brand_name": ctr.BrandName,
  637. "created_at": ctr.CreatedAt.Unix(),
  638. "uptime_percent": ctr.UptimePercent,
  639. "gpus_per_container": ctr.GPUsPerContainer,
  640. "public_url": ctr.PublicURL,
  641. "events": events,
  642. })
  643. }
  644. }
  645. response := gin.H{
  646. "total": 0,
  647. "containers": items,
  648. }
  649. if containers != nil {
  650. response["total"] = containers.Total
  651. }
  652. common.ApiSuccess(c, response)
  653. }
  654. func GetContainerDetails(c *gin.Context) {
  655. client, ok := getIoEnterpriseClient(c)
  656. if !ok {
  657. return
  658. }
  659. deploymentID, ok := requireDeploymentID(c)
  660. if !ok {
  661. return
  662. }
  663. containerID, ok := requireContainerID(c)
  664. if !ok {
  665. return
  666. }
  667. details, err := client.GetContainerDetails(deploymentID, containerID)
  668. if err != nil {
  669. common.ApiError(c, err)
  670. return
  671. }
  672. if details == nil {
  673. common.ApiErrorMsg(c, "container details not found")
  674. return
  675. }
  676. events := make([]map[string]interface{}, 0, len(details.ContainerEvents))
  677. for _, event := range details.ContainerEvents {
  678. events = append(events, map[string]interface{}{
  679. "time": event.Time.Unix(),
  680. "message": event.Message,
  681. })
  682. }
  683. data := gin.H{
  684. "deployment_id": deploymentID,
  685. "container_id": details.ContainerID,
  686. "device_id": details.DeviceID,
  687. "status": strings.ToLower(strings.TrimSpace(details.Status)),
  688. "hardware": details.Hardware,
  689. "brand_name": details.BrandName,
  690. "created_at": details.CreatedAt.Unix(),
  691. "uptime_percent": details.UptimePercent,
  692. "gpus_per_container": details.GPUsPerContainer,
  693. "public_url": details.PublicURL,
  694. "events": events,
  695. }
  696. common.ApiSuccess(c, data)
  697. }