spider.go 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801
  1. package front
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "log"
  7. "mongodb"
  8. "regexp"
  9. "sort"
  10. "spider"
  11. "strconv"
  12. mu "mfw/util"
  13. qu "qfw/util"
  14. util "spiderutil"
  15. "strings"
  16. "time"
  17. u "util"
  18. )
  19. type Base struct {
  20. SpiderCode string
  21. SpiderCodeOld string
  22. SpiderName string
  23. SpiderChannel string
  24. SpiderDownDetailPage bool
  25. SpiderStartPage int
  26. SpiderMaxPage int
  27. SpiderRunRate int
  28. Spider2Collection string
  29. SpiderPageEncoding string
  30. SpiderStoreMode int //1,2
  31. SpiderStoreToMsgEvent int
  32. SpiderTargetChannelUrl string
  33. SpiderLastDownloadTime string
  34. SpiderIsHistoricalMend bool
  35. SpiderIsMustDownload bool
  36. }
  37. type Step1 struct {
  38. Address string
  39. ContentChooser string
  40. DateFormat string
  41. Expert string
  42. Types int
  43. }
  44. type Step2 struct {
  45. Listadd string
  46. Listadds string
  47. BlockChooser string
  48. AddressChooser string
  49. TitleChooser string
  50. DateChooser string
  51. DateFormat string
  52. Expert string
  53. Types int
  54. }
  55. type Step3 struct {
  56. ContentChooser string
  57. ElementChooser string
  58. T_title string
  59. T_href string
  60. T_date string
  61. Expert string
  62. Types int
  63. }
  64. type StepRe3 struct {
  65. Checked bool
  66. Expert string
  67. }
  68. type OtherBase struct {
  69. IsFlow int //爬虫所采集数据是否参与数据流程标识
  70. SpiderType string //爬虫类型:increment增量;history历史
  71. SpiderHistoryMaxPage int //采集历史数据时的采集最大页
  72. SpiderMoveEvent string //爬虫采集完历史后要转移到的节点 comm:队列模式、bid:高性能模式
  73. }
  74. //加载某个爬虫
  75. func (f *Front) LoadSpider(codeTaskIdReState string) error {
  76. tmpStr := strings.Split(codeTaskIdReState, "__")
  77. code := tmpStr[0]
  78. taskId := tmpStr[1]
  79. auth := qu.IntAll(f.GetSession("auth"))
  80. restate := -1
  81. if taskId == "restate=1" { //重采编辑
  82. restate = 1
  83. } else if taskId == "restate=2" {
  84. restate = 2
  85. } else if taskId == "restate=3" {
  86. restate = 3
  87. } else {
  88. if auth == u.Role_Dev && qu.ObjToString(f.GetSession(taskId)) == "" {
  89. xgTime := time.Unix(time.Now().Unix(), 0).Format("2006-01-02 15:04:05")
  90. f.SetSession(taskId, xgTime)
  91. }
  92. }
  93. copy := f.GetString("copy")
  94. if f.Method() == "GET" {
  95. code := util.Se.Decode4Hex(code)
  96. f.T["actiontext"] = "编辑"
  97. //lua, _ := u.MgoE.FindOne("luaconfig", map[string]interface{}{"code": code})
  98. lua, _ := u.MgoEB.FindOne("luaconfig", map[string]interface{}{"code": code})
  99. auth := qu.IntAll(f.GetSession("auth"))
  100. if qu.ObjToString((*lua)["createuserid"]) == f.GetSession("userid").(string) || auth >= 1 {
  101. if len(*lua) > 0 {
  102. if qu.IntAll((*lua)["event"]) == 7000 && qu.IntAll((*lua)["urgency"]) == 0 && qu.IntAll((*lua)["state"]) == 0 {
  103. q := map[string]interface{}{
  104. "event": 7000,
  105. "state": 0,
  106. "urgency": 1,
  107. "modifyuserid": f.GetSession("userid"),
  108. }
  109. if u.MgoEB.Count("luaconfig", q) > 0 {
  110. f.Write("名下还有7000节点待完成的紧急爬虫,暂无法处理该爬虫!")
  111. return nil
  112. }
  113. }
  114. if copy != "" {
  115. //luacopy, _ := u.MgoE.FindOne("luaconfig", map[string]interface{}{"code": copy})
  116. luacopy, _ := u.MgoEB.FindOne("luaconfig", map[string]interface{}{"code": copy})
  117. if len(*luacopy) > 0 {
  118. (*lua)["model"] = (*luacopy)["model"]
  119. common_copy := (*luacopy)["param_common"].([]interface{})
  120. common := (*lua)["param_common"].([]interface{})
  121. common_copy[0] = common[0]
  122. common_copy[1] = common[1]
  123. common_copy[2] = common[2]
  124. common_copy[11] = common[11]
  125. (*lua)["param_common"] = (*luacopy)["param_common"]
  126. (*lua)["param_time"] = (*luacopy)["param_time"]
  127. (*lua)["param_list"] = (*luacopy)["param_list"]
  128. (*lua)["param_content"] = (*luacopy)["param_content"]
  129. (*lua)["str_list"] = (*luacopy)["str_list"]
  130. (*lua)["str_time"] = (*luacopy)["str_time"]
  131. (*lua)["str_content"] = (*luacopy)["str_content"]
  132. (*lua)["Thref"] = (*luacopy)["Thref"]
  133. (*lua)["Tpublishtime"] = (*luacopy)["Tpublishtime"]
  134. (*lua)["Ttitle"] = (*luacopy)["Ttitle"]
  135. (*lua)["Tdate"] = (*luacopy)["Tdate"]
  136. (*lua)["type_content"] = (*luacopy)["type_content"]
  137. (*lua)["type_list"] = (*luacopy)["type_list"]
  138. (*lua)["type_time"] = (*luacopy)["type_time"]
  139. }
  140. }
  141. if (*lua)["listcheck"] != nil {
  142. listcheck := (*lua)["listcheck"].(string)
  143. listcheck = strings.Replace(listcheck, "\\n", "\n", -1)
  144. listcheck = strings.Replace(listcheck, "\\", "", -1)
  145. (*lua)["listcheck"] = listcheck
  146. }
  147. if (*lua)["contentcheck"] != nil {
  148. contentcheck := (*lua)["contentcheck"].(string)
  149. contentcheck = strings.Replace(contentcheck, "\\n", "\n", -1)
  150. contentcheck = strings.Replace(contentcheck, "\\", "", -1)
  151. (*lua)["contentcheck"] = contentcheck
  152. }
  153. js, _ := json.MarshalIndent((*lua)["model"], "", " ")
  154. (*lua)["js"] = string(js)
  155. f.T["lua"] = lua
  156. f.T["taskId"] = taskId
  157. f.T["restate"] = restate
  158. f.T["isflow"] = (*lua)["isflow"]
  159. f.T["spidertype"] = (*lua)["spidertype"]
  160. f.T["spidermovevent"] = (*lua)["spidermovevent"]
  161. f.T["spiderhistorymaxpage"] = (*lua)["spiderhistorymaxpage"]
  162. events := []string{}
  163. for k, _ := range util.Config.Uploadevents {
  164. events = append(events, k)
  165. }
  166. sort.Strings(events)
  167. f.T["events"] = events
  168. if (*lua)["oldlua"] != nil {
  169. return f.Render("oldedit.html", &f.T)
  170. }
  171. return f.Render("spideredit.html", &f.T)
  172. }
  173. } else {
  174. f.Write("您没有编辑他人脚本的权限")
  175. }
  176. }
  177. return nil
  178. }
  179. //查看某个爬虫
  180. func (f *Front) ViewSpider(id string) error {
  181. auth := qu.IntAll(f.GetSession("auth"))
  182. if auth >= 1 {
  183. if f.Method() == "GET" {
  184. code := util.Se.Decode4Hex(id)
  185. f.T["actiontext"] = "编辑"
  186. //lua, _ := u.MgoE.FindOne("luaconfig", map[string]interface{}{"code": code})
  187. lua, _ := u.MgoEB.FindOne("luaconfig", map[string]interface{}{"code": code})
  188. if len(*lua) > 0 {
  189. if (*lua)["listcheck"] != nil {
  190. listcheck := (*lua)["listcheck"].(string)
  191. listcheck = strings.Replace(listcheck, "\\n", "\n", -1)
  192. listcheck = strings.Replace(listcheck, "\\", "", -1)
  193. (*lua)["listcheck"] = listcheck
  194. }
  195. if (*lua)["contentcheck"] != nil {
  196. contentcheck := (*lua)["contentcheck"].(string)
  197. contentcheck = strings.Replace(contentcheck, "\\n", "\n", -1)
  198. contentcheck = strings.Replace(contentcheck, "\\", "", -1)
  199. (*lua)["contentcheck"] = contentcheck
  200. }
  201. js, _ := json.MarshalIndent((*lua)["model"], "", " ")
  202. (*lua)["js"] = string(js)
  203. f.T["lua"] = lua
  204. f.T["isflow"] = (*lua)["isflow"]
  205. f.T["spidertype"] = (*lua)["spidertype"]
  206. f.T["spidermovevent"] = (*lua)["spidermovevent"]
  207. f.T["spiderhistorymaxpage"] = (*lua)["spiderhistorymaxpage"]
  208. events := []string{}
  209. for k, _ := range util.Config.Uploadevents {
  210. events = append(events, k)
  211. }
  212. sort.Strings(events)
  213. f.T["events"] = events
  214. if (*lua)["oldlua"] != nil {
  215. return f.Render("oldedit.html", &f.T)
  216. }
  217. return f.Render("spiderview.html", &f.T)
  218. } else {
  219. f.Write("没有对应记录!")
  220. return nil
  221. }
  222. }
  223. return f.Redirect("/center")
  224. } else {
  225. f.Write("您没有查看他人脚本的权限")
  226. return nil
  227. }
  228. }
  229. func (f *Front) LoadModel(id string) error {
  230. if f.Method() == "GET" {
  231. //lua, _ := u.MgoE.Find("luaconfig", map[string]interface{}{"code": id}, nil, map[string]interface{}{"model": 1}, true, -1, -1)
  232. lua, _ := u.MgoEB.Find("luaconfig", map[string]interface{}{"code": id}, nil, map[string]interface{}{"model": 1}, true, -1, -1)
  233. if len(*lua) > 0 {
  234. f.ServeJson((*lua)[0])
  235. }
  236. }
  237. return f.Redirect("/center")
  238. }
  239. func (f *Front) SaveStep() {
  240. userid, _ := f.GetSession("userid").(string)
  241. auth := qu.IntAll(f.GetSession("auth"))
  242. rep := map[string]interface{}{}
  243. if f.GetString("oldlua") != "" {
  244. id := f.GetString("code")
  245. //one, _ := u.MgoE.FindOne("luaconfig", map[string]interface{}{"code": id})
  246. one, _ := u.MgoEB.FindOne("luaconfig", map[string]interface{}{"code": id})
  247. id = qu.ObjToString((*one)["code"])
  248. script := f.GetStringComm("script")
  249. if strings.Index(script, id) == -1 {
  250. rep["msg"] = "code/名称都不能更改"
  251. f.ServeJson(rep)
  252. return
  253. } else {
  254. upset := map[string]interface{}{"luacontent": script}
  255. upset["modifytime"] = time.Now().Unix()
  256. //b := u.MgoE.Update("luaconfig", map[string]interface{}{"code": id}, map[string]interface{}{"$set": upset}, true, false)
  257. b := u.MgoEB.Update("luaconfig", map[string]interface{}{"code": id}, map[string]interface{}{"$set": upset}, true, false)
  258. if b {
  259. rep["msg"] = "保存成功"
  260. rep["code"] = util.Se.Encode2Hex(id)
  261. f.ServeJson(rep)
  262. return
  263. }
  264. }
  265. } else {
  266. if f.Base.SpiderName != "" && f.Base.SpiderCode != "" {
  267. code := f.Base.SpiderCode
  268. //one, _ := u.MgoE.FindOne("luaconfig", map[string]interface{}{"code": f.Base.SpiderCode})
  269. one, _ := u.MgoEB.FindOne("luaconfig", map[string]interface{}{"code": f.Base.SpiderCode})
  270. //记录上架操作前的第一次保存时的爬虫历史
  271. user := f.GetSession("username").(string)
  272. LuaSaveLog(f.Base.SpiderCode, user, one, 0)
  273. state := qu.IntAllDef((*one)["state"], 0)
  274. restate := qu.IntAll((*one)["restate"])
  275. infoformat := qu.IntAll((*one)["infoformat"])
  276. comeintime := time.Now().Unix()
  277. if len((*one)) > 0 {
  278. comeintime = qu.Int64All((*one)["comeintime"])
  279. ouserid := qu.ObjToString((*one)["createuserid"])
  280. if ouserid != userid && auth == u.Role_Dev {
  281. f.Write("权限不够,不能修改他人脚本")
  282. return
  283. } else {
  284. code = qu.ObjToString((*one)["code"])
  285. f.Base.SpiderCode = code
  286. f.Base.SpiderName = ((*one)["param_common"].([]interface{}))[1].(string)
  287. }
  288. } else {
  289. if auth != u.Role_Admin {
  290. f.Write("不能新建爬虫,请联系管理员导入")
  291. return
  292. }
  293. }
  294. listcheck := f.GetString("listcheck")
  295. contentcheck := f.GetString("contentcheck")
  296. if auth == u.Role_Dev {
  297. //f.Base.SpiderStoreToMsgEvent = 4002
  298. }
  299. common := []interface{}{
  300. f.Base.SpiderCode,
  301. f.Base.SpiderName,
  302. f.Base.SpiderChannel,
  303. f.Base.SpiderDownDetailPage,
  304. f.Base.SpiderStartPage,
  305. f.Base.SpiderMaxPage,
  306. f.Base.SpiderRunRate,
  307. f.Base.Spider2Collection,
  308. f.Base.SpiderPageEncoding,
  309. f.Base.SpiderStoreMode,
  310. f.Base.SpiderStoreToMsgEvent,
  311. f.Base.SpiderTargetChannelUrl,
  312. f.Base.SpiderLastDownloadTime,
  313. f.Base.SpiderIsHistoricalMend,
  314. f.Base.SpiderIsMustDownload,
  315. }
  316. ptime := []interface{}{
  317. f.Step1.DateFormat,
  318. f.Step1.Address,
  319. f.Step1.ContentChooser,
  320. }
  321. list := []interface{}{
  322. f.Step2.Listadd,
  323. f.Step2.Listadds,
  324. f.Step2.BlockChooser,
  325. f.Step2.AddressChooser,
  326. f.Step2.TitleChooser,
  327. f.Step2.DateChooser,
  328. f.Step2.DateFormat,
  329. }
  330. content := []interface{}{
  331. f.Step3.ContentChooser,
  332. f.Step3.ElementChooser,
  333. }
  334. param := map[string]interface{}{}
  335. common[4] = 1
  336. param["param_common"] = common
  337. param["channel"] = f.Base.SpiderChannel
  338. param["href"] = f.Base.SpiderTargetChannelUrl
  339. //向导模式
  340. param["param_time"] = ptime
  341. param["param_list"] = list
  342. param["param_content"] = content
  343. param["type_time"] = f.Step1.Types
  344. param["type_list"] = f.Step2.Types
  345. param["type_content"] = f.Step3.Types
  346. //专家模式
  347. param["str_time"] = f.Step1.Expert
  348. param["str_list"] = f.Step2.Expert
  349. param["str_content"] = f.Step3.Expert
  350. param["comeintime"] = comeintime
  351. listcheck = strings.Replace(listcheck, "\n", "\\\\n", -1)
  352. param["listcheck"] = strings.Replace(listcheck, "\"", "\\\\\"", -1)
  353. contentcheck = strings.Replace(contentcheck, "\n", "\\\\n", -1)
  354. param["contentcheck"] = strings.Replace(contentcheck, "\"", "\\\\\"", -1)
  355. //补充模型
  356. s_model := f.GetString("model")
  357. configModel := util.Config.Model[s_model]
  358. model := map[string]interface{}{}
  359. for k, _ := range configModel {
  360. model[k] = f.GetString(k)
  361. }
  362. model["model"] = s_model
  363. param["code"] = f.Base.SpiderCode
  364. param["model"] = model
  365. if len((*one)) > 0 {
  366. param["createuser"] = (*one)["createuser"]
  367. param["createuserid"] = (*one)["createuserid"]
  368. param["code"] = (*one)["code"]
  369. //开发员关联任务修改爬虫状态
  370. state = qu.IntAll((*one)["state"])
  371. if auth == u.Role_Dev && state >= Sp_state_3 && restate != 1 { //开发员修改,已经审核通过(不包含已上架),状态重置为待完成(restate!=1判断,重采修改保存爬虫时不修改爬虫状态)
  372. param["state"] = 0
  373. } else {
  374. param["state"] = state
  375. }
  376. } else {
  377. param["createuser"] = f.GetSession("loginuser")
  378. param["createuserid"] = f.GetSession("userid")
  379. param["createuseremail"] = f.GetSession("email")
  380. param["next"] = f.GetSession("email")
  381. param["state"] = 0
  382. }
  383. if qu.ObjToString((*one)["modifyuser"]) == "" {
  384. param["modifyuser"] = param["createuser"]
  385. param["modifyuserid"] = param["createuserid"]
  386. }
  387. param["modifytime"] = time.Now().Unix()
  388. param["Ttitle"] = f.Step3.T_title
  389. param["Thref"] = f.Step3.T_href
  390. param["Tdate"] = f.Step3.T_date
  391. //其他信息
  392. param["isflow"] = f.OtherBase.IsFlow
  393. param["spidertype"] = f.OtherBase.SpiderType
  394. param["spiderhistorymaxpage"] = f.OtherBase.SpiderHistoryMaxPage
  395. qu.Debug(f.OtherBase.SpiderMoveEvent)
  396. tmpEvent, err := strconv.Atoi(f.OtherBase.SpiderMoveEvent) //f.OtherBase.SpiderMoveEvent此处SpiderMoveEvent已不表示comm、bid,表示增量的节点
  397. if f.OtherBase.SpiderType == "history" { //爬虫类型是history的放到7000节点,并记录历史节点
  398. param["event"] = 7000
  399. if err == nil {
  400. param["incrementevent"] = tmpEvent //开发人员切换增量节点
  401. } else if event := qu.IntAll((*one)["event"]); event != 7000 { //默认增量节点
  402. param["incrementevent"] = event
  403. }
  404. param["urgency"] = 1 //保存到7000时,爬虫紧急度变为紧急(控制7000节点爬虫紧急未写完不能写普通)
  405. } else if f.OtherBase.SpiderType == "increment" && err == nil { //增量
  406. param["event"] = tmpEvent //开发人员切换增量节点
  407. }
  408. if movevent, ok := util.Config.Uploadevents[f.OtherBase.SpiderMoveEvent].(string); ok && movevent != "" {
  409. param["spidermovevent"] = movevent
  410. }
  411. //开发人员修改爬虫节点后,在审核人员上架时,要在原来的节点下架,临时记录要下架的节点downevent
  412. if event := qu.IntAll((*one)["event"]); event != tmpEvent && event != 7000 {
  413. param["downevent"] = event
  414. }
  415. //三级页复制
  416. param["str_recontent"] = f.StepRe3.Expert
  417. param["iscopycontent"] = f.StepRe3.Checked
  418. //
  419. param["listisfilter"] = ListFilterReg.MatchString(f.Step2.Expert) //列表页校验是否含“--关键词过滤”
  420. matchLua, msg := LuaTextCheck(f.Step2.Expert, f.Step3.Expert, f.Step2.Types, infoformat, model)
  421. if !matchLua {
  422. issave := spider.SaveSpider(code, param) //保存脚本
  423. if issave {
  424. for k, v := range *one {
  425. if k != "_id" && param[k] == nil {
  426. param[k] = v
  427. }
  428. }
  429. Wlog(f.Base.SpiderName, f.Base.SpiderCode, user, f.GetSession("userid").(string), "修改", param)
  430. rep["msg"] = "保存成功"
  431. } else {
  432. rep["msg"] = "保存失败"
  433. }
  434. } else {
  435. rep["msg"] = "保存失败," + msg
  436. }
  437. rep["code"] = util.Se.Encode2Hex(code)
  438. f.ServeJson(rep)
  439. }
  440. }
  441. }
  442. func (f *Front) SaveChannels() {
  443. code := f.GetString("code")
  444. channels := f.GetString("channels")
  445. channels = strings.ReplaceAll(channels, ",", ",")
  446. arr := strings.Split(channels, ",")
  447. ok := u.MgoEB.Update("luaconfig", map[string]interface{}{"code": code}, map[string]interface{}{
  448. "$addToSet": map[string]interface{}{
  449. "channels": map[string]interface{}{"$each": arr},
  450. },
  451. }, false, false)
  452. f.ServeJson(map[string]interface{}{"ok": ok})
  453. }
  454. func (f *Front) SaveJs() {
  455. //param_type := f.GetString("param_type")
  456. //return_type := f.GetString("return_type")
  457. js_name := f.GetString("js_name")
  458. js_alias := f.GetString("js_alias")
  459. jstext := f.GetString("jstext")
  460. step := f.GetString("step")
  461. code := f.GetString("code")
  462. update := map[string]interface{}{
  463. "runjs": true,
  464. }
  465. js := map[string]interface{}{
  466. "js_text": jstext,
  467. "js_name": js_name,
  468. "js_step": step,
  469. "js_alias": js_alias,
  470. "js_param": "string",
  471. "js_return": "string",
  472. }
  473. if step == "list" {
  474. update["js_list"] = js
  475. } else if step == "detail" {
  476. update["js_detail"] = js
  477. }
  478. u.MgoEB.Update("luaconfig", map[string]interface{}{"code": code}, map[string]interface{}{"$set": update}, false, false)
  479. f.ServeJson(map[string]interface{}{"ok": true})
  480. }
  481. func LuaSaveLog(code, user string, data *map[string]interface{}, stype int) {
  482. saveOne, _ := u.MgoEB.FindOne("luasavelog", map[string]interface{}{"state": 0, "code": code})
  483. if stype == 0 { //保存记录
  484. if len(*saveOne) == 0 && len(*data) > 0 { //重新记录
  485. delete(*data, "_id")
  486. save := map[string]interface{}{
  487. "code": code,
  488. "state": 0,
  489. "saveuser": user,
  490. "comeintime": time.Now().Unix(),
  491. "luaold": data,
  492. }
  493. u.MgoEB.Save("luasavelog", save)
  494. }
  495. } else if stype == 1 { //对比
  496. if len(*saveOne) > 0 {
  497. tmp := (*saveOne)["luaold"].(map[string]interface{})
  498. updateMap := map[string]interface{}{} //记录字段改变值
  499. for k, v := range *data {
  500. if k != "_id" && k != "state" && k != "modifytime" {
  501. if tmpV := tmp[k]; tmpV != nil { //历史记录存在字段
  502. tmpJson, _ := json.Marshal(tmpV)
  503. dataJson, _ := json.Marshal(v)
  504. if string(tmpJson) != string(dataJson) {
  505. updateMap[k] = v
  506. }
  507. delete(tmp, k) //删除对比过的字段
  508. } else { //历史记录不存在字段
  509. updateMap[k] = v
  510. }
  511. }
  512. }
  513. if len(tmp) > 0 {
  514. for k, _ := range tmp { //上架时爬虫较历史爬虫少的字段信息
  515. updateMap[k] = nil
  516. }
  517. }
  518. set := map[string]interface{}{"state": 1, "updatetime": time.Now().Unix(), "updateuser": user}
  519. if len(updateMap) > 0 { //有字段改变
  520. set["luaupdate"] = updateMap
  521. set["lusnew"] = data
  522. }
  523. u.MgoEB.UpdateById("luasavelog", (*saveOne)["_id"], map[string]interface{}{"$set": set})
  524. }
  525. }
  526. }
  527. //爬虫保存时,检查列表页和三级页代码中是否含lua原生方法
  528. func LuaTextCheck(list, detail string, type_list, infoformat int, model map[string]interface{}) (b bool, msg string) {
  529. defer qu.Catch()
  530. if LuaReg.MatchString(list) || LuaReg.MatchString(detail) {
  531. msg = "代码中含有lua原生方法;"
  532. }
  533. if ListFilterReg.MatchString(detail) && !strings.Contains(detail, "delete") { //三级页含过滤但是没有data["delete"]="true"
  534. msg = `三级页缺少data["delete"]="true"`
  535. }
  536. sln_reg := regexp.MustCompile(`sendListNum\(pageno,list\)`)
  537. slnIndexArr := sln_reg.FindAllStringIndex(list, -1)
  538. if type_list != 0 && len(slnIndexArr) == 0 { //列表页专家模式且不含sendListNum
  539. msg = "代码中缺少sendListNum(pageno,list)方法;" + msg
  540. } else if type_list == 1 && len(slnIndexArr) > 0 { //判断sendListNum方法的位置
  541. trim_reg := regexp.MustCompile("trim")
  542. insert_reg := regexp.MustCompile("insert")
  543. trIndexArr := trim_reg.FindAllStringIndex(list, -1)
  544. irIndexArr := insert_reg.FindAllStringIndex(list, -1)
  545. slIndex := slnIndexArr[len(slnIndexArr)-1] //sendListNum位置
  546. trIndex := trIndexArr[len(trIndexArr)-1] //com.trim位置
  547. irIndex := irIndexArr[len(irIndexArr)-1] //insert位置
  548. qu.Debug("sendListNum位置:", trIndex, slIndex, irIndex)
  549. if slIndex[1] < trIndex[0] || slIndex[0] > irIndex[1] { //sendListNum方法必须在com.trim方法后,table.insert方法前
  550. msg = "sendListNum方法位置错误;" + msg
  551. }
  552. }
  553. if type_list == 1 {
  554. area := qu.ObjToString(model["area"])
  555. city := qu.ObjToString(model["city"])
  556. district := qu.ObjToString(model["district"])
  557. if area != "" && !strings.Contains(list, area) {
  558. msg += "省份信息与模板不一致;"
  559. }
  560. if city != "" && !strings.Contains(list, city) {
  561. msg += "城市信息与模板不一致;"
  562. }
  563. if district != "" && !strings.Contains(list, district) {
  564. msg += "区/县信息与模板不一致;"
  565. }
  566. if infoformat == 2 && !strings.Contains(detail, "projectname") {
  567. msg += "拟建/审批数据缺少projectname字段;"
  568. }
  569. }
  570. b = msg != ""
  571. return
  572. }
  573. //方法测试
  574. func (f *Front) RunStep() {
  575. imodal, _ := f.GetInteger("imodal")
  576. script, _ := f.GetBool("script")
  577. listcheck := f.GetString("listcheck")
  578. contentcheck := f.GetString("contentcheck")
  579. downloadnode := f.GetString("downloadnode") //下载节点
  580. common := []interface{}{
  581. f.Base.SpiderCode,
  582. f.Base.SpiderName,
  583. f.Base.SpiderChannel,
  584. f.Base.SpiderDownDetailPage,
  585. f.Base.SpiderStartPage,
  586. f.Base.SpiderMaxPage,
  587. f.Base.SpiderRunRate,
  588. f.Base.Spider2Collection,
  589. f.Base.SpiderPageEncoding,
  590. f.Base.SpiderStoreMode,
  591. f.Base.SpiderStoreToMsgEvent,
  592. f.Base.SpiderTargetChannelUrl,
  593. f.Base.SpiderLastDownloadTime,
  594. f.Base.SpiderIsHistoricalMend,
  595. f.Base.SpiderIsMustDownload,
  596. "",
  597. "",
  598. "",
  599. }
  600. if f.Method() == "POST" {
  601. switch f.GetString("step") {
  602. case "step1": //publishtime
  603. ptime := []interface{}{
  604. f.Step1.DateFormat,
  605. f.Step1.Address,
  606. f.Step1.ContentChooser,
  607. }
  608. if script {
  609. _, scripts := spider.GetLastPublishTime(common, ptime, f.Step1.Expert, downloadnode, imodal, 1)
  610. f.ServeJson(scripts)
  611. return
  612. }
  613. rs, err := spider.GetLastPublishTime(common, ptime, f.Step1.Expert, downloadnode, imodal)
  614. if err == nil {
  615. f.ServeJson(rs)
  616. }
  617. case "step2": //list
  618. addrs := strings.Split(f.Step2.Listadds, "\n")
  619. if len(addrs) > 0 {
  620. for k, v := range addrs {
  621. addrs[k] = "'" + v + "'"
  622. }
  623. f.Step2.Listadds = strings.Join(addrs, ",")
  624. } else if len(f.Step2.Listadds) > 5 {
  625. f.Step2.Listadds = "'" + f.Step2.Listadds + "'"
  626. } else {
  627. f.Step2.Listadds = ""
  628. }
  629. list := []interface{}{
  630. f.Step2.Listadd,
  631. f.Step2.Listadds,
  632. f.Step2.BlockChooser,
  633. f.Step2.AddressChooser,
  634. f.Step2.TitleChooser,
  635. f.Step2.DateChooser,
  636. f.Step2.DateFormat,
  637. }
  638. listcheck = strings.Replace(listcheck, "\n", "\\n", -1)
  639. listcheck = strings.Replace(listcheck, "\"", "\\\"", -1)
  640. s_model := f.GetString("model")
  641. configModel := util.Config.Model[s_model]
  642. model := map[string]interface{}{}
  643. for k, _ := range configModel {
  644. model[k] = f.GetString(k)
  645. }
  646. if script {
  647. _, script := spider.GetPageList(common, list, model, listcheck, f.Step2.Expert, downloadnode, imodal, 1)
  648. f.ServeJson(script)
  649. return
  650. }
  651. rs, err := spider.GetPageList(common, list, model, listcheck, f.Step2.Expert, downloadnode, imodal)
  652. if err == nil {
  653. f.ServeJson(rs)
  654. } else if err.(error).Error() == "no" {
  655. f.ServeJson(rs[0])
  656. }
  657. case "step3": //detail
  658. content := []interface{}{
  659. f.Step3.ContentChooser,
  660. f.Step3.ElementChooser,
  661. }
  662. contentcheck = strings.Replace(contentcheck, "\n", "\\n", -1)
  663. contentcheck = strings.Replace(contentcheck, "\"", "\\\"", -1)
  664. data := map[string]interface{}{}
  665. data["title"] = f.Step3.T_title
  666. data["href"] = f.Step3.T_href
  667. data["publishtime"] = f.Step3.T_date
  668. if script {
  669. _, script := spider.GetContentInfo(common, content, data, contentcheck, f.Step3.Expert, downloadnode, imodal, 1)
  670. f.ServeJson(script)
  671. return
  672. }
  673. rs, err := spider.GetContentInfo(common, content, data, contentcheck, f.Step3.Expert, downloadnode, imodal)
  674. if projectinfo, ok := rs["projectinfo"].(map[string]interface{}); ok && projectinfo != nil {
  675. if attachments, ok := projectinfo["attachments"].(map[string]interface{}); ok && attachments != nil {
  676. for _, tmp := range attachments {
  677. tmpMap := tmp.(map[string]interface{})
  678. if qu.ObjToString(tmpMap["filename"]) == "附件中含有乱码" {
  679. rs["msg"] = "附件中含有乱码"
  680. }
  681. }
  682. }
  683. }
  684. if err == nil {
  685. f.ServeJson(rs)
  686. } else {
  687. f.ServeJson(rs["no"])
  688. }
  689. }
  690. }
  691. }
  692. //爬虫测试数据json
  693. func (f *Front) GetJson() {
  694. code := f.GetString("code")
  695. username := f.GetSession("username").(string)
  696. if tr := TestResultMap[username+code]; tr != nil {
  697. task, _ := u.MgoEB.FindOne("task", map[string]interface{}{"code": code, "i_state": 3})
  698. comeintime := int64(0)
  699. if len(*task) > 0 {
  700. comeintime = qu.Int64All((*task)["l_comeintime"])
  701. tr.task_remark = "审核任务创建时间:" + qu.FormatDateByInt64(&comeintime, qu.Date_Short_Layout) + ";" + tr.task_remark
  702. }
  703. data := tr.dataInfo
  704. result := tr.listInfo
  705. if len(data) > 0 {
  706. data["contenthtml"] = ""
  707. }
  708. num := 0
  709. list_fir := []map[string]interface{}{}
  710. list_sec := []map[string]interface{}{}
  711. for page, list := range result {
  712. for k, v := range list {
  713. v["a_index"] = k + 1
  714. num++
  715. }
  716. if page == 1 {
  717. list_fir = list
  718. } else if page == 2 {
  719. list_sec = list
  720. }
  721. }
  722. f.T["list_fir"] = list_fir
  723. f.T["list_sec"] = list_sec
  724. f.T["data"] = data
  725. f.T["num"] = num
  726. f.T["descript"] = tr.task_descript
  727. f.T["remark"] = tr.task_remark
  728. f.T["rateremark"] = tr.task_rateremark
  729. f.T["reason"] = tr.reason
  730. f.T["msg"] = tr.msg
  731. f.T["comeintime"] = comeintime
  732. delete(TestResultMap, username+code)
  733. }
  734. f.Render("jsonInfo.html", &f.T)
  735. }
  736. var TestResultMap = map[string]*TestResult{} //username+code
  737. //某个爬虫整体测试结果
  738. type TestResult struct {
  739. task_remark string
  740. task_rateremark []string
  741. task_descript string
  742. reason string
  743. msg string
  744. listInfo map[int64][]map[string]interface{}
  745. dataInfo map[string]interface{}
  746. }
  747. //整体测试
  748. func (f *Front) SpiderPass() {
  749. defer mu.Catch()
  750. tr := &TestResult{}
  751. result := map[int64][]map[string]interface{}{}
  752. data := map[string]interface{}{}
  753. msgArr := []string{}
  754. code := f.GetString("code")
  755. downloadnode := f.GetString("node")
  756. //根据code查询待确认任务
  757. query := map[string]interface{}{
  758. "s_code": code,
  759. "i_state": 3,
  760. }
  761. task, _ := u.MgoEB.FindOne("task", query)
  762. descript := "null"
  763. remark := "null"
  764. remarktmp := []string{}
  765. rateremarktmp := []string{}
  766. if len(*task) > 0 {
  767. descript = (*task)["s_descript"].(string)
  768. if mrecord, ok := (*task)["a_mrecord"].([]interface{}); ok {
  769. for _, m := range mrecord {
  770. remarkInfo := m.(map[string]interface{})
  771. if remark := qu.ObjToString(remarkInfo["s_mrecord_remark"]); remark != "" {
  772. remarktmp = append(remarktmp, remark+";")
  773. }
  774. if rateremark := qu.ObjToString(remarkInfo["s_mrecord_rateremark"]); rateremark != "" {
  775. rateremarktmp = append(rateremarktmp, rateremark+";")
  776. }
  777. }
  778. }
  779. }
  780. if len(remarktmp) > 0 {
  781. remark = ""
  782. remark = strings.Join(remarktmp, "")
  783. }
  784. tr.task_remark = remark
  785. tr.task_rateremark = rateremarktmp
  786. tr.task_descript = descript
  787. //基本信息、方法一(发布时间)、方法二(列表页)、方法三(详情页)、总请求次数、go方法一、go方法二、go方法三、列表页条数
  788. steps := []interface{}{false, false, false, false, 0, 0, 0, 0, 0}
  789. one, _ := u.MgoEB.FindOne("luaconfig", map[string]interface{}{"code": code})
  790. reason, _ := (*one)["reason"].(string)
  791. tr.reason = reason
  792. if len(*one) > 0 && (*one)["oldlua"] == nil {
  793. common := (*one)["param_common"].([]interface{})
  794. if len(common) < 13 {
  795. f.ServeJson(steps)
  796. return
  797. } else {
  798. steps[0] = true
  799. }
  800. } else {
  801. steps[0] = true
  802. }
  803. script, liststr, contentstr := "", "", ""
  804. if (*one)["oldlua"] == nil {
  805. script, liststr, contentstr = spider.GetScript(code)
  806. } else {
  807. script = (*one)["luacontent"].(string)
  808. }
  809. if liststr != "" && contentstr != "" {
  810. msgArr = u.SpiderPassCheckLua(liststr, contentstr, (*one)) //校验
  811. }
  812. s := spider.CreateSpider(downloadnode, script)
  813. s.SpiderMaxPage = 2 //采集列表页总页数
  814. s.Timeout = 60
  815. timestr, timeerr := s.GetLastPublishTime()
  816. if timeerr == nil && len(timestr) > 4 {
  817. steps[1] = true //发布时间获取成功
  818. downloadNum := 0
  819. result, downloadNum, _ = s.DownListPageItem() //列表页采集结果
  820. if downloadNum == 0 {
  821. f.ServeJson(steps)
  822. return
  823. }
  824. steps[2] = true //列表页获取成功
  825. steps[8] = downloadNum //下载量
  826. tr.listInfo = result
  827. if s.DownDetail {
  828. onePageList := result[1] //第一页数据
  829. if onePageDataNum := len(onePageList); onePageDataNum > 0 {
  830. index := onePageDataNum / 2 //取一条数据下载三级页
  831. param := map[string]string{}
  832. for k, v := range onePageList[index] {
  833. param[k] = qu.ObjToString(v)
  834. }
  835. data = map[string]interface{}{}
  836. s.DownloadDetailPage(param, data)
  837. tr.dataInfo = data
  838. if len(data) == 0 || qu.ObjToString(data["detail"]) == "" {
  839. steps[3] = false //详情页获取失败
  840. } else {
  841. steps[3] = true //详情页获取成功
  842. }
  843. }
  844. } else {
  845. steps[3] = true //详情页获取成功
  846. }
  847. //list, _ = s.DownListPageItem()
  848. //for _, l := range list {
  849. // if publishtime := qu.ObjToString(l["publishtime"]); publishtime == "0" || publishtime == "" {
  850. // msgArr = append(msgArr, "列表页publishtime取值异常")
  851. // break
  852. // } else {
  853. // t, err := time.ParseInLocation(qu.Date_Full_Layout, publishtime, time.Local)
  854. // if err != nil || t.Unix() <= 0 {
  855. // msgArr = append(msgArr, "列表页publishtime取值异常")
  856. // break
  857. // }
  858. // }
  859. //}
  860. //if len(list) > 0 {
  861. // tr.listInfo = list
  862. // listone := list[0]
  863. // if len(qu.ObjToString(listone["href"])) < 7 ||
  864. // (qu.ObjToString(listone["publishtime"]) != "0" && len(qu.ObjToString(listone["publishtime"])) < 5) ||
  865. // len(qu.ObjToString(listone["title"])) < 3 {
  866. // f.ServeJson(steps)
  867. // return
  868. // } else {
  869. // steps[2] = true
  870. // if s.DownDetail {
  871. // param := map[string]string{}
  872. // index := 0
  873. // if len(list) > 0 {
  874. // steps[8] = len(list)
  875. // index = len(list) / 2
  876. // for k, v := range list[index] {
  877. // param[k] = qu.ObjToString(v)
  878. // }
  879. // data = map[string]interface{}{}
  880. // s.DownloadDetailPage(param, data)
  881. // if len(data) > 0 {
  882. // tr.dataInfo = data
  883. // }
  884. // if len(data) == 0 || data["detail"].(string) == "" {
  885. // steps[3] = false
  886. // } else {
  887. // steps[3] = true
  888. // }
  889. // }
  890. // } else {
  891. // steps[3] = true
  892. // }
  893. // }
  894. //}
  895. }
  896. //关闭laustate
  897. s.L.Close()
  898. steps[4] = s.Test_luareqcount
  899. steps[5] = s.Test_goreqtime
  900. steps[6] = s.Test_goreqlist
  901. steps[7] = s.Test_goreqcon
  902. //校验
  903. msg := u.SpiderPassCheckListAndDetail(result, data)
  904. msgArr = append(msgArr, msg...)
  905. username := f.GetSession("username").(string)
  906. tr.msg = strings.Join(msgArr, ";")
  907. TestResultMap[username+code] = tr
  908. f.ServeJson(steps)
  909. }
  910. func (f *Front) DownSpider(code string) {
  911. auth := qu.IntAll(f.GetSession("auth"))
  912. user := f.GetSession("loginuser")
  913. success := false
  914. script := ""
  915. if auth > u.Role_Dev {
  916. success = true
  917. //one, _ := u.MgoE.FindOne("luaconfig", map[string]interface{}{"code": code})
  918. one, _ := u.MgoEB.FindOne("luaconfig", map[string]interface{}{"code": code})
  919. createuserid := qu.ObjToString((*one)["createuserid"])
  920. filename := code + ".lua"
  921. if len(*one) > 0 {
  922. if (*one)["oldlua"] != nil {
  923. if (*one)["luacontent"] != nil {
  924. script = (*one)["luacontent"].(string)
  925. }
  926. } else {
  927. user, _ := u.MgoEB.FindById("user", createuserid, nil)
  928. name := (*one)["createuser"]
  929. email := (*user)["s_email"]
  930. upload := time.Now().Format("2006-01-02 15:04:05")
  931. script, _, _ = spider.GetScript(code, name, email, upload)
  932. }
  933. }
  934. f.ResponseWriter.Header().Del("Content-Type")
  935. f.ResponseWriter.Header().Add("Content-Type", "application/x-download")
  936. f.ResponseWriter.Header().Add("Content-Disposition", "attachment;filename=spider_"+filename)
  937. f.WriteBytes([]byte(script))
  938. } else {
  939. f.Write("您没有权限")
  940. }
  941. //记录日志
  942. downlogs := map[string]interface{}{
  943. "code": code,
  944. "user": user,
  945. "auth": auth,
  946. "time": time.Now().Unix(),
  947. "success": success,
  948. "script": script,
  949. }
  950. u.MgoEB.Save("luadownlogs", downlogs)
  951. }
  952. //下架删除心跳
  953. func DelSpiderHeart(code string) bool {
  954. return u.MgoS.Update("spider_heart", map[string]interface{}{"code": code}, map[string]interface{}{"$set": map[string]interface{}{"del": true}}, false, true)
  955. }
  956. //下架删除download数据
  957. //func delDownloadData(code string) bool {
  958. // return mgu.Del("download", "spider", "spider", `{"code":"`+code+`"}`)
  959. //}
  960. //批量作废删除download数据
  961. //func disableDelDownloadData(code []string) {
  962. // for _, v := range code {
  963. // flag := delDownloadData(v)
  964. // log.Println(code, "---批量删除download数据:", flag)
  965. // }
  966. //}
  967. //爬虫核对
  968. func (f *Front) Checktime() {
  969. code := f.GetString("code")
  970. auth := qu.IntAll(f.GetSession("auth"))
  971. if auth != u.Role_Admin {
  972. f.ServeJson(false)
  973. } else {
  974. //b := u.MgoE.Update("luaconfig", map[string]interface{}{"code": code}, map[string]interface{}{"$set": map[string]interface{}{
  975. // "l_checktime": time.Now().Unix(),
  976. //}}, true, false)
  977. b := u.MgoEB.Update("luaconfig", map[string]interface{}{"code": code}, map[string]interface{}{"$set": map[string]interface{}{
  978. "l_checktime": time.Now().Unix(),
  979. }}, true, false)
  980. f.ServeJson(b)
  981. }
  982. }
  983. //批量作废
  984. func (f *Front) Disables() error {
  985. auth := qu.IntAll(f.GetSession("auth"))
  986. names := strings.Split(f.GetString("names"), ",")
  987. ids := strings.Split(f.GetString("ids"), ",")
  988. codes := strings.Split(f.GetString("codes"), ",")
  989. disablereason := f.GetString("disablereason")
  990. res := ""
  991. if IsHasUpState(auth, Sp_state_4) {
  992. for k, id := range ids {
  993. b, err := UpStateAndUpSpider("", id, disablereason, "", Sp_state_4)
  994. if b { //作废成功
  995. //修改任务状态
  996. UpTaskState(codes, 4, "", int64(0))
  997. //删除download表数据
  998. //go disableDelDownloadData(codes)
  999. if err != nil {
  1000. res = res + names[k] + ",ok" + qu.ObjToString(err.Error()) + ";"
  1001. } else {
  1002. res = res + names[k] + ",ok" + ";"
  1003. }
  1004. } else {
  1005. res = res + names[k] + "," + qu.ObjToString(err.Error()) + ";"
  1006. }
  1007. }
  1008. } else {
  1009. res = "没有权限"
  1010. }
  1011. f.ServeJson(res)
  1012. return nil
  1013. }
  1014. //批量上下架
  1015. func (f *Front) BatchShelves() {
  1016. codes := strings.Split(f.GetString("codes"), ",")
  1017. state, _ := f.GetInteger("state")
  1018. auth := qu.IntAll(f.GetSession("auth"))
  1019. errCode := []string{}
  1020. var err error
  1021. b := false
  1022. if IsHasUpState(auth, Sp_state_5) {
  1023. if state == 5 { //批量上架
  1024. for _, code := range codes {
  1025. _, err = UpStateAndUpSpider(code, "", "", "", Sp_state_5)
  1026. if err != nil {
  1027. errCode = append(errCode, code)
  1028. }
  1029. }
  1030. } else { //批量下架
  1031. for _, code := range codes {
  1032. b, err = UpStateAndUpSpider(code, "", "", "", Sp_state_6)
  1033. if !b || err != nil {
  1034. errCode = append(errCode, code)
  1035. }
  1036. //下架删除download数据
  1037. //if b {
  1038. // flag := delDownloadData(code)
  1039. // log.Println(code, "---删除download数据:", flag)
  1040. //}
  1041. }
  1042. }
  1043. } else {
  1044. errCode = append(errCode, "没有权限")
  1045. }
  1046. f.ServeJson(errCode)
  1047. }
  1048. //更新爬虫状态
  1049. func (f *Front) UpState() error {
  1050. username := f.GetSession("username").(string)
  1051. code := f.GetString("code")
  1052. state, _ := f.GetInt("state")
  1053. id := f.GetString("taskId")
  1054. reason := f.GetString("reason")
  1055. auth := qu.IntAll(f.GetSession("auth"))
  1056. var codeArr = []string{code}
  1057. var taskid []string
  1058. //修改任务状态
  1059. istotask := false
  1060. res := map[string]interface{}{
  1061. "istotask": istotask,
  1062. "err": "没有权限",
  1063. "code": util.Se.Encode2Hex(code),
  1064. "taskid": taskid,
  1065. }
  1066. var xgTime int64
  1067. if f.GetSession(id) == nil || f.GetSession(id) == "" {
  1068. xgTime = time.Now().Unix()
  1069. } else {
  1070. xgTimeStr := qu.ObjToString(f.GetSession(id))
  1071. xgTimeTmp, _ := time.ParseInLocation("2006-01-02 15:04:05", xgTimeStr, time.Local)
  1072. xgTime = xgTimeTmp.Unix()
  1073. }
  1074. f.DelSession(id)
  1075. if IsHasUpState(auth, int(state)) {
  1076. b, err := UpStateAndUpSpider(code, "", reason, username, int(state)) //更新爬虫状态
  1077. if b && state == Sp_state_1 { //提交审核
  1078. //有对应任务跳转提交记录页
  1079. taskid = checkTask(codeArr, 1)
  1080. if len(taskid) > 0 {
  1081. res["istotask"] = true
  1082. res["taskid"] = taskid[0]
  1083. }
  1084. } else if b && state == Sp_state_2 { //打回
  1085. taskid = checkTask(codeArr, 2)
  1086. if len(taskid) > 0 {
  1087. //UpTaskState([]string{taskid}, 2) //修改状态
  1088. UpTaskState(taskid, 2, "", int64(0)) //修改任务状态
  1089. SaveRemark(taskid, reason, username) //保存记录信息
  1090. }
  1091. } else if b && state == Sp_state_3 { //审核通过
  1092. taskid = checkTask(codeArr, 3)
  1093. if len(taskid) > 0 {
  1094. //UpTaskState([]string{taskid}, 3)
  1095. UpTaskState(taskid, 3, "", int64(0))
  1096. SaveRemark(taskid, "", username)
  1097. }
  1098. } else if b && state == Sp_state_6 { //下架
  1099. //下架成功删除心跳数据
  1100. flag := DelSpiderHeart(code)
  1101. log.Println(code, "---下架删除download数据:", flag)
  1102. } else if b && state == Sp_state_7 { //反馈
  1103. taskid = checkTask(codeArr, 7)
  1104. if len(taskid) > 0 {
  1105. UpTaskState(taskid, 7, reason, xgTime)
  1106. }
  1107. }
  1108. if err != nil {
  1109. res["err"] = err.Error()
  1110. f.ServeJson(res)
  1111. } else {
  1112. res["err"] = ""
  1113. f.ServeJson(res)
  1114. }
  1115. } else {
  1116. f.ServeJson(res)
  1117. }
  1118. return nil
  1119. }
  1120. func (f *Front) Assort() {
  1121. state, _ := f.GetInteger("state")
  1122. code := f.GetString("code")
  1123. codes := u.SymbolReg.Split(code, -1)
  1124. success := true
  1125. msg := ""
  1126. for _, code := range codes {
  1127. query := map[string]interface{}{
  1128. "code": code,
  1129. }
  1130. //下架爬虫
  1131. //lua, _ := u.MgoE.FindOne("luaconfig", query)
  1132. lua, _ := u.MgoEB.FindOne("luaconfig", query)
  1133. upresult, err := spider.UpdateSpiderByCodeState(code, "6", qu.IntAll((*lua)["event"]))
  1134. qu.Debug("下架爬虫:", code, upresult, err)
  1135. if upresult && err == nil {
  1136. //更新爬虫状态
  1137. update := map[string]interface{}{
  1138. "$set": map[string]interface{}{
  1139. "state": state,
  1140. //"modifytime": time.Now().Unix(),
  1141. "l_uploadtime": time.Now().Unix(),
  1142. },
  1143. }
  1144. //u.MgoE.Update("luaconfig", query, update, false, false)
  1145. u.MgoEB.Update("luaconfig", query, update, false, false)
  1146. //关闭任务
  1147. query = map[string]interface{}{
  1148. "s_code": code,
  1149. }
  1150. update = map[string]interface{}{
  1151. "$set": map[string]interface{}{
  1152. "i_state": 6,
  1153. },
  1154. }
  1155. u.MgoEB.Update("task", query, update, false, true)
  1156. //删除心跳
  1157. DelSpiderHeart(code)
  1158. } else {
  1159. success = false
  1160. msg += code + ";"
  1161. }
  1162. }
  1163. f.ServeJson(map[string]interface{}{"success": success, "msg": msg})
  1164. }
  1165. //更新爬虫状态,并判断是否更新节点爬虫
  1166. func UpStateAndUpSpider(code, id, reason, username string, state int) (bool, error) {
  1167. upresult := false
  1168. var err error
  1169. one := &map[string]interface{}{}
  1170. if code != "" {
  1171. //one, _ = u.MgoE.FindOne("luaconfig", map[string]interface{}{"code": code})
  1172. one, _ = u.MgoEB.FindOne("luaconfig", map[string]interface{}{"code": code})
  1173. } else {
  1174. //one, _ = u.MgoE.FindById("luaconfig", id, nil)
  1175. one, _ = u.MgoEB.FindById("luaconfig", id, nil)
  1176. code = qu.ObjToString((*one)["code"])
  1177. }
  1178. if len(*one) > 0 {
  1179. var event int
  1180. unset := map[string]interface{}{}
  1181. if (*one)["event"] != nil {
  1182. event = qu.IntAll((*one)["event"])
  1183. } else {
  1184. for k, _ := range util.Config.Uploadevents { //?
  1185. event = qu.IntAll(k)
  1186. break
  1187. }
  1188. //r := rand.New(rand.NewSource(time.Now().UnixNano()))
  1189. //event = util.Config.Uploadevents[r.Intn(len(util.Config.Uploadevents))]
  1190. }
  1191. //oldstate := qu.IntAll(one["state"])
  1192. switch state {
  1193. case Sp_state_4, Sp_state_6: //作废、下架
  1194. // if oldstate == Sp_state_5 {
  1195. // upresult = false
  1196. // err = errors.New("已上架不允许作废")
  1197. // } else {
  1198. // upresult = true
  1199. // }
  1200. upresult, err = spider.UpdateSpiderByCodeState(code, fmt.Sprint(state), event) //下架
  1201. qu.Debug("下架:", upresult, code)
  1202. case Sp_state_5: //上架(爬虫端在更新上架的时候为了更新内存中字段,采用先下架上架)
  1203. if downevent := qu.IntAll((*one)["downevent"]); downevent != 0 { //爬虫开发修改爬虫节点,审核人员上架爬虫时,原来爬虫所在节点下架
  1204. upresult, err = spider.UpdateSpiderByCodeState(code, "6", downevent)
  1205. if upresult && err == nil {
  1206. unset = map[string]interface{}{"downevent": ""}
  1207. }
  1208. }
  1209. upresult, err = spider.UpdateSpiderByCodeState(code, "6", event)
  1210. qu.Debug("下架:", upresult, code)
  1211. if upresult && err == nil {
  1212. upresult, err = spider.UpdateSpiderByCodeState(code, fmt.Sprint(state), event)
  1213. qu.Debug("上架:", upresult, code)
  1214. }
  1215. default:
  1216. upresult = true
  1217. err = nil
  1218. }
  1219. if err != nil && strings.Contains(err.Error(), "timeout") {
  1220. err = errors.New("连接节点" + fmt.Sprint(event) + "超时")
  1221. upresult = true
  1222. }
  1223. if upresult && err == nil {
  1224. upset := map[string]interface{}{"state": state} //修改状态
  1225. if (*one)["oldlua"] != nil { //老脚本上传
  1226. //upresult = u.MgoE.Update("luaconfig", map[string]interface{}{"code": code}, map[string]interface{}{"$set": upset}, true, false)
  1227. up := map[string]interface{}{
  1228. "$set": upset,
  1229. }
  1230. if len(unset) > 0 {
  1231. up["$unset"] = unset
  1232. }
  1233. upresult = u.MgoEB.Update("luaconfig", map[string]interface{}{"code": code}, up, true, false)
  1234. } else {
  1235. if state == Sp_state_1 { //提交审核
  1236. upset["l_complete"] = time.Now().Unix()
  1237. upset["report"] = ""
  1238. } else if state == Sp_state_7 { //反馈问题
  1239. upset["report"] = reason
  1240. upset["state"] = 1 //反馈后爬虫改为待审核
  1241. upset["l_complete"] = time.Now().Unix()
  1242. } else if state == Sp_state_3 { //审核通过
  1243. if (*one)["event"] == nil {
  1244. upset["event"] = event
  1245. //upset["modifytime"] = time.Now().Unix()
  1246. }
  1247. upset["frequencyerrtimes"] = 0 //爬虫审核通过,重置采集频率异常次数
  1248. upset["l_uploadtime"] = time.Now().Unix()
  1249. } else if state == Sp_state_2 { //打回原因
  1250. upset["reason"] = reason
  1251. } else if state == Sp_state_5 { //上架,核对时间重置
  1252. upset["l_checktime"] = 0
  1253. LuaSaveLog(code, username, one, 1)
  1254. } else if state == Sp_state_4 { //作废,作废原因
  1255. upset["disablereason"] = reason
  1256. //upset["modifytime"] = time.Now().Unix()
  1257. upset["l_uploadtime"] = time.Now().Unix() //l_complete爬虫完成时间
  1258. }
  1259. up := map[string]interface{}{
  1260. "$set": upset,
  1261. }
  1262. if len(unset) > 0 {
  1263. up["$unset"] = unset
  1264. }
  1265. //upresult = u.MgoE.Update("luaconfig", map[string]interface{}{"code": code}, map[string]interface{}{"$set": upset}, false, false)
  1266. upresult = u.MgoEB.Update("luaconfig", map[string]interface{}{"code": code}, up, false, false)
  1267. qu.Debug("提交日志:", code, upset, upresult)
  1268. if upresult && (state == Sp_state_2 || state == Sp_state_3) { //打回、审核记录日志
  1269. types := "打回"
  1270. if state == Sp_state_3 {
  1271. types = "审核"
  1272. }
  1273. event := qu.IntAll((*one)["event"])
  1274. obj := map[string]interface{}{
  1275. "code": code,
  1276. "auditor": username,
  1277. "types": types,
  1278. "comeintime": time.Now().Unix(),
  1279. "reason": reason,
  1280. "spideruser": (*one)["createuser"],
  1281. "modifytime": (*one)["modifytime"],
  1282. "event": event,
  1283. "site": (*one)["site"],
  1284. "channel": (*one)["channel"],
  1285. }
  1286. if !strings.HasSuffix(code, u.Bu) { //凡是以_bu结尾的爬虫一律不计入审核记录
  1287. //新爬虫审核记录表
  1288. if event == 7000 && (state == Sp_state_3 || state == Sp_state_2) {
  1289. count := u.MgoEB.Count("lua_logs_auditor", map[string]interface{}{"code": code, "types": "审核"})
  1290. if count == 0 { //新爬虫审核记录
  1291. u.MgoEB.Save("lua_logs_auditor_new", obj)
  1292. }
  1293. }
  1294. u.MgoEB.Save("lua_logs_auditor", obj) //历史维护爬虫审核记录
  1295. }
  1296. }
  1297. }
  1298. }
  1299. }
  1300. return upresult, err
  1301. }
  1302. //保存记录信息
  1303. func SaveRemark(taskid []string, reason, username string) {
  1304. timeNow := time.Now().Unix()
  1305. if reason == "" {
  1306. reason = "审核通过"
  1307. }
  1308. for _, id := range taskid {
  1309. task, _ := u.MgoEB.FindById("task", id, nil)
  1310. if task != nil && len(*task) > 0 {
  1311. checkData := (*task)["a_check"]
  1312. var checkArr []map[string]interface{}
  1313. newData := make(map[string]interface{})
  1314. newData["s_check_checkUser"] = username
  1315. newData["l_check_checkTime"] = timeNow
  1316. newData["s_check_checkRemark"] = reason
  1317. if checkData != nil {
  1318. myArr := qu.ObjArrToMapArr(checkData.([]interface{}))
  1319. if myArr != nil && len(myArr) > 0 {
  1320. for _, v := range myArr {
  1321. checkArr = append(checkArr, v)
  1322. }
  1323. }
  1324. }
  1325. checkArr = append(checkArr, newData)
  1326. (*task)["a_check"] = checkArr
  1327. u.MgoEB.UpdateById("task", id, map[string]interface{}{"$set": &task})
  1328. }
  1329. }
  1330. }
  1331. //修改任务状态
  1332. func UpTaskState(code []string, num int, reason string, startTime int64) {
  1333. query := map[string]interface{}{}
  1334. update := map[string]interface{}{}
  1335. for _, v := range code {
  1336. if num == 1 || num == 2 || num == 3 || num == 7 { //id
  1337. query = map[string]interface{}{
  1338. "_id": mongodb.StringTOBsonId(v),
  1339. }
  1340. } else {
  1341. query = map[string]interface{}{ //code
  1342. "s_code": v,
  1343. }
  1344. }
  1345. if num == 1 { //提交审核
  1346. update = map[string]interface{}{
  1347. "$set": map[string]interface{}{
  1348. "i_state": 3,
  1349. },
  1350. }
  1351. } else if num == 2 { //打回 -->未通过
  1352. update = map[string]interface{}{
  1353. "$set": map[string]interface{}{
  1354. "i_state": 5,
  1355. "l_updatetime": time.Now().Unix(),
  1356. },
  1357. }
  1358. } else if num == 3 { //发布(审核通过) -->审核通过
  1359. update = map[string]interface{}{
  1360. "$set": map[string]interface{}{
  1361. "i_state": 4,
  1362. "l_updatetime": time.Now().Unix(),
  1363. "l_uploadtime": time.Now().Unix(),
  1364. },
  1365. }
  1366. } else if num == 4 { //批量作废 -->关闭
  1367. update = map[string]interface{}{
  1368. "$set": map[string]interface{}{
  1369. "i_state": 6,
  1370. "l_complete": time.Now().Unix(),
  1371. "l_updatetime": time.Now().Unix(),
  1372. },
  1373. }
  1374. } else if num == 7 { //反馈信息 -->待审核
  1375. newData := map[string]interface{}{
  1376. "l_mrecord_comeintime": startTime,
  1377. "l_mrecord_complete": time.Now().Unix(),
  1378. "s_mrecord_remark": reason,
  1379. }
  1380. mrecord := []interface{}{}
  1381. mrecord = append(mrecord, newData)
  1382. update = map[string]interface{}{
  1383. "$set": map[string]interface{}{
  1384. "i_state": 3,
  1385. "l_complete": time.Now().Unix(),
  1386. "a_mrecord": mrecord,
  1387. "l_updatetime": time.Now().Unix(),
  1388. },
  1389. }
  1390. }
  1391. flag := u.MgoEB.Update("task", query, update, false, true)
  1392. log.Println("codeOrId:", query, " 修改任务状态:", flag)
  1393. }
  1394. }
  1395. //更新节点
  1396. func (f *Front) ChangeEvent() {
  1397. auth := qu.IntAll(f.GetSession("auth"))
  1398. if auth != u.Role_Admin {
  1399. f.ServeJson("没有权限")
  1400. }
  1401. code := f.GetString("code")
  1402. event, _ := f.GetInt("event")
  1403. eventok := false
  1404. for k, _ := range util.Config.Uploadevents {
  1405. if event == qu.Int64All(k) {
  1406. eventok = true
  1407. break
  1408. }
  1409. }
  1410. if !eventok {
  1411. f.ServeJson("没有对应节点")
  1412. return
  1413. }
  1414. //info, _ := u.MgoE.FindOne("luaconfig", map[string]interface{}{"code": code})
  1415. info, _ := u.MgoEB.FindOne("luaconfig", map[string]interface{}{"code": code})
  1416. if len(*info) > 0 {
  1417. oldevent := qu.IntAll((*info)["event"])
  1418. if qu.IntAll((*info)["state"]) == Sp_state_5 {
  1419. //源节点下架
  1420. _, err := spider.UpdateSpiderByCodeState(code, fmt.Sprint(Sp_state_6), oldevent)
  1421. set := map[string]interface{}{
  1422. "$set": map[string]interface{}{
  1423. "event": qu.IntAll(event),
  1424. "state": Sp_state_6,
  1425. },
  1426. }
  1427. //u.MgoE.Update("luaconfig", map[string]interface{}{"code": code}, set, true, false)
  1428. u.MgoEB.Update("luaconfig", map[string]interface{}{"code": code}, set, true, false)
  1429. if err != nil && strings.Contains(err.Error(), "timeout") {
  1430. f.ServeJson("连接节点" + fmt.Sprint(oldevent) + "超时")
  1431. } else {
  1432. f.ServeJson(err.Error())
  1433. }
  1434. } else {
  1435. set := map[string]interface{}{
  1436. "$set": map[string]interface{}{
  1437. "event": qu.IntAll(event),
  1438. },
  1439. }
  1440. //u.MgoE.Update("luaconfig", map[string]interface{}{"code": code}, set, true, false)
  1441. u.MgoEB.Update("luaconfig", map[string]interface{}{"code": code}, set, true, false)
  1442. }
  1443. } else {
  1444. f.ServeJson("没有对应记录")
  1445. }
  1446. }
  1447. //验证用户是否有更改状态权限
  1448. func IsHasUpState(auth, state int) bool {
  1449. rep := false
  1450. switch auth {
  1451. case u.Role_Dev:
  1452. if state == Sp_state_1 || state == Sp_state_7 {
  1453. rep = true
  1454. }
  1455. case u.Role_Examine:
  1456. if state == Sp_state_2 || state == Sp_state_3 {
  1457. rep = true
  1458. }
  1459. case u.Role_Admin:
  1460. rep = true
  1461. default:
  1462. }
  1463. return rep
  1464. }
  1465. var list_fields = `{"_id":1,"code":1,"createuser":1,"modifyuser":1,"modifytime":1,"l_uploadtime":1,"l_checktime":1,"state":1,"param_common":1,"event":1,"urgency":1,"platform":1,"pendstate":1}`
  1466. //脚本管理,结合爬虫运行信息
  1467. func (f *Front) LuaList() {
  1468. auth := qu.IntAll(f.GetSession("auth"))
  1469. if auth != u.Role_Admin {
  1470. f.ServeJson("没有权限!")
  1471. return
  1472. }
  1473. if f.Method() == "POST" {
  1474. state, _ := f.GetInteger("state")
  1475. event, _ := f.GetInteger("event")
  1476. start, _ := f.GetInteger("start")
  1477. limit, _ := f.GetInteger("length")
  1478. draw, _ := f.GetInteger("draw")
  1479. searchStr := f.GetString("search[value]")
  1480. //search := strings.Replace(searchStr, " ", "", -1)
  1481. search := strings.TrimSpace(searchStr)
  1482. platform := f.GetString("platform")
  1483. query := map[string]interface{}{}
  1484. queryArr := []interface{}{}
  1485. //搜索条件
  1486. if search != "" {
  1487. q1 := map[string]interface{}{}
  1488. q1["$or"] = []interface{}{
  1489. map[string]interface{}{"code": map[string]interface{}{"$regex": search}},
  1490. map[string]interface{}{"createuser": map[string]interface{}{"$regex": search}},
  1491. map[string]interface{}{"param_common.1": map[string]interface{}{"$regex": search}},
  1492. }
  1493. queryArr = append(queryArr, q1)
  1494. }
  1495. //爬虫状态
  1496. q2 := map[string]interface{}{}
  1497. if state > -1 {
  1498. q2 = map[string]interface{}{"state": state}
  1499. } else {
  1500. q2 = map[string]interface{}{
  1501. "state": map[string]interface{}{
  1502. "$in": []int{Sp_state_3, Sp_state_5, Sp_state_6},
  1503. },
  1504. }
  1505. }
  1506. queryArr = append(queryArr, q2)
  1507. //爬虫节点
  1508. q3 := map[string]interface{}{}
  1509. if event > -1 {
  1510. q3 = map[string]interface{}{"event": event}
  1511. queryArr = append(queryArr, q3)
  1512. }
  1513. //爬虫平台
  1514. q4 := map[string]interface{}{}
  1515. if platform != "-1" {
  1516. q4 = map[string]interface{}{"platform": platform}
  1517. queryArr = append(queryArr, q4)
  1518. }
  1519. query["$and"] = queryArr
  1520. sort := `{"%s":%d}`
  1521. orderIndex := f.GetString("order[0][column]")
  1522. orderName := f.GetString(fmt.Sprintf("columns[%s][data]", orderIndex))
  1523. orderType := 1
  1524. if f.GetString("order[0][dir]") != "asc" {
  1525. orderType = -1
  1526. }
  1527. sort = fmt.Sprintf(sort, orderName, orderType)
  1528. page := start / 10
  1529. //luas, _ := u.MgoE.Find("luaconfig", query, sort, list_fields, false, start, limit)
  1530. //count := u.MgoE.Count("luaconfig", query)
  1531. luas, _ := u.MgoEB.Find("luaconfig", query, sort, list_fields, false, start, limit)
  1532. count := u.MgoEB.Count("luaconfig", query)
  1533. qu.Debug("query:", query, start, limit, count, len(*luas))
  1534. for k, v := range *luas {
  1535. v["num"] = k + 1 + page*10
  1536. l_uploadtime := qu.Int64All(v["l_uploadtime"])
  1537. v["l_uploadtime"] = qu.FormatDateByInt64(&l_uploadtime, qu.Date_Full_Layout)
  1538. l_checktime := qu.Int64All(v["l_checktime"])
  1539. v["l_checktime"] = qu.FormatDateByInt64(&l_checktime, qu.Date_Full_Layout)
  1540. if l_checktime > 0 { //核对
  1541. v["is_check"] = true
  1542. } else { //未核对
  1543. v["is_check"] = false
  1544. }
  1545. if tmp, ok := spinfos.Load(v["code"]); ok {
  1546. info := tmp.(*spinfo)
  1547. v["modifytime"] = info.lastHeartbeat
  1548. v["yesterday"] = fmt.Sprint(info.yesterdayDowncount) + "/" + fmt.Sprint(info.yestoDayRequestNum)
  1549. v["terday"] = fmt.Sprint(info.todayDowncount) + "/" + fmt.Sprint(info.toDayRequestNum)
  1550. v["lastdowncount"] = info.lastDowncount
  1551. v["lstate"] = info.lstate
  1552. } else {
  1553. v["modifytime"] = ""
  1554. v["yesterday"] = ""
  1555. v["terday"] = ""
  1556. v["lastdowncount"] = 0
  1557. v["lstate"] = ""
  1558. }
  1559. }
  1560. f.ServeJson(map[string]interface{}{"draw": draw, "data": luas, "recordsFiltered": count, "recordsTotal": count})
  1561. } else {
  1562. events := []string{}
  1563. for k, _ := range util.Config.Uploadevents {
  1564. events = append(events, k)
  1565. }
  1566. sort.Strings(events)
  1567. f.T["events"] = events
  1568. f.Render("lualist.html", &f.T)
  1569. }
  1570. }
  1571. //心跳监控
  1572. func (f *Front) Heart() {
  1573. if f.Method() == "POST" {
  1574. event, _ := f.GetInteger("event")
  1575. start, _ := f.GetInteger("start")
  1576. limit, _ := f.GetInteger("length")
  1577. draw, _ := f.GetInteger("draw")
  1578. searchStr := f.GetString("search[value]")
  1579. search := strings.TrimSpace(searchStr) //只能搜索code
  1580. //qu.Debug("search:", search, "start:", start, "limit:", limit, "draw:", draw)
  1581. query := map[string]interface{}{
  1582. "del": false,
  1583. }
  1584. if event > -1 {
  1585. query["event"] = event
  1586. }
  1587. if search != "" {
  1588. query["code"] = search
  1589. }
  1590. sort := `{"%s":%d}`
  1591. orderIndex := f.GetString("order[0][column]")
  1592. orderName := f.GetString(fmt.Sprintf("columns[%s][data]", orderIndex))
  1593. orderType := 1
  1594. if f.GetString("order[0][dir]") != "asc" {
  1595. orderType = -1
  1596. }
  1597. sort = fmt.Sprintf(sort, orderName, orderType)
  1598. qu.Debug("query:", query, "sort:", sort)
  1599. list, _ := u.MgoS.Find("spider_heart", query, sort, nil, false, start, limit)
  1600. count := u.MgoS.Count("spider_heart", query)
  1601. for _, l := range *list {
  1602. code := qu.ObjToString(l["code"])
  1603. qu.Debug(code)
  1604. //d, _ := u.MgoE.FindOneByField("luaconfig", map[string]interface{}{"code": code}, map[string]interface{}{"state": 1, "param_common": 1, "str_list": 1, "type_list": 1})
  1605. d, _ := u.MgoEB.FindOneByField("luaconfig", map[string]interface{}{"code": code}, map[string]interface{}{"state": 1, "param_common": 1, "str_list": 1, "type_list": 1, "pendtime": 1})
  1606. l["state"] = (*d)["state"]
  1607. l["param_common"] = (*d)["param_common"]
  1608. if lt := qu.Int64All(l["list"]); lt != 0 {
  1609. l["list"] = qu.FormatDateByInt64(&lt, qu.Date_Full_Layout)
  1610. } else {
  1611. l["list"] = 0
  1612. }
  1613. if dt := qu.Int64All(l["detail"]); dt != 0 {
  1614. l["detail"] = qu.FormatDateByInt64(&dt, qu.Date_Full_Layout)
  1615. } else {
  1616. l["detail"] = 0
  1617. }
  1618. if det := qu.Int64All(l["detailexecute"]); det != 0 {
  1619. l["detailexecute"] = qu.FormatDateByInt64(&det, qu.Date_Full_Layout)
  1620. } else {
  1621. l["detailexecute"] = 0
  1622. }
  1623. if ft := qu.Int64All(l["findlist"]); ft != 0 {
  1624. l["findlist"] = qu.FormatDateByInt64(&ft, qu.Date_Full_Layout)
  1625. } else {
  1626. l["findlist"] = 0
  1627. }
  1628. ut := qu.Int64All(l["updatetime"])
  1629. l["updatetime"] = qu.FormatDateByInt64(&ut, qu.Date_Full_Layout)
  1630. pendtime := qu.Int64All((*d)["pendtime"])
  1631. if pendtime != 0 {
  1632. l["pendtime"] = qu.FormatDateByInt64(&pendtime, qu.Date_Full_Layout)
  1633. } else {
  1634. l["pendtime"] = "0"
  1635. }
  1636. //l["isfindlist"] = "否"
  1637. //typeList := qu.IntAll((*d)["type_list"])
  1638. //strList := qu.ObjToString((*d)["str_list"])
  1639. //if typeList == 1 && strings.Contains(strList, "findListHtml") {
  1640. // l["isfindlist"] = "是"
  1641. //}
  1642. }
  1643. f.ServeJson(map[string]interface{}{"draw": draw, "data": list, "recordsFiltered": count, "recordsTotal": count})
  1644. } else {
  1645. events := []string{}
  1646. for k, _ := range util.Config.Uploadevents {
  1647. events = append(events, k)
  1648. }
  1649. sort.Strings(events)
  1650. f.T["events"] = events
  1651. f.Render("heart.html", &f.T)
  1652. }
  1653. }
  1654. //爬虫信息
  1655. type spinfo struct {
  1656. code string
  1657. todayDowncount, toDayRequestNum int
  1658. yesterdayDowncount, yestoDayRequestNum int
  1659. totalDowncount, totalRequestNum int
  1660. errorNum, roundCount, runRate int
  1661. lastDowncount int
  1662. lastHeartbeat string
  1663. lstate string
  1664. }
  1665. //爬虫信息
  1666. func SpiderInfo(data string) {
  1667. data = util.Se.DecodeString(data)
  1668. infos := []map[string]interface{}{}
  1669. err := json.Unmarshal([]byte(data), &infos)
  1670. if err != nil {
  1671. return
  1672. }
  1673. for _, tmp := range infos {
  1674. lastHeartbeat := qu.Int64All(tmp["lastHeartbeat"])
  1675. info := &spinfo{
  1676. code: fmt.Sprint(tmp["code"]),
  1677. todayDowncount: qu.IntAll(tmp["todayDowncount"]),
  1678. toDayRequestNum: qu.IntAll(tmp["toDayRequestNum"]),
  1679. yesterdayDowncount: qu.IntAll(tmp["yesterdayDowncount"]),
  1680. yestoDayRequestNum: qu.IntAll(tmp["yestoDayRequestNum"]),
  1681. totalDowncount: qu.IntAll(tmp["totalDowncount"]),
  1682. totalRequestNum: qu.IntAll(tmp["totalRequestNum"]),
  1683. errorNum: qu.IntAll(tmp["errorNum"]),
  1684. roundCount: qu.IntAll(tmp["roundCount"]),
  1685. runRate: qu.IntAll(tmp["runRate"]),
  1686. lastHeartbeat: qu.FormatDateByInt64(&lastHeartbeat, qu.Date_Full_Layout),
  1687. lastDowncount: qu.IntAll(tmp["lastDowncount"]),
  1688. lstate: fmt.Sprint(tmp["lstate"]),
  1689. }
  1690. spinfos.Store(info.code, info)
  1691. //log.Println(info)
  1692. }
  1693. }
  1694. //接受维护任务信息
  1695. func SpiderModifyTask(data string) {
  1696. data = util.Se.DecodeString(data)
  1697. mtasks := []map[string]interface{}{}
  1698. err := json.Unmarshal([]byte(data), &mtasks)
  1699. if err != nil {
  1700. return
  1701. }
  1702. for k, tmp := range mtasks {
  1703. log.Println(k, tmp)
  1704. }
  1705. }
  1706. //查看是否有该任务
  1707. func checkTask(codes []string, num int) []string {
  1708. // var id string = ""
  1709. query := map[string]interface{}{}
  1710. var idArr []string
  1711. if len(codes) > 0 {
  1712. for _, v := range codes {
  1713. if num == 1 {
  1714. query = map[string]interface{}{
  1715. "s_code": v,
  1716. "i_state": map[string]interface{}{
  1717. "$in": []int{1, 2, 5},
  1718. },
  1719. }
  1720. } else if num == 2 { //打回时查询待审核的任务
  1721. query = map[string]interface{}{
  1722. "s_code": v,
  1723. "i_state": 3,
  1724. }
  1725. } else if num == 3 { //审核通过时查询待处理、处理中、待审核、未通过的任务
  1726. query = map[string]interface{}{
  1727. "s_code": v,
  1728. "i_state": map[string]interface{}{
  1729. "$in": []int{1, 2, 3, 5},
  1730. },
  1731. }
  1732. } else if num == 7 {
  1733. query = map[string]interface{}{
  1734. "s_code": v,
  1735. "i_state": map[string]interface{}{
  1736. "$in": []int{2, 5},
  1737. },
  1738. }
  1739. }
  1740. task, _ := u.MgoEB.Find("task", query, nil, nil, false, -1, -1)
  1741. if task != nil {
  1742. for _, t := range *task {
  1743. idArr = append(idArr, mongodb.BsonIdToSId(t["_id"]))
  1744. }
  1745. }
  1746. return idArr
  1747. }
  1748. }
  1749. return idArr
  1750. }