updater.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package main
  2. //import "surdeus.su/core/ss/u"
  3. import "surdeus.su/core/amo"
  4. import "surdeus.su/core/cli/mtool"
  5. import "encoding/json"
  6. import "os"
  7. import "log"
  8. import "time"
  9. type Updater[V, VR any] interface {
  10. UpdateValues(*amo.Client, []V) (VR, error)
  11. GetNameMul() string
  12. GetFuncName() string
  13. }
  14. func RunUpdater[V, VR any, U Updater[V, VR]](
  15. u U, flags *mtool.Flags,
  16. ) {
  17. now := time.Now()
  18. var (
  19. opts DefaultFlags
  20. )
  21. MakeDefaultFlags(&opts, flags)
  22. flags.Parse()
  23. client, err := amo.NewClient(opts.SecretPath)
  24. if err != nil {
  25. log.Fatalf("NewAmoClient(...): %s\n", err)
  26. }
  27. values := []V{}
  28. dec := json.NewDecoder(os.Stdin)
  29. err = dec.Decode(&values)
  30. if err != nil {
  31. log.Fatalf("json.Decode(...): %s\n", err)
  32. }
  33. finish := RunForSliceInThreads[V](
  34. opts.Threads, opts.MEPR,
  35. values, func(thread int, s []V){
  36. _, err := u.UpdateValues(client, s)
  37. if err != nil {
  38. log.Printf(
  39. "thread(%d): %s(...): %s\n",
  40. thread, u.GetFuncName(), err,
  41. )
  42. return
  43. }
  44. if opts.Verbose {
  45. log.Printf(
  46. "thread(%d): Updated %d %s\n",
  47. thread, len(s), u.GetNameMul(),
  48. )
  49. }
  50. },
  51. )
  52. <-finish
  53. if opts.Verbose {
  54. rm := client.API.RequestsMade()
  55. /*log.Printf(
  56. "Summarized got %d %s\n",
  57. len(finalValues), g.GetNameMul(),
  58. )*/
  59. log.Printf(
  60. "Made %d requests in process\n",
  61. rm,
  62. )
  63. took := time.Since(now).Seconds()
  64. log.Printf(
  65. "Took %f seconds\n",
  66. took,
  67. )
  68. log.Printf("RPS = %f\n", float64(rm)/took)
  69. }
  70. }