如何在Golang中正确循环遍历两个嵌套的JSON结构。

huangapple go评论103阅读模式
英文:

How do I properly loop through two nested json structures in golang

问题

我是你的中文翻译助手,以下是你要翻译的内容:

我刚开始学习Go语言。我有两个相同的嵌套的JSON结构,每个结构都填充了两个API调用的输出。一个调用获取CPU指标,另一个调用获取内存指标。我可以分别解组每个结构,并打印出项目名称以及CPU和内存的值,尽管这需要使用两个不同的代码块。我遇到的问题是,我希望能够在同一行上打印出CPU和内存指标,紧挨着它们的项目名称。

这是我用于打印按项目分类的CPU指标的代码。它创建了一个漂亮的CSV格式输出:

  1. // CPU指标
  2. // 循环遍历月份、项目、CPU请求和CPU使用数据
  3. fmt.Println("月份, 项目, CPU请求(核小时), CPU使用(核小时)\n")
  4. for _, value_cpu := range rh_values_cpu.Data {
  5. for _, val_cpu := range value_cpu.Projects {
  6. str := val_cpu.Project
  7. s := strings.Contains(str, "openshift")
  8. if s == true {
  9. continue
  10. }
  11. fmt.Printf("%s, %s, ", value_cpu.Date, val_cpu.Project)
  12. for _, v_cpu := range val_cpu.Values {
  13. fmt.Printf("%.1f, %.1f\n", v_cpu.Request.Value, v_cpu.Usage.Value)
  14. }
  15. }
  16. }

我对内存指标也有类似的代码,也能正常工作。

这是我用于循环遍历这两个JSON结构的代码。我怀疑我没有正确使用嵌套循环,或者需要以不同的方式解决这个问题。

  1. // CPU和内存指标
  2. // 循环遍历月份、项目、CPU请求、CPU使用、内存请求和内存使用数据
  3. fmt.Println("月份, 项目, CPU请求(核小时), CPU使用(核小时), 内存请求(兆字节), 内存使用(兆字节)\n")
  4. for _, value_cpu := range rh_values_cpu.Data {
  5. for _, value_mem := range rh_values_MEM.Data {
  6. for _, val_cpu := range value_cpu.Projects {
  7. for _, val_mem := range value_mem.Projects {
  8. str := val_cpu.Project
  9. s := strings.Contains(str, "openshift")
  10. if s == true {
  11. continue
  12. }
  13. fmt.Printf("%s, %s, ", value_cpu.Date, val_cpu.Project)
  14. for _, v_cpu := range val_cpu.Values {
  15. fmt.Printf("%.1f, %.1f ", v_cpu.Request.Value, v_cpu.Usage.Value)
  16. for _,v_mem := range val_mem.Values {
  17. fmt.Printf("%.1f, %.1f\n", v_mem.Request.Value, v_mem.Usage.Value)
  18. }
  19. }
  20. }
  21. }
  22. }
  23. }

