print.go 6.4 KB

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