spider.go 56 KB

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