这是其中一个JSON结构的片段:

  1. type RH_Output_MEM struct {
  2. Meta struct {
  3. Count int `json:"count"`
  4. Others int `json:"others"`
  5. Currency string `json:"currency"`
  6. Filter struct {
  7. Resolution string `json:"resolution"`
  8. TimeScopeValue string `json:"time_scope_value"`
  9. TimeScopeUnits string `json:"time_scope_units"`
  10. Limit int `json:"limit"`
  11. Offset int `json:"offset"`
  12. } `json:"filter"`
  13. GroupBy struct {
  14. Project []string `json:"project"`
  15. } `json:"group_by"`
  16. OrderBy struct {
  17. } `json:"order_by"`
  18. Exclude struct {
  19. } `json:"exclude"`
  20. Total struct {
  21. Usage struct {
  22. Value float64 `json:"value"`
  23. Units string `json:"units"`
  24. } `json:"usage"`
  25. Request struct {
  26. Value float64 `json:"value"`
  27. Units string `json:"units"`
  28. } `json:"request"`
  29. Limit struct {
  30. Value float64 `json:"value"`
  31. Units string `json:"units"`
  32. } `json:"limit"`
  33. Capacity struct {
  34. Value float64 `json:"value"`
  35. Units string `json:"units"`
  36. } `json:"capacity"`
  37. Infrastructure struct {
  38. Raw struct {
  39. Value float64 `json:"value"`
  40. Units string `json:"units"`
  41. } `json:"raw"`
  42. Markup struct {
  43. Value float64 `json:"value"`
  44. Units string `json:"units"`
  45. } `json:"markup"`
  46. Usage struct {
  47. Value float64 `json:"value"`
  48. Units string `json:"units"`
  49. } `json:"usage"`
  50. Distributed struct {
  51. Value float64 `json:"value"`
  52. Units string `json:"units"`
  53. } `json:"distributed"`
  54. Total struct {
  55. Value float64 `json:"value"`
  56. Units string `json:"units"`
  57. } `json:"total"`
  58. } `json:"infrastructure"`
  59. Supplementary struct {
  60. Raw struct {
  61. Value float64 `json:"value"`
  62. Units string `json:"units"`
  63. } `json:"raw"`
  64. Markup struct {
  65. Value float64 `json:"value"`
  66. Units string `json:"units"`
  67. } `json:"markup"`
  68. Usage struct {
  69. Value float64 `json:"value"`
  70. Units string `json:"units"`
  71. } `json:"usage"`
  72. Distributed struct {
  73. Value float64 `json:"value"`
  74. Units string `json:"units"`
  75. } `json:"distributed"`
  76. Total struct {
  77. Value float64 `json:"value"`
  78. Units string `json:"units"`
  79. } `json:"total"`
  80. } `json:"supplementary"`
  81. Cost struct {
  82. Raw struct {
  83. Value float64 `json:"value"`
  84. Units string `json:"units"`
  85. } `json:"raw"`
  86. Markup struct {
  87. Value float64 `json:"value"`
  88. Units string `json:"units"`
  89. } `json:"markup"`
  90. Usage struct {
  91. Value float64 `json:"value"`
  92. Units string `json:"units"`
  93. } `json:"usage"`
  94. Distributed struct {
  95. Value float64 `json:"value"`
  96. Units string `json:"units"`
  97. } `json:"distributed"`
  98. Total struct {
  99. Value float64 `json:"value"`
  100. Units string `json:"units"`
  101. } `json:"total"`
  102. } `json:"cost"`
  103. } `json:"total"`
  104. } `json:"meta"`
  105. Links struct {
  106. First string `json:"first"`
  107. Next string `json:"next"`
  108. Previous interface{} `json:"previous"`
  109. Last string `json:"last"`
  110. } `json:"links"`
  111. Data []struct {
  112. Date string `json:"date"`
  113. Projects []struct {
  114. Project string `json:"project"`
  115. Values []struct {
  116. Date string `json:"date"`
  117. Project string `json:"project"`
  118. Usage struct {
  119. Value float64 `json:"value"`
  120. Units string `json:"units"`
  121. } `json:"usage"`
  122. Request struct {
  123. Value float64 `json:"value"`
  124. Units string `json:"units"`
  125. } `json:"request"`
  126. Limit struct {
  127. Value float64 `json:"value"`
  128. Units string `json:"units"`
  129. } `json:"limit"`
  130. Capacity struct {
  131. Value float64 `json:"value"`
  132. Units string `json:"units"`
  133. } `json:"capacity"`
  134. Classification string `json:"classification"`
  135. SourceUUID []string `json:"source_uuid"`
  136. Clusters []string `json:"clusters"`
  137. Infrastructure struct {
  138. Raw struct {
  139. Value float64 `json:"value"`
  140. Units string `json:"units"`
  141. } `json:"raw"`
  142. Markup struct {
  143. Value float64 `json:"value"`
  144. Units string `json:"units"`
  145. } `json:"markup"`
  146. Usage struct {
  147. Value float64 `json:"value"`
  148. Units string `json:"units"`
  149. } `json:"usage"`
  150. Distributed struct {
  151. Value float64 `json:"value"`
  152. Units string `json:"units"`
  153. } `json:"distributed"`
  154. Total struct {
  155. Value float64 `json:"value"`
  156. Units string `json:"units"`
  157. } `json:"total"`
  158. } `json:"infrastructure"`
  159. Supplementary struct {
  160. Raw struct {
  161. Value float64 `json:"value"`
  162. Units string `json:"units"`
  163. } `json:"raw"`
  164. Markup struct {
  165. Value float64 `json:"value"`
  166. Units string `json:"units"`
  167. } `json:"markup"`
  168. Usage struct {
  169. Value float64 `json:"value"`
  170. Units string `json:"units"`
  171. } `json:"usage"`
  172. Distributed struct {
  173. Value float64 `json:"value"`
  174. Units string `json:"units"`
  175. } `json:"distributed"`
  176. Total struct {
  177. Value float64 `json:"value"`
  178. Units string `json:"units"`
  179. } `json:"total"`
  180. } `json:"supplementary"`
  181. Cost struct {
  182. Raw struct {
  183. Value float64 `json:"value"`
  184. Units string `json:"units"`
  185. } `json:"raw"`
  186. Markup struct {
  187. Value float64 `json:"value"`
  188. Units string `json:"units"`
  189. } `json:"markup"`
  190. Usage struct {
  191. Value float64 `json:"value"`
  192. Units string `json:"units"`
  193. } `json:"usage"`
  194. Distributed struct {
  195. Value float64 `json:"value"`
  196. Units string `json:"units"`
  197. } `json:"distributed"`
  198. Total struct {
  199. Value float64 `json:"value"`
  200. Units string `json:"units"`
  201. } `json:"total"`
  202. } `json:"cost"`
  203. } `json:"values"`
  204. } `json:"projects"`
  205. } `json:"data"`
  206. }

