report2img.go 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. // 先取报告转长图的host配置, 如果没有再取原有的报告分享配置(兼容只配了其中一个的客户)
  36. conf, e := models.GetBusinessConf()
  37. if e != nil {
  38. err = fmt.Errorf("获取商家配置失败, Err: %s", e.Error())
  39. return
  40. }
  41. reportViewHost := conf[models.BusinessConfReport2ImgUrl]
  42. if reportViewHost == "" {
  43. reportViewHost = conf[models.BusinessConfReportViewUrl]
  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. ossClient := NewOssClient()
  112. if ossClient == nil {
  113. errImg = fmt.Errorf("初始化OSS服务失败")
  114. return
  115. }
  116. fmt.Println("start UploadFile")
  117. si, e := ossClient.UploadFile(imgFileName, apiResult[0], "")
  118. if e != nil {
  119. fmt.Println("UploadFile,Err:" + e.Error())
  120. errImg = fmt.Errorf("文件上传失败, Err: %s", e.Error())
  121. return
  122. }
  123. fmt.Println("end UploadFile:" + imgFileName)
  124. //if utils.ObjectStorageClient == "minio" {
  125. // si, e := UploadMinIoToDir(imgFileName, apiResult[0], uploadDir, fileDir)
  126. // if e != nil {
  127. // errImg = e
  128. // return
  129. // }
  130. // imgUrl = si
  131. // return
  132. //}
  133. //// 默认OSS
  134. //si, e := UploadAliyunToDir(imgFileName, apiResult[0], uploadDir, fileDir)
  135. //if e != nil {
  136. // errImg = e
  137. // return
  138. //}
  139. imgUrl = si
  140. var clearReq ReportHtml2ImgApiClearLocalReq
  141. clearReq.FileName = imgFileName
  142. e = CurlReportHtml2ImgApiClearLocal(clearReq)
  143. if e != nil {
  144. errImg = e
  145. return
  146. }
  147. }()
  148. go func() {
  149. defer func() {
  150. wg.Done()
  151. }()
  152. pdfFileName := fileName + ".pdf"
  153. ossClient := NewOssClient()
  154. if ossClient == nil {
  155. errImg = fmt.Errorf("初始化OSS服务失败")
  156. return
  157. }
  158. sp, e := ossClient.UploadFile(pdfFileName, apiResult[1], "")
  159. if e != nil {
  160. errImg = fmt.Errorf("文件上传失败, Err: %s", e.Error())
  161. return
  162. }
  163. //if utils.ObjectStorageClient == "minio" {
  164. // sp, e := UploadMinIoToDir(pdfFileName, apiResult[1], uploadDir, fileDir)
  165. // if e != nil {
  166. // errPdf = e
  167. // return
  168. // }
  169. // pdfUrl = sp
  170. // return
  171. //}
  172. //sp, e := UploadAliyunToDir(pdfFileName, apiResult[1], uploadDir, fileDir)
  173. //if e != nil {
  174. // errPdf = e
  175. // return
  176. //}
  177. pdfUrl = sp
  178. var clearReq ReportHtml2ImgApiClearLocalReq
  179. clearReq.FileName = pdfFileName
  180. e = CurlReportHtml2ImgApiClearLocal(clearReq)
  181. if e != nil {
  182. errImg = e
  183. return
  184. }
  185. }()
  186. wg.Wait()
  187. if errImg != nil {
  188. err = fmt.Errorf("upload img err: %s", e.Error())
  189. return
  190. }
  191. if errPdf != nil {
  192. err = fmt.Errorf("upload pdf err: %s", e.Error())
  193. return
  194. }
  195. fmt.Println("imgUrl:" + imgUrl + ";pdfUrl:" + pdfUrl)
  196. // 更新报告链接
  197. item.DetailImgUrl = imgUrl
  198. item.DetailPdfUrl = pdfUrl
  199. item.ModifyTime = time.Now().Local()
  200. updateCols := []string{"DetailImgUrl", "DetailPdfUrl", "ModifyTime"}
  201. if e = item.Update(updateCols); e != nil {
  202. err = fmt.Errorf("更新报告链接失败, Err: %s", e.Error())
  203. return
  204. }
  205. return
  206. }
  207. // ReportHtml2ImgApiReq 报告生成图片接口请求体
  208. type ReportHtml2ImgApiReq struct {
  209. ReportUrl string `json:"report_url" description:"报告详情分享地址"`
  210. FileName string `json:"file_name" description:"生成的文件名"`
  211. OutputType string `json:"output_type" description:"生成类型: img/pdf, 为空则两种均生成"`
  212. }
  213. // ReportHtml2ImgApiResp 报告生成图片接口响应体
  214. type ReportHtml2ImgApiResp struct {
  215. Code int `json:"code" description:"状态码"`
  216. Msg string `json:"error" description:"提示信息"`
  217. Data []string `json:"data" description:"返回数据"`
  218. }
  219. // CurlReportHtml2ImgApi 请求报告生成图片接口
  220. func CurlReportHtml2ImgApi(params ReportHtml2ImgApiReq) (apiResp []string, err error) {
  221. if utils.Report2ImgServerUrl == "" {
  222. err = fmt.Errorf("服务地址为空")
  223. return
  224. }
  225. url := fmt.Sprint(utils.Report2ImgServerUrl, PythonReportHtml2ImgApi)
  226. //fmt.Println("url: ", url)
  227. jsonData, e := json.Marshal(params)
  228. if e != nil {
  229. err = fmt.Errorf("data json marshal err: %s", e.Error())
  230. return
  231. }
  232. //fmt.Println(string(jsonData))
  233. //fmt.Println("http post start")
  234. resp, e := http.Post(url, "application/json", bytes.NewBuffer(jsonData))
  235. if e != nil {
  236. err = fmt.Errorf("http post err: %s", e.Error())
  237. return
  238. }
  239. defer resp.Body.Close()
  240. //fmt.Println("http post end")
  241. b, e := ioutil.ReadAll(resp.Body)
  242. if e != nil {
  243. err = fmt.Errorf("resp body read err: %s", e.Error())
  244. return
  245. }
  246. if len(b) == 0 {
  247. err = fmt.Errorf("resp body is empty")
  248. return
  249. }
  250. result := new(ReportHtml2ImgApiResp)
  251. if e = json.Unmarshal(b, &result); e != nil {
  252. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
  253. return
  254. }
  255. if result.Code != 200 {
  256. err = fmt.Errorf("result: %s", string(b))
  257. return
  258. }
  259. apiResp = result.Data
  260. return
  261. }
  262. // ReportHtml2ImgApiClearLocalReq 清除本地文件接口请求体
  263. type ReportHtml2ImgApiClearLocalReq struct {
  264. FileName string `json:"file_name" description:"生成的文件名"`
  265. }
  266. // ReportHtml2ImgApiClearLocalResp 清除本地文件接口响应体
  267. type ReportHtml2ImgApiClearLocalResp struct {
  268. Code int `json:"code" description:"状态码"`
  269. Msg string `json:"error" description:"提示信息"`
  270. Data string `json:"data" description:"返回数据"`
  271. }
  272. // CurlReportHtml2ImgApiClearLocal 请求清除本地文件
  273. func CurlReportHtml2ImgApiClearLocal(params ReportHtml2ImgApiClearLocalReq) (err error) {
  274. if utils.Report2ImgServerUrl == "" {
  275. err = fmt.Errorf("服务地址为空")
  276. return
  277. }
  278. url := fmt.Sprint(utils.Report2ImgServerUrl, PythonReportHtml2ImgClearLocalApi)
  279. jsonData, e := json.Marshal(params)
  280. if e != nil {
  281. err = fmt.Errorf("data json marshal err: %s", e.Error())
  282. return
  283. }
  284. //fmt.Println(string(jsonData))
  285. resp, e := http.Post(url, "application/json", bytes.NewBuffer(jsonData))
  286. if e != nil {
  287. err = fmt.Errorf("http post err: %s", e.Error())
  288. return
  289. }
  290. defer resp.Body.Close()
  291. b, e := ioutil.ReadAll(resp.Body)
  292. if e != nil {
  293. err = fmt.Errorf("resp body read err: %s", e.Error())
  294. return
  295. }
  296. if len(b) == 0 {
  297. err = fmt.Errorf("resp body is empty")
  298. return
  299. }
  300. result := new(ReportHtml2ImgApiClearLocalResp)
  301. if e = json.Unmarshal(b, &result); e != nil {
  302. err = fmt.Errorf("result unmarshal err: %s\nresult: %s", e.Error(), string(b))
  303. return
  304. }
  305. if result.Code != 200 {
  306. err = fmt.Errorf("result: %s", string(b))
  307. return
  308. }
  309. return
  310. }