mirror of
https://github.com/absmach/supermq.git
synced 2026-06-23 07:00:25 +00:00
dcd5ff914d
* initial implementation Signed-off-by: nyagamunene <stevenyaga2014@gmail.com> * initial implementation Signed-off-by: nyagamunene <stevenyaga2014@gmail.com> * add remove report from nats handler Signed-off-by: nyagamunene <stevenyaga2014@gmail.com> * add license header Signed-off-by: nyagamunene <stevenyaga2014@gmail.com> * fix failing linter Signed-off-by: nyagamunene <stevenyaga2014@gmail.com> * remove unused code Signed-off-by: nyagamunene <stevenyaga2014@gmail.com> * update docker compose Signed-off-by: nyagamunene <stevenyaga2014@gmail.com> * address comments Signed-off-by: nyagamunene <stevenyaga2014@gmail.com> * fix failing linter Signed-off-by: nyagamunene <stevenyaga2014@gmail.com> * move runinfo to pkg Signed-off-by: nyagamunene <stevenyaga2014@gmail.com> * update report handler Signed-off-by: nyagamunene <stevenyaga2014@gmail.com> * update reports handler Signed-off-by: nyagamunene <stevenyaga2014@gmail.com> * update handler in reports Signed-off-by: nyagamunene <stevenyaga2014@gmail.com> * update repo method from time to due Signed-off-by: nyagamunene <stevenyaga2014@gmail.com> * fix validation methods Signed-off-by: nyagamunene <stevenyaga2014@gmail.com> * address comments Signed-off-by: nyagamunene <stevenyaga2014@gmail.com> * update reports port to 9017 Signed-off-by: nyagamunene <stevenyaga2014@gmail.com> * update nginx to support reports Signed-off-by: nyagamunene <stevenyaga2014@gmail.com> * fix reports location in nginx Signed-off-by: nyagamunene <stevenyaga2014@gmail.com> * update env variable Signed-off-by: nyagamunene <stevenyaga2014@gmail.com> --------- Signed-off-by: nyagamunene <stevenyaga2014@gmail.com>
124 lines
2.5 KiB
Go
124 lines
2.5 KiB
Go
// Copyright (c) Abstract Machines
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package schedule
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"time"
|
|
)
|
|
|
|
var ErrInvalidRecurringType = errors.New("invalid recurring type")
|
|
|
|
// Type can be daily, weekly or monthly.
|
|
type Recurring uint
|
|
|
|
const (
|
|
None Recurring = iota
|
|
Daily
|
|
Weekly
|
|
Monthly
|
|
)
|
|
|
|
func (rt Recurring) String() string {
|
|
switch rt {
|
|
case Daily:
|
|
return "daily"
|
|
case Weekly:
|
|
return "weekly"
|
|
case Monthly:
|
|
return "monthly"
|
|
default:
|
|
return "none"
|
|
}
|
|
}
|
|
|
|
func (rt Recurring) MarshalJSON() ([]byte, error) {
|
|
return json.Marshal(rt.String())
|
|
}
|
|
|
|
func (rt *Recurring) UnmarshalJSON(data []byte) error {
|
|
var s string
|
|
if err := json.Unmarshal(data, &s); err != nil {
|
|
return err
|
|
}
|
|
|
|
switch s {
|
|
case "daily":
|
|
*rt = Daily
|
|
case "weekly":
|
|
*rt = Weekly
|
|
case "monthly":
|
|
*rt = Monthly
|
|
case "none":
|
|
*rt = None
|
|
default:
|
|
return ErrInvalidRecurringType
|
|
}
|
|
return nil
|
|
}
|
|
|
|
type Schedule struct {
|
|
StartDateTime time.Time `json:"start_datetime"` // When the schedule becomes active
|
|
Time time.Time `json:"time"` // Specific time for the rule to run
|
|
Recurring Recurring `json:"recurring"` // None, Daily, Weekly, Monthly
|
|
RecurringPeriod uint `json:"recurring_period"` // Controls how many intervals to skip between executions: 1 = every interval, 2 = every second interval, etc.
|
|
}
|
|
|
|
func (s Schedule) MarshalJSON() ([]byte, error) {
|
|
type Alias Schedule
|
|
jTimes := struct {
|
|
StartDateTime string `json:"start_datetime"`
|
|
Time string `json:"time"`
|
|
*Alias
|
|
}{
|
|
StartDateTime: s.StartDateTime.Format(time.RFC3339),
|
|
Time: s.Time.Format(time.RFC3339),
|
|
Alias: (*Alias)(&s),
|
|
}
|
|
return json.Marshal(jTimes)
|
|
}
|
|
|
|
func (s *Schedule) UnmarshalJSON(data []byte) error {
|
|
type Alias Schedule
|
|
aux := struct {
|
|
StartDateTime string `json:"start_datetime"`
|
|
Time string `json:"time"`
|
|
*Alias
|
|
}{
|
|
Alias: (*Alias)(s),
|
|
}
|
|
if err := json.Unmarshal(data, &aux); err != nil {
|
|
return err
|
|
}
|
|
|
|
startDateTime, err := time.Parse(time.RFC3339, aux.StartDateTime)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
s.StartDateTime = startDateTime
|
|
|
|
if aux.Time != "" {
|
|
time, err := time.Parse(time.RFC3339, aux.Time)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
s.Time = time
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s Schedule) NextDue() time.Time {
|
|
switch s.Recurring {
|
|
case Daily:
|
|
return s.Time.AddDate(0, 0, int(s.RecurringPeriod))
|
|
case Weekly:
|
|
return s.Time.AddDate(0, 0, int(s.RecurringPeriod)*7)
|
|
case Monthly:
|
|
return s.Time.AddDate(0, int(s.RecurringPeriod), 0)
|
|
default:
|
|
return time.Time{}
|
|
}
|
|
}
|