Skip to content

Commit

Permalink
Merge pull request #921 from jackchenjc/issue-920
Browse files Browse the repository at this point in the history
feat: Add Start, End Timestamp for ScheduleJob and ID for ScheduleAction
  • Loading branch information
cloudxxx8 committed Aug 19, 2024
2 parents 8700801 + 8789206 commit 424ac31
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 24 deletions.
16 changes: 16 additions & 0 deletions dtos/scheduleactionrecord.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ func ToScheduleActionRecordModel(dto ScheduleActionRecord) models.ScheduleAction
return model
}

func ToScheduleActionRecordModels(dtos []ScheduleActionRecord) []models.ScheduleActionRecord {
scheduleActionRecordModels := make([]models.ScheduleActionRecord, len(dtos))
for i, dto := range dtos {
scheduleActionRecordModels[i] = ToScheduleActionRecordModel(dto)
}
return scheduleActionRecordModels
}

func FromScheduleActionRecordModelToDTO(model models.ScheduleActionRecord) ScheduleActionRecord {
var dto ScheduleActionRecord
dto.Id = model.Id
Expand All @@ -58,3 +66,11 @@ func FromScheduleActionRecordModelToDTO(model models.ScheduleActionRecord) Sched

return dto
}

func FromScheduleActionRecordModelsToDTOs(records []models.ScheduleActionRecord) []ScheduleActionRecord {
scheduleActionRecordDTOs := make([]ScheduleActionRecord, len(records))
for i, record := range records {
scheduleActionRecordDTOs[i] = FromScheduleActionRecordModelToDTO(record)
}
return scheduleActionRecordDTOs
}
10 changes: 10 additions & 0 deletions dtos/scheduleactionrecord_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,17 @@ func TestToScheduleActionRecordModel(t *testing.T) {
assert.Equal(t, scheduleActionRecordModel, result, "ToScheduleActionRecordModel did not result in ScheduleActionRecord model")
}

func TestToScheduleActionRecordModels(t *testing.T) {
result := ToScheduleActionRecordModels([]ScheduleActionRecord{scheduleActionRecord})
assert.Equal(t, []models.ScheduleActionRecord{scheduleActionRecordModel}, result, "ToScheduleActionRecordModels did not result in ScheduleActionRecord model slice")
}

func TestFromScheduleActionRecordModelToDTO(t *testing.T) {
result := FromScheduleActionRecordModelToDTO(scheduleActionRecordModel)
assert.Equal(t, scheduleActionRecord, result, "FromScheduleActionRecordModelToDTO did not result in ScheduleActionRecord dto")
}

func TestFromScheduleActionRecordModelsToDTOs(t *testing.T) {
result := FromScheduleActionRecordModelsToDTOs([]models.ScheduleActionRecord{scheduleActionRecordModel})
assert.Equal(t, []ScheduleActionRecord{scheduleActionRecord}, result, "FromScheduleActionRecordModelsToDTOs did not result in ScheduleActionRecord dto slice")
}
30 changes: 25 additions & 5 deletions dtos/schedulejob.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ func (s *ScheduleJob) Validate() error {
}

type ScheduleDef struct {
Type string `json:"type" validate:"oneof='INTERVAL' 'CRON'"`
Type string `json:"type" validate:"oneof='INTERVAL' 'CRON'"`
StartTimestamp int64 `json:"startTimestamp,omitempty"`
EndTimestamp int64 `json:"endTimestamp,omitempty"`

IntervalScheduleDef `json:",inline" validate:"-"`
CronScheduleDef `json:",inline" validate:"-"`
Expand All @@ -85,6 +87,12 @@ func (s *ScheduleDef) Validate() error {
}
}

if s.EndTimestamp != 0 {
if s.EndTimestamp < s.StartTimestamp {
return errors.NewCommonEdgeX(errors.KindContractInvalid, "endTimestamp must be greater than startTimestamp", nil)
}
}

return nil
}

