print.go 6.4 KB

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