diff --git a/cmd/amocli/getter.go b/cmd/amocli/getter.go index 1477d9f..218aa2a 100644 --- a/cmd/amocli/getter.go +++ b/cmd/amocli/getter.go @@ -100,12 +100,14 @@ func RunGetter[V any, G Getter[V]](g G, flags *mtool.Flags) { }, ) if err != nil { - log.Printf("GetLeadsByIDs(...): %s\n", err) + log.Printf("thread(%d): %s(...): %s\n", + thread, g.GetFuncName(), err) + return } valueChan <- values if opts.Verbose { log.Printf( - "%d: Got %d %s\n", + "thread(%d): Got %d %s\n", thread, len(values), g.GetNameMul(), ) diff --git a/cmd/amocli/main.go b/cmd/amocli/main.go index 7998eb8..653d819 100644 --- a/cmd/amocli/main.go +++ b/cmd/amocli/main.go @@ -1,7 +1,10 @@ package main import "surdeus.su/core/cli/mtool" +import "runtime/debug" import "os" +import "log" +import "fmt" func main() { tool.Run(os.Args[1:]) @@ -14,4 +17,12 @@ var tool = mtool.T("amocli").Subs( updateComs, getContacts, + mtool.T("version").Func(func(flags *mtool.Flags){ + bi, ok := debug.ReadBuildInfo() + if !ok { + log.Fatalf("could not read build info\n") + return + } + fmt.Println(bi.Main.Version) + }), ) diff --git a/cmd/amocli/updatecom.go b/cmd/amocli/updatecom.go index 11235b6..7cb6772 100644 --- a/cmd/amocli/updatecom.go +++ b/cmd/amocli/updatecom.go @@ -1,56 +1,30 @@ package main import "surdeus.su/core/amo" -import "surdeus.su/core/amo/api" +//import "surdeus.su/core/amo/api" import "surdeus.su/core/cli/mtool" -import "surdeus.su/core/amo/companies" -import "encoding/json" -import "log" -import "os" +//import "surdeus.su/core/amo/companies" +//import "encoding/json" +//import "log" +//import "os" -const MEPR = api.MaxEntitiesPerRequest +type CompanyUpdater struct{} + +func (u CompanyUpdater) UpdateValues( + c *amo.Client, cs []amo.Company, +) ([]amo.Company, error) { + return c.UpdateCompanies(cs) +} + +func (u CompanyUpdater) GetNameMul() string { + return "companies" +} + +func (u CompanyUpdater) GetFuncName() string { + return "amo.UpdateCompanies" +} var updateComs = mtool.T("update-companies").Func(func(flags *mtool.Flags){ - var ( - opts DefaultFlags - ) - MakeDefaultFlags(&opts, flags) - flags.Parse() - - client, err := amo.NewClient(opts.SecretPath) - if err != nil { - log.Fatalf("NewAmoClient(...): %s\n", err) - } - - cs := []companies.Company{} - dec := json.NewDecoder(os.Stdin) - err = dec.Decode(&cs) - if err != nil { - log.Fatalf("json.Decode(...): %s\n", err) - } - - n := len(cs)/MEPR + 1 - for i:=0 ; i= len(cs) { - end = len(cs) - } - if len(cs) == 0 { - continue - } - slice := cs[start:end] - if opts.Verbose { - log.Printf("Updating %d companies...\n", len(slice)) - } - err = client.UpdateCompanies(slice) - if err != nil { - log.Fatalf( - "client.UpdateCompanies(...) len(...) =%d: %s\n", - len(slice), - err, - ) - } - } + RunUpdater(CompanyUpdater{}, flags) }) diff --git a/cmd/amocli/updater.go b/cmd/amocli/updater.go new file mode 100644 index 0000000..9698d75 --- /dev/null +++ b/cmd/amocli/updater.go @@ -0,0 +1,76 @@ +package main + +//import "surdeus.su/core/ss/u" +import "surdeus.su/core/amo" +import "surdeus.su/core/cli/mtool" +import "encoding/json" +import "os" +import "log" +import "time" + +type Updater[V, VR any] interface { + UpdateValues(*amo.Client, []V) (VR, error) + GetNameMul() string + GetFuncName() string +} + +func RunUpdater[V, VR any, U Updater[V, VR]]( + u U, flags *mtool.Flags, +) { + now := time.Now() + var ( + opts DefaultFlags + ) + MakeDefaultFlags(&opts, flags) + flags.Parse() + + client, err := amo.NewClient(opts.SecretPath) + if err != nil { + log.Fatalf("NewAmoClient(...): %s\n", err) + } + + values := []V{} + dec := json.NewDecoder(os.Stdin) + err = dec.Decode(&values) + if err != nil { + log.Fatalf("json.Decode(...): %s\n", err) + } + + finish := RunForSliceInThreads[V]( + opts.Threads, opts.MEPR, + values, func(thread int, s []V){ + _, err := u.UpdateValues(client, s) + if err != nil { + log.Printf( + "thread(%d): %s(...): %s\n", + thread, u.GetFuncName(), err, + ) + return + } + if opts.Verbose { + log.Printf( + "thread(%d): Updated %d %s\n", + thread, len(s), u.GetNameMul(), + ) + } + }, + ) + <-finish + if opts.Verbose { + rm := client.API.RequestsMade() + /*log.Printf( + "Summarized got %d %s\n", + len(finalValues), g.GetNameMul(), + )*/ + log.Printf( + "Made %d requests in process\n", + rm, + ) + took := time.Since(now).Seconds() + log.Printf( + "Took %f seconds\n", + took, + ) + log.Printf("RPS = %f\n", float64(rm)/took) + } +} diff --git a/companies.go b/companies.go index 080068e..fcc85f6 100644 --- a/companies.go +++ b/companies.go @@ -73,20 +73,20 @@ func (client *Client) UpdateCompany( func (client *Client) UpdateCompanies( cs []companies.Company, -) error { +) ([]Company, error) { if len(cs) > MEPR { - return ErrTooManyEntities + return nil, ErrTooManyEntities } + //ret := []Company{} err := client.API.Patch( "/api/v4/companies", cs, nil, ) - if err != nil { - return err + return nil, err } - return nil + return nil, nil }