|
@@ -0,0 +1,256 @@
|
|
|
+package lua
|
|
|
+
|
|
|
+import (
|
|
|
+ "encoding/json"
|
|
|
+ "fmt"
|
|
|
+ "github.com/go-xweb/xweb"
|
|
|
+ "go.mongodb.org/mongo-driver/bson/primitive"
|
|
|
+ "mongodb"
|
|
|
+ qu "qfw/util"
|
|
|
+ "strings"
|
|
|
+ "time"
|
|
|
+ . "util"
|
|
|
+)
|
|
|
+
|
|
|
+/*
|
|
|
+ spider_task程序统计spider_warn表中异常数据,并做同一条数据不同异常信息过滤入spider_warn_err表
|
|
|
+*/
|
|
|
+
|
|
|
+type Lua struct {
|
|
|
+ *xweb.Action
|
|
|
+ //spider_warn
|
|
|
+ spiderWarn xweb.Mapper `xweb:"/lua/spiderwarn"` //spider_warn异常数据
|
|
|
+ spiderEdit xweb.Mapper `xweb:"/lua/spideredit"` //编辑异常数据
|
|
|
+ spiderSave xweb.Mapper `xweb:"/lua/spiersave"` //数据更新、保存
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+func (l *Lua) SpiderWarn() {
|
|
|
+ defer qu.Catch()
|
|
|
+ if l.Method() == "POST" {
|
|
|
+ defer qu.Catch()
|
|
|
+ level, _ := l.GetInteger("level")
|
|
|
+ start, _ := l.GetInteger("start")
|
|
|
+ limit, _ := l.GetInteger("length")
|
|
|
+ draw, _ := l.GetInteger("draw")
|
|
|
+ startTime, _ := l.GetInt("starttime")
|
|
|
+ qu.Debug(startTime, start, limit, draw, level)
|
|
|
+ sort := `{"%s":%d}`
|
|
|
+ orderIndex := l.GetString("order[0][column]")
|
|
|
+ orderName := l.GetString(fmt.Sprintf("columns[%s][data]", orderIndex))
|
|
|
+ orderType := 1
|
|
|
+ if l.GetString("order[0][dir]") != "asc" {
|
|
|
+ orderType = -1
|
|
|
+ }
|
|
|
+ sort = fmt.Sprintf(sort, orderName, orderType)
|
|
|
+ query := map[string]interface{}{
|
|
|
+ "ok": false,
|
|
|
+ }
|
|
|
+ if startTime > 0 {
|
|
|
+ query["comeintime"] = map[string]interface{}{
|
|
|
+ "$gte": startTime,
|
|
|
+ "$lt": startTime + 86400,
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if level != -1 {
|
|
|
+ query["level"] = level
|
|
|
+ }
|
|
|
+ fields := map[string]interface{}{
|
|
|
+ "data": 0,
|
|
|
+ }
|
|
|
+ count := MgoS.Count("spider_warn_err", query)
|
|
|
+ qu.Debug("query:", query, "sort:", sort, count)
|
|
|
+ list, _ := MgoS.Find("spider_warn_err", query, sort, fields, false, start, limit)
|
|
|
+ l.ServeJson(map[string]interface{}{
|
|
|
+ "data": list,
|
|
|
+ "draw": draw,
|
|
|
+ "recordsFiltered": count,
|
|
|
+ "recordsTotal": count,
|
|
|
+ })
|
|
|
+ } else {
|
|
|
+ l.Render("lua/spiderwarn.html", &l.T)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func (l *Lua) SpiderEdit() {
|
|
|
+ defer qu.Catch()
|
|
|
+ id := l.GetString("id")
|
|
|
+ data := map[string]interface{}{}
|
|
|
+ bidId := ""
|
|
|
+ one, _ := MgoS.FindById("spider_warn_err", id, map[string]interface{}{"data": 1, "field": 1, "level": 1})
|
|
|
+ if one != nil && len(*one) > 0 {
|
|
|
+ data, _ = (*one)["data"].(map[string]interface{})
|
|
|
+ detail := qu.ObjToString((data)["detail"])
|
|
|
+ sha := Sha(detail)
|
|
|
+ contenthtml := qu.ObjToString((data)["contenthtml"])
|
|
|
+ summary := qu.ObjToString((data)["summary"])
|
|
|
+ data["s_sha"] = sha
|
|
|
+ //l.T["sha"] = sha
|
|
|
+ l.T["detail"] = detail
|
|
|
+ l.T["contenthtml"] = contenthtml
|
|
|
+ l.T["summary"] = summary
|
|
|
+ qu.Debug("id:", id, " sha:", sha)
|
|
|
+ /*
|
|
|
+ 1、如果field中出现一个以上字段表示spider_warn_err表在汇总spider_warn数据时出现一条数据多个异常信息(两个错误异常数据不可能是同条数据)
|
|
|
+ 且该异常信息的原信息已经入bidding库,需要通过sha找bidding信息id
|
|
|
+ 2、异常类型为警告时,原信息也已经入bidding库
|
|
|
+ */
|
|
|
+ if qu.IntAll((*one)["level"]) == 1 || len(strings.Split(qu.ObjToString((*one)["field"]), ",")) > 1 {
|
|
|
+ bidData, _ := JYMgo.FindOne("bidding", map[string]interface{}{"s_sha": sha})
|
|
|
+ if bidData != nil && len(*bidData) > 0 {
|
|
|
+ bidId = mongodb.BsonIdToSId((*bidData)["_id"])
|
|
|
+ data = *bidData
|
|
|
+ } else {
|
|
|
+ qu.Debug("异常信息id:", id)
|
|
|
+ l.Render("com/err.html")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+ delete(data, "_id")
|
|
|
+ delete(data, "detail")
|
|
|
+ delete(data, "contenthtml")
|
|
|
+ delete(data, "summary")
|
|
|
+ delete(data, "_d")
|
|
|
+ delete(data, "T")
|
|
|
+ delete(data, "publishdept")
|
|
|
+ delete(data, "l_np_publishtime")
|
|
|
+ }
|
|
|
+ l.T["id"] = id //spider_warn_err id
|
|
|
+ l.T["bid"] = bidId //bidding id
|
|
|
+ l.T["data"] = data
|
|
|
+ l.Render("lua/spideredit.html", &l.T)
|
|
|
+}
|
|
|
+
|
|
|
+func (l *Lua) SpiderSave() {
|
|
|
+ defer qu.Catch()
|
|
|
+ success := false
|
|
|
+ rep := ""
|
|
|
+ id := l.GetString("id")
|
|
|
+ stype, _ := l.GetInteger("stype")
|
|
|
+ qu.Debug("id:", id, "stype:", stype)
|
|
|
+ if stype == 2 { //无需更新:只在spider_warn_err上打标记
|
|
|
+ TagToSpiderWarnErr(stype, id, "无需修改", map[string]interface{}{})
|
|
|
+ success = true
|
|
|
+ } else {
|
|
|
+ bid := l.GetString("bid")
|
|
|
+ qu.Debug("bidding id:", bid)
|
|
|
+ reasons := l.GetString("reasons")
|
|
|
+ updateStr := l.GetStringComm("update")
|
|
|
+ update := map[string]interface{}{}
|
|
|
+ if err := json.Unmarshal([]byte(updateStr), &update); err != nil {
|
|
|
+ qu.Debug("data Unmarshal Failed:", err)
|
|
|
+ }
|
|
|
+ modifyStr := l.GetString("modifyinfo")
|
|
|
+ modifyinfo := map[string]interface{}{}
|
|
|
+ if err := json.Unmarshal([]byte(modifyStr), &modifyinfo); err != nil {
|
|
|
+ qu.Debug("modifyinfo Unmarshal Failed:", err)
|
|
|
+ }
|
|
|
+ if len(update) > 0 { //处理字段
|
|
|
+ for k, v := range update {
|
|
|
+ if k == "extracttype" || k == "dataging" {
|
|
|
+ (update)[k] = qu.IntAll(v)
|
|
|
+ } else if k == "publishtime" || k == "bidopentime" {
|
|
|
+ (update)[k] = qu.Int64All(v)
|
|
|
+ } else if k == "budget" || k == "bidamount" {
|
|
|
+ (update)[k] = qu.Float64All(v)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ FormatNumber(update) //解决超大金额转成科学计数法的问题
|
|
|
+ }
|
|
|
+ qu.Debug(modifyinfo)
|
|
|
+ if stype == 1 { //修复更新
|
|
|
+ if bid == "" {
|
|
|
+ rep = "bidding id 为空"
|
|
|
+ goto L
|
|
|
+ }
|
|
|
+ old_data, _ := JYMgo.FindById("bidding", bid, nil) //查询原始信息,用作修改日志记录
|
|
|
+ if old_data != nil && len(*old_data) > 0 {
|
|
|
+ delete(*old_data, "_id")
|
|
|
+ if (*old_data)["modifyinfo"] != nil {
|
|
|
+ if m, ok := (*old_data)["modifyinfo"].(map[string]interface{}); ok { //合并modifyinfo
|
|
|
+ for k, v := range m {
|
|
|
+ modifyinfo[k] = v
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ rep = "未找到bidding数据,id:" + bid
|
|
|
+ goto L
|
|
|
+ }
|
|
|
+ if len(modifyinfo) > 0 { //记录修改字段
|
|
|
+ update["modifyinfo"] = modifyinfo
|
|
|
+ }
|
|
|
+ b := JYMgo.UpdateById("bidding", bid, map[string]interface{}{"$set": update}) //更新
|
|
|
+ if b { //更新成功 //更新成功 重生bidding索引 项目合并
|
|
|
+ qu.Debug("更新成功")
|
|
|
+ //udptask.BiddingIndexUdp(bid, "bidding") //coll:bidding 是因为调用此方法的数据都是增量数据
|
|
|
+ //if IsModifyPro(modifyinfo) { //修改了影响项目合并的字段 进行项目合并
|
|
|
+ // udptask.ProjectSetUdp(bid, "bidding")
|
|
|
+ //}
|
|
|
+ //delName1 := RedisDelKey1 + bid + "_*"
|
|
|
+ //redis.DelByCodePattern(RedisJYName, delName1)
|
|
|
+ delete(update, "modifyinfo")
|
|
|
+ l.SaveUpdateLog(2, reasons, bid, *old_data, update, modifyinfo) //标签日志
|
|
|
+ TagToSpiderWarnErr(stype, id, reasons, update) //spider_warn_err日志
|
|
|
+ success = true
|
|
|
+ } else { //更新失败
|
|
|
+ rep = "更新bidding失败,id:" + bid
|
|
|
+ qu.Debug("更新失败")
|
|
|
+ }
|
|
|
+ } else { //3:修复发布 4:直接发布
|
|
|
+ href := qu.ObjToString(update["href"])
|
|
|
+ shaid := qu.ObjToString(update["s_sha"])
|
|
|
+ competehref := qu.ObjToString(update["competehref"]) //竞品数据
|
|
|
+ iscompete, _ := update["iscompete"].(bool) //新爬虫数据
|
|
|
+ newId := primitive.NewObjectID()
|
|
|
+ newSid := mongodb.BsonIdToSId(newId)
|
|
|
+ qu.Debug("newId:", newSid)
|
|
|
+ update["_id"] = newId
|
|
|
+ if competehref != "" { //竞品爬虫
|
|
|
+ href = competehref
|
|
|
+ update["href"] = GetJyHref(newSid)
|
|
|
+ } else if iscompete { //新爬虫
|
|
|
+ update["competehref"] = "#"
|
|
|
+ }
|
|
|
+ PutHrefRedis(href, shaid, newSid) //添加href进redis
|
|
|
+ update["comeintime"] = time.Now().Unix() //更新入库时间
|
|
|
+ qu.Debug(update)
|
|
|
+ JYMgo.SaveByOriID("bidding", update) //入bidding
|
|
|
+ l.SaveUpdateLog(1, reasons, newSid, map[string]interface{}{}, update, map[string]interface{}{}) //标签日志
|
|
|
+ TagToSpiderWarnErr(stype, id, reasons, update) //spider_warn_err日志
|
|
|
+ success = true
|
|
|
+ }
|
|
|
+ }
|
|
|
+L:
|
|
|
+ l.ServeJson(map[string]interface{}{"success": success, "rep": rep})
|
|
|
+}
|
|
|
+
|
|
|
+func TagToSpiderWarnErr(state int, id, reason string, update map[string]interface{}) {
|
|
|
+ defer qu.Catch()
|
|
|
+ set := map[string]interface{}{
|
|
|
+ "ok": true,
|
|
|
+ "state": state,
|
|
|
+ "reason": reason,
|
|
|
+ "updatetime": time.Now().Unix(),
|
|
|
+ }
|
|
|
+ if len(update) > 0 {
|
|
|
+ set["update"] = update
|
|
|
+ }
|
|
|
+ MgoS.UpdateById("spider_warn_err", id, map[string]interface{}{"$set": set})
|
|
|
+}
|
|
|
+
|
|
|
+func (l *Lua) SaveUpdateLog(s_type int, reasons, bid string, old_data, update, modifyinfo map[string]interface{}) {
|
|
|
+ //日志记录
|
|
|
+ user := l.GetSession("user").(map[string]interface{})
|
|
|
+ log_data := map[string]interface{}{
|
|
|
+ "s_modifyuser": user["name"],
|
|
|
+ "s_type": s_type,
|
|
|
+ "i_modifytime": time.Now().Unix(),
|
|
|
+ "s_modifyreason": reasons,
|
|
|
+ "s_backupid": bid,
|
|
|
+ "o_oldinfo": old_data,
|
|
|
+ "o_newinfo": update,
|
|
|
+ "modifyinfo": modifyinfo,
|
|
|
+ }
|
|
|
+ Mgo.Save(JyRecord, log_data)
|
|
|
+}
|