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 4.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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: int64(hub.HubID),
  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: int64(storage.StorageID),
  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: int64(hubReq.SourceID),
  51. TargetType: hubReq.TargetType,
  52. TargetID: int64(hubReq.TargetID),
  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: strconv.FormatInt(block.StorageID, 10),
  104. //block index
  105. Label: 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, strconv.FormatInt(block.StorageID, 10), "storage") {
  113. combo := models.Combo{
  114. ID: strconv.FormatInt(block.StorageID, 10),
  115. Label: "存储中心" + strconv.FormatInt(block.StorageID, 10),
  116. ParentId: strconv.Itoa(block.Status),
  117. ComboType: "storage",
  118. }
  119. combos = append(combos, combo)
  120. }
  121. //添加state combo信息
  122. if !containsCombo(combos, strconv.Itoa(block.Status), "state") {
  123. combo := models.Combo{
  124. ID: strconv.Itoa(block.Status),
  125. Label: "存储状态" + string(block.Status),
  126. ComboType: "state",
  127. }
  128. combos = append(combos, combo)
  129. }
  130. }
  131. //edges data trans between storage and storage
  132. relations, _ := repoStorageTrans.GetStorageTransferCountByObjectID(objectID)
  133. for _, relation := range relations {
  134. edge := models.DistEdge{
  135. Source: strconv.FormatInt(relation.SourceStorageID, 10),
  136. Target: strconv.FormatInt(relation.TargetStorageID, 10),
  137. }
  138. edges = append(edges, edge)
  139. }
  140. result := models.ObjectDistribution{
  141. Nodes: nodes,
  142. Combos: combos,
  143. Edges: edges,
  144. }
  145. c.JSON(http.StatusOK, result)
  146. }

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