2024-06-05 00:08:54 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import "surdeus.su/core/amo"
|
|
|
|
import "surdeus.su/core/ss/urlenc"
|
|
|
|
|
|
|
|
import "surdeus.su/core/cli/mtool"
|
|
|
|
|
2024-06-05 21:36:57 +03:00
|
|
|
//import "fmt"
|
2024-06-05 00:08:54 +03:00
|
|
|
import "log"
|
|
|
|
import "time"
|
|
|
|
import "os"
|
2024-06-05 21:36:57 +03:00
|
|
|
import "os/signal"
|
2024-06-05 00:08:54 +03:00
|
|
|
import "encoding/json"
|
|
|
|
|
|
|
|
type Getter[V any] interface {
|
|
|
|
GetValues(*amo.Client, ...urlenc.Builder) ([]V, amo.NextFunc[[]V], error)
|
|
|
|
GetNameMul() string
|
|
|
|
GetFuncName() string
|
|
|
|
}
|
|
|
|
|
|
|
|
func RunGetter[V any, G Getter[V]](g G, flags *mtool.Flags) {
|
|
|
|
var (
|
|
|
|
opts DefaultFlags
|
|
|
|
)
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
MakeDefaultFlags(&opts, flags)
|
|
|
|
MakeGetterFlags(&opts, flags)
|
|
|
|
|
|
|
|
idStrs := flags.Parse()
|
|
|
|
c, err := amo.NewClient(opts.SecretPath)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("NewAmoClient(...): %s\n", err)
|
|
|
|
}
|
|
|
|
c.API.SetMRPS(opts.MRPS)
|
2024-06-05 22:46:14 +03:00
|
|
|
enc := json.NewEncoder(os.Stdout)
|
|
|
|
if opts.Indent {
|
|
|
|
enc.SetIndent("", " ")
|
|
|
|
}
|
2024-06-05 00:08:54 +03:00
|
|
|
|
2024-06-05 21:36:57 +03:00
|
|
|
inter := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(inter, os.Interrupt)
|
|
|
|
go func() {
|
|
|
|
<-inter
|
2024-06-05 22:46:14 +03:00
|
|
|
os.Stdout.Sync()
|
2024-06-05 21:36:57 +03:00
|
|
|
os.Exit(0)
|
|
|
|
}()
|
|
|
|
|
2024-06-05 22:46:14 +03:00
|
|
|
finalNum := 0
|
2024-06-05 00:08:54 +03:00
|
|
|
if opts.All {
|
|
|
|
page := opts.StartPage
|
|
|
|
values, next, err := g.GetValues(
|
|
|
|
c,
|
|
|
|
urlenc.Value[int]{"page", page},
|
|
|
|
urlenc.Value[int]{"limit", opts.MEPR},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf(
|
|
|
|
"%s(...): %s\n", g.GetFuncName(), err,
|
|
|
|
)
|
|
|
|
}
|
2024-06-05 22:46:14 +03:00
|
|
|
finalNum += len(values)
|
|
|
|
|
2024-06-05 00:08:54 +03:00
|
|
|
if opts.Verbose {
|
|
|
|
log.Printf("Got %d %s (%d, page %d)\n",
|
2024-06-05 22:46:14 +03:00
|
|
|
len(values), g.GetNameMul(), finalNum, page)
|
2024-06-05 00:08:54 +03:00
|
|
|
}
|
2024-06-05 22:46:14 +03:00
|
|
|
|
|
|
|
for _, value := range values {
|
|
|
|
err := enc.Encode(value)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("json.Encode(...): %s\n", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-05 00:08:54 +03:00
|
|
|
page++
|
|
|
|
for page <= opts.EndPage && next != nil {
|
|
|
|
values, next, err = next()
|
|
|
|
if err != nil {
|
2024-06-05 19:53:00 +03:00
|
|
|
log.Fatalf(
|
|
|
|
"%s(...): %s\n",
|
|
|
|
g.GetFuncName(), err,
|
|
|
|
)
|
2024-06-05 00:08:54 +03:00
|
|
|
}
|
2024-06-05 22:46:14 +03:00
|
|
|
finalNum += len(values)
|
2024-06-05 00:08:54 +03:00
|
|
|
if opts.Verbose {
|
|
|
|
log.Printf("Got %d %s (%d, page %d)\n",
|
|
|
|
len(values), g.GetNameMul(),
|
2024-06-05 22:46:14 +03:00
|
|
|
finalNum, page)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, value := range values {
|
|
|
|
err = enc.Encode(value)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("json.Encode(...): %s\n", err)
|
|
|
|
}
|
2024-06-05 00:08:54 +03:00
|
|
|
}
|
2024-06-05 22:46:14 +03:00
|
|
|
|
2024-06-05 00:08:54 +03:00
|
|
|
page++
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
ids := ReadIDs(idStrs)
|
|
|
|
if len(ids) == 0 {
|
|
|
|
log.Fatalf("Got no IDs to read %s", g.GetNameMul())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
valueChan := make(chan []V)
|
|
|
|
finish := RunForSliceInThreads[int](
|
|
|
|
opts.Threads, opts.MEPR,
|
|
|
|
ids, func(thread int, s []int){
|
|
|
|
values, _, err := g.GetValues(
|
|
|
|
c,
|
|
|
|
urlenc.Array[int]{
|
|
|
|
"id",
|
|
|
|
s,
|
|
|
|
},
|
|
|
|
urlenc.Value[string]{
|
|
|
|
"with",
|
|
|
|
"contacts",
|
|
|
|
},
|
|
|
|
urlenc.Value[int]{
|
|
|
|
"limit",
|
|
|
|
opts.MEPR,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
2024-06-05 15:50:54 +03:00
|
|
|
log.Printf("thread(%d): %s(...): %s\n",
|
|
|
|
thread, g.GetFuncName(), err)
|
|
|
|
return
|
2024-06-05 00:08:54 +03:00
|
|
|
}
|
|
|
|
valueChan <- values
|
|
|
|
if opts.Verbose {
|
|
|
|
log.Printf(
|
2024-06-05 15:50:54 +03:00
|
|
|
"thread(%d): Got %d %s\n",
|
2024-06-05 00:08:54 +03:00
|
|
|
thread, len(values),
|
|
|
|
g.GetNameMul(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
//var wg sync.WaitGroup
|
|
|
|
go func(){
|
|
|
|
// Waiting for appending so we do not lose data.
|
|
|
|
<-finish
|
|
|
|
for len(valueChan) > 0 {}
|
|
|
|
close(valueChan)
|
|
|
|
}()
|
|
|
|
|
|
|
|
for values := range valueChan {
|
2024-06-05 22:46:14 +03:00
|
|
|
finalNum += len(values)
|
|
|
|
for _, value := range values {
|
|
|
|
err := enc.Encode(value)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("json.Encode(...): %s\n", err)
|
|
|
|
}
|
|
|
|
}
|
2024-06-05 21:36:57 +03:00
|
|
|
if opts.Verbose {
|
|
|
|
log.Printf(
|
|
|
|
"thtread(main) %.2f%%, sument=%d",
|
2024-06-05 22:46:14 +03:00
|
|
|
float32(finalNum)/float32(len(ids))*100.,
|
|
|
|
finalNum,
|
2024-06-05 21:36:57 +03:00
|
|
|
)
|
|
|
|
}
|
2024-06-05 00:08:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if opts.Verbose {
|
|
|
|
rm := c.API.RequestsMade()
|
|
|
|
log.Printf(
|
|
|
|
"Summarized got %d %s\n",
|
2024-06-05 22:46:14 +03:00
|
|
|
finalNum, g.GetNameMul(),
|
2024-06-05 00:08:54 +03:00
|
|
|
)
|
|
|
|
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)
|
|
|
|
}
|
2024-06-05 21:36:57 +03:00
|
|
|
|
2024-06-05 00:08:54 +03:00
|
|
|
}
|