Files
EdgeAdmin/internal/web/actions/default/log/exportExcel.go
2020-11-20 17:08:10 +08:00

113 lines
3.0 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package log
import (
"bytes"
"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/actionutils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/iwind/TeaGo/lists"
timeutil "github.com/iwind/TeaGo/utils/time"
"github.com/tealeg/xlsx/v3"
"strconv"
"strings"
)
type ExportExcelAction struct {
actionutils.ParentAction
}
func (this *ExportExcelAction) Init() {
this.Nav("", "", "")
}
func (this *ExportExcelAction) RunGet(params struct {
DayFrom string
DayTo string
Keyword string
}) {
logsResp, err := this.RPC().LogRPC().ListLogs(this.AdminContext(), &pb.ListLogsRequest{
Offset: 0,
Size: 1000, // 日志最大导出1000条TODO 将来可以配置
DayFrom: params.DayFrom,
DayTo: params.DayTo,
Keyword: params.Keyword,
})
if err != nil {
this.ErrorPage(err)
return
}
wb := xlsx.NewFile()
sheet, err := wb.AddSheet("default")
if err != nil {
this.ErrorPage(err)
return
}
// 头部
{
row := sheet.AddRow()
row.SetHeight(25)
row.AddCell().SetString("ID")
row.AddCell().SetString("日期")
row.AddCell().SetString("用户")
row.AddCell().SetString("描述")
row.AddCell().SetString("IP")
row.AddCell().SetString("区域")
row.AddCell().SetString("运营商")
row.AddCell().SetString("页面地址")
}
// 数据
for _, log := range logsResp.Logs {
regionName := ""
ispName := ""
regionResp, err := this.RPC().IPLibraryRPC().LookupIPRegion(this.AdminContext(), &pb.LookupIPRegionRequest{Ip: log.Ip})
if err != nil {
this.ErrorPage(err)
return
}
if regionResp.Region != nil {
pieces := []string{}
if len(regionResp.Region.Country) > 0 {
pieces = append(pieces, regionResp.Region.Country)
}
if len(regionResp.Region.Province) > 0 && !lists.ContainsString(pieces, regionResp.Region.Province) {
pieces = append(pieces, regionResp.Region.Province)
}
if len(regionResp.Region.City) > 0 && !lists.ContainsString(pieces, regionResp.Region.City) && !lists.ContainsString(pieces, strings.TrimSuffix(regionResp.Region.Province, "市")) {
pieces = append(pieces, regionResp.Region.City)
}
regionName = strings.Join(pieces, " ")
if len(regionResp.Region.Isp) > 0 {
ispName = regionResp.Region.Isp
}
}
row := sheet.AddRow()
row.SetHeight(25)
row.AddCell().SetInt64(log.Id)
row.AddCell().SetString(timeutil.FormatTime("Y-m-d H:i:s", log.CreatedAt))
row.AddCell().SetString(log.UserName)
row.AddCell().SetString(log.Description)
row.AddCell().SetString(log.Ip)
row.AddCell().SetString(regionName)
row.AddCell().SetString(ispName)
row.AddCell().SetString(log.Action)
}
this.AddHeader("Content-Type", "application/vnd.ms-excel")
this.AddHeader("Content-Disposition", "attachment; filename=\"LOG-"+timeutil.Format("YmdHis")+".xlsx\"")
this.AddHeader("Cache-Control", "max-age=0")
buf := bytes.NewBuffer([]byte{})
err = wb.Write(buf)
if err != nil {
this.ErrorPage(err)
return
}
this.AddHeader("Content-Length", strconv.Itoa(buf.Len()))
this.Write(buf.Bytes())
}