parse_diff_log.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. package service
  2. import (
  3. "bufio"
  4. "encoding/json"
  5. "fmt"
  6. "io"
  7. "os"
  8. "sort"
  9. "strings"
  10. "go-common/app/job/main/passport-game-data/model"
  11. "go-common/library/log"
  12. )
  13. const (
  14. _cloudJobGoroutineNum = 32
  15. )
  16. // ParseDiffLog parse diff log printed by compare proc.
  17. func ParseDiffLog(src, dst string) (err error) {
  18. f, err := os.Open(src)
  19. if err != nil {
  20. log.Error("failed to open file %s, error(%v)", src, err)
  21. return
  22. }
  23. defer f.Close()
  24. dstFile, err := os.Create(dst)
  25. if err != nil {
  26. log.Error("failed to open file %s, error(%v)", dst, err)
  27. return
  28. }
  29. defer dstFile.Close()
  30. var (
  31. line string
  32. skippedCount = 0
  33. res = make([]*model.CompareRes, 0)
  34. rd = bufio.NewReader(f)
  35. )
  36. for {
  37. line, err = rd.ReadString('\n')
  38. if err != nil || io.EOF == err {
  39. break
  40. }
  41. idx := strings.LastIndex(line, "]")
  42. if idx == -1 {
  43. log.Error("failed to parse log, expected have ] in string but not")
  44. skippedCount++
  45. continue
  46. }
  47. logJSON := line[idx+1:]
  48. l := new(model.Log)
  49. if err = json.Unmarshal([]byte(logJSON), &l); err != nil {
  50. log.Error("failed to parse log, json.Unmarshal(%s) error(%v), skip", logJSON, err)
  51. skippedCount++
  52. continue
  53. }
  54. var cRes *model.CompareRes
  55. if cRes, err = diffLog2CompareRes(l.Log); err != nil {
  56. log.Error("diffLog2CompareRes(%s) error(%v), skip", l.Log, err)
  57. skippedCount++
  58. continue
  59. }
  60. // compare local encrypted and cloud, parse diff flags
  61. flags := diff(cRes.Cloud, cRes.LocalEncrypted)
  62. if flags == _diffTypeNon {
  63. continue
  64. }
  65. cRes.Flags = flags
  66. cRes.FlagsDesc = formatFlags(flags)
  67. cRes.Seq = cRes.Local.Mid % _cloudJobGoroutineNum
  68. res = append(res, cRes)
  69. }
  70. percentMap := make(map[uint8]*model.CountAndPercent)
  71. seqMap := make(map[int64]*model.SeqCountAndPercent)
  72. for _, v := range res {
  73. percent, ok := percentMap[v.Flags]
  74. if !ok {
  75. percent = &model.CountAndPercent{
  76. DiffType: v.FlagsDesc,
  77. }
  78. percentMap[v.Flags] = percent
  79. }
  80. percent.Count++
  81. seq, ok := seqMap[v.Seq]
  82. if !ok {
  83. seq = &model.SeqCountAndPercent{
  84. Seq: v.Seq,
  85. }
  86. seqMap[v.Seq] = seq
  87. }
  88. seq.Count++
  89. }
  90. sort.Slice(res, func(i, j int) bool {
  91. return res[i].Cloud.Mtime.After(res[j].Cloud.Mtime)
  92. })
  93. percentList := make([]*model.CountAndPercent, 0)
  94. for _, v := range percentMap {
  95. v.Percent = fmt.Sprintf("%0.2f", 100*float64(v.Count)/float64(len(res))) + "%"
  96. percentList = append(percentList, v)
  97. }
  98. sort.Slice(percentList, func(i, j int) bool {
  99. return percentList[i].Count > percentList[j].Count
  100. })
  101. seqList := make([]*model.SeqCountAndPercent, 0)
  102. for _, v := range seqMap {
  103. v.Percent = fmt.Sprintf("%0.2f", 100*float64(v.Count)/float64(_cloudJobGoroutineNum)) + "%"
  104. seqList = append(seqList, v)
  105. }
  106. sort.Slice(seqList, func(i, j int) bool {
  107. return seqList[i].Count > seqList[j].Count
  108. })
  109. stat := &model.DiffParseResp{
  110. Total: len(res),
  111. SeqAndPercents: seqList,
  112. CompareResList: res,
  113. CountAndPercents: percentList,
  114. }
  115. str, _ := json.Marshal(stat)
  116. _, err = dstFile.WriteString(string(str))
  117. if err != nil {
  118. log.Info("failed to write parse diff log result to file %s, error(%v)", dst, err)
  119. }
  120. log.Info("len res: %d, write ok", len(res))
  121. return
  122. }
  123. func diffLog2CompareRes(str string) (*model.CompareRes, error) {
  124. idx := strings.Index(str, "local")
  125. if idx == -1 {
  126. return nil, fmt.Errorf("failed to parse diff log, expected have local in string but not")
  127. }
  128. res := replace(str[idx:])
  129. cRes := new(model.CompareRes)
  130. err := json.Unmarshal([]byte(res), &cRes)
  131. return cRes, err
  132. }
  133. // parse string like "local({\"mid\":1}) local_encrypted({\"mid\":1}) cloud({\"mid\":1})" to json string {"local":{},"local_encrypted":{},"cloud":{}}
  134. func replace(str string) string {
  135. res := strings.Replace(str, "local(", `{"local":`, -1)
  136. res = strings.Replace(res, "local_encrypted(", `"local_encrypted":`, -1)
  137. res = strings.Replace(res, "cloud(", `"cloud":`, -1)
  138. res = strings.Replace(res, ")", ",", -1)
  139. res = strings.Replace(res, "\\", "", -1)
  140. if strings.HasSuffix(res, ",") {
  141. res = res[:len(res)-1]
  142. }
  143. res = res + "}"
  144. return res
  145. }
  146. const (
  147. _diffTypeNon = uint8(0) // 0x00000000
  148. _diffTypePwd = uint8(1) // 0x00000001
  149. _diffTypeEmail = uint8(2) // 0x00000010
  150. _diffTypeTel = uint8(4) // 0x00000100
  151. _diffTypeCountryID = uint8(16) // 0x00001000
  152. _diffTypeMobileVerified = uint8(32) // 0x00010000
  153. _diffTypeIsLeak = uint8(64) // 0x00100000
  154. )
  155. func formatFlags(flags uint8) string {
  156. fs := make([]string, 0)
  157. if flags&_diffTypePwd > 0 {
  158. fs = append(fs, "pwd")
  159. }
  160. if flags&_diffTypeEmail > 0 {
  161. fs = append(fs, "email")
  162. }
  163. if flags&_diffTypeTel > 0 {
  164. fs = append(fs, "tel")
  165. }
  166. if flags&_diffTypeCountryID > 0 {
  167. fs = append(fs, "country_id")
  168. }
  169. if flags&_diffTypeMobileVerified > 0 {
  170. fs = append(fs, "mobile_verified")
  171. }
  172. if flags&_diffTypeIsLeak > 0 {
  173. fs = append(fs, "is_leak")
  174. }
  175. if len(fs) == 0 {
  176. return "non"
  177. }
  178. return strings.Join(fs, ",")
  179. }
  180. func diff(cloud, localEncrypted *model.AsoAccount) uint8 {
  181. if localEncrypted == cloud {
  182. return _diffTypeNon
  183. }
  184. if localEncrypted == nil || cloud == nil {
  185. return _diffTypePwd | _diffTypeEmail | _diffTypeTel
  186. }
  187. res := _diffTypeNon
  188. if cloud.Salt != localEncrypted.Salt || cloud.Pwd != localEncrypted.Pwd {
  189. res = res | _diffTypePwd
  190. }
  191. if cloud.Email != localEncrypted.Email {
  192. res = res | _diffTypeEmail
  193. }
  194. if cloud.Tel != localEncrypted.Tel {
  195. res = res | _diffTypeTel
  196. }
  197. if cloud.CountryID != localEncrypted.CountryID {
  198. res = res | _diffTypeCountryID
  199. }
  200. if cloud.MobileVerified != localEncrypted.MobileVerified {
  201. res = res | _diffTypeMobileVerified
  202. }
  203. if cloud.Isleak != localEncrypted.Isleak {
  204. res = res | _diffTypeIsLeak
  205. }
  206. return res
  207. }