这是我运行程序时得到的输出片段。你可以看到,日期、项目和内部循环(CPU指标)重复出现,而外部循环(内存指标)运行:

我希望每个项目有一行输出(月份、项目、CPU指标、内存指标)。

  1. 月份, 项目, CPU请求(核小时), CPU使用(核小时), 内存请求(兆字节), 内存使用(兆字节)
  2. 2022-12, amq-demo-streams, 0.0, 34.0, 0.0, 4353.2
  3. 2022-12, amq-demo-streams, 0.0, 34.0, 1115.6, 1081.4
  4. 2022-12, amq-demo-streams, 0.0, 34.0, 0.0, 10675.9
  5. 2022-12, amq-demo-streams, 0.0, 34.0, 100.9, 284.0
  6. 2022-12, amq-demo-streams, 0.0, 34.0, 0.0, 70064.5
  7. 2022-12, amq-demo-streams, 0.0, 34.0, 773088.9, 427757.8
  8. 2022-12, amq-demo-streams, 0.0, 34.0, 9440.0, 11610.3
  9. 2022-12, amq-demo-streams, 0.0, 34.0, 9471.3, 11696.9
  10. 2022-12, amq-demo-streams, 0.0, 34.0, 0.0, 2455.2
  11. 2022-12, amq-demo-streams, 0.0, 34.0, 0.0, 3.3
  12. 2022-12, amq-demo-streams, 0.0, 34.0, 0.0, 0.0
  13. 2022-12, amq-demo-streams, 0.0, 34.0, -0.3, 0.0
  14. 2022-12, amq-demo-streams, 0.0, 34.0, 3785.0, 6610.4
  15. 2022-12, amq-demo-streams, 0.0, 34.0, 252.3, 1007.8
  16. 2022-12, amq-demo-streams, 0.0, 34.0, 757.0, 883.0
  17. 2022-12, amq-demo-streams, 0.0, 34.0, 1009.4, 1613.4
  18. 2022-12, amq-demo-streams, 0.0, 34.0, 378.5, 413.5
  19. 2022-12, amq-demo-streams, 0.0, 34.0, 908.4, 2856.8
  20. 2022-12, amq-demo-streams, 0.0, 34.0, 252.3, 248.7
  21. 2022-12, amq-demo-streams, 0.0, 34.0, 66873.8, 21035.3
  22. 2022-12, amq-demo-streams, 0.0, 34.0, 353.3, 611.9
  23. 2022-12, amq-demo-streams, 0.0, 34.0, 10203.6, 12418.3
  24. 2022-12, amq-demo-streams, 0.0, 34.0, 504.7, 398.3
  25. 2022-12, amq-demo-streams, 0.0, 34.0, 1135.5, 2248.5
  26. 2022-12, amq-demo-streams, 0.0, 34.0, 252.3, 610.6
  27. 2022-12, amq-demo-streams, 0.0, 34.0, 252.3, 370.6
英文:

I am new to Go. I have 2 identical json nested structures that are each populated with the output of 2 api calls. One call fetches cpu and the other memory metrics. I can unmarshal each of them individually and print out the project name and values of cpu and memory, albeit in 2 separate code blocks. The problem I am having is that I would like to print out both cpu and memory metrics on the same line, next to their project name.

