<commit_before>package lib

import (
	"Yearning-go/src/model"
	pb "Yearning-go/src/proto"
	"context"
	"google.golang.org/grpc"
	"google.golang.org/grpc/connectivity"
	"log"
	"sync"
	"sync/atomic"
	"time"
)

var (
	globalGRPCconns *grpc.ClientConn
	lock            sync.Mutex
	config          atomic.Value
)

const TIMEOUT = time.Second * 60

type QueryDeal struct {
	Sql              string   `json:"sql"`
	DataBase         string   `json:"data_base"`
	Source           string   `json:"source"`
	InsulateWordList []string `json:"insulate_word_list"`
}

func FetchGRPCConn() (*grpc.ClientConn, error) {
	if c := config.Load(); c != nil {
		if c.(*grpc.ClientConn).GetState() == connectivity.Ready {
			return c.(*grpc.ClientConn), nil
		}
	}

	lock.Lock()

	defer lock.Unlock()

	cli, err := newGrpcConn()

	cli.Target()

	if err != nil {
		return nil, err
	}

	config.Store(cli)

	return cli, nil
}

func newGrpcConn() (*grpc.ClientConn, error) {
	conn, err := grpc.Dial(
		model.Grpc,
		grpc.WithInsecure(),
	)
	if err != nil {
		return nil, err
	}
	return conn, nil
}

func TsClient(order *pb.LibraAuditOrder) ([]*pb.Record, error) {

	conn, err := FetchGRPCConn()

	if err != nil {
		return nil, err
	}

	c := pb.NewJunoClient(conn)
	ctx, cancel := context.WithTimeout(context.Background(), TIMEOUT)
	defer func() {
		cancel()
	}()
	r, err := c.OrderDeal(ctx, order)
	if err != nil {
		log.Printf("could not connect: %v", err)
		return []*pb.Record{}, err
	}
	return r.Record, nil
}

func ExDDLClient(order *pb.LibraAuditOrder) {
	// Set up a connection to the server.

	conn, err := FetchGRPCConn()

	if err != nil {
		log.Println(err.Error())
		return
	}

	c := pb.NewJunoClient(conn)
	ctx, cancel := context.WithTimeout(context.Background(), TIMEOUT)
	defer func() {
		cancel()
	}()
	_, err = c.OrderDDLExec(ctx, order)
	if err != nil {
		log.Printf("could not connect: %v", err)
		MessagePush(order.WorkId, 4, "")
		return
	}
	MessagePush(order.WorkId, 1, "")
}

func ExDMLClient(order *pb.LibraAuditOrder) {

	conn, err := FetchGRPCConn()

	if err != nil {
		log.Println(err.Error())
		return
	}

	// Set up a connection to the server.
	c := pb.NewJunoClient(conn)

	ctx, cancel := context.WithTimeout(context.Background(), TIMEOUT)
	defer func() {
		cancel()
	}()
	_, err = c.OrderDMLExec(ctx, order)
	if err != nil {
		log.Printf("could not connect: %v", err)
		MessagePush(order.WorkId, 4, "")
	}
	MessagePush(order.WorkId, 1, "")
}

func ExAutoTask(order *pb.LibraAuditOrder) bool {

	conn, err := FetchGRPCConn()

	if err != nil {
		log.Println(err.Error())
		return false
	}

	c := pb.NewJunoClient(conn)

	ctx, cancel := context.WithTimeout(context.Background(), TIMEOUT)

	defer func() {
		cancel()
	}()<commit_msg>@FIXED #499<commit_after>package lib

import (
	"Yearning-go/src/model"
	pb "Yearning-go/src/proto"
	"context"
	"google.golang.org/grpc"
	"google.golang.org/grpc/connectivity"
	"log"
	"sync"
	"sync/atomic"
	"time"
)

var (
	globalGRPCconns *grpc.ClientConn
	lock            sync.Mutex
	config          atomic.Value
)

const TIMEOUT = time.Second * 60

type QueryDeal struct {
	Sql              string   `json:"sql"`
	DataBase         string   `json:"data_base"`
	Source           string   `json:"source"`
	InsulateWordList []string `json:"insulate_word_list"`
}

func FetchGRPCConn() (*grpc.ClientConn, error) {
	if c := config.Load(); c != nil {
		if c.(*grpc.ClientConn).GetState() == connectivity.Ready {
			return c.(*grpc.ClientConn), nil
		}
	}

	lock.Lock()

	defer lock.Unlock()

	cli, err := newGrpcConn()

	cli.Target()

	if err != nil {
		return nil, err
	}

	config.Store(cli)

	return cli, nil
}

func newGrpcConn() (*grpc.ClientConn, error) {
	conn, err := grpc.Dial(
		model.Grpc,
		grpc.WithInsecure(),
	)
	if err != nil {
		return nil, err
	}
	return conn, nil
}

func TsClient(order *pb.LibraAuditOrder) ([]*pb.Record, error) {

	conn, err := FetchGRPCConn()

	if err != nil {
		return nil, err
	}

	c := pb.NewJunoClient(conn)
	ctx, cancel := context.WithTimeout(context.Background(), TIMEOUT)
	defer func() {
		cancel()
	}()
	r, err := c.OrderDeal(ctx, order)
	if err != nil {
		log.Printf("could not connect: %v", err)
		return []*pb.Record{}, err
	}
	return r.Record, nil
}

func ExDDLClient(order *pb.LibraAuditOrder) {
	// Set up a connection to the server.

	conn, err := FetchGRPCConn()

	if err != nil {
		log.Println(err.Error())
		return
	}

	c := pb.NewJunoClient(conn)
	ctx, cancel := context.WithTimeout(context.Background(), TIMEOUT)
	defer func() {
		cancel()
	}()
	_, err = c.OrderDDLExec(ctx, order)
	if err != nil {
		log.Printf("could not connect: %v", err)
		MessagePush(order.WorkId, 4, "")
		return
	}
	MessagePush(order.WorkId, 1, "")
}

func ExDMLClient(order *pb.LibraAuditOrder) {

	conn, err := FetchGRPCConn()

	if err != nil {
		log.Println(err.Error())
		return
	}

	// Set up a connection to the server.
	c := pb.NewJunoClient(conn)

	ctx, cancel := context.WithTimeout(context.Background(), TIMEOUT)
	defer func() {
		cancel()
	}()
	_, err = c.OrderDMLExec(ctx, order)
	if err != nil {
		log.Printf("could not connect: %v", err)
		MessagePush(order.WorkId, 4, "")
		return
	}
	MessagePush(order.WorkId, 1, "")
}

func ExAutoTask(order *pb.LibraAuditOrder) bool {

	conn, err := FetchGRPCConn()

	if err != nil {
		log.Println(err.Error())
		return false
	}

	c := pb.NewJunoClient(conn)

	ctx, cancel := context.WithTimeout(context.Background(), TIMEOUT)

	defer func() {
		cancel()
	}()
{'fraction_non_alphanumeric': 0.15871274456024867, 'fraction_numerical': 0.0027427317608337905, 'mean_word_length': 3.4580277098614505, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4893184', 'n_tokens_mistral': 2180, 'n_tokens_neox': 2089, 'n_words': 476}