From e981111774b8c5417d54260c0968545e5e58464e Mon Sep 17 00:00:00 2001 From: surdeus Date: Wed, 5 Jun 2024 23:36:57 +0500 Subject: [PATCH] fix: added needed options, now handling ctrl-c. --- cmd/amocli/common.go | 8 ++ cmd/amocli/getter.go | 37 +++++++-- lead-tuple.go | 5 +- scripts/inn-check.tengo | 118 +++++++++++++++++++++++++++++ scripts/len-uniq-tuple-leads.tengo | 17 +++++ scripts/len.tengo | 10 +++ 6 files changed, 189 insertions(+), 6 deletions(-) create mode 100644 scripts/inn-check.tengo create mode 100644 scripts/len-uniq-tuple-leads.tengo create mode 100644 scripts/len.tengo diff --git a/cmd/amocli/common.go b/cmd/amocli/common.go index 7573433..02d65f4 100644 --- a/cmd/amocli/common.go +++ b/cmd/amocli/common.go @@ -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. diff --git a/cmd/amocli/getter.go b/cmd/amocli/getter.go index 6dd31f0..0961069 100644 --- a/cmd/amocli/getter.go +++ b/cmd/amocli/getter.go @@ -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, "", " ") - if err != nil { - log.Fatalf("json.Marshal(...): %s\n", err) + + enc := json.NewEncoder(os.Stdout) + if opts.Indent { + enc.SetIndent("", " ") + } + err = enc.Encode(finalValues) + if err != nil { + log.Fatalf("json.Encode(...): %s\n", err) } - fmt.Printf("%s\n", bts) } diff --git a/lead-tuple.go b/lead-tuple.go index 91d572e..05e5fd0 100644 --- a/lead-tuple.go +++ b/lead-tuple.go @@ -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) } diff --git a/scripts/inn-check.tengo b/scripts/inn-check.tengo new file mode 100644 index 0000000..75a40fc --- /dev/null +++ b/scripts/inn-check.tengo @@ -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)) + diff --git a/scripts/len-uniq-tuple-leads.tengo b/scripts/len-uniq-tuple-leads.tengo new file mode 100644 index 0000000..a58750b --- /dev/null +++ b/scripts/len-uniq-tuple-leads.tengo @@ -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)) diff --git a/scripts/len.tengo b/scripts/len.tengo new file mode 100644 index 0000000..fbdd168 --- /dev/null +++ b/scripts/len.tengo @@ -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))