This commit is contained in:
Andrey Parhomenko 2024-06-05 17:50:54 +05:00
parent c8cab4def3
commit cbe56c94d1
5 changed files with 117 additions and 54 deletions

View file

@ -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(),
)

View file

@ -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)
}),
)

View file

@ -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<n ; i++ {
start := i * MEPR
end := start + MEPR
if end >= 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)
})

76
cmd/amocli/updater.go Normal file
View file

@ -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)
}
}

View file

@ -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
}