print.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. package api
  2. // import (
  3. // "box-cost/db/model"
  4. // "box-cost/db/repo"
  5. // randc "crypto/rand"
  6. // "encoding/json"
  7. // "fmt"
  8. // "log"
  9. // "math/big"
  10. // "math/rand"
  11. // "sort"
  12. // "sync"
  13. // "time"
  14. // "unsafe"
  15. // "github.com/gin-gonic/gin"
  16. // "go.mongodb.org/mongo-driver/bson"
  17. // "go.mongodb.org/mongo-driver/mongo"
  18. // )
  19. // func Printr(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  20. // // https://sku3d-test.obs.cn-east-3.myhuaweicloud.com/image/jpg/1675757991547cnt7X9_2.jpg zhang
  21. // // https://sku3d-test.obs.cn-east-3.myhuaweicloud.com/image/jpg/1675758027305uLKtda_1.jpg zhu
  22. // loc, _ := time.LoadLocation("Local")
  23. // date := time.Now().Format("2006-01-02")
  24. // startDate := date + " 00:00:00"
  25. // startTime, _ := time.ParseInLocation("2006-01-02 15:04:05", startDate, loc)
  26. // endDate := date + " 23:59:59"
  27. // endTime, _ := time.ParseInLocation("2006-01-02 15:04:05", endDate, loc)
  28. // fmt.Println(startTime.Unix())
  29. // fmt.Println(endTime.Unix())
  30. // return "success", nil
  31. // }
  32. // // 生成百万数据
  33. // func GenData(_ *gin.Context, apictx *ApiSession) (interface{}, error) {
  34. // var wg sync.WaitGroup
  35. // for i := 0; i < 100; i++ {
  36. // wg.Add(1)
  37. // go insertData(apictx, &wg)
  38. // }
  39. // wg.Wait()
  40. // return true, nil
  41. // }
  42. // // 聚合查询百万数据
  43. // func SearchData(_ *gin.Context, apictx *ApiSession) (interface{}, error) {
  44. // dataStatResult := make(chan bson.M)
  45. // var output ResultSlice
  46. // var wg sync.WaitGroup
  47. // // 82个gorutine 执行聚合
  48. // for i := 18; i < 100; i++ {
  49. // wg.Add(1)
  50. // go DataAggregate(apictx, i, dataStatResult, &wg)
  51. // }
  52. // // 从管道中获取聚合结果 保存在数组中
  53. // for value := range dataStatResult {
  54. // output = append(output, OutPut{
  55. // Age: value["age"].(int32),
  56. // Sex: value["sex"].(int32),
  57. // Total: value["total"].(int32),
  58. // AvgSalary: value["avgSalary"].(float64),
  59. // })
  60. // if len(output) == 164 { // 如果大于164,不会跳出;如果小于164,wg.done != wg.add ,wg.wait阻塞
  61. // break
  62. // }
  63. // }
  64. // wg.Wait()
  65. // //倒序排列
  66. // sort.Sort(output)
  67. // for _, v := range output {
  68. // result, err := json.Marshal(&v)
  69. // if err != nil {
  70. // fmt.Println("json.marshal failed, err:", err)
  71. // return nil, err
  72. // }
  73. // fmt.Println(string(result))
  74. // }
  75. // return output, nil
  76. // }
  77. // func insertData(apictx *ApiSession, wg *sync.WaitGroup) {
  78. // collectName := "aggregate-test"
  79. // rowNum := 10000
  80. // for i := 0; i < rowNum; i++ {
  81. // repo.RepoAddDoc(apictx.CreateRepoCtx(), collectName, &model.AggregateTest{
  82. // Name: randName(6),
  83. // Age: randAge(),
  84. // Sex: randSex(),
  85. // Salary: randSalary(),
  86. // })
  87. // }
  88. // wg.Done()
  89. // }
  90. // func randSex() *int {
  91. // result, _ := randc.Int(randc.Reader, big.NewInt(2))
  92. // sex := int(result.Int64())
  93. // return &sex
  94. // }
  95. // func randAge() int {
  96. // result, _ := randc.Int(randc.Reader, big.NewInt(82))
  97. // return int(result.Int64() + 18)
  98. // }
  99. // func randSalary() int {
  100. // result, _ := randc.Int(randc.Reader, big.NewInt(10000))
  101. // return int(result.Int64() + 2000)
  102. // }
  103. // const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  104. // var src = rand.NewSource(time.Now().UnixNano())
  105. // const (
  106. // // 6 bits to represent a letter index
  107. // letterIdBits = 6
  108. // // All 1-bits as many as letterIdBits
  109. // letterIdMask = 1<<letterIdBits - 1
  110. // letterIdMax = 63 / letterIdBits
  111. // )
  112. // func randName(n int) string {
  113. // b := make([]byte, n)
  114. // // A rand.Int63() generates 63 random bits, enough for letterIdMax letters!
  115. // for i, cache, remain := n-1, src.Int63(), letterIdMax; i >= 0; {
  116. // if remain == 0 {
  117. // cache, remain = src.Int63(), letterIdMax
  118. // }
  119. // if idx := int(cache & letterIdMask); idx < len(letters) {
  120. // b[i] = letters[idx]
  121. // i--
  122. // }
  123. // cache >>= letterIdBits
  124. // remain--
  125. // }
  126. // return *(*string)(unsafe.Pointer(&b))
  127. // }
  128. // func genPipeline(age int) (bson.D, bson.D, bson.D) {
  129. // matchStage := bson.D{
  130. // {"$match", bson.D{
  131. // {"age",
  132. // bson.D{
  133. // {"$eq", age},
  134. // }},
  135. // }},
  136. // }
  137. // groupStage := bson.D{
  138. // {"$group", bson.D{
  139. // {"_id", bson.D{
  140. // {"age", "$age"},
  141. // {"sex", "$sex"},
  142. // }},
  143. // {"age", bson.D{
  144. // {"$first", "$age"},
  145. // }},
  146. // {"sex", bson.D{
  147. // {"$first", "$sex"},
  148. // }},
  149. // {"total", bson.D{
  150. // {"$sum", 1},
  151. // }},
  152. // {"avgSalary", bson.D{
  153. // {"$avg", "$salary"},
  154. // }},
  155. // }},
  156. // }
  157. // projectStage := bson.D{
  158. // {"$project", bson.D{
  159. // {"_id", 0},
  160. // {"age", 1},
  161. // {"sex", 1},
  162. // {"total", 1},
  163. // {"avgSalary", 1},
  164. // }},
  165. // }
  166. // return matchStage, groupStage, projectStage
  167. // }
  168. // func DataAggregate(apictx *ApiSession, age int, resultChan chan bson.M, wg *sync.WaitGroup) {
  169. // matchStage, groupStage, projectStage := genPipeline(age)
  170. // // opts := options.Aggregate().SetMaxTime(15 * time.Second)
  171. // cursor, err := apictx.Svc.Mongo.GetCollection("aggregate-test").Aggregate(apictx.CreateRepoCtx().Ctx, mongo.Pipeline{matchStage, groupStage, projectStage})
  172. // if err != nil {
  173. // log.Println(err)
  174. // }
  175. // //打印文档内容
  176. // var results []bson.M
  177. // if err = cursor.All(apictx.CreateRepoCtx().Ctx, &results); err != nil {
  178. // log.Println(err)
  179. // }
  180. // log.Printf("%#v\n", results)
  181. // for _, result := range results {
  182. // resultChan <- result
  183. // }
  184. // wg.Done()
  185. // }
  186. // type OutPut struct {
  187. // Age int32 `json:"age"`
  188. // Sex int32 `json:"sex"`
  189. // Total int32 `json:"total"`
  190. // AvgSalary float64 `json:"avg_salary"`
  191. // }
  192. // type ResultSlice []OutPut
  193. // func (a ResultSlice) Len() int { // 重写 Len() 方法
  194. // return len(a)
  195. // }
  196. // func (a ResultSlice) Swap(i, j int) { // 重写 Swap() 方法
  197. // a[i], a[j] = a[j], a[i]
  198. // }
  199. // func (a ResultSlice) Less(i, j int) bool { // 重写 Less() 方法, 从大到小排序
  200. // return a[j].Age < a[i].Age
  201. // }