// Copyright 2019 getensh.com. All rights reserved.
// Use of this source code is governed by getensh.com.

/*
 package gd_crontab 作为日报手动统计微服务客户端向网关提供服务
*/

package gd_crontab

import (
	"context"
	"fmt"
	"time"

	"github.com/smallnest/rpcx/client"
	"github.com/smallnest/rpcx/protocol"
	"github.com/smallnest/rpcx/share"
)

type GdCrontabXClient struct {
	xcli client.XClient
}

func (a *GdCrontabXClient) Init(etcdAddrs []string, basePath, servicePath string) {
	opt := client.Option{
		Retries:        3,
		RPCPath:        share.DefaultRPCPath,
		ConnectTimeout: 10 * time.Second,
		SerializeType:  protocol.JSON,
		CompressType:   protocol.None,
		BackupLatency:  10 * time.Millisecond,
	}

	d := client.NewEtcdDiscovery(basePath, servicePath, etcdAddrs, nil)
	a.xcli = client.NewXClient(servicePath, client.Failtry, client.RandomSelect, d, opt)

}

func (a *GdCrontabXClient) InitForK8s(servicePath string, k8sServiceName string, k8sServicePort string) {
	opt := client.Option{
		Retries:        1,
		RPCPath:        share.DefaultRPCPath,
		ConnectTimeout: 10 * time.Second,
		SerializeType:  protocol.JSON,
		CompressType:   protocol.None,
		BackupLatency:  10 * time.Millisecond,
	}
	sd := client.NewPeer2PeerDiscovery(fmt.Sprintf("tcp@%s:%s", k8sServiceName, k8sServicePort), "")
	a.xcli = client.NewXClient(servicePath, client.Failtry, client.RandomSelect, sd, opt)
}

// LogGenerateReport 手动统计日报
func (a *GdCrontabXClient) LogGenerateReport(ctx context.Context, req *LogGenerateReportReq) (reply LogGenerateReportReply, err error) {
	err = a.xcli.Call(ctx, "LogGenerateReport", req, &reply)
	return
}
