forked from all_user/stat_center
971 lines
28 KiB
Go
971 lines
28 KiB
Go
package innerserverser
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"os"
|
|
"path"
|
|
"sort"
|
|
"stat/api/proto"
|
|
"stat/common/log"
|
|
"stat/common/tgbot"
|
|
"stat/common/utils"
|
|
"stat/models/appInfoMod"
|
|
"stat/models/dailystatmod"
|
|
"stat/statg"
|
|
"time"
|
|
|
|
"github.com/360EntSecGroup-Skylar/excelize"
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
)
|
|
|
|
const titlestyle = `{
|
|
"font":{
|
|
"color":"#333333",
|
|
"bold":true,
|
|
"size":16,
|
|
"family":"arial"
|
|
},
|
|
|
|
"alignment":{
|
|
"vertical":"center",
|
|
"horizontal":"center",
|
|
"wrap_text":true
|
|
}
|
|
}`
|
|
|
|
const cellstyle = `{
|
|
"font":{
|
|
"color":"#333333",
|
|
"bold":false,
|
|
"size":10,
|
|
"family":"arial"
|
|
},
|
|
"alignment":{
|
|
"vertical":"center",
|
|
"horizontal":"center",
|
|
"wrap_text":true
|
|
}
|
|
}`
|
|
|
|
func InnerRequest(url string, reqproto interface{}, bind interface{}) error {
|
|
data, err := json.Marshal(reqproto)
|
|
|
|
if err != nil {
|
|
log.Error("InnerRequest json.Marshal", log.Any("err", err))
|
|
return err
|
|
}
|
|
|
|
//resp, err = InnerPlainPost(url, data)
|
|
bodyReader := bytes.NewReader(data)
|
|
req, err := http.NewRequest(http.MethodPost, url, bodyReader)
|
|
if err != nil {
|
|
log.Error("InnerRequest http.NewRequest", log.Any("err", err))
|
|
return err
|
|
}
|
|
timeout := 30 * time.Second
|
|
req.Header.Set("Content-Type", "application/json")
|
|
client := http.Client{
|
|
Timeout: timeout,
|
|
}
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
log.Error("InnerRequest http.NewRequest", log.Any("err", err))
|
|
return err
|
|
}
|
|
|
|
if resp.StatusCode != 200 {
|
|
err = fmt.Errorf("error status %s, http code %d", resp.Status, resp.StatusCode)
|
|
log.Error("InnerRequest http.NewRequest resp", log.Any("err", err))
|
|
return err
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
data, err = io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
log.Error("InnerRequest io.ReadAll", log.Any("err", err))
|
|
return err
|
|
}
|
|
|
|
return json.Unmarshal(data, bind)
|
|
}
|
|
|
|
// bili请求过来的
|
|
func InnerRecgaregeList(ctx context.Context, req *proto.InnerRechargeReq) (
|
|
*proto.InnerDailyRechargeList, error) {
|
|
if req.InnerId != int32(statg.Conf.ServerIndex) {
|
|
err := fmt.Errorf("InnerRecgaregeList req.InnerId %d", req.InnerId)
|
|
log.Error("InnerRecgaregeList req.InnerId", log.Any("err", err))
|
|
|
|
return nil, err
|
|
}
|
|
|
|
resp := &proto.InnerDailyRechargeList{}
|
|
resp.List = make([]*proto.InnerDailyRecharge, 0)
|
|
|
|
begin := req.Begin
|
|
end := req.End
|
|
|
|
list, appinfo, err := GetDailyStatList(ctx, &end, &begin)
|
|
if err != nil {
|
|
log.Error("InnerRecgaregeList:GetDbList", log.E(err))
|
|
return nil, err
|
|
}
|
|
for _, info := range list {
|
|
if info.AppId == 889 {
|
|
continue
|
|
}
|
|
name := ""
|
|
if data, ok := appinfo[info.AppId]; ok {
|
|
name = data.Name
|
|
}
|
|
temp := &proto.InnerDailyRecharge{
|
|
AppId: info.AppId,
|
|
Name: name,
|
|
Type: "",
|
|
SumDate: info.SumDate,
|
|
DateTime: info.SumDate.Format(time.DateOnly),
|
|
DailyVistCount: info.TotalVisitCount,
|
|
DailyRegister: info.NewTotalCount,
|
|
DailyAppAmount: info.TotalAmount,
|
|
DailyGameAmount: info.GameRechageAmount,
|
|
RechargeOrder: info.SuccessRechargeOrder,
|
|
}
|
|
resp.List = append(resp.List, temp)
|
|
}
|
|
return resp, nil
|
|
}
|
|
|
|
func GetDbList(ctx context.Context, day *time.Time, yesterday *time.Time) (
|
|
[]*dailystatmod.DailyStatMod, map[int32]*appInfoMod.AppInfo, error) {
|
|
list := make([]*dailystatmod.DailyStatMod, 0)
|
|
appinfo, serr := appInfoMod.GetAllAppIdMap(ctx, bson.M{})
|
|
if serr != nil {
|
|
log.Error("InnerServerDailyStat appInfoMod.GetAllAppInfo", log.Any("serr", serr))
|
|
return nil, nil, serr
|
|
}
|
|
for _, info := range appinfo {
|
|
//data, serr := dailystatmod.FindOneDailyStat(ctx, info.AppId, yesterday)
|
|
rechargelist, serr := dailystatmod.GetDailyStatTotalList(ctx, &info.AppId, yesterday, day)
|
|
if serr != nil {
|
|
log.Error("InnerServerDailyStat dailystatmod.GetDailyStatTotalList", log.Any("serr", serr))
|
|
continue
|
|
}
|
|
if len(rechargelist) != 1 {
|
|
log.Error("InnerServerDailyStat dailystatmod.GetDailyStatTotalList",
|
|
log.Any("len(rechargelist)", len(rechargelist)), log.Any("appid", info.AppId),
|
|
log.Any("yesterday", yesterday), log.Any("day", day))
|
|
continue
|
|
}
|
|
if rechargelist != nil {
|
|
list = append(list, rechargelist...)
|
|
} else {
|
|
log.Error("InnerServerDailyStat rechargelist nil")
|
|
}
|
|
}
|
|
return list, appinfo, nil
|
|
}
|
|
|
|
// 从这个方法开始bili本机调用
|
|
func DocExsit(path string) bool {
|
|
_, err := os.Stat(path)
|
|
if err != nil {
|
|
if os.IsExist(err) {
|
|
return true
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
// bili请求麻豆知乎信息只在bili启动麻豆知乎只响应数据
|
|
func InnerServerDailyStat(ctx context.Context) error {
|
|
if statg.Conf.ServerIndex != statg.ServerIndexBili {
|
|
log.Error("InnerServerDailyStat serverindex",
|
|
log.Any("serverindex", statg.Conf.ServerIndex))
|
|
return nil
|
|
}
|
|
log.Info("InnerServerDailyStat start")
|
|
list := make([]*proto.InnerDailyRecharge, 0)
|
|
now := time.Now()
|
|
|
|
for id, info := range statg.Conf.InnerServers {
|
|
if id == statg.ServerIndexBili {
|
|
continue
|
|
}
|
|
|
|
if id != statg.ServerIndexBili && info == "" {
|
|
log.Error("statg.Conf.InnerServers server not found",
|
|
log.Any("statg.Conf.InnerServers", id))
|
|
continue
|
|
}
|
|
req := &proto.InnerRechargeReq{
|
|
InnerId: int32(id),
|
|
Begin: utils.BeginWithTime(now).Add(-24 * time.Hour),
|
|
End: utils.BeginWithTime(now),
|
|
}
|
|
url := info + "/api/stat/inner/recharge/list"
|
|
log.Info("InnerServerDailyStat", log.Any("req", url))
|
|
res := &proto.InnerDailyRechargeResp{}
|
|
err := InnerRequest(url, req, res)
|
|
if err != nil {
|
|
log.Error("InnerServerDailyStat InnerRequest",
|
|
log.Any("InnerRequest", err))
|
|
continue
|
|
}
|
|
if res == nil {
|
|
log.Error("InnerServerDailyStat InnerRequest data not found")
|
|
continue
|
|
}
|
|
if res.Data.List != nil && len(res.Data.List) > 0 {
|
|
list = append(list, res.Data.List...)
|
|
}
|
|
}
|
|
|
|
/*appinfo, serr := appInfoMod.GetAllAppIdMap(ctx, bson.M{})
|
|
if serr != nil {
|
|
log.Error("InnerServerDailyStat appInfoMod.GetAllAppInfo", log.Any("serr", serr))
|
|
return nil, serr
|
|
}
|
|
*/
|
|
|
|
yesterday := utils.BeginOfYesterday()
|
|
day := utils.BeginOfToday()
|
|
|
|
bililist, appinfo, err := GetDbList(ctx, &day, &yesterday)
|
|
if err != nil {
|
|
log.Error("InnerServerDailyStat:GetDbList", log.E(err))
|
|
return err
|
|
}
|
|
|
|
for _, info := range bililist {
|
|
name := ""
|
|
if data, ok := appinfo[info.AppId]; ok {
|
|
name = data.Name
|
|
}
|
|
temp := &proto.InnerDailyRecharge{
|
|
AppId: info.AppId,
|
|
Name: name,
|
|
Type: "",
|
|
DateTime: "",
|
|
DailyVistCount: info.TotalVisitCount,
|
|
DailyRegister: info.NewTotalCount,
|
|
DailyAppAmount: info.TotalAmount,
|
|
DailyGameAmount: info.GameRechageAmount,
|
|
RechargeOrder: info.SuccessRechargeOrder,
|
|
}
|
|
list = append(list, temp)
|
|
}
|
|
|
|
temp := &proto.InnerDailyRecharge{}
|
|
for _, info := range list {
|
|
temp.AppId = 0
|
|
temp.Name = "天宇总充值"
|
|
temp.CellType = 1
|
|
temp.DailyVistCount += info.DailyVistCount
|
|
temp.DailyRegister += info.DailyRegister
|
|
temp.DailyAppAmount += info.DailyAppAmount
|
|
temp.DailyGameAmount += info.DailyGameAmount
|
|
temp.RechargeOrder += info.RechargeOrder
|
|
}
|
|
list = append(list, temp)
|
|
for i, j := 0, len(list)-1; i < j; i, j = i+1, j-1 {
|
|
list[i], list[j] = list[j], list[i]
|
|
}
|
|
dir := yesterday.Format("2006-01-02")
|
|
filePath := "/usr/local" + "/bakexel/" + dir + "/"
|
|
//filename := utils.BeginOfToday().Format("2006-01-02")
|
|
if !DocExsit(filePath) {
|
|
err = os.Mkdir(filePath, os.ModePerm)
|
|
if err != nil {
|
|
log.Error("InnerServerDailyStat:export save file to disk failed",
|
|
log.Any("fileName", filePath),
|
|
log.E(err))
|
|
return err
|
|
}
|
|
}
|
|
|
|
f := excelize.NewFile()
|
|
|
|
if f == nil {
|
|
log.Error("InnerServerDailyStat f failed")
|
|
err = errors.New("InnerServerDailyStat f failed")
|
|
return err
|
|
}
|
|
|
|
// 设置列宽
|
|
f.SetColWidth(sheetName, "A", "L", 23)
|
|
|
|
// 设置表头行高
|
|
f.SetRowHeight(sheetName, 1, 25)
|
|
// 设置表头样式
|
|
headStyleID, _ := f.NewStyle(titlestyle)
|
|
headStyleID0, _ := f.NewStyle(cellstyle)
|
|
f.SetCellStyle(sheetName, "A1", "L1", headStyleID)
|
|
idx := f.NewSheet(sheetName)
|
|
var exportTransWaterTitleold = map[string]string{
|
|
"A1": "产品",
|
|
"B1": "归属",
|
|
"C1": "日期",
|
|
"D1": "日活跃",
|
|
"E1": "日注册",
|
|
"F1": "视频日充值",
|
|
"G1": "棋牌日充值",
|
|
"H1": "交易笔数",
|
|
}
|
|
|
|
rowindex := 'A'
|
|
for k, v := range exportTransWaterTitleold {
|
|
f.SetCellValue(sheetName, k, v)
|
|
row := fmt.Sprintf("%s%d", string(rowindex), 2)
|
|
|
|
f.SetCellStyle(sheetName, row, row, headStyleID0)
|
|
rowindex++
|
|
}
|
|
|
|
for k, v := range list {
|
|
i := 'A' // i 不能超过Z,即列输不能超过26列,超出将出现bug
|
|
if v.AppId != 0 {
|
|
f.SetCellValue(sheetName, fmt.Sprintf("%s%d", string(i), k+2), v.Name) //名称
|
|
}
|
|
i++
|
|
|
|
/*if v.AppId != 0 {
|
|
if v.AppId == 888 {
|
|
f.SetCellValue(sheetName, fmt.Sprintf("%s%d", string(i), k+2), "集团") //归属
|
|
} else {
|
|
f.SetCellValue(sheetName, fmt.Sprintf("%s%d", string(i), k+2), "内部") //归属
|
|
}
|
|
}
|
|
*/
|
|
i++
|
|
|
|
f.SetCellValue(sheetName, fmt.Sprintf("%s%d", string(i), k+2), dir) //时间
|
|
i++
|
|
|
|
totalamount := fmt.Sprintf("%.2f", float64(v.DailyVistCount)*0.01)
|
|
f.SetCellValue(sheetName, fmt.Sprintf("%s%d", string(i), k+2), totalamount) //日活跃
|
|
i++
|
|
|
|
totalamount = fmt.Sprintf("%.2f", float64(v.DailyRegister)*0.01)
|
|
f.SetCellValue(sheetName, fmt.Sprintf("%s%d", string(i), k+2), totalamount) //日注册
|
|
i++
|
|
|
|
totalamount = fmt.Sprintf("%.2f", float64(v.DailyAppAmount)*0.0001)
|
|
f.SetCellValue(sheetName, fmt.Sprintf("%s%d", string(i), k+2), totalamount) //视频日充值
|
|
i++
|
|
|
|
if v.AppId == 0 || v.AppId == 888 {
|
|
totalamount = fmt.Sprintf("%.2f", float64(v.DailyGameAmount)*0.0001)
|
|
f.SetCellValue(sheetName, fmt.Sprintf("%s%d", string(i), k+2), totalamount) //棋牌日充值
|
|
}
|
|
i++
|
|
|
|
totalamount = fmt.Sprintf("%.2f", float64(v.RechargeOrder)*0.01)
|
|
f.SetCellValue(sheetName, fmt.Sprintf("%s%d", string(i), k+2), totalamount) //交易笔数
|
|
}
|
|
|
|
f.SetActiveSheet(idx)
|
|
fileName := filePath + dir + ".xlsx"
|
|
|
|
if err = f.SaveAs(fileName); err != nil {
|
|
log.Error("InnerServerDailyStat:export save file to disk failed",
|
|
log.Any("fileName", fileName),
|
|
log.E(err))
|
|
return err
|
|
}
|
|
|
|
if statg.RchssendBot != nil {
|
|
err = statg.RchssendBot.Sendrchstochat("天宇公司日报", fileName, tgbot.FileTypeDocument)
|
|
//statg.Bot.Sendrchstochat("充值", fileName, tgbot.FileTypeDocument)
|
|
if err != nil {
|
|
log.Error("Sendrchstochat", log.Any("Sendrchstochat", err))
|
|
}
|
|
}
|
|
|
|
log.Info("InnerServerDailyStat end")
|
|
|
|
return nil
|
|
}
|
|
|
|
// bili请求麻豆知乎信息只在bili启动麻豆知乎只响应数据
|
|
func InnerServerMonthStat(ctx context.Context, begin, end *time.Time) error {
|
|
if statg.Conf.ServerIndex != statg.ServerIndexBili {
|
|
log.Error("InnerServerMonthStat serverindex",
|
|
log.Any("serverindex", statg.Conf.ServerIndex))
|
|
return nil
|
|
}
|
|
log.Info("InnerServerMonthStat start")
|
|
var f *excelize.File
|
|
newxlslfile := false
|
|
var err error
|
|
yesterday := utils.BeginOfYesterday()
|
|
day := utils.BeginOfToday()
|
|
|
|
if begin != nil && end != nil {
|
|
yesterday = *begin
|
|
day = *end
|
|
}
|
|
dir := yesterday.Format("2006-01")
|
|
dayrow := yesterday.Format("2006-01-02")
|
|
filePath := "/usr/local" + "/bakmonth/" + dir + "/"
|
|
filename := filePath + dir + ".xlsx"
|
|
if !DocExsit(filePath) {
|
|
err = os.MkdirAll(filePath, os.ModePerm)
|
|
if err != nil {
|
|
log.Error("InnerServerMonthStat:export save file to disk failed",
|
|
log.Any("fileName", filePath),
|
|
log.E(err))
|
|
return err
|
|
}
|
|
f = excelize.NewFile()
|
|
newxlslfile = true
|
|
} else {
|
|
if !DocExsit(filename) {
|
|
f = excelize.NewFile()
|
|
newxlslfile = true
|
|
} else {
|
|
f, err = excelize.OpenFile(filename)
|
|
if err != nil {
|
|
log.Error("InnerServerMonthStat excelize.OpenFile failed",
|
|
log.Any("fileName", filePath),
|
|
log.E(err))
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
|
|
if f == nil {
|
|
log.Error("InnerServerMonthStat f failed")
|
|
err = errors.New("InnerServerMonthStat f failed")
|
|
return err
|
|
}
|
|
|
|
list := make([]*proto.InnerDailyRecharge, 0)
|
|
for id, info := range statg.Conf.InnerServers {
|
|
if id == statg.ServerIndexBili {
|
|
continue
|
|
}
|
|
|
|
if id != statg.ServerIndexBili && info == "" {
|
|
log.Error("InnerServerMonthStat server not found",
|
|
log.Any("statg.Conf.InnerServers", id))
|
|
continue
|
|
}
|
|
req := &proto.InnerRechargeReq{
|
|
InnerId: int32(id),
|
|
Begin: yesterday,
|
|
End: day,
|
|
}
|
|
url := info + "/api/stat/inner/recharge/list"
|
|
log.Info("InnerServerMonthStat", log.Any("req", url))
|
|
res := &proto.InnerDailyRechargeResp{}
|
|
err = InnerRequest(url, req, res)
|
|
if err != nil {
|
|
log.Error("InnerServerMonthStat InnerRequest",
|
|
log.Any("InnerRequest", err))
|
|
continue
|
|
}
|
|
if res == nil {
|
|
log.Error("InnerServerMonthStat InnerRequest data not found")
|
|
continue
|
|
}
|
|
if res.Data.List != nil && len(res.Data.List) > 0 {
|
|
list = append(list, res.Data.List...)
|
|
}
|
|
}
|
|
|
|
bililist, appinfo, err := GetDbList(ctx, &day, &yesterday)
|
|
if err != nil {
|
|
log.Error("InnerServerMonthStat:GetDbList", log.E(err))
|
|
return err
|
|
}
|
|
|
|
for _, info := range bililist {
|
|
name := ""
|
|
if data, ok := appinfo[info.AppId]; ok {
|
|
name = data.Name
|
|
}
|
|
temp := &proto.InnerDailyRecharge{
|
|
AppId: info.AppId,
|
|
Name: name,
|
|
Type: "",
|
|
DateTime: "",
|
|
DailyVistCount: info.TotalVisitCount,
|
|
DailyRegister: info.NewTotalCount,
|
|
DailyAppAmount: info.TotalAmount,
|
|
DailyGameAmount: info.GameRechageAmount,
|
|
RechargeOrder: info.SuccessRechargeOrder,
|
|
}
|
|
list = append(list, temp)
|
|
}
|
|
|
|
temp := &proto.InnerDailyRecharge{}
|
|
for _, info := range list {
|
|
temp.AppId = 0
|
|
temp.CellType = 1
|
|
temp.Name = "天宇总充值"
|
|
temp.DailyVistCount += info.DailyVistCount
|
|
temp.DailyRegister += info.DailyRegister
|
|
temp.DailyAppAmount += info.DailyAppAmount
|
|
temp.DailyGameAmount += info.DailyGameAmount
|
|
temp.RechargeOrder += info.RechargeOrder
|
|
}
|
|
list = append(list, temp)
|
|
for i, j := 0, len(list)-1; i < j; i, j = i+1, j-1 {
|
|
list[i], list[j] = list[j], list[i]
|
|
}
|
|
|
|
// 设置表头样式
|
|
headStyleID, _ := f.NewStyle(titlestyle)
|
|
// 设置表头样式
|
|
headStyleID0, _ := f.NewStyle(cellstyle)
|
|
|
|
if newxlslfile {
|
|
err = NewXlslFile0(f, list, headStyleID, headStyleID0)
|
|
if err != nil {
|
|
log.Error("InnerServerMonthStat excelize.OpenFile failed",
|
|
log.Any("fileName", filePath),
|
|
log.E(err))
|
|
return err
|
|
}
|
|
} else {
|
|
err = OpenOldXlsl(f, list)
|
|
if err != nil {
|
|
log.Error("InnerServerMonthStat excelize.OpenFile failed",
|
|
log.Any("fileName", filePath),
|
|
log.E(err))
|
|
return err
|
|
}
|
|
}
|
|
|
|
row := 0
|
|
cols := f.GetRows(sheetName)
|
|
for rowindex, col := range cols {
|
|
for colindex, rowCell := range col {
|
|
if rowCell == "" {
|
|
continue
|
|
}
|
|
if colindex == 0 {
|
|
if rowCell == dayrow {
|
|
row = rowindex + 1
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if row == 0 {
|
|
return errors.New("InnerServerMonthStat row <= 0")
|
|
}
|
|
|
|
for _, info := range list {
|
|
cellday, serr := utils.CoordinatesToCellName(info.BeginCol, row)
|
|
if serr != nil {
|
|
log.Error("InnerServerMonthStat CoordinatesToCellName ", log.Any("err", serr))
|
|
return serr
|
|
}
|
|
cellregister, serr := utils.CoordinatesToCellName(info.BeginCol+1, row)
|
|
if serr != nil {
|
|
log.Error("InnerServerMonthStat CoordinatesToCellName ", log.Any("err", serr))
|
|
return serr
|
|
}
|
|
cellrecharge, serr := utils.CoordinatesToCellName(info.BeginCol+2, row)
|
|
if serr != nil {
|
|
log.Error("InnerServerMonthStat CoordinatesToCellName ", log.Any("err", serr))
|
|
|
|
return serr
|
|
}
|
|
cellgame, serr := utils.CoordinatesToCellName(info.BeginCol+3, row)
|
|
if serr != nil {
|
|
log.Error("InnerServerMonthStat CoordinatesToCellName ", log.Any("err", serr))
|
|
|
|
return serr
|
|
}
|
|
cellorder, serr := utils.CoordinatesToCellName(info.BeginCol+4, row)
|
|
if serr != nil {
|
|
log.Error("InnerServerMonthStat CoordinatesToCellName ", log.Any("err", serr))
|
|
|
|
return serr
|
|
}
|
|
|
|
f.SetCellStyle(sheetName, cellday, cellday, headStyleID0)
|
|
totalamount := fmt.Sprintf("%.2f", float64(info.DailyVistCount)*0.01)
|
|
f.SetCellValue(sheetName, cellday, totalamount)
|
|
|
|
f.SetCellStyle(sheetName, cellregister, cellregister, headStyleID0)
|
|
totalamount = fmt.Sprintf("%.2f", float64(info.DailyRegister)*0.01)
|
|
f.SetCellValue(sheetName, cellregister, totalamount)
|
|
|
|
f.SetCellStyle(sheetName, cellrecharge, cellrecharge, headStyleID0)
|
|
totalamount = fmt.Sprintf("%.2f", float64(info.DailyAppAmount)*0.0001)
|
|
f.SetCellValue(sheetName, cellrecharge, totalamount)
|
|
|
|
if info.AppId == 0 || info.AppId == 888 {
|
|
f.SetCellStyle(sheetName, cellgame, cellgame, headStyleID0)
|
|
totalamount = fmt.Sprintf("%.2f", float64(info.DailyGameAmount)*0.0001)
|
|
f.SetCellValue(sheetName, cellgame, totalamount)
|
|
}
|
|
|
|
f.SetCellStyle(sheetName, cellorder, cellorder, headStyleID0)
|
|
totalamount = fmt.Sprintf("%.2f", float64(info.RechargeOrder)*0.01)
|
|
f.SetCellValue(sheetName, cellorder, totalamount)
|
|
}
|
|
if newxlslfile {
|
|
err = f.SaveAs(filename)
|
|
if err != nil {
|
|
log.Error("InnerServerMonthStat SaveAs ", log.Any("err", err))
|
|
|
|
return err
|
|
}
|
|
} else {
|
|
err = f.Save()
|
|
if err != nil {
|
|
log.Error("InnerServerMonthStat Save ", log.Any("err", err))
|
|
|
|
return err
|
|
}
|
|
}
|
|
input, err := os.ReadFile(filename)
|
|
if err != nil {
|
|
log.Error("InnerServerMonthStat ReadFile ", log.Any("err", err))
|
|
|
|
return err
|
|
}
|
|
|
|
destinationFile := filePath + dayrow + ".xlsx"
|
|
err = os.WriteFile(destinationFile, input, 0777)
|
|
if err != nil {
|
|
log.Error("InnerServerMonthStat WriteFile ", log.Any("err", err))
|
|
|
|
return err
|
|
}
|
|
if statg.RchssendBot != nil {
|
|
err = statg.RchssendBot.Sendrchstochat("天宇公司综合日报", destinationFile,
|
|
tgbot.FileTypeDocument)
|
|
if err != nil {
|
|
log.Error("InnerServerMonthStat", log.Any("Sendrchstochat", err))
|
|
}
|
|
}
|
|
|
|
log.Info("InnerServerMonthStat end")
|
|
|
|
return nil
|
|
}
|
|
|
|
func InnerServerMonthStat2(ctx context.Context, begin, end *time.Time, cmd *tgbot.Command) error {
|
|
if statg.Conf.ServerIndex != statg.ServerIndexBili {
|
|
log.Error("InnerServerMonthStat serverindex",
|
|
log.Any("serverindex", statg.Conf.ServerIndex))
|
|
return nil
|
|
}
|
|
log.Info("InnerServerMonthStat start")
|
|
f := excelize.NewFile()
|
|
now := time.Now().AddDate(0, 0, -1)
|
|
start := utils.BeginOfMonth(now)
|
|
stop := utils.BeginWithTime(now).AddDate(0, 0, 1)
|
|
if begin != nil {
|
|
start = *begin
|
|
}
|
|
if end != nil {
|
|
stop = *end
|
|
}
|
|
listOrg := make([]*proto.InnerDailyRecharge, 0)
|
|
for id, info := range statg.Conf.InnerServers {
|
|
req := &proto.InnerRechargeReq{
|
|
InnerId: int32(id),
|
|
Begin: start,
|
|
End: stop,
|
|
}
|
|
url := info + "/api/stat/inner/recharge/list"
|
|
log.Info("InnerServerMonthStat", log.Any("req", url))
|
|
res := &proto.InnerDailyRechargeResp{}
|
|
err := InnerRequest(url, req, res)
|
|
if err != nil {
|
|
log.Error("InnerServerMonthStat InnerRequest",
|
|
log.Any("InnerRequest", err))
|
|
continue
|
|
}
|
|
if res == nil {
|
|
log.Error("InnerServerMonthStat InnerRequest data not found")
|
|
continue
|
|
}
|
|
if res.Data.List != nil && len(res.Data.List) > 0 {
|
|
listOrg = append(listOrg, res.Data.List...)
|
|
}
|
|
}
|
|
appNames := make(map[int32]string)
|
|
appNames[0] = "天宇总充值"
|
|
source := map[int64]*DayRecharge{}
|
|
for _, info := range listOrg {
|
|
appNames[info.AppId] = info.Name
|
|
temp := &proto.InnerDailyRecharge{
|
|
AppId: info.AppId,
|
|
Name: info.Name,
|
|
SumDate: info.SumDate,
|
|
DateTime: info.SumDate.Format(time.DateOnly),
|
|
DailyVistCount: info.DailyVistCount,
|
|
DailyRegister: info.DailyRegister,
|
|
DailyAppAmount: info.DailyAppAmount,
|
|
DailyGameAmount: info.DailyGameAmount,
|
|
RechargeOrder: info.RechargeOrder,
|
|
}
|
|
found, ok := source[info.SumDate.Unix()]
|
|
if !ok {
|
|
found = &DayRecharge{
|
|
SumDate: info.SumDate,
|
|
List: make([]*proto.InnerDailyRecharge, 0),
|
|
Dict: make(map[int32]*proto.InnerDailyRecharge),
|
|
}
|
|
source[info.SumDate.Unix()] = found
|
|
}
|
|
found.List = append(found.List, temp)
|
|
found.Dict[info.AppId] = temp
|
|
found.Total.Name = "天宇总充值"
|
|
found.Total.DateTime = info.SumDate.Format(time.DateOnly)
|
|
found.Total.SumDate = info.SumDate
|
|
found.Total.DailyVistCount += temp.DailyVistCount
|
|
found.Total.DailyRegister += temp.DailyRegister
|
|
found.Total.DailyAppAmount += temp.DailyAppAmount
|
|
found.Total.DailyGameAmount += temp.DailyGameAmount
|
|
found.Total.RechargeOrder += temp.RechargeOrder
|
|
}
|
|
appIds := make([]int, 0)
|
|
for id, _ := range appNames {
|
|
appIds = append(appIds, int(id))
|
|
}
|
|
sort.Sort(sort.IntSlice(appIds))
|
|
AppInfos := make([]*AppInformation, 0)
|
|
for _, id := range appIds {
|
|
AppInfos = append(AppInfos, &AppInformation{
|
|
Id: id,
|
|
Name: appNames[int32(id)],
|
|
})
|
|
}
|
|
for _, value := range source {
|
|
value.Dict[0] = &value.Total
|
|
}
|
|
// 按APP汇总
|
|
for _, app := range AppInfos {
|
|
for _, value := range source {
|
|
if info, ok := value.Dict[int32(app.Id)]; ok {
|
|
app.Total.DailyVistCount += info.DailyVistCount
|
|
app.Total.DailyRegister += info.DailyRegister
|
|
app.Total.DailyAppAmount += info.DailyAppAmount
|
|
app.Total.DailyGameAmount += info.DailyGameAmount
|
|
app.Total.RechargeOrder += info.RechargeOrder
|
|
}
|
|
}
|
|
}
|
|
// 设置表头样式
|
|
headStyleID, _ := f.NewStyle(titlestyle)
|
|
// 设置表头样式
|
|
headStyleID0, _ := f.NewStyle(cellstyle)
|
|
// 设置表结构
|
|
err := NewXlslFile2(f, AppInfos, headStyleID, headStyleID0, now)
|
|
if err != nil {
|
|
log.Error("InnerServerMonthStat excelize.OpenFile failed", log.E(err))
|
|
return err
|
|
}
|
|
// 设置数据
|
|
row := 4
|
|
for day := start; day.Before(stop); day = day.AddDate(0, 0, 1) {
|
|
if found, ok := source[day.Unix()]; ok {
|
|
for _, app := range AppInfos {
|
|
if info, ok2 := found.Dict[int32(app.Id)]; ok2 {
|
|
_ = SetAppData(f, info, row, app.BeginCol, headStyleID0)
|
|
}
|
|
}
|
|
}
|
|
row++
|
|
}
|
|
// 设置总计
|
|
row = utils.EndOfMonth(now).Day() - utils.BeginOfMonth(now).Day() + 1 + 4
|
|
for _, app := range AppInfos {
|
|
_ = SetAppData(f, &app.Total, row, app.BeginCol, headStyleID0)
|
|
}
|
|
// 保存文件
|
|
tempDir := os.TempDir()
|
|
fmt.Println("临时文件目录:", tempDir)
|
|
|
|
// 创建一个临时文件
|
|
yesterday := utils.BeginOfYesterday().Format(time.DateOnly)
|
|
fileName := path.Join(tempDir, fmt.Sprintf("%s.xlsx", yesterday))
|
|
//defer os.Remove(fileName) // 确保程序结束后删除临时文件
|
|
err = f.SaveAs(fileName)
|
|
if err != nil {
|
|
log.Error("InnerServerMonthStat SaveAs ", log.Any("err", err))
|
|
return err
|
|
}
|
|
// 发送文件
|
|
if cmd != nil {
|
|
err = cmd.SendFile("天宇公司综合日报", fileName)
|
|
} else {
|
|
if statg.RchssendBot != nil {
|
|
err = statg.RchssendBot.Sendrchstochat("天宇公司综合日报", fileName, tgbot.FileTypeDocument)
|
|
if err != nil {
|
|
log.Error("InnerServerMonthStat", log.Any("Sendrchstochat", err))
|
|
}
|
|
}
|
|
}
|
|
return err
|
|
}
|
|
|
|
func SetAppData(f *excelize.File, info *proto.InnerDailyRecharge, row, col int, headStyle int) error {
|
|
cellday, serr := utils.CoordinatesToCellName(col, row)
|
|
if serr != nil {
|
|
log.Error("InnerServerMonthStat CoordinatesToCellName ", log.Any("err", serr))
|
|
return serr
|
|
}
|
|
cellregister, serr := utils.CoordinatesToCellName(col+1, row)
|
|
if serr != nil {
|
|
log.Error("InnerServerMonthStat CoordinatesToCellName ", log.Any("err", serr))
|
|
return serr
|
|
}
|
|
cellrecharge, serr := utils.CoordinatesToCellName(col+2, row)
|
|
if serr != nil {
|
|
log.Error("InnerServerMonthStat CoordinatesToCellName ", log.Any("err", serr))
|
|
|
|
return serr
|
|
}
|
|
cellgame, serr := utils.CoordinatesToCellName(col+3, row)
|
|
if serr != nil {
|
|
log.Error("InnerServerMonthStat CoordinatesToCellName ", log.Any("err", serr))
|
|
|
|
return serr
|
|
}
|
|
cellorder, serr := utils.CoordinatesToCellName(col+4, row)
|
|
if serr != nil {
|
|
log.Error("InnerServerMonthStat CoordinatesToCellName ", log.Any("err", serr))
|
|
|
|
return serr
|
|
}
|
|
|
|
f.SetCellStyle(sheetName, cellday, cellday, headStyle)
|
|
totalamount := fmt.Sprintf("%.2f", float64(info.DailyVistCount)*0.01)
|
|
f.SetCellValue(sheetName, cellday, totalamount)
|
|
|
|
f.SetCellStyle(sheetName, cellregister, cellregister, headStyle)
|
|
totalamount = fmt.Sprintf("%.2f", float64(info.DailyRegister)*0.01)
|
|
f.SetCellValue(sheetName, cellregister, totalamount)
|
|
|
|
f.SetCellStyle(sheetName, cellrecharge, cellrecharge, headStyle)
|
|
totalamount = fmt.Sprintf("%.2f", float64(info.DailyAppAmount)*0.0001)
|
|
f.SetCellValue(sheetName, cellrecharge, totalamount)
|
|
|
|
if info.AppId == 0 || info.AppId == 888 {
|
|
f.SetCellStyle(sheetName, cellgame, cellgame, headStyle)
|
|
totalamount = fmt.Sprintf("%.2f", float64(info.DailyGameAmount)*0.0001)
|
|
f.SetCellValue(sheetName, cellgame, totalamount)
|
|
}
|
|
|
|
f.SetCellStyle(sheetName, cellorder, cellorder, headStyle)
|
|
totalamount = fmt.Sprintf("%.2f", float64(info.RechargeOrder)*0.01)
|
|
f.SetCellValue(sheetName, cellorder, totalamount)
|
|
|
|
return nil
|
|
}
|
|
|
|
func GetDailyStatList(ctx context.Context, day *time.Time, yesterday *time.Time) (
|
|
[]*dailystatmod.DailyStatMod, map[int32]*appInfoMod.AppInfo, error) {
|
|
list := make([]*dailystatmod.DailyStatMod, 0)
|
|
appinfo, serr := appInfoMod.GetAllAppIdMap(ctx, bson.M{})
|
|
if serr != nil {
|
|
log.Error("InnerServerDailyStat appInfoMod.GetAllAppInfo", log.Any("serr", serr))
|
|
return nil, nil, serr
|
|
}
|
|
for _, info := range appinfo {
|
|
rechargelist, serr := dailystatmod.GetDailyStatTotalList(ctx, &info.AppId, yesterday, day)
|
|
if serr != nil {
|
|
log.Error("InnerServerDailyStat dailystatmod.GetDailyStatTotalList", log.Any("serr", serr))
|
|
continue
|
|
}
|
|
if rechargelist != nil {
|
|
list = append(list, rechargelist...)
|
|
} else {
|
|
log.Error("InnerServerDailyStat rechargelist nil")
|
|
}
|
|
}
|
|
return list, appinfo, nil
|
|
}
|
|
|
|
type appDailyDataStat struct {
|
|
AppName string `json:"appName"` // 产品名称
|
|
AppId int32 `json:"appId"` // 产品Id
|
|
SumDate time.Time `json:"sumDate"` // 日期
|
|
Type string `json:"type"` // 产品类型,pay:付费,free:免费
|
|
TotalVisitCount float64 `json:"totalVisitCount"` // 日活跃
|
|
TotalNewCount float64 `json:"totalNewCount"` // 日注册
|
|
TotalVidAmount float64 `json:"totalVidAmount"` // 视频日充值
|
|
TotalQpAmount float64 `json:"totalQpAmount"` // 棋牌日充值
|
|
VipRechargeCount float64 `json:"vipRechargeCount"` // vip交易笔数
|
|
CoinRechargeCount float64 `json:"coinRechargeCount"` // 金币交易笔数
|
|
RechargeCount float64 `json:"rechargeCount"` // 交易总笔数
|
|
MonthAmount float64 `json:"monthAmount"` // 当月总充值
|
|
MonthNewCount float64 `json:"monthNewCount"` // 当月总注册
|
|
MonthRechargeCount float64 `json:"monthRechargeCount"` // 当月总交易笔数
|
|
MonthVisitCount float64 `json:"monthVisitCount"` // 当月总日活
|
|
}
|
|
|
|
func TianyuAppDailyData(ctx context.Context) ([]*appDailyDataStat, error) {
|
|
resp := make([]*appDailyDataStat, 0)
|
|
if statg.Conf.ServerIndex != statg.ServerIndexBili {
|
|
log.Error("InnerServerMonthStat serverindex",
|
|
log.Any("serverindex", statg.Conf.ServerIndex))
|
|
return resp, nil
|
|
}
|
|
|
|
log.Info("InnerServerMonthStat start")
|
|
now := time.Now().AddDate(0, 0, -1)
|
|
start := utils.BeginOfMonth(now)
|
|
stop := utils.BeginWithTime(now).AddDate(0, 0, 1)
|
|
|
|
listOrg := make([]*proto.InnerDailyRecharge, 0)
|
|
for id, info := range statg.Conf.InnerServers {
|
|
req := &proto.InnerRechargeReq{
|
|
InnerId: int32(id),
|
|
Begin: start,
|
|
End: stop,
|
|
}
|
|
url := info + "/api/stat/inner/recharge/list"
|
|
log.Info("InnerServerMonthStat", log.Any("req", url))
|
|
res := &proto.InnerDailyRechargeResp{}
|
|
err := InnerRequest(url, req, res)
|
|
if err != nil {
|
|
log.Error("InnerServerMonthStat InnerRequest",
|
|
log.Any("InnerRequest", err))
|
|
continue
|
|
}
|
|
if res == nil {
|
|
log.Error("InnerServerMonthStat InnerRequest data not found")
|
|
continue
|
|
}
|
|
if res.Data.List != nil && len(res.Data.List) > 0 {
|
|
listOrg = append(listOrg, res.Data.List...)
|
|
}
|
|
}
|
|
|
|
for _, info := range listOrg {
|
|
temp := &appDailyDataStat{
|
|
AppId: 100000 + info.AppId,
|
|
AppName: info.Name,
|
|
SumDate: info.SumDate,
|
|
TotalVisitCount: float64(info.DailyVistCount),
|
|
TotalNewCount: float64(info.DailyRegister),
|
|
TotalVidAmount: float64(info.DailyAppAmount),
|
|
TotalQpAmount: float64(info.DailyGameAmount),
|
|
RechargeCount: float64(info.RechargeOrder),
|
|
}
|
|
resp = append(resp, temp)
|
|
|
|
}
|
|
return resp, nil
|
|
}
|