From 546600a027a88b30903512059c643786abb7c855 Mon Sep 17 00:00:00 2001 From: surdeus Date: Fri, 31 May 2024 22:19:41 +0500 Subject: [PATCH] fix: fixed the MRPS. --- api/api.go | 16 ++++++++-------- cmd/amocli/getlead.go | 4 ++++ common/common.go | 29 +++++++++++++++++++++++------ contacts.go | 2 ++ 4 files changed, 37 insertions(+), 14 deletions(-) diff --git a/api/api.go b/api/api.go index 3ed30e7..1265a6e 100644 --- a/api/api.go +++ b/api/api.go @@ -40,7 +40,7 @@ type Client struct { mrps int ticker *time.Ticker - req chan struct{} + req, reqNeed chan struct{} requestsMade int64 } @@ -125,16 +125,16 @@ func NewAPI(secretPath string) (*Client, error) { func (client *Client) SetMRPS(rps int) *Client { client.mrps = rps client.req = make(chan struct{}) - client.ticker = time.NewTicker(time.Second) + client.reqNeed = make(chan struct{}) + client.ticker = time.NewTicker( + time.Second/time.Duration(rps), + ) go func() { for { - for _ = range make( - []struct{}, - client.mrps - client.availableRequests, - ) { + select { + case <- client.reqNeed : client.req <- struct{}{} } - client.availableRequests = client.mrps <-client.ticker.C } }() @@ -147,8 +147,8 @@ func (client *Client) waitInQueue() bool { return false } + client.reqNeed <- struct{}{} <- client.req - client.availableRequests-- return true } diff --git a/cmd/amocli/getlead.go b/cmd/amocli/getlead.go index 732bae5..b5c9c22 100644 --- a/cmd/amocli/getlead.go +++ b/cmd/amocli/getlead.go @@ -43,6 +43,10 @@ var getLead = mtool.T("get-leads").Func(func(flags *mtool.Flags){ "id", ids, }, + urlenc.Value[string]{ + "with", + "contacts", + }, ) if err != nil { log.Fatalf("GetLeadsByIDs(...): %s\n", err) diff --git a/common/common.go b/common/common.go index 2140659..b9a4fac 100644 --- a/common/common.go +++ b/common/common.go @@ -1,6 +1,10 @@ package common -type Values struct { +import ( + "regexp" +) + +type Value struct { Value any `json:"value,omitempty"` EnumID int `json:"enum_id,omitempty"` Enum string `json:"enum,omitempty"` @@ -10,19 +14,32 @@ type CustomFieldsValue struct { FieldName string `json:"field_name,omitempty"` FieldCode string `json:"field_code,omitempty"` FieldType string `json:"field_type,omitempty"` - Values []Values `json:"values"` + Values []Value `json:"values"` } type CustomFieldsValues []CustomFieldsValue -func (vs CustomFieldsValues) GetValuesByID( +func (vs CustomFieldsValues) GetByID( id int, -) ([]Values, bool) { +) (CustomFieldsValue, bool) { for _, v := range vs { if v.FieldID == id { - return v.Values, true + return v, true } } - return nil, false + return CustomFieldsValue{}, false } +func (fields CustomFieldsValues) FilterByNameRegex( + re *regexp.Regexp, +) CustomFieldsValues { + ret := CustomFieldsValues{} + for _, field := range fields { + if re.MatchString(field.FieldName) { + ret = append(ret, field) + } + } + return ret +} + + diff --git a/contacts.go b/contacts.go index 21b8ac6..469c217 100644 --- a/contacts.go +++ b/contacts.go @@ -5,6 +5,7 @@ import "surdeus.su/core/ss/urlenc" import "errors" import "surdeus.su/core/amo/api" import "fmt" +//import "log" func (client *Client) GetContact( contactID int, @@ -61,4 +62,5 @@ func (client *Client) UpdateContact(contact *contacts.Contact) error { contact.ID, contact, ) + }