You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

handlers.go 5.2 kB

10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package handlers
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "gitlink.org.cn/cloudream/storage/datamap/internal/models"
  5. "gorm.io/gorm"
  6. "net/http"
  7. "strconv"
  8. )
  9. // DB 全局数据库连接实例
  10. var DB *gorm.DB
  11. // SetDB 设置数据库连接实例
  12. func SetDB(db *gorm.DB) {
  13. DB = db
  14. }
  15. // GetHubInfo 获取 节点交互数据
  16. func GetHubInfo(c *gin.Context) {
  17. repoHub := models.NewHubRepository(DB)
  18. repoStorage := models.NewStorageRepository(DB)
  19. repoHubReq := models.NewHubRequestRepository(DB)
  20. nodes := make([]models.Node, 0)
  21. edges := make([]models.Edge, 0)
  22. //添加所有节点信息
  23. hubs, _ := repoHub.GetAllHubs()
  24. storages, _ := repoStorage.GetAllStorages()
  25. for _, hub := range hubs {
  26. node := models.Node{
  27. ID: "hub" + strconv.FormatInt(int64(hub.HubID), 10),
  28. NodeType: "hub",
  29. Name: hub.Name,
  30. Address: hub.Address,
  31. }
  32. nodes = append(nodes, node)
  33. }
  34. for _, storage := range storages {
  35. node := models.Node{
  36. ID: "storage" + strconv.FormatInt(int64(storage.StorageID), 10),
  37. NodeType: "storage",
  38. Name: storage.StorageName,
  39. DataCount: storage.DataCount,
  40. NewDataCount: storage.NewDataCount,
  41. Timestamp: storage.Timestamp,
  42. }
  43. nodes = append(nodes, node)
  44. }
  45. // 添加所有边信息
  46. hubReqs, _ := repoHubReq.GetAllHubRequests()
  47. for _, hubReq := range hubReqs {
  48. edge := models.Edge{
  49. SourceType: hubReq.SourceType,
  50. SourceID: hubReq.SourceType + strconv.FormatInt(int64(hubReq.SourceID), 10),
  51. TargetType: hubReq.TargetType,
  52. TargetID: hubReq.TargetType + strconv.FormatInt(int64(hubReq.TargetID), 10),
  53. DataTransferCount: hubReq.DataTransferCount,
  54. RequestCount: hubReq.RequestCount,
  55. FailedRequestCount: hubReq.FailedRequestCount,
  56. AvgTransferCount: hubReq.AvgTransferCount,
  57. MaxTransferCount: hubReq.MaxTransferCount,
  58. MinTransferCount: hubReq.MinTransferCount,
  59. StartTimestamp: hubReq.StartTimestamp,
  60. EndTimestamp: hubReq.EndTimestamp,
  61. }
  62. edges = append(edges, edge)
  63. }
  64. hubRelationship := models.HubRelationship{
  65. Nodes: nodes,
  66. Edges: edges,
  67. }
  68. c.JSON(http.StatusOK, hubRelationship)
  69. }
  70. func containsCombo(combos []models.Combo, targetID string, targetComboType string) bool {
  71. for _, combo := range combos {
  72. if combo.ID == targetID && combo.ComboType == targetComboType {
  73. return true
  74. }
  75. }
  76. return false
  77. }
  78. // GetDataTransfer 数据对象的节点间传输量
  79. func GetDataTransfer(c *gin.Context) {
  80. repoObject := models.NewObjectRepository(DB)
  81. repoBlockDistribution := models.NewBlockDistributionRepository(DB)
  82. repoStorageTrans := models.NewStorageTransferCountRepository(DB)
  83. //首先判断object是否存在
  84. objectIDStr := c.Param("objectID")
  85. objectID, _ := strconv.ParseInt(objectIDStr, 10, 64)
  86. object, _ := repoObject.GetObjectByID(objectID)
  87. if object == nil {
  88. c.JSON(http.StatusOK, []interface{}{})
  89. return
  90. }
  91. nodes := make([]models.DistNode, 0)
  92. combos := make([]models.Combo, 0)
  93. edges := make([]models.DistEdge, 0)
  94. //根据ObjectID查询出在所有storage中存储的块或副本
  95. blocks, _ := repoBlockDistribution.GetBlockDistributionByObjectID(objectID)
  96. for _, block := range blocks {
  97. //nodes --------- block
  98. //添加node信息
  99. node := models.DistNode{
  100. //block id
  101. ID: strconv.FormatInt(block.BlockID, 10),
  102. //storage id
  103. ComboID: "storage" + strconv.FormatInt(block.StorageID, 10),
  104. //block index
  105. Label: block.Type + strconv.FormatInt(block.Index, 10),
  106. //block type
  107. NodeType: block.Type,
  108. }
  109. nodes = append(nodes, node)
  110. //combos ------- state or storage
  111. //添加storage combo信息
  112. if !containsCombo(combos, "storage"+strconv.FormatInt(block.StorageID, 10), "storage") {
  113. combo := models.Combo{
  114. ID: "storage" + strconv.FormatInt(block.StorageID, 10),
  115. Label: "存储中心" + strconv.FormatInt(block.StorageID, 10),
  116. ParentId: "state" + strconv.Itoa(block.Status),
  117. ComboType: "storage",
  118. }
  119. combos = append(combos, combo)
  120. }
  121. //添加state combo信息
  122. if !containsCombo(combos, "state"+strconv.Itoa(block.Status), "state") {
  123. var statusStr string
  124. switch block.Status {
  125. case 0:
  126. statusStr = "实时情况"
  127. case 1:
  128. statusStr = block.Timestamp.Format("2006-01-02") + "布局调整后"
  129. case 2:
  130. statusStr = block.Timestamp.Format("2006-01-02") + "布局调整前"
  131. case 3:
  132. statusStr = block.Timestamp.Format("2006-01-02") + "布局调整后"
  133. default:
  134. statusStr = "未知状态"
  135. }
  136. combo := models.Combo{
  137. ID: "state" + strconv.Itoa(block.Status),
  138. Label: statusStr,
  139. ComboType: "state",
  140. }
  141. combos = append(combos, combo)
  142. }
  143. }
  144. //edges data trans between storage and storage
  145. relations, _ := repoStorageTrans.GetStorageTransferCountByObjectID(objectID)
  146. for _, relation := range relations {
  147. edge := models.DistEdge{
  148. Source: "storage" + strconv.FormatInt(relation.SourceStorageID, 10),
  149. Target: "storage" + strconv.FormatInt(relation.TargetStorageID, 10),
  150. }
  151. edges = append(edges, edge)
  152. }
  153. result := models.ObjectDistribution{
  154. Nodes: nodes,
  155. Combos: combos,
  156. Edges: edges,
  157. }
  158. c.JSON(http.StatusOK, result)
  159. }

本项目旨在将云际存储公共基础设施化,使个人及企业可低门槛使用高效的云际存储服务(安装开箱即用云际存储客户端即可,无需关注其他组件的部署),同时支持用户灵活便捷定制云际存储的功能细节。