mirror of
https://github.com/yusing/godoxy.git
synced 2026-03-21 08:39:03 +01:00
96 lines
1.9 KiB
Go
96 lines
1.9 KiB
Go
package accesslog
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
|
|
strutils "github.com/yusing/goutils/strings"
|
|
)
|
|
|
|
type Retention struct {
|
|
Days int64 `json:"days,omitempty" validate:"min=0"`
|
|
Last int64 `json:"last,omitempty" validate:"min=0"`
|
|
KeepSize int64 `json:"keep_size,omitempty" validate:"min=0"`
|
|
} // @name LogRetention
|
|
|
|
var (
|
|
ErrInvalidSyntax = errors.New("invalid syntax")
|
|
ErrZeroValue = errors.New("zero value")
|
|
)
|
|
|
|
// see back_scanner_test.go#L210 for benchmarks
|
|
var defaultChunkSize = 32 * kilobyte
|
|
|
|
// Syntax:
|
|
//
|
|
// <N> days|weeks|months
|
|
//
|
|
// last <N>
|
|
//
|
|
// <N> KB|MB|GB|kb|mb|gb
|
|
//
|
|
// Parse implements strutils.Parser.
|
|
func (r *Retention) Parse(v string) (err error) {
|
|
split := strings.Fields(v)
|
|
if len(split) != 2 {
|
|
return fmt.Errorf("%w: %s", ErrInvalidSyntax, v)
|
|
}
|
|
switch split[0] {
|
|
case "last":
|
|
r.Last, err = strconv.ParseInt(split[1], 10, 64)
|
|
default: // <N> days|weeks|months
|
|
n, err := strconv.ParseInt(split[0], 10, 64)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
switch split[1] {
|
|
case "day", "days":
|
|
r.Days = n
|
|
case "week", "weeks":
|
|
r.Days = n * 7
|
|
case "month", "months":
|
|
r.Days = n * 30
|
|
case "kb", "Kb":
|
|
r.KeepSize = n * kilobits
|
|
case "KB":
|
|
r.KeepSize = n * kilobyte
|
|
case "mb", "Mb":
|
|
r.KeepSize = n * megabits
|
|
case "MB":
|
|
r.KeepSize = n * megabyte
|
|
case "gb", "Gb":
|
|
r.KeepSize = n * gigabits
|
|
case "GB":
|
|
r.KeepSize = n * gigabyte
|
|
default:
|
|
return fmt.Errorf("%w: unit %s", ErrInvalidSyntax, split[1])
|
|
}
|
|
}
|
|
if !r.IsValid() {
|
|
return ErrZeroValue
|
|
}
|
|
return err
|
|
}
|
|
|
|
func (r *Retention) String() string {
|
|
if r.Days > 0 {
|
|
return fmt.Sprintf("%d days", r.Days)
|
|
}
|
|
if r.Last > 0 {
|
|
return fmt.Sprintf("last %d", r.Last)
|
|
}
|
|
if r.KeepSize > 0 {
|
|
return strutils.FormatByteSize(r.KeepSize)
|
|
}
|
|
return "<invalid>"
|
|
}
|
|
|
|
func (r *Retention) IsValid() bool {
|
|
if r == nil {
|
|
return false
|
|
}
|
|
return r.Days > 0 || r.Last > 0 || r.KeepSize > 0
|
|
}
|