print.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. // buf, _ := f.WriteToBuffer()
  44. // res, err := excelToPdf(buf, apictx.Svc.Conf.PdfApiAddr)
  45. // if err != nil {
  46. // return nil, errors.New("转化pdf失败")
  47. // }
  48. // body := res.Body
  49. // byteData, err := io.ReadAll(body)
  50. // if err != nil {
  51. // return nil, err
  52. // }
  53. // defer res.Body.Close()
  54. // // os.Mkdir("tmp", 0666)
  55. // if err := os.WriteFile("aaa.pdf", byteData, 0666); err != nil {
  56. // log.Fatal(err)
  57. // }
  58. // // 去重相邻元素
  59. // func removeDuplicationSort(arr []string) []string {
  60. // length := len(arr)
  61. // if length == 0 {
  62. // return arr
  63. // }
  64. // j := 0
  65. // for i := 1; i < length; i++ {
  66. // if arr[i] != arr[j] {
  67. // j++
  68. // if j < i {
  69. // swap(arr, i, j)
  70. // }
  71. // }
  72. // }
  73. // return arr[:j+1]
  74. // }
  75. // func swap(arr []string, a, b int) {
  76. // arr[a], arr[b] = arr[b], arr[a]
  77. // }
  78. // // 生成百万数据
  79. // func GenData(_ *gin.Context, apictx *ApiSession) (interface{}, error) {
  80. // var wg sync.WaitGroup
  81. // for i := 0; i < 100; i++ {
  82. // wg.Add(1)
  83. // go insertData(apictx, &wg)
  84. // }
  85. // wg.Wait()
  86. // return true, nil
  87. // }
  88. // // 聚合查询百万数据
  89. // func SearchData(_ *gin.Context, apictx *ApiSession) (interface{}, error) {
  90. // dataStatResult := make(chan bson.M)
  91. // var output ResultSlice
  92. // var wg sync.WaitGroup
  93. // // 82个gorutine 执行聚合
  94. // for i := 18; i < 100; i++ {
  95. // wg.Add(1)
  96. // go DataAggregate(apictx, i, dataStatResult, &wg)
  97. // }
  98. // // 从管道中获取聚合结果 保存在数组中
  99. // for value := range dataStatResult {
  100. // output = append(output, OutPut{
  101. // Age: value["age"].(int32),
  102. // Sex: value["sex"].(int32),
  103. // Total: value["total"].(int32),
  104. // AvgSalary: value["avgSalary"].(float64),
  105. // })
  106. // if len(output) == 164 { // 如果大于164,不会跳出;如果小于164,wg.done != wg.add ,wg.wait阻塞
  107. // break
  108. // }
  109. // }
  110. // wg.Wait()
  111. // //倒序排列
  112. // sort.Sort(output)
  113. // for _, v := range output {
  114. // result, err := json.Marshal(&v)
  115. // if err != nil {
  116. // fmt.Println("json.marshal failed, err:", err)
  117. // return nil, err
  118. // }
  119. // fmt.Println(string(result))
  120. // }
  121. // return output, nil
  122. // }
  123. // func insertData(apictx *ApiSession, wg *sync.WaitGroup) {
  124. // collectName := "aggregate-test"
  125. // rowNum := 10000
  126. // for i := 0; i < rowNum; i++ {
  127. // repo.RepoAddDoc(apictx.CreateRepoCtx(), collectName, &model.AggregateTest{
  128. // Name: randName(6),
  129. // Age: randAge(),
  130. // Sex: randSex(),
  131. // Salary: randSalary(),
  132. // })
  133. // }
  134. // wg.Done()
  135. // }
  136. // func randSex() *int {
  137. // result, _ := randc.Int(randc.Reader, big.NewInt(2))
  138. // sex := int(result.Int64())
  139. // return &sex
  140. // }
  141. // func randAge() int {
  142. // result, _ := randc.Int(randc.Reader, big.NewInt(82))
  143. // return int(result.Int64() + 18)
  144. // }
  145. // func randSalary() int {
  146. // result, _ := randc.Int(randc.Reader, big.NewInt(10000))
  147. // return int(result.Int64() + 2000)
  148. // }
  149. // const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  150. // var src = rand.NewSource(time.Now().UnixNano())
  151. // const (
  152. // // 6 bits to represent a letter index
  153. // letterIdBits = 6
  154. // // All 1-bits as many as letterIdBits
  155. // letterIdMask = 1<<letterIdBits - 1
  156. // letterIdMax = 63 / letterIdBits
  157. // )
  158. // func randName(n int) string {
  159. // b := make([]byte, n)
  160. // // A rand.Int63() generates 63 random bits, enough for letterIdMax letters!
  161. // for i, cache, remain := n-1, src.Int63(), letterIdMax; i >= 0; {
  162. // if remain == 0 {
  163. // cache, remain = src.Int63(), letterIdMax
  164. // }
  165. // if idx := int(cache & letterIdMask); idx < len(letters) {
  166. // b[i] = letters[idx]
  167. // i--
  168. // }
  169. // cache >>= letterIdBits
  170. // remain--
  171. // }
  172. // return *(*string)(unsafe.Pointer(&b))
  173. // }
  174. // func genPipeline(age int) (bson.D, bson.D, bson.D) {
  175. // matchStage := bson.D{
  176. // {"$match", bson.D{
  177. // {"age",
  178. // bson.D{
  179. // {"$eq", age},
  180. // }},
  181. // }},
  182. // }
  183. // groupStage := bson.D{
  184. // {"$group", bson.D{
  185. // {"_id", bson.D{
  186. // {"age", "$age"},
  187. // {"sex", "$sex"},
  188. // }},
  189. // {"age", bson.D{
  190. // {"$first", "$age"},
  191. // }},
  192. // {"sex", bson.D{
  193. // {"$first", "$sex"},
  194. // }},
  195. // {"total", bson.D{
  196. // {"$sum", 1},
  197. // }},
  198. // {"avgSalary", bson.D{
  199. // {"$avg", "$salary"},
  200. // }},
  201. // }},
  202. // }
  203. // projectStage := bson.D{
  204. // {"$project", bson.D{
  205. // {"_id", 0},
  206. // {"age", 1},
  207. // {"sex", 1},
  208. // {"total", 1},
  209. // {"avgSalary", 1},
  210. // }},
  211. // }
  212. // return matchStage, groupStage, projectStage
  213. // }
  214. // func DataAggregate(apictx *ApiSession, age int, resultChan chan bson.M, wg *sync.WaitGroup) {
  215. // matchStage, groupStage, projectStage := genPipeline(age)
  216. // // opts := options.Aggregate().SetMaxTime(15 * time.Second)
  217. // cursor, err := apictx.Svc.Mongo.GetCollection("aggregate-test").Aggregate(apictx.CreateRepoCtx().Ctx, mongo.Pipeline{matchStage, groupStage, projectStage})
  218. // if err != nil {
  219. // log.Println(err)
  220. // }
  221. // //打印文档内容
  222. // var results []bson.M
  223. // if err = cursor.All(apictx.CreateRepoCtx().Ctx, &results); err != nil {
  224. // log.Println(err)
  225. // }
  226. // log.Printf("%#v\n", results)
  227. // for _, result := range results {
  228. // resultChan <- result
  229. // }
  230. // wg.Done()
  231. // }
  232. // type OutPut struct {
  233. // Age int32 `json:"age"`
  234. // Sex int32 `json:"sex"`
  235. // Total int32 `json:"total"`
  236. // AvgSalary float64 `json:"avg_salary"`
  237. // }
  238. // type ResultSlice []OutPut
  239. // func (a ResultSlice) Len() int { // 重写 Len() 方法
  240. // return len(a)
  241. // }
  242. // func (a ResultSlice) Swap(i, j int) { // 重写 Swap() 方法
  243. // a[i], a[j] = a[j], a[i]
  244. // }
  245. // func (a ResultSlice) Less(i, j int) bool { // 重写 Less() 方法, 从大到小排序
  246. // return a[j].Age < a[i].Age
  247. // }