validations.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // Copyright Joyent, Inc.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this
  5. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. package main
  7. import (
  8. "bytes"
  9. "fmt"
  10. "net/url"
  11. "sort"
  12. "strings"
  13. "time"
  14. "github.com/gofrs/uuid"
  15. // "github.com/jawher/mow.cli"
  16. "github.com/olekukonko/tablewriter"
  17. )
  18. type Validations struct {
  19. *Conch
  20. }
  21. func (c *Conch) Validations() *Validations {
  22. return &Validations{c}
  23. }
  24. /***/
  25. type ValidationPlan struct {
  26. ID uuid.UUID `json:"id"`
  27. Name string `json:"name"`
  28. Description string `json:"description"`
  29. Created time.Time `json:"created"`
  30. }
  31. // type ValidationPlans []ValidationPlan
  32. func (v Validations) GetPlan(id uuid.UUID) (vp ValidationPlan) {
  33. if (id == uuid.UUID{}) {
  34. return vp
  35. }
  36. uri := fmt.Sprintf("/validation_plan/%s", url.PathEscape(id.String()))
  37. res := v.Do(v.Sling().New().Get(uri))
  38. if ok := res.Parse(&vp); !ok {
  39. panic(res)
  40. }
  41. return vp
  42. }
  43. /***/
  44. type ValidationResult struct {
  45. ID uuid.UUID `json:"id,omitempty"`
  46. Category string `json:"category"`
  47. Component string `json:"component,omitempty"`
  48. HardwareProductID uuid.UUID `json:"hardware_product_id"`
  49. Hint string `json:"hint,omitempty"`
  50. Message string `json:"message"`
  51. Order int `json:"order"`
  52. Status string `json:"status"`
  53. ValidationID uuid.UUID `json:"validation_id"`
  54. }
  55. type ValidationResults []ValidationResult
  56. func (v ValidationResults) Len() int {
  57. return len(v)
  58. }
  59. func (v ValidationResults) Swap(i, j int) {
  60. v[i], v[j] = v[j], v[i]
  61. }
  62. func (v ValidationResults) Less(i, j int) bool {
  63. return v[i].Category < v[j].Category
  64. }
  65. func (v ValidationResults) String() string {
  66. sort.Sort(v)
  67. if API.JsonOnly {
  68. return API.AsJSON(v)
  69. }
  70. if len(v) == 0 {
  71. return ""
  72. }
  73. tableString := &strings.Builder{}
  74. table := tablewriter.NewWriter(tableString)
  75. TableToMarkdown(table)
  76. table.SetRowLine(true)
  77. table.SetHeader([]string{
  78. "Status",
  79. "Category",
  80. "Component",
  81. "Message",
  82. })
  83. for _, r := range v {
  84. table.Append([]string{
  85. r.Status,
  86. r.Category,
  87. r.Component,
  88. r.Message,
  89. })
  90. }
  91. table.Render()
  92. return tableString.String()
  93. }
  94. /***/
  95. type ValidationStateWithResults struct {
  96. ID uuid.UUID `json:"id"`
  97. Completed time.Time `json:"completed"`
  98. Created time.Time `json:"created"`
  99. DeviceID uuid.UUID `json:"device_id"`
  100. Status string `json:"status"`
  101. ValidationPlanID uuid.UUID `json:"validation_plan_id"`
  102. DeviceReportID uuid.UUID `json:"device_report_id"`
  103. Results ValidationResults `json:"results"`
  104. }
  105. type ValidationStatesWithResults []ValidationStateWithResults
  106. func (v ValidationStatesWithResults) Len() int {
  107. return len(v)
  108. }
  109. func (v ValidationStatesWithResults) Swap(i, j int) {
  110. v[i], v[j] = v[j], v[i]
  111. }
  112. func (v ValidationStatesWithResults) Less(i, j int) bool {
  113. return v[i].Created.Before(v[j].Created)
  114. }
  115. func (vs ValidationStatesWithResults) String() string {
  116. sort.Sort(vs)
  117. if API.JsonOnly {
  118. return API.AsJSON(vs)
  119. }
  120. type extendedVsR struct {
  121. ValidationStateWithResults
  122. ValidationPlan ValidationPlan `json:"-"`
  123. }
  124. out := make([]extendedVsR, 0)
  125. for _, v := range vs {
  126. out = append(out, extendedVsR{
  127. v,
  128. API.Validations().GetPlan(v.ValidationPlanID),
  129. })
  130. }
  131. t, err := NewTemplate().Parse(validationStatesWithResultsTemplate)
  132. if err != nil {
  133. panic(err)
  134. }
  135. buf := new(bytes.Buffer)
  136. if err := t.Execute(buf, out); err != nil {
  137. panic(err)
  138. }
  139. return buf.String()
  140. }