report2img.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. package services
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "eta/eta_report/models"
  6. "eta/eta_report/services/alarm_msg"
  7. "eta/eta_report/utils"
  8. "fmt"
  9. "io/ioutil"
  10. "net/http"
  11. "sync"
  12. "time"
  13. )
  14. const (
  15. PythonReportHtml2ImgApi = "/api/report/html2img" // 图片生成接口
  16. PythonReportHtml2ImgClearLocalApi = "/api/report/clear_local_file" // 清除本地图片文件接口
  17. )
  18. // CreateReportImgAndPdf 报告详情生成长图及PDF
  19. func CreateReportImgAndPdf(req Report2ImgQueueReq) {
  20. // 研报以后大概率也需要, 暂只处理智能研报的
  21. if req.ReportType != 2 {
  22. return
  23. }
  24. if req.ReportCode == "" {
  25. return
  26. }
  27. var err error
  28. defer func() {
  29. if err != nil {
  30. fmt.Println(err.Error())
  31. tips := fmt.Sprintf("报告详情转长图, ErrMsg: %s", err.Error())
  32. go alarm_msg.SendAlarmMsg(tips, 3)
  33. }
  34. }()
  35. // 报告分享域名
  36. reportViewHost := ""
  37. conf, e := models.GetBusinessConf()
  38. if e != nil {
  39. err = fmt.Errorf("获取商家配置失败, Err: %s", e.Error())
  40. return
  41. }
  42. if v, ok := conf[models.BusinessConfReportViewUrl]; ok {
  43. reportViewHost = v
  44. }
  45. if reportViewHost == "" {
  46. err = fmt.Errorf("报告分享域名未配置")
  47. return
  48. }
  49. // 校验报告是否被删
  50. reportOb := new(models.SmartReport)
  51. cond := ` AND report_code = ?`
  52. pars := make([]interface{}, 0)
  53. pars = append(pars, req.ReportCode)
  54. item, e := reportOb.GetItemByCondition(cond, pars)
  55. if e != nil {
  56. if e.Error() == utils.ErrNoRow() {
  57. return
  58. }
  59. err = fmt.Errorf("获取报告失败, Err: %s", e.Error())
  60. return
  61. }
  62. // 报告平均生成时间基本在3min以下, 设定一个6min的超时以免后续阻塞
  63. apiDone := make(chan bool, 1)
  64. apiError := make(chan error, 1)
  65. fileName := utils.GetRandStringNoSpecialChar(28)
  66. apiResult := make([]string, 0)
  67. go func() {
  68. var apiReq ReportHtml2ImgApiReq
  69. apiReq.ReportUrl = fmt.Sprintf("%s/%s?code=%s", reportViewHost, "smart_report_getImg", req.ReportCode)
  70. //if utils.RunMode == "debug" {
  71. // apiReq.ReportUrl = "https://ficc.hzinsights.com/reportshare_crm_report?code=4e38d30e656da5ae9d3a425109ce9e04"
  72. //}
  73. //fmt.Println(apiReq.ReportUrl)
  74. apiReq.FileName = fileName
  75. //fmt.Println("curl start")
  76. res, e := CurlReportHtml2ImgApi(apiReq)
  77. if e != nil {
  78. apiError <- fmt.Errorf("CurlReportHtml2ImgApi err: %s", e.Error())
  79. return
  80. }
  81. //fmt.Println("curl end")
  82. apiResult = res
  83. apiDone <- true
  84. }()
  85. select {
  86. case <-time.After(6 * time.Minute):
  87. err = fmt.Errorf("报告生成长图超时")
  88. return
  89. case e = <-apiError:
  90. err = e
  91. return
  92. case <-apiDone:
  93. fmt.Println("api done")
  94. }
  95. if len(apiResult) != 2 {
  96. err = fmt.Errorf("文件生成有误")
  97. return
  98. }
  99. // 上传IMG, PDF
  100. var imgUrl, pdfUrl string
  101. var errImg, errPdf error
  102. uploadDir := "static/report_images/"
  103. fileDir := ""
  104. wg := sync.WaitGroup{}
  105. wg.Add(2)
  106. go func() {
  107. defer func() {
  108. wg.Done()
  109. }()
  110. imgFileName := fileName + ".png"
  111. if utils.ObjectStorageClient == "minio" {
  112. si, e := UploadMinIoToDir(imgFileName, apiResult[0], uploadDir, fileDir)
  113. if e != nil {
  114. errImg = e
  115. return
  116. }
  117. imgUrl = si
  118. return
  119. }
  120. // 默认OSS
  121. si, e := UploadAliyunToDir(imgFileName, apiResult[0], uploadDir, fileDir)
  122. if e != nil {
  123. errImg = e
  124. return
  125. }
  126. imgUrl = si
  127. var clearReq ReportHtml2ImgApiClearLocalReq
  128. clearReq.FileName = imgFileName
  129. e = CurlReportHtml2ImgApiClearLocal(clearReq)
  130. if e != nil {
  131. errImg = e
  132. return
  133. }
  134. }()
  135. go func() {
  136. defer func() {
  137. wg.Done()
  138. }()
  139. pdfFileName := fileName + ".pdf"
  140. if utils.ObjectStorageClient == "minio" {
  141. sp, e := UploadMinIoToDir(pdfFileName, apiResult[1], uploadDir, fileDir)
  142. if e != nil {
  143. errPdf = e
  144. return
  145. }
  146. pdfUrl = sp
  147. return
  148. }
  149. sp, e := UploadAliyunToDir(pdfFileName, apiResult[1], uploadDir, fileDir)
  150. if e != nil {
  151. errPdf = e
  152. return
  153. }
  154. pdfUrl = sp
  155. var clearReq ReportHtml2ImgApiClearLocalReq
  156. clearReq.FileName = pdfFileName
  157. e = CurlReportHtml2ImgApiClearLocal(clearReq)
  158. if e != nil {
  159. errImg = e
  160. return
  161. }
  162. }()
  163. wg.Wait()
  164. if errImg != nil {
  165. err = fmt.Errorf("upload img err: %s", e.Error())
  166. return
  167. }
  168. if errPdf != nil {
  169. err = fmt.Errorf("upload pdf err: %s", e.Error())
  170. return
  171. }
  172. // 更新报告链接
  173. item.DetailImgUrl = imgUrl
  174. item.DetailPdfUrl = pdfUrl
  175. item.ModifyTime = time.Now().Local()
  176. updateCols := []string{"DetailImgUrl", "DetailPdfUrl", "ModifyTime"}
  177. if e = item.Update(updateCols); e != nil {
  178. err = fmt.Errorf("更新报告链接失败, Err: %s", e.Error())
  179. return
  180. }
  181. return
  182. }
  183. // ReportHtml2ImgApiReq 报告生成图片接口请求体
  184. type ReportHtml2ImgApiReq struct {
  185. ReportUrl string `json:"report_url" description:"报告详情分享地址"`
  186. FileName string `json:"file_name" description:"生成的文件名"`
  187. OutputType string `json:"output_type" description:"生成类型: img/pdf, 为空则两种均生成"`
  188. }
  189. // ReportHtml2ImgApiResp 报告生成图片接口响应体
  190. type ReportHtml2ImgApiResp struct {
  191. Code int `json:"code" description:"状态码"`
  192. Msg string `json:"error" description:"提示信息"`
  193. Data []string `json:"data" description:"返回数据"`
  194. }
  195. // CurlReportHtml2ImgApi 请求报告生成图片接口
  196. func CurlReportHtml2ImgApi(params ReportHtml2ImgApiReq) (apiResp []string, err error) {
  197. if utils.Report2ImgServerUrl == "" {
  198. err = fmt.Errorf("服务地址为空")
  199. return
  200. }
  201. url := fmt.Sprint(utils.Report2ImgServerUrl, PythonReportHtml2ImgApi)
  202. //fmt.Println("url: ", url)
  203. jsonData, e := json.Marshal(params)
  204. if e != nil {
  205. err = fmt.Errorf("data json marshal err: %s", e.Error())
  206. return
  207. }
  208. //fmt.Println(string(jsonData))
  209. //fmt.Println("http post start")
  210. resp, e := http.Post(url, "application/json", bytes.NewBuffer(jsonData))
  211. if e != nil {
  212. err = fmt.Errorf("http post err: %s", e.Error())
  213. return
  214. }
  215. defer resp.Body.Close()
  216. //fmt.Println("http post end")
  217. b, e := ioutil.ReadAll(resp.Body)
  218. if e != nil {
  219. err = fmt.Errorf("resp body read err: %s", e.Error())
  220. return
  221. }
  222. if len(b) == 0 {
  223. err = fmt.Errorf("resp body is empty")
  224. return
  225. }
  226. result := new(ReportHtml2ImgApiResp)
  227. if e = json.Unmarshal(b, &result); e != nil {
  228. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
  229. return
  230. }
  231. if result.Code != 200 {
  232. err = fmt.Errorf("result: %s", string(b))
  233. return
  234. }
  235. apiResp = result.Data
  236. return
  237. }
  238. // ReportHtml2ImgApiClearLocalReq 清除本地文件接口请求体
  239. type ReportHtml2ImgApiClearLocalReq struct {
  240. FileName string `json:"file_name" description:"生成的文件名"`
  241. }
  242. // ReportHtml2ImgApiClearLocalResp 清除本地文件接口响应体
  243. type ReportHtml2ImgApiClearLocalResp struct {
  244. Code int `json:"code" description:"状态码"`
  245. Msg string `json:"error" description:"提示信息"`
  246. Data string `json:"data" description:"返回数据"`
  247. }
  248. // CurlReportHtml2ImgApiClearLocal 请求清除本地文件
  249. func CurlReportHtml2ImgApiClearLocal(params ReportHtml2ImgApiClearLocalReq) (err error) {
  250. if utils.Report2ImgServerUrl == "" {
  251. err = fmt.Errorf("服务地址为空")
  252. return
  253. }
  254. url := fmt.Sprint(utils.Report2ImgServerUrl, PythonReportHtml2ImgClearLocalApi)
  255. jsonData, e := json.Marshal(params)
  256. if e != nil {
  257. err = fmt.Errorf("data json marshal err: %s", e.Error())
  258. return
  259. }
  260. //fmt.Println(string(jsonData))
  261. resp, e := http.Post(url, "application/json", bytes.NewBuffer(jsonData))
  262. if e != nil {
  263. err = fmt.Errorf("http post err: %s", e.Error())
  264. return
  265. }
  266. defer resp.Body.Close()
  267. b, e := ioutil.ReadAll(resp.Body)
  268. if e != nil {
  269. err = fmt.Errorf("resp body read err: %s", e.Error())
  270. return
  271. }
  272. if len(b) == 0 {
  273. err = fmt.Errorf("resp body is empty")
  274. return
  275. }
  276. result := new(ReportHtml2ImgApiClearLocalResp)
  277. if e = json.Unmarshal(b, &result); e != nil {
  278. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
  279. return
  280. }
  281. if result.Code != 200 {
  282. err = fmt.Errorf("result: %s", string(b))
  283. return
  284. }
  285. return
  286. }