table_row.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. package client
  2. import (
  3. "errors"
  4. "git.sxidc.com/service-supports/dps-sdk/pb/v1/request"
  5. "git.sxidc.com/service-supports/dps-sdk/pb/v1/response"
  6. "reflect"
  7. "strings"
  8. "time"
  9. )
  10. type TableRow struct {
  11. row map[string]any
  12. }
  13. func NewTableRow() *TableRow {
  14. return &TableRow{row: make(map[string]any)}
  15. }
  16. func (tableRow *TableRow) AddColumnValueTime(columnName string, value time.Time) *TableRow {
  17. tableRow.row[columnName] = value
  18. return tableRow
  19. }
  20. func (tableRow *TableRow) AddColumnValueBool(columnName string, value bool) *TableRow {
  21. tableRow.row[columnName] = value
  22. return tableRow
  23. }
  24. func (tableRow *TableRow) AddColumnValueString(columnName string, value string) *TableRow {
  25. tableRow.row[columnName] = value
  26. return tableRow
  27. }
  28. func (tableRow *TableRow) AddColumnValueInt(columnName string, value int) *TableRow {
  29. tableRow.row[columnName] = uint64(value)
  30. return tableRow
  31. }
  32. func (tableRow *TableRow) AddColumnValueInt8(columnName string, value int8) *TableRow {
  33. tableRow.row[columnName] = uint64(value)
  34. return tableRow
  35. }
  36. func (tableRow *TableRow) AddColumnValueInt16(columnName string, value int16) *TableRow {
  37. tableRow.row[columnName] = uint64(value)
  38. return tableRow
  39. }
  40. func (tableRow *TableRow) AddColumnValueInt32(columnName string, value int32) *TableRow {
  41. tableRow.row[columnName] = uint64(value)
  42. return tableRow
  43. }
  44. func (tableRow *TableRow) AddColumnValueInt64(columnName string, value int64) *TableRow {
  45. tableRow.row[columnName] = uint64(value)
  46. return tableRow
  47. }
  48. func (tableRow *TableRow) AddColumnValueUint(columnName string, value uint) *TableRow {
  49. tableRow.row[columnName] = uint64(value)
  50. return tableRow
  51. }
  52. func (tableRow *TableRow) AddColumnValueUint8(columnName string, value uint8) *TableRow {
  53. tableRow.row[columnName] = uint64(value)
  54. return tableRow
  55. }
  56. func (tableRow *TableRow) AddColumnValueUint16(columnName string, value uint16) *TableRow {
  57. tableRow.row[columnName] = uint64(value)
  58. return tableRow
  59. }
  60. func (tableRow *TableRow) AddColumnValueUint32(columnName string, value uint32) *TableRow {
  61. tableRow.row[columnName] = uint64(value)
  62. return tableRow
  63. }
  64. func (tableRow *TableRow) AddColumnValueUint64(columnName string, value uint64) *TableRow {
  65. tableRow.row[columnName] = value
  66. return tableRow
  67. }
  68. func (tableRow *TableRow) AddColumnValueFloat32(columnName string, value float32) *TableRow {
  69. tableRow.row[columnName] = float64(value)
  70. return tableRow
  71. }
  72. func (tableRow *TableRow) AddColumnValueFloat64(columnName string, value float64) *TableRow {
  73. tableRow.row[columnName] = value
  74. return tableRow
  75. }
  76. func (tableRow *TableRow) ToMap() map[string]any {
  77. return tableRow.row
  78. }
  79. func (tableRow *TableRow) SnapshotColumnValue(prefix string, version string, columnName string) any {
  80. value, ok := tableRow.row[prefix+"_snaps_"+version+"."+columnName]
  81. if !ok {
  82. return nil
  83. }
  84. return value
  85. }
  86. func (tableRow *TableRow) ColumnValueTime(columnName string) time.Time {
  87. value, ok := tableRow.row[columnName].(time.Time)
  88. if !ok {
  89. return time.Time{}
  90. }
  91. return value
  92. }
  93. func (tableRow *TableRow) ColumnValueBool(columnName string) bool {
  94. value, ok := tableRow.row[columnName].(bool)
  95. if !ok {
  96. return false
  97. }
  98. return value
  99. }
  100. func (tableRow *TableRow) ColumnValueString(columnName string) string {
  101. value, ok := tableRow.row[columnName].(string)
  102. if !ok {
  103. return ""
  104. }
  105. return value
  106. }
  107. func (tableRow *TableRow) ColumnValueInt(columnName string) int {
  108. value, ok := tableRow.row[columnName].(uint64)
  109. if !ok {
  110. return 0
  111. }
  112. return int(value)
  113. }
  114. func (tableRow *TableRow) ColumnValueInt8(columnName string) int8 {
  115. value, ok := tableRow.row[columnName].(uint64)
  116. if !ok {
  117. return 0
  118. }
  119. return int8(value)
  120. }
  121. func (tableRow *TableRow) ColumnValueInt16(columnName string) int16 {
  122. value, ok := tableRow.row[columnName].(uint64)
  123. if !ok {
  124. return 0
  125. }
  126. return int16(value)
  127. }
  128. func (tableRow *TableRow) ColumnValueInt32(columnName string) int32 {
  129. value, ok := tableRow.row[columnName].(uint64)
  130. if !ok {
  131. return 0
  132. }
  133. return int32(value)
  134. }
  135. func (tableRow *TableRow) ColumnValueInt64(columnName string) int64 {
  136. value, ok := tableRow.row[columnName].(uint64)
  137. if !ok {
  138. return 0
  139. }
  140. return int64(value)
  141. }
  142. func (tableRow *TableRow) ColumnValueUint(columnName string) uint {
  143. value, ok := tableRow.row[columnName].(uint64)
  144. if !ok {
  145. return 0
  146. }
  147. return uint(value)
  148. }
  149. func (tableRow *TableRow) ColumnValueUint8(columnName string) uint8 {
  150. value, ok := tableRow.row[columnName].(uint64)
  151. if !ok {
  152. return 0
  153. }
  154. return uint8(value)
  155. }
  156. func (tableRow *TableRow) ColumnValueUint16(columnName string) uint16 {
  157. value, ok := tableRow.row[columnName].(uint64)
  158. if !ok {
  159. return 0
  160. }
  161. return uint16(value)
  162. }
  163. func (tableRow *TableRow) ColumnValueUint32(columnName string) uint32 {
  164. value, ok := tableRow.row[columnName].(uint64)
  165. if !ok {
  166. return 0
  167. }
  168. return uint32(value)
  169. }
  170. func (tableRow *TableRow) ColumnValueUint64(columnName string) uint64 {
  171. value, ok := tableRow.row[columnName].(uint64)
  172. if !ok {
  173. return 0
  174. }
  175. return value
  176. }
  177. func (tableRow *TableRow) ColumnValueFloat32(columnName string) float32 {
  178. value, ok := tableRow.row[columnName].(float64)
  179. if !ok {
  180. return 0
  181. }
  182. return float32(value)
  183. }
  184. func (tableRow *TableRow) ColumnValueFloat64(columnName string) float64 {
  185. value, ok := tableRow.row[columnName].(float64)
  186. if !ok {
  187. return 0
  188. }
  189. return value
  190. }
  191. func (tableRow *TableRow) ToDPSTableRow() (*request.TableRow, error) {
  192. if tableRow.row == nil || len(tableRow.row) == 0 {
  193. return &request.TableRow{Columns: make([]*request.Column, 0)}, nil
  194. }
  195. columns := make([]*request.Column, 0)
  196. for columnName, value := range tableRow.row {
  197. valueType := reflect.TypeOf(value)
  198. typedValue := new(request.ColumnValue)
  199. typedValue.Type = valueType.Name()
  200. switch valueType.Name() {
  201. case "Time":
  202. timeObj := value.(time.Time)
  203. typedValue.TypedValue = &request.ColumnValue_Uint64Value{Uint64Value: uint64(timeObj.UnixMicro())}
  204. case "string":
  205. typedValue.TypedValue = &request.ColumnValue_StringValue{StringValue: value.(string)}
  206. case "bool":
  207. typedValue.TypedValue = &request.ColumnValue_BoolValue{BoolValue: value.(bool)}
  208. case "uint64":
  209. typedValue.TypedValue = &request.ColumnValue_Uint64Value{Uint64Value: value.(uint64)}
  210. case "float64":
  211. typedValue.TypedValue = &request.ColumnValue_Float64Value{Float64Value: value.(float64)}
  212. default:
  213. return nil, errors.New("不支持的数据类型" + valueType.Name())
  214. }
  215. columns = append(columns, &request.Column{
  216. Name: columnName,
  217. Value: typedValue,
  218. })
  219. }
  220. return &request.TableRow{Columns: columns}, nil
  221. }
  222. func (tableRow *TableRow) FromDSPInfoData(infoData *response.InfoData) error {
  223. for _, column := range infoData.Columns {
  224. typeStr := column.Value.Type
  225. if typeStr == "Time" {
  226. timeObj := time.UnixMicro(int64(column.Value.GetUint64Value()))
  227. tableRow.row[column.Name] = timeObj
  228. } else if typeStr == "string" {
  229. tableRow.row[column.Name] = column.Value.GetStringValue()
  230. } else if typeStr == "bool" {
  231. tableRow.row[column.Name] = column.Value.GetBoolValue()
  232. } else if strings.Contains(typeStr, "int") {
  233. tableRow.row[column.Name] = column.Value.GetUint64Value()
  234. } else if strings.Contains(typeStr, "float") {
  235. tableRow.row[column.Name] = column.Value.GetFloat64Value()
  236. } else {
  237. return errors.New("不支持的数据类型" + column.Value.Type)
  238. }
  239. }
  240. return nil
  241. }
  242. func FromDSPInfosData(infosData []*response.InfoData) ([]TableRow, error) {
  243. tableRows := make([]TableRow, 0)
  244. for _, infoData := range infosData {
  245. tableRow := NewTableRow()
  246. err := tableRow.FromDSPInfoData(infoData)
  247. if err != nil {
  248. return nil, err
  249. }
  250. tableRows = append(tableRows, *tableRow)
  251. }
  252. return tableRows, nil
  253. }