2018-01-12 17:30:54 +01:00
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
2020-12-11 18:48:09 +01:00
|
|
|
"encoding/json"
|
2021-11-04 11:12:57 +01:00
|
|
|
"errors"
|
2018-01-12 17:30:54 +01:00
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2020-12-07 17:19:20 +01:00
|
|
|
// BwPair represents an upload and a download bandwidth
|
|
|
|
type BwPair struct {
|
|
|
|
Tx SizeSuffix // upload bandwidth
|
|
|
|
Rx SizeSuffix // download bandwidth
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns a printable representation of a BwPair
|
|
|
|
func (bp *BwPair) String() string {
|
|
|
|
var out strings.Builder
|
|
|
|
out.WriteString(bp.Tx.String())
|
|
|
|
if bp.Rx != bp.Tx {
|
|
|
|
out.WriteRune(':')
|
|
|
|
out.WriteString(bp.Rx.String())
|
|
|
|
}
|
|
|
|
return out.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the bandwidth from a string which is either
|
|
|
|
// SizeSuffix or SizeSuffix:SizeSuffix (for tx:rx bandwidth)
|
|
|
|
func (bp *BwPair) Set(s string) (err error) {
|
|
|
|
colon := strings.Index(s, ":")
|
|
|
|
stx, srx := s, ""
|
|
|
|
if colon >= 0 {
|
|
|
|
stx, srx = s[:colon], s[colon+1:]
|
|
|
|
}
|
|
|
|
err = bp.Tx.Set(stx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if colon < 0 {
|
|
|
|
bp.Rx = bp.Tx
|
|
|
|
} else {
|
|
|
|
err = bp.Rx.Set(srx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsSet returns true if either of the bandwidth limits are set
|
|
|
|
func (bp *BwPair) IsSet() bool {
|
|
|
|
return bp.Tx > 0 || bp.Rx > 0
|
|
|
|
}
|
|
|
|
|
2018-01-12 17:30:54 +01:00
|
|
|
// BwTimeSlot represents a bandwidth configuration at a point in time.
|
|
|
|
type BwTimeSlot struct {
|
2018-06-17 19:38:09 +02:00
|
|
|
DayOfTheWeek int
|
|
|
|
HHMM int
|
2020-12-07 17:19:20 +01:00
|
|
|
Bandwidth BwPair
|
2018-01-12 17:30:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// BwTimetable contains all configured time slots.
|
|
|
|
type BwTimetable []BwTimeSlot
|
|
|
|
|
|
|
|
// String returns a printable representation of BwTimetable.
|
|
|
|
func (x BwTimetable) String() string {
|
2020-12-07 17:19:20 +01:00
|
|
|
var out strings.Builder
|
|
|
|
bwOnly := len(x) == 1 && x[0].DayOfTheWeek == 0 && x[0].HHMM == 0
|
2018-01-12 17:30:54 +01:00
|
|
|
for _, ts := range x {
|
2020-12-07 17:19:20 +01:00
|
|
|
if out.Len() != 0 {
|
|
|
|
out.WriteRune(' ')
|
|
|
|
}
|
|
|
|
if !bwOnly {
|
|
|
|
_, _ = fmt.Fprintf(&out, "%s-%02d:%02d,", time.Weekday(ts.DayOfTheWeek).String()[:3], ts.HHMM/100, ts.HHMM%100)
|
|
|
|
}
|
|
|
|
out.WriteString(ts.Bandwidth.String())
|
2018-01-12 17:30:54 +01:00
|
|
|
}
|
2020-12-07 17:19:20 +01:00
|
|
|
return out.String()
|
2018-01-12 17:30:54 +01:00
|
|
|
}
|
|
|
|
|
2018-06-17 19:38:09 +02:00
|
|
|
// Basic hour format checking
|
|
|
|
func validateHour(HHMM string) error {
|
|
|
|
if len(HHMM) != 5 {
|
2021-11-04 11:12:57 +01:00
|
|
|
return fmt.Errorf("invalid time specification (hh:mm): %q", HHMM)
|
2018-06-17 19:38:09 +02:00
|
|
|
}
|
|
|
|
hh, err := strconv.Atoi(HHMM[0:2])
|
|
|
|
if err != nil {
|
2021-11-04 11:12:57 +01:00
|
|
|
return fmt.Errorf("invalid hour in time specification %q: %v", HHMM, err)
|
2018-06-17 19:38:09 +02:00
|
|
|
}
|
|
|
|
if hh < 0 || hh > 23 {
|
2021-11-04 11:12:57 +01:00
|
|
|
return fmt.Errorf("invalid hour (must be between 00 and 23): %q", hh)
|
2018-06-17 19:38:09 +02:00
|
|
|
}
|
|
|
|
mm, err := strconv.Atoi(HHMM[3:])
|
|
|
|
if err != nil {
|
2021-11-04 11:12:57 +01:00
|
|
|
return fmt.Errorf("invalid minute in time specification: %q: %v", HHMM, err)
|
2018-06-17 19:38:09 +02:00
|
|
|
}
|
|
|
|
if mm < 0 || mm > 59 {
|
2021-11-04 11:12:57 +01:00
|
|
|
return fmt.Errorf("invalid minute (must be between 00 and 59): %q", hh)
|
2018-06-17 19:38:09 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Basic weekday format checking
|
|
|
|
func parseWeekday(dayOfWeek string) (int, error) {
|
|
|
|
dayOfWeek = strings.ToLower(dayOfWeek)
|
|
|
|
if dayOfWeek == "sun" || dayOfWeek == "sunday" {
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
if dayOfWeek == "mon" || dayOfWeek == "monday" {
|
|
|
|
return 1, nil
|
|
|
|
}
|
|
|
|
if dayOfWeek == "tue" || dayOfWeek == "tuesday" {
|
|
|
|
return 2, nil
|
|
|
|
}
|
|
|
|
if dayOfWeek == "wed" || dayOfWeek == "wednesday" {
|
|
|
|
return 3, nil
|
|
|
|
}
|
|
|
|
if dayOfWeek == "thu" || dayOfWeek == "thursday" {
|
|
|
|
return 4, nil
|
|
|
|
}
|
|
|
|
if dayOfWeek == "fri" || dayOfWeek == "friday" {
|
|
|
|
return 5, nil
|
|
|
|
}
|
|
|
|
if dayOfWeek == "sat" || dayOfWeek == "saturday" {
|
|
|
|
return 6, nil
|
|
|
|
}
|
2021-11-04 11:12:57 +01:00
|
|
|
return 0, fmt.Errorf("invalid weekday: %q", dayOfWeek)
|
2018-06-17 19:38:09 +02:00
|
|
|
}
|
|
|
|
|
2018-01-12 17:30:54 +01:00
|
|
|
// Set the bandwidth timetable.
|
|
|
|
func (x *BwTimetable) Set(s string) error {
|
|
|
|
// The timetable is formatted as:
|
Spelling fixes
Fix spelling of: above, already, anonymous, associated,
authentication, bandwidth, because, between, blocks, calculate,
candidates, cautious, changelog, cleaner, clipboard, command,
completely, concurrently, considered, constructs, corrupt, current,
daemon, dependencies, deprecated, directory, dispatcher, download,
eligible, ellipsis, encrypter, endpoint, entrieslist, essentially,
existing writers, existing, expires, filesystem, flushing, frequently,
hierarchy, however, implementation, implements, inaccurate,
individually, insensitive, longer, maximum, metadata, modified,
multipart, namedirfirst, nextcloud, obscured, opened, optional,
owncloud, pacific, passphrase, password, permanently, persimmon,
positive, potato, protocol, quota, receiving, recommends, referring,
requires, revisited, satisfied, satisfies, satisfy, semver,
serialized, session, storage, strategies, stringlist, successful,
supported, surprise, temporarily, temporary, transactions, unneeded,
update, uploads, wrapped
Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>
2020-10-09 02:17:24 +02:00
|
|
|
// "dayOfWeek-hh:mm,bandwidth dayOfWeek-hh:mm,bandwidth..." ex: "Mon-10:00,10G Mon-11:30,1G Tue-18:00,off"
|
2018-01-12 17:30:54 +01:00
|
|
|
// If only a single bandwidth identifier is provided, we assume constant bandwidth.
|
|
|
|
|
|
|
|
if len(s) == 0 {
|
|
|
|
return errors.New("empty string")
|
|
|
|
}
|
|
|
|
// Single value without time specification.
|
|
|
|
if !strings.Contains(s, " ") && !strings.Contains(s, ",") {
|
|
|
|
ts := BwTimeSlot{}
|
|
|
|
if err := ts.Bandwidth.Set(s); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-06-17 19:38:09 +02:00
|
|
|
ts.DayOfTheWeek = 0
|
2018-01-12 17:30:54 +01:00
|
|
|
ts.HHMM = 0
|
|
|
|
*x = BwTimetable{ts}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-07-17 12:04:01 +02:00
|
|
|
// Split the timetable string by both spaces and semicolons
|
|
|
|
for _, tok := range strings.FieldsFunc(s, func(r rune) bool {
|
|
|
|
return r == ' ' || r == ';'
|
|
|
|
}) {
|
2018-01-12 17:30:54 +01:00
|
|
|
tv := strings.Split(tok, ",")
|
|
|
|
|
2018-06-17 19:38:09 +02:00
|
|
|
// Format must be dayOfWeek-HH:MM,BW
|
2018-01-12 17:30:54 +01:00
|
|
|
if len(tv) != 2 {
|
2021-11-04 11:12:57 +01:00
|
|
|
return fmt.Errorf("invalid time/bandwidth specification: %q", tok)
|
2018-01-12 17:30:54 +01:00
|
|
|
}
|
|
|
|
|
2018-06-17 19:38:09 +02:00
|
|
|
weekday := 0
|
|
|
|
HHMM := ""
|
|
|
|
if !strings.Contains(tv[0], "-") {
|
|
|
|
HHMM = tv[0]
|
|
|
|
if err := validateHour(HHMM); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for i := 0; i < 7; i++ {
|
|
|
|
hh, _ := strconv.Atoi(HHMM[0:2])
|
|
|
|
mm, _ := strconv.Atoi(HHMM[3:])
|
|
|
|
ts := BwTimeSlot{
|
|
|
|
DayOfTheWeek: i,
|
|
|
|
HHMM: (hh * 100) + mm,
|
|
|
|
}
|
|
|
|
if err := ts.Bandwidth.Set(tv[1]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*x = append(*x, ts)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
timespec := strings.Split(tv[0], "-")
|
|
|
|
if len(timespec) != 2 {
|
2021-11-04 11:12:57 +01:00
|
|
|
return fmt.Errorf("invalid time specification: %q", tv[0])
|
2018-06-17 19:38:09 +02:00
|
|
|
}
|
|
|
|
var err error
|
|
|
|
weekday, err = parseWeekday(timespec[0])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
HHMM = timespec[1]
|
|
|
|
if err := validateHour(HHMM); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
hh, _ := strconv.Atoi(HHMM[0:2])
|
|
|
|
mm, _ := strconv.Atoi(HHMM[3:])
|
|
|
|
ts := BwTimeSlot{
|
|
|
|
DayOfTheWeek: weekday,
|
|
|
|
HHMM: (hh * 100) + mm,
|
|
|
|
}
|
|
|
|
// Bandwidth limit for this time slot.
|
|
|
|
if err := ts.Bandwidth.Set(tv[1]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*x = append(*x, ts)
|
2018-01-12 17:30:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-08-05 17:35:41 +02:00
|
|
|
// Difference in minutes between lateDayOfWeekHHMM and earlyDayOfWeekHHMM
|
2018-06-17 19:38:09 +02:00
|
|
|
func timeDiff(lateDayOfWeekHHMM int, earlyDayOfWeekHHMM int) int {
|
|
|
|
|
|
|
|
lateTimeMinutes := (lateDayOfWeekHHMM / 10000) * 24 * 60
|
|
|
|
lateTimeMinutes += ((lateDayOfWeekHHMM / 100) % 100) * 60
|
|
|
|
lateTimeMinutes += lateDayOfWeekHHMM % 100
|
|
|
|
|
|
|
|
earlyTimeMinutes := (earlyDayOfWeekHHMM / 10000) * 24 * 60
|
|
|
|
earlyTimeMinutes += ((earlyDayOfWeekHHMM / 100) % 100) * 60
|
|
|
|
earlyTimeMinutes += earlyDayOfWeekHHMM % 100
|
|
|
|
|
|
|
|
return lateTimeMinutes - earlyTimeMinutes
|
|
|
|
}
|
|
|
|
|
2018-01-12 17:30:54 +01:00
|
|
|
// LimitAt returns a BwTimeSlot for the time requested.
|
|
|
|
func (x BwTimetable) LimitAt(tt time.Time) BwTimeSlot {
|
2018-06-17 19:38:09 +02:00
|
|
|
// If the timetable is empty, we return an unlimited BwTimeSlot starting at Sunday midnight.
|
2018-01-12 17:30:54 +01:00
|
|
|
if len(x) == 0 {
|
2020-12-07 17:19:20 +01:00
|
|
|
return BwTimeSlot{Bandwidth: BwPair{-1, -1}}
|
2018-01-12 17:30:54 +01:00
|
|
|
}
|
|
|
|
|
2018-06-17 19:38:09 +02:00
|
|
|
dayOfWeekHHMM := int(tt.Weekday())*10000 + tt.Hour()*100 + tt.Minute()
|
2018-01-12 17:30:54 +01:00
|
|
|
|
|
|
|
// By default, we return the last element in the timetable. This
|
|
|
|
// satisfies two conditions: 1) If there's only one element it
|
|
|
|
// will always be selected, and 2) The last element of the table
|
2018-06-17 19:38:09 +02:00
|
|
|
// will "wrap around" until overridden by an earlier time slot.
|
2018-01-12 17:30:54 +01:00
|
|
|
// there's only one time slot in the timetable.
|
|
|
|
ret := x[len(x)-1]
|
|
|
|
mindif := 0
|
|
|
|
first := true
|
|
|
|
|
|
|
|
// Look for most recent time slot.
|
|
|
|
for _, ts := range x {
|
|
|
|
// Ignore the past
|
2018-06-17 19:38:09 +02:00
|
|
|
if dayOfWeekHHMM < (ts.DayOfTheWeek*10000)+ts.HHMM {
|
2018-01-12 17:30:54 +01:00
|
|
|
continue
|
|
|
|
}
|
2018-06-17 19:38:09 +02:00
|
|
|
dif := timeDiff(dayOfWeekHHMM, (ts.DayOfTheWeek*10000)+ts.HHMM)
|
2018-01-12 17:30:54 +01:00
|
|
|
if first {
|
|
|
|
mindif = dif
|
|
|
|
first = false
|
|
|
|
}
|
|
|
|
if dif <= mindif {
|
|
|
|
mindif = dif
|
|
|
|
ret = ts
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
// Type of the value
|
|
|
|
func (x BwTimetable) Type() string {
|
|
|
|
return "BwTimetable"
|
|
|
|
}
|
2020-12-11 18:48:09 +01:00
|
|
|
|
|
|
|
// UnmarshalJSON unmarshals a string value
|
|
|
|
func (x *BwTimetable) UnmarshalJSON(in []byte) error {
|
|
|
|
var s string
|
|
|
|
err := json.Unmarshal(in, &s)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return x.Set(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON marshals as a string value
|
|
|
|
func (x BwTimetable) MarshalJSON() ([]byte, error) {
|
|
|
|
s := x.String()
|
|
|
|
return json.Marshal(s)
|
|
|
|
}
|