Here is the code I am using to print out the CPU metrics by Project. It creates a nice CSV formatted output:

  1. // CPU Metrics
  2. // Loop through the data for the Month, Project, CPU requests, and CPU Usage
  3. fmt.Println("Month, Project, CPU Request(Core hours), CPU Usage(Core hours)\n")
  4. for _, value_cpu := range rh_values_cpu.Data {
  5. for _, val_cpu := range value_cpu.Projects {
  6. str := val_cpu.Project
  7. s := strings.Contains(str, "openshift")
  8. if s == true {
  9. continue
  10. }
  11. fmt.Printf("%s, %s, ", value_cpu.Date, val_cpu.Project)
  12. for _, v_cpu := range val_cpu.Values {
  13. fmt.Printf("%.1f, %.1f\n", v_cpu.Request.Value, v_cpu.Usage.Value)
  14. }
  15. }
  16. }

I have similar code for the memory metrics which also works fine.

Here is the code I am using to loop through the two json structures. I suspect that I'm not using the nested loops properly or need to solve the problem differently.

  1. // CPU & Memory Metrics
  2. // Loop through the data for the Month, Project, CPU requests, CPU Usage, Memory requests, and Memory Usage
  3. fmt.Println("Month, Project, CPU Request(Core hours), CPU Usage(Core hours) Memory Request(mBytes), Memory Usage(mBytes)\n")
  4. for _, value_cpu := range rh_values_cpu.Data {
  5. for _, value_mem := range rh_values_MEM.Data {
  6. for _, val_cpu := range value_cpu.Projects {
  7. for _, val_mem := range value_mem.Projects {
  8. str := val_cpu.Project
  9. s := strings.Contains(str, "openshift")
  10. if s == true {
  11. continue
  12. }
  13. fmt.Printf("%s, %s, ", value_cpu.Date, val_cpu.Project)
  14. for _, v_cpu := range val_cpu.Values {
  15. fmt.Printf("%.1f, %.1f ", v_cpu.Request.Value, v_cpu.Usage.Value)
  16. for _,v_mem := range val_mem.Values {
  17. fmt.Printf("%.1f, %.1f\n", v_mem.Request.Value, v_mem.Usage.Value)
  18. }
  19. }
  20. }
  21. }
  22. }
  23. }

