report2img.go 8.7 KB

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