Expand Down Expand Up @@ -221,13 +229,21 @@ func ToScheduleDefModel(dto ScheduleDef) models.ScheduleDef {
switch dto.Type {
case common.DefInterval:
model = models.IntervalScheduleDef{
BaseScheduleDef: models.BaseScheduleDef{Type: common.DefInterval},
Interval: dto.Interval,
BaseScheduleDef: models.BaseScheduleDef{
Type: common.DefInterval,
StartTimestamp: dto.StartTimestamp,
EndTimestamp: dto.EndTimestamp,
},
Interval: dto.Interval,
}
case common.DefCron:
model = models.CronScheduleDef{
BaseScheduleDef: models.BaseScheduleDef{Type: common.DefCron},
Crontab: dto.Crontab,
BaseScheduleDef: models.BaseScheduleDef{
Type: common.DefCron,
StartTimestamp: dto.StartTimestamp,
EndTimestamp: dto.EndTimestamp,
},
Crontab: dto.Crontab,
}
}

Expand All @@ -242,12 +258,16 @@ func FromScheduleDefModelToDTO(model models.ScheduleDef) ScheduleDef {
durationModel := model.(models.IntervalScheduleDef)
dto = ScheduleDef{
Type: common.DefInterval,
StartTimestamp: durationModel.StartTimestamp,
EndTimestamp: durationModel.EndTimestamp,
IntervalScheduleDef: IntervalScheduleDef{Interval: durationModel.Interval},
}
case common.DefCron:
cronModel := model.(models.CronScheduleDef)
dto = ScheduleDef{
Type: common.DefCron,
StartTimestamp: cronModel.StartTimestamp,
EndTimestamp: cronModel.EndTimestamp,
CronScheduleDef: CronScheduleDef{Crontab: cronModel.Crontab},
}
}
Expand Down
36 changes: 28 additions & 8 deletions dtos/schedulejob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ import (
)

const (
jobName = "mock-job-name"
payload = "eyJ0ZXN0I"
topic = "mock-topic"
crontab = "0 0 0 1 1 *"
jobName = "mock-job-name"
payload = "eyJ0ZXN0I"
topic = "mock-topic"
crontab = "0 0 0 1 1 *"
startTimestamp = 1724052774
endTimestamp = 1824052774
)

var scheduleActionEdgeXMessageBus = ScheduleAction{
Expand Down Expand Up @@ -81,29 +83,37 @@ var scheduleActionDeviceControlModel = models.DeviceControlAction{
}

var scheduleIntervalDef = ScheduleDef{
Type: common.DefInterval,
Type: common.DefInterval,
StartTimestamp: startTimestamp,
EndTimestamp: endTimestamp,
IntervalScheduleDef: IntervalScheduleDef{
Interval: interval,
},
}

var scheduleIntervalDefModel = models.IntervalScheduleDef{
BaseScheduleDef: models.BaseScheduleDef{
Type: common.DefInterval,
Type: common.DefInterval,
StartTimestamp: startTimestamp,
EndTimestamp: endTimestamp,
},
Interval: interval,
}

var scheduleCronDef = ScheduleDef{
Type: common.DefCron,
Type: common.DefCron,
StartTimestamp: startTimestamp,
EndTimestamp: endTimestamp,
CronScheduleDef: CronScheduleDef{
Crontab: crontab,
},
}

var scheduleCronDefModel = models.CronScheduleDef{
BaseScheduleDef: models.BaseScheduleDef{
Type: common.DefCron,
Type: common.DefCron,
StartTimestamp: startTimestamp,
EndTimestamp: endTimestamp,
},
Crontab: crontab,
}
Expand Down Expand Up @@ -149,6 +159,15 @@ func TestScheduleJob_Validate(t *testing.T) {
Crontab: "",
},
}
invalidDef := scheduleJob
invalidDef.Definition = ScheduleDef{
Type: common.DefCron,
StartTimestamp: endTimestamp,
EndTimestamp: startTimestamp,
CronScheduleDef: CronScheduleDef{
Crontab: "",
},
}
emptyActions := scheduleJob
emptyActions.Actions = nil
invalidEdgeXMessageBusAction := scheduleJob
Expand Down Expand Up @@ -189,6 +208,7 @@ func TestScheduleJob_Validate(t *testing.T) {
{"invalid ScheduleJob, empty Definition", emptyDef, true},
{"invalid ScheduleJob, invalid Interval Definition", invalidIntervalDef, true},
{"invalid ScheduleJob, invalid Cron Definition", invalidCronDef, true},
{"invalid ScheduleJob, invalid Definition, endTimestamp must be greater than startTimestamp", invalidDef, true},
{"invalid ScheduleJob, empty Actions", emptyActions, true},
{"invalid ScheduleJob, invalid EdgeXMessageBus Actions", invalidEdgeXMessageBusAction, true},
{"invalid ScheduleJob, invalid REST Actions", invalidRestAction, true},
Expand Down
17 changes: 9 additions & 8 deletions models/const_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ const (
TestSubscriptionName = "TestSubscriptionName"
TestSubscriptionReceiver = "TestReceiver"

TestScheduleJobName = "TestScheduleJob"
TestCrontab = "0 0 1 1 *"
TestContentType = "application/json"
TestPayload = "eyJ0ZXN0I"
TestAddress = "http://localhost:12345/test/address"
TestDeviceName = "TestDeviceName"
TestSourceName = "TestSourceName"
TestScheduleActionRecordStatus = "SUCCEED"
TestScheduleJobName = "TestScheduleJob"
TestCrontab = "0 0 1 1 *"
TestContentType = "application/json"
TestPayload = "eyJ0ZXN0I"
TestAddress = "http://localhost:12345/test/address"
TestDeviceName = "TestDeviceName"
TestSourceName = "TestSourceName"
TestStartTimestamp = 1724052774
TestEndTimestamp = 1824052774
)
6 changes: 5 additions & 1 deletion models/schedulejob.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ func unmarshalScheduleDef(b []byte) (def ScheduleDef, err error) {
}

type BaseScheduleDef struct {
Type ScheduleDefType
Type ScheduleDefType
StartTimestamp int64
EndTimestamp int64
}

type IntervalScheduleDef struct {
Expand Down Expand Up @@ -177,6 +179,8 @@ func UnmarshalScheduleAction(b []byte) (action ScheduleAction, err error) {
}

type BaseScheduleAction struct {
// Id is the identifier of the action, no need to be in the DTO
Id string
Type ScheduleActionType
ContentType string
Payload []byte
Expand Down
24 changes: 22 additions & 2 deletions models/schedulejob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,18 @@ import (

var intervalScheduleDef = IntervalScheduleDef{
BaseScheduleDef: BaseScheduleDef{
Type: common.DefInterval,
Type: common.DefInterval,
StartTimestamp: TestStartTimestamp,
EndTimestamp: TestEndTimestamp,
},
Interval: TestInterval,
}

var cronScheduleDef = CronScheduleDef{
BaseScheduleDef: BaseScheduleDef{
Type: common.DefCron,
Type: common.DefCron,
StartTimestamp: TestStartTimestamp,
EndTimestamp: TestEndTimestamp,
},
Crontab: TestCrontab,
}
Expand All @@ -34,6 +38,8 @@ var scheduleJobWithInvalidIntervalScheduleDef = `{
"name": "TestScheduleJob",
"definition": {
"Type": "INTERVAL",
"StartTimestamp": 1724052774,
"EndTimestamp": 1824052774,
"Interval": ["123"]
},
"actions": []
Expand All @@ -44,6 +50,8 @@ var scheduleJobWithInvalidCronScheduleDef = `{
"name": "TestScheduleJob",
"definition": {
"Type": "CRON",
"StartTimestamp": 1724052774,
"EndTimestamp": 1824052774,
"Crontab": ["123"]
},
"actions": []
Expand All @@ -54,6 +62,8 @@ var scheduleJobWithUnsupportedScheduleDef = `{
"name": "TestScheduleJob",
"definition": {
"Type": "NOT_SUPPORTED",
"StartTimestamp": 1724052774,
"EndTimestamp": 1824052774,
"Interval": "10m"
},
"actions": []
Expand All @@ -71,6 +81,8 @@ var scheduleJobWithInvalidEdgeXMessageBusAction = `{
"name": "TestScheduleJob",
"definition": {
"Type": "INTERVAL",
"StartTimestamp": 1724052774,
"EndTimestamp": 1824052774,
"Interval": "10m"
},
"actions": [
Expand All @@ -88,6 +100,8 @@ var scheduleJobWithInvalidRestAction = `{
"name": "TestScheduleJob",
"definition": {
"Type": "INTERVAL",
"StartTimestamp": 1724052774,
"EndTimestamp": 1824052774,
"Interval": "10m"
},
"actions": [
Expand All @@ -105,6 +119,8 @@ var scheduleJobWithInvalidDeviceControlAction = `{
"name": "TestScheduleJob",
"definition": {
"Type": "INTERVAL",
"StartTimestamp": 1724052774,
"EndTimestamp": 1824052774,
"Interval": "10m"
},
"actions": [
Expand All @@ -123,6 +139,8 @@ var scheduleJobWithUnsupportedAction = `{
"name": "TestScheduleJob",
"definition": {
"Type": "INTERVAL",
"StartTimestamp": 1724052774,
"EndTimestamp": 1824052774,
"Interval": "10m"
},
"actions": [
Expand All @@ -137,6 +155,8 @@ var scheduleJobWithInvalidScheduleAction = `{
"name": "TestScheduleJob",
"definition": {
"Type": "INTERVAL",
"StartTimestamp": 1724052774,
"EndTimestamp": 1824052774,
"Interval": "10m"
},
"actions": ["123"]
Expand Down

0 comments on commit 424ac31

Please sign in to comment.