And here is of one the json structures:

  1. type RH_Output_MEM struct {
  2. Meta struct {
  3. Count int `json:"count"`
  4. Others int `json:"others"`
  5. Currency string `json:"currency"`
  6. Filter struct {
  7. Resolution string `json:"resolution"`
  8. TimeScopeValue string `json:"time_scope_value"`
  9. TimeScopeUnits string `json:"time_scope_units"`
  10. Limit int `json:"limit"`
  11. Offset int `json:"offset"`
  12. } `json:"filter"`
  13. GroupBy struct {
  14. Project []string `json:"project"`
  15. } `json:"group_by"`
  16. OrderBy struct {
  17. } `json:"order_by"`
  18. Exclude struct {
  19. } `json:"exclude"`
  20. Total struct {
  21. Usage struct {
  22. Value float64 `json:"value"`
  23. Units string `json:"units"`
  24. } `json:"usage"`
  25. Request struct {
  26. Value float64 `json:"value"`
  27. Units string `json:"units"`
  28. } `json:"request"`
  29. Limit struct {
  30. Value float64 `json:"value"`
  31. Units string `json:"units"`
  32. } `json:"limit"`
  33. Capacity struct {
  34. Value float64 `json:"value"`
  35. Units string `json:"units"`
  36. } `json:"capacity"`
  37. Infrastructure struct {
  38. Raw struct {
  39. Value float64 `json:"value"`
  40. Units string `json:"units"`
  41. } `json:"raw"`
  42. Markup struct {
  43. Value float64 `json:"value"`
  44. Units string `json:"units"`
  45. } `json:"markup"`
  46. Usage struct {
  47. Value float64 `json:"value"`
  48. Units string `json:"units"`
  49. } `json:"usage"`
  50. Distributed struct {
  51. Value float64 `json:"value"`
  52. Units string `json:"units"`
  53. } `json:"distributed"`
  54. Total struct {
  55. Value float64 `json:"value"`
  56. Units string `json:"units"`
  57. } `json:"total"`
  58. } `json:"infrastructure"`
  59. Supplementary struct {
  60. Raw struct {
  61. Value float64 `json:"value"`
  62. Units string `json:"units"`
  63. } `json:"raw"`
  64. Markup struct {
  65. Value float64 `json:"value"`
  66. Units string `json:"units"`
  67. } `json:"markup"`
  68. Usage struct {
  69. Value float64 `json:"value"`
  70. Units string `json:"units"`
  71. } `json:"usage"`
  72. Distributed struct {
  73. Value float64 `json:"value"`
  74. Units string `json:"units"`
  75. } `json:"distributed"`
  76. Total struct {
  77. Value float64 `json:"value"`
  78. Units string `json:"units"`
  79. } `json:"total"`
  80. } `json:"supplementary"`
  81. Cost struct {
  82. Raw struct {
  83. Value float64 `json:"value"`
  84. Units string `json:"units"`
  85. } `json:"raw"`
  86. Markup struct {
  87. Value float64 `json:"value"`
  88. Units string `json:"units"`
  89. } `json:"markup"`
  90. Usage struct {
  91. Value float64 `json:"value"`
  92. Units string `json:"units"`
  93. } `json:"usage"`
  94. Distributed struct {
  95. Value float64 `json:"value"`
  96. Units string `json:"units"`
  97. } `json:"distributed"`
  98. Total struct {
  99. Value float64 `json:"value"`
  100. Units string `json:"units"`
  101. } `json:"total"`
  102. } `json:"cost"`
  103. } `json:"total"`
  104. } `json:"meta"`
  105. Links struct {
  106. First string `json:"first"`
  107. Next string `json:"next"`
  108. Previous interface{} `json:"previous"`
  109. Last string `json:"last"`
  110. } `json:"links"`
  111. Data []struct {
  112. Date string `json:"date"`
  113. Projects []struct {
  114. Project string `json:"project"`
  115. Values []struct {
  116. Date string `json:"date"`
  117. Project string `json:"project"`
  118. Usage struct {
  119. Value float64 `json:"value"`
  120. Units string `json:"units"`
  121. } `json:"usage"`
  122. Request struct {
  123. Value float64 `json:"value"`
  124. Units string `json:"units"`
  125. } `json:"request"`
  126. Limit struct {
  127. Value float64 `json:"value"`
  128. Units string `json:"units"`
  129. } `json:"limit"`
  130. Capacity struct {
  131. Value float64 `json:"value"`
  132. Units string `json:"units"`
  133. } `json:"capacity"`
  134. Classification string `json:"classification"`
  135. SourceUUID []string `json:"source_uuid"`
  136. Clusters []string `json:"clusters"`
  137. Infrastructure struct {
  138. Raw struct {
  139. Value float64 `json:"value"`
  140. Units string `json:"units"`
  141. } `json:"raw"`
  142. Markup struct {
  143. Value float64 `json:"value"`
  144. Units string `json:"units"`
  145. } `json:"markup"`
  146. Usage struct {
  147. Value float64 `json:"value"`
  148. Units string `json:"units"`
  149. } `json:"usage"`
  150. Distributed struct {
  151. Value float64 `json:"value"`
  152. Units string `json:"units"`
  153. } `json:"distributed"`
  154. Total struct {
  155. Value float64 `json:"value"`
  156. Units string `json:"units"`
  157. } `json:"total"`
  158. } `json:"infrastructure"`
  159. Supplementary struct {
  160. Raw struct {
  161. Value float64 `json:"value"`
  162. Units string `json:"units"`
  163. } `json:"raw"`
  164. Markup struct {
  165. Value float64 `json:"value"`
  166. Units string `json:"units"`
  167. } `json:"markup"`
  168. Usage struct {
  169. Value float64 `json:"value"`
  170. Units string `json:"units"`
  171. } `json:"usage"`
  172. Distributed struct {
  173. Value float64 `json:"value"`
  174. Units string `json:"units"`
  175. } `json:"distributed"`
  176. Total struct {
  177. Value float64 `json:"value"`
  178. Units string `json:"units"`
  179. } `json:"total"`
  180. } `json:"supplementary"`
  181. Cost struct {
  182. Raw struct {
  183. Value float64 `json:"value"`
  184. Units string `json:"units"`
  185. } `json:"raw"`
  186. Markup struct {
  187. Value float64 `json:"value"`
  188. Units string `json:"units"`
  189. } `json:"markup"`
  190. Usage struct {
  191. Value float64 `json:"value"`
  192. Units string `json:"units"`
  193. } `json:"usage"`
  194. Distributed struct {
  195. Value float64 `json:"value"`
  196. Units string `json:"units"`
  197. } `json:"distributed"`
  198. Total struct {
  199. Value float64 `json:"value"`
  200. Units string `json:"units"`
  201. } `json:"total"`
  202. } `json:"cost"`
  203. } `json:"values"`
  204. } `json:"projects"`
  205. } `json:"data"`
  206. }

