analyze_hilo.go 3.63 KB
Newer Older
hujiebin's avatar
1  
hujiebin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
package main

import (
	"bufio"
	"encoding/json"
	"fmt"
	"github.com/tealeg/xlsx"
	"log"
	"os"
	"strings"
)

// LogEntry 表示每行日志记录的结构
type DataD struct {
	File  string `json:"file"`
	Func  string `json:"func"`
	Level string `json:"level"`
	Msg   string `json:"msg"`
	Time  string `json:"time"`
}

// LogEntry 表示日志记录的结构
type LogEntry struct {
	Content string `json:"__CONTENT__"`
}

type RealData struct {
	Uid         string
	Ip          string
	Lang        string
	Timezone    string
	Country     string
	CountryCode string
	Imei        string
	Device      string
	Version     string
	Carrier     string
	Vpn         string
	Time        string
}

func main() {
	// 打开文件
	file, err := os.Open("log.json")
	if err != nil {
		log.Fatalf("无法打开文件: %v", err)
	}
	defer file.Close()
	var res []RealData

	scanner := bufio.NewScanner(file)
	for scanner.Scan() {
		// 读取每一行
		line := scanner.Text()

		// 解析JSON
		var entry LogEntry
		err := json.Unmarshal([]byte(line), &entry)
		if err != nil {
			log.Printf("解析JSON失败: %v", err)
			continue
		}
		var data DataD
		json.Unmarshal([]byte(strings.Replace(entry.Content, "\\", "", -1)), &data)
		// 打印解析后的结构
		realDatas := strings.Split(data.Msg, "--")
		uid := strings.Split(realDatas[1], ":")
		ip := strings.Split(realDatas[2], ":")
		lang := strings.Split(realDatas[3], ":")
		timezone := strings.Split(realDatas[4], ":")
		country := strings.Split(realDatas[5], ":")
		countryCode := strings.Split(realDatas[6], ":")
		imei := strings.Split(realDatas[7], ":")
		device := strings.Split(realDatas[8], ":")
		version := strings.Split(realDatas[9], ":")
		carrier := strings.Split(realDatas[10], ":")
		vpn := strings.Split(realDatas[11], ":")
		fmt.Printf("uid:%v,ip:%v,lang:%v,timezone:%v,country:%v,countryCode:%v,imei:%v,device:%v,version:%v,carrier:%v,vpn:%v,time:%v\n",
			uid[1], ip[1], lang[1], timezone[1], country[1], countryCode[1], imei[1], device[1], version[1], carrier[1], vpn[1], data.Time)
		res = append(res, RealData{
			Uid:         uid[1],
			Ip:          ip[1],
			Lang:        lang[1],
			Timezone:    timezone[1],
			Country:     country[1],
			CountryCode: countryCode[1],
			Imei:        imei[1],
			Device:      device[1],
			Version:     version[1],
			Carrier:     carrier[1],
			Vpn:         vpn[1],
			Time:        data.Time,
		})
	}

	if err := scanner.Err(); err != nil {
		log.Fatalf("读取文件时出错: %v", err)
	}
	excelFileName := fmt.Sprintf("./用户地区分析.xlsx")
	xlFile := xlsx.NewFile()
	sheet, err := xlFile.AddSheet("wealth")
	if err != nil {
		panic(err)
	}
	row := sheet.AddRow()
	c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12 := row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell()
	c1.Value, c2.Value, c3.Value, c4.Value, c5.Value, c6.Value, c7.Value, c8.Value, c9.Value, c10.Value, c11.Value, c12.Value =
		"uid", "ip", "lang", "timezone", "country", "countryCode", "imei", "device", "version", "carrier", "vpn", "time"
	for _, d := range res {
		row := sheet.AddRow()
		c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12 := row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell(), row.AddCell()
		c1.Value, c2.Value, c3.Value, c4.Value, c5.Value, c6.Value, c7.Value, c8.Value, c9.Value, c10.Value, c11.Value, c12.Value =
			d.Uid, d.Ip, d.Lang, d.Timezone, d.Country, d.CountryCode, d.Imei, d.Device, d.Version, d.Carrier, d.Vpn, d.Time
	}
	_ = xlFile.Save(excelFileName)
}