mirror of
https://github.com/aykhans/dodo.git
synced 2025-04-20 11:11:26 +00:00
🔨 Add 'types' package
This commit is contained in:
parent
7a40bbbd3c
commit
3068e0acae
@ -6,6 +6,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
. "github.com/aykhans/dodo/types"
|
||||||
"github.com/aykhans/dodo/utils"
|
"github.com/aykhans/dodo/utils"
|
||||||
"github.com/jedib0t/go-pretty/v6/table"
|
"github.com/jedib0t/go-pretty/v6/table"
|
||||||
)
|
)
|
||||||
@ -92,12 +93,12 @@ func (config *RequestConfig) GetMaxConns(minConns uint) uint {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Method string `json:"method" validate:"http_method"` // custom validations: http_method
|
Method string `json:"method" validate:"http_method"` // custom validations: http_method
|
||||||
URL string `json:"url" validate:"http_url,required"`
|
URL string `json:"url" validate:"http_url,required"`
|
||||||
Timeout uint32 `json:"timeout" validate:"gte=1,lte=100000"`
|
Timeout uint32 `json:"timeout" validate:"gte=1,lte=100000"`
|
||||||
DodosCount uint `json:"dodos_count" validate:"gte=1"`
|
DodosCount uint `json:"dodos_count" validate:"gte=1"`
|
||||||
RequestCount uint `json:"request_count" validation_name:"request-count" validate:"gte=1"`
|
RequestCount uint `json:"request_count" validation_name:"request-count" validate:"gte=1"`
|
||||||
NoProxyCheck utils.Option[bool] `json:"no_proxy_check"`
|
NoProxyCheck Option[bool] `json:"no_proxy_check"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewConfig(
|
func NewConfig(
|
||||||
@ -105,10 +106,10 @@ func NewConfig(
|
|||||||
timeout uint32,
|
timeout uint32,
|
||||||
dodosCount uint,
|
dodosCount uint,
|
||||||
requestCount uint,
|
requestCount uint,
|
||||||
noProxyCheck utils.Option[bool],
|
noProxyCheck Option[bool],
|
||||||
) *Config {
|
) *Config {
|
||||||
if noProxyCheck == nil {
|
if noProxyCheck == nil {
|
||||||
noProxyCheck = utils.NewNoneOption[bool]()
|
noProxyCheck = NewNoneOption[bool]()
|
||||||
}
|
}
|
||||||
|
|
||||||
return &Config{
|
return &Config{
|
||||||
@ -155,7 +156,7 @@ func (config *Config) SetDefaults() {
|
|||||||
config.RequestCount = DefaultRequestCount
|
config.RequestCount = DefaultRequestCount
|
||||||
}
|
}
|
||||||
if config.NoProxyCheck.IsNone() {
|
if config.NoProxyCheck.IsNone() {
|
||||||
config.NoProxyCheck = utils.NewOption(false)
|
config.NoProxyCheck = NewOption(false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,6 +5,7 @@ import (
|
|||||||
|
|
||||||
"github.com/aykhans/dodo/config"
|
"github.com/aykhans/dodo/config"
|
||||||
"github.com/aykhans/dodo/custom_errors"
|
"github.com/aykhans/dodo/custom_errors"
|
||||||
|
. "github.com/aykhans/dodo/types"
|
||||||
"github.com/aykhans/dodo/utils"
|
"github.com/aykhans/dodo/utils"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"github.com/spf13/pflag"
|
"github.com/spf13/pflag"
|
||||||
@ -61,7 +62,7 @@ func CLIConfigReader() (*config.CLIConfig, error) {
|
|||||||
case "timeout":
|
case "timeout":
|
||||||
cliConfig.Timeout = timeout
|
cliConfig.Timeout = timeout
|
||||||
case "no-proxy-check":
|
case "no-proxy-check":
|
||||||
cliConfig.NoProxyCheck = utils.NewOption(noProxyCheck)
|
cliConfig.NoProxyCheck = NewOption(noProxyCheck)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
if returnNil {
|
if returnNil {
|
||||||
|
41
types/durations.go
Normal file
41
types/durations.go
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
package types
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sort"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Durations []time.Duration
|
||||||
|
|
||||||
|
func (d Durations) Sort(ascending ...bool) {
|
||||||
|
// If ascending is provided and is false, sort in descending order
|
||||||
|
if len(ascending) > 0 && ascending[0] == false {
|
||||||
|
sort.Slice(d, func(i, j int) bool {
|
||||||
|
return d[i] > d[j]
|
||||||
|
})
|
||||||
|
} else { // Otherwise, sort in ascending order
|
||||||
|
sort.Slice(d, func(i, j int) bool {
|
||||||
|
return d[i] < d[j]
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d Durations) First() *time.Duration {
|
||||||
|
return &d[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d Durations) Last() *time.Duration {
|
||||||
|
return &d[len(d)-1]
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d Durations) Sum() time.Duration {
|
||||||
|
sum := time.Duration(0)
|
||||||
|
for _, duration := range d {
|
||||||
|
sum += duration
|
||||||
|
}
|
||||||
|
return sum
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d Durations) Avg() time.Duration {
|
||||||
|
return d.Sum() / time.Duration(len(d))
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package utils
|
package types
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
Loading…
x
Reference in New Issue
Block a user