fix: added needed options, now handling ctrl-c.

This commit is contained in:
Andrey Parhomenko 2024-06-05 23:36:57 +05:00
parent a8a0331890
commit e981111774
6 changed files with 189 additions and 6 deletions

View file

@ -19,6 +19,8 @@ type DefaultFlags struct {
All bool
StartPage int
EndPage int
Indent bool
}
func MakeDefaultFlags(opts *DefaultFlags, flags *mtool.Flags) {
@ -77,6 +79,12 @@ func MakeGetterFlags(
math.MaxInt,
"the page to end the requests",
)
flags.BoolVar(
&opts.Indent,
"no-indent",
true,
"disable indenting for JSON output",
)
}
// Run function for slice's parts in different threads.

View file

@ -5,10 +5,11 @@ import "surdeus.su/core/ss/urlenc"
import "surdeus.su/core/cli/mtool"
import "fmt"
//import "fmt"
import "log"
import "time"
import "os"
import "os/signal"
import "encoding/json"
type Getter[V any] interface {
@ -34,6 +35,21 @@ func RunGetter[V any, G Getter[V]](g G, flags *mtool.Flags) {
c.API.SetMRPS(opts.MRPS)
finalValues := []V{}
inter := make(chan os.Signal, 1)
signal.Notify(inter, os.Interrupt)
go func() {
<-inter
enc := json.NewEncoder(os.Stdout)
if opts.Indent {
enc.SetIndent("", " ")
}
err = enc.Encode(finalValues)
if err != nil {
log.Fatalf("json.Encode(...): %s\n", err)
}
os.Exit(0)
}()
if opts.All {
page := opts.StartPage
values, next, err := g.GetValues(
@ -128,6 +144,13 @@ func RunGetter[V any, G Getter[V]](g G, flags *mtool.Flags) {
for values := range valueChan {
finalValues = append(finalValues, values...)
if opts.Verbose {
log.Printf(
"thtread(main) %.2f%%, sument=%d",
float32(len(finalValues))/float32(len(ids))*100.,
len(finalValues),
)
}
}
if opts.Verbose {
@ -147,9 +170,13 @@ func RunGetter[V any, G Getter[V]](g G, flags *mtool.Flags) {
)
log.Printf("RPS = %f\n", float64(rm)/took)
}
bts, err := json.MarshalIndent(finalValues, "", " ")
enc := json.NewEncoder(os.Stdout)
if opts.Indent {
enc.SetIndent("", " ")
}
err = enc.Encode(finalValues)
if err != nil {
log.Fatalf("json.Marshal(...): %s\n", err)
log.Fatalf("json.Encode(...): %s\n", err)
}
fmt.Printf("%s\n", bts)
}

View file

@ -15,9 +15,12 @@ type LeadTuple struct {
func (client *Client) GetLeadTuples(
opts ...urlenc.Builder,
) ([]LeadTuple, NextFunc[[]LeadTuple], error) {
opts = append(
opts, urlenc.Value[string]{"with", "contacts"},
)
res := fmt.Sprintf(
"/api/v4/leads?%s",
urlenc.Join(opts...).Encode,
urlenc.Join(opts...).Encode(),
)
return client.GetLeadTuplesByURL(res)
}

118
scripts/inn-check.tengo Normal file
View file

@ -0,0 +1,118 @@
os := import("os")
fmt := import("fmt")
json := import("json")
args := os.args()[2:]
file_path := args[0]
contact_field_id := 231711
company_field_id := 1300614
lead_field_id := 686597
holders := json.decode(os.read_file(file_path))
//fmt.println(len(holders))
//os.exit(0)
patch := []
find_by_field_id := func(id, fields){
for _, field in fields {
if field.field_id == id {
return field
}
}
}
is_field_correct := func(field){
if !int(field.values[0].value) {
return false
}
str := string(field.values[0].value)
if len(str) <9 || len(str) > 12 {
return false
}
return true
}
//holders = holders[:2]
for i, h in holders {
company := {}
if !h.company_id {
continue
}
company.id = h.company_id
company_inn := find_by_field_id(company_field_id, h.company_inns)
//fmt.println(i, " reachd", company_inn)
if company_inn {
continue
}
contact_inn := find_by_field_id(contact_field_id, h.contact_inns)
lead_inn := find_by_field_id(lead_field_id, h.lead_inns)
if !contact_inn {
if !lead_inn || !is_field_correct(lead_inn) {
continue
}
company.__reason = "Взято из лида (в контакте не было)"
company.custom_fields_values = [{
field_id: company_field_id,
values: [{
value: string(lead_inn.values[0].value)
}]
}]
patch += [company]
continue
}
if !lead_inn {
if !is_field_correct(contact_inn) {
continue
}
company.__reason = "Взято из контакта (в лиде не было)"
company.custom_fields_values =[{
field_id: company_field_id,
values: [{
value: string(contact_inn.values[0].value)
}]
}]
} else {
if int(contact_inn.values[0].value) != int(lead_inn.values[0].value) {
continue
}
if !is_field_correct(contact_inn) {
continue
}
company.__reason = "Взято из лида и контакта"
company.custom_fields_values =[{
field_id: company_field_id,
values: [{
value: string(contact_inn.values[0].value)
}]
}]
}
patch += [company]
/*if h.company_inns {
for _, field in h.company_inns {
company.custom_fields_values += [{
field_id: field.field_id,
values: [{
value: field.values[0].value
}]
}]
}
}
companies += [company]*/
}
//patch = patch[:500]
//fmt.println(len(patch))
bts := json.indent(json.encode(patch), "", " ")
fmt.println(string(bts))

View file

@ -0,0 +1,17 @@
os := import("os")
fmt := import("fmt")
json := import("json")
args := os.args()[2:]
file_path := args[0]
arr := json.decode(os.read_file(file_path))
ret := {}
for _, v in arr {
if !v.lead || !v.lead.id{
continue
}
ret[string(v.lead.id)] = 1
}
fmt.println(len(ret))

10
scripts/len.tengo Normal file
View file

@ -0,0 +1,10 @@
os := import("os")
fmt := import("fmt")
json := import("json")
args := os.args()[2:]
file_path := args[0]
arr := json.decode(os.read_file(file_path))
fmt.println(len(arr))