And here is a snippet of the output I am getting when running the program. As you can see, the Date, Project, and inner loop (CPU metrics) repeats itself, while the outer loop (Memory metrics) runs:

I'm looking for an output where I have one line per project (Month, Project, CPU metrics, Memory metrics)

  1. Month, Project, CPU Request(Core hours), CPU Usage(Core hours) Memory Request(mBytes), Memory Usage(mBytes)
  2. 2022-12, amq-demo-streams, 0.0, 34.0, 0.0, 4353.2
  3. 2022-12, amq-demo-streams, 0.0, 34.0, 1115.6, 1081.4
  4. 2022-12, amq-demo-streams, 0.0, 34.0, 0.0, 10675.9
  5. 2022-12, amq-demo-streams, 0.0, 34.0, 100.9, 284.0
  6. 2022-12, amq-demo-streams, 0.0, 34.0, 0.0, 70064.5
  7. 2022-12, amq-demo-streams, 0.0, 34.0, 773088.9, 427757.8
  8. 2022-12, amq-demo-streams, 0.0, 34.0, 9440.0, 11610.3
  9. 2022-12, amq-demo-streams, 0.0, 34.0, 9471.3, 11696.9
  10. 2022-12, amq-demo-streams, 0.0, 34.0, 0.0, 2455.2
  11. 2022-12, amq-demo-streams, 0.0, 34.0, 0.0, 3.3
  12. 2022-12, amq-demo-streams, 0.0, 34.0, 0.0, 0.0
  13. 2022-12, amq-demo-streams, 0.0, 34.0, -0.3, 0.0
  14. 2022-12, amq-demo-streams, 0.0, 34.0, 3785.0, 6610.4
  15. 2022-12, amq-demo-streams, 0.0, 34.0, 252.3, 1007.8
  16. 2022-12, amq-demo-streams, 0.0, 34.0, 757.0, 883.0
  17. 2022-12, amq-demo-streams, 0.0, 34.0, 1009.4, 1613.4
  18. 2022-12, amq-demo-streams, 0.0, 34.0, 378.5, 413.5
  19. 2022-12, amq-demo-streams, 0.0, 34.0, 908.4, 2856.8
  20. 2022-12, amq-demo-streams, 0.0, 34.0, 252.3, 248.7
  21. 2022-12, amq-demo-streams, 0.0, 34.0, 66873.8, 21035.3
  22. 2022-12, amq-demo-streams, 0.0, 34.0, 353.3, 611.9
  23. 2022-12, amq-demo-streams, 0.0, 34.0, 10203.6, 12418.3
  24. 2022-12, amq-demo-streams, 0.0, 34.0, 504.7, 398.3
  25. 2022-12, amq-demo-streams, 0.0, 34.0, 1135.5, 2248.5
  26. 2022-12, amq-demo-streams, 0.0, 34.0, 252.3, 610.6
  27. 2022-12, amq-demo-streams, 0.0, 34.0, 252.3, 370.6

答案1

得分: 0

我允许自己使用更简单的结构定义。您可以根据自己的结构随时调整此代码。

  1. type Cpu struct {
  2. Project string
  3. Data []Data
  4. }
  5. type Memory struct {
  6. Project string
  7. Data []Data
  8. }
  9. type Data struct {
  10. Date string
  11. Projects []Project
  12. }
  13. type Project struct {
  14. Project string
  15. Values []struct {
  16. Request float64
  17. Value float64
  18. }
  19. }
  20. func CSVOutput(cpu Cpu, mem Memory) error {
  21. // 如果cpu和memory的数据长度不相等,则返回错误
  22. if len(cpu.Data) != len(mem.Data) {
  23. return fmt.Errorf("cpu.Data和mem.Data的长度不相等")
  24. }
  25. // 打印CSV文件头部
  26. fmt.Println("月份, 项目, CPU请求(核心小时), CPU使用(核心小时), 内存请求(MB), 内存使用(MB)")
  27. for i := range cpu.Data {
  28. cpuData := cpu.Data[i]
  29. memData := mem.Data[i]
  30. // 使用Errorf的格式添加错误上下文
  31. if len(cpuData.Projects) != len(memData.Projects) {
  32. return fmt.Errorf("cpu.Data[%d].Projects和mem.Data[%d].Projects的长度不相等", i, i)
  33. }
  34. for j := range cpuData.Projects {
  35. cpuProject := cpuData.Projects[j]
  36. memProject := memData.Projects[j]
  37. if len(cpuProject.Values) != len(memProject.Values) {
  38. return fmt.Errorf("cpu.Data[%d].Projects[%d].Values和mem.Data[%d].Projects[%d].Values的长度不相等", i, j, i, j)
  39. }
  40. name := cpuProject.Project
  41. date := cpuData.Date
  42. // 如果cpu项目涉及openshift,则跳过
  43. if strings.Contains(name, "openshift") {
  44. continue
  45. }
  46. for k := range cpuProject.Values {
  47. cpuValue := cpuProject.Values[k]
  48. memValue := memProject.Values[k]
  49. fmt.Printf("%s, %s, %.1f, %.1f, %.1f, %.1f", date, name, cpuValue.Request, cpuValue.Value, memValue.Request, memValue.Value)
  50. }
  51. }
  52. }
  53. return nil
  54. }

此代码仅在接收到与内存数据相同数量的CPU数据时才有效。
如果不是这种情况,您将需要找到一种将特定的CPU数据与其内存等效数据关联起来的方法。
如果您认为可能会出现这种情况,我们可以进一步讨论此问题。

英文:

I allowed myself to use simpler struct definition. You can always adapt this code to your structs.

  1. type Cpu struct {
  2. Project string
  3. Data []Data
  4. }
  5. type Memory struct {
  6. Project string
  7. Data []Data
  8. }
  9. type Data struct {
  10. Date string
  11. Projects []Project
  12. }
  13. type Project struct {
  14. Project string
  15. Values []struct {
  16. Request float64
  17. Value float64
  18. }
  19. }
  20. func CSVOutput(cpu Cpu, mem Memory) error {
  21. // Returns an error if cpu & memory's data are the same length
  22. if len(cpu.Data) != len(mem.Data) {
  23. return fmt.Errorf("cpu.Data and mem.Data don't have the same length")
  24. }
  25. // Printing CSV file header
  26. fmt.Println("Month, Project, CPU Request(Core hours), CPU Usage(Core hours) Memory Request(mBytes), Memory Usage(mBytes)")
  27. for i := range cpu.Data {
  28. cpuData := cpu.Data[i]
  29. memData := mem.Data[i]
  30. // Using the format from Errorf to add context to the error
  31. if len(cpuData.Projects) != len(memData.Projects) {
  32. return fmt.Errorf("cpu.Data[%d].Projects and mem.Data[%d].Projects don't have the same length", i, i)
  33. }
  34. for j := range cpuData.Projects {
  35. cpuProject := cpuData.Projects[j]
  36. memProject := memData.Projects[j]
  37. if len(cpuProject.Values) != len(memProject.Values) {
  38. return fmt.Errorf("cpu.Data[%d].Projects[%d].Values and mem.Data[%d].Projects[%d].Values don't have the same length", i, j, i, j)
  39. }
  40. name := cpuProject.Project
  41. date := cpuData.Date
  42. // Continue if the cpu project concerns openshift
  43. if strings.Contains(name, "openshift") {
  44. continue
  45. }
  46. for k := range cpuProject.Values {
  47. cpuValue := cpuProject.Values[k]
  48. memValue := memProject.Values[k]
  49. fmt.Printf("%s, %s, %.1f, %.1f, %.1f, %.1f", date, name, cpuValue.Request, cpuValue.Value, memValue.Request, memValue.Value)
  50. }
  51. }
  52. }
  53. return nil
  54. }

This code only works if you receive as much cpu's data as memory's data.
If it isn't the case, you will have to find a way to link a certain cpu's data to its memory equivalent.
This issue can be furthermore discussed if you think the situation might show up.

huangapple
  • 本文由 发表于 2023年1月12日 00:28:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75086181.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定