@@ -22,21 +22,34 @@ import (
22
22
23
23
const (
24
24
scheduleActionRecordTable = "scheduler.schedule_action_record"
25
+ actionIdCol = "action_id"
25
26
jobNameCol = "job_name"
26
27
actionCol = "action"
27
28
scheduledAtCol = "scheduled_at"
28
29
)
29
30
30
31
// AddScheduleActionRecord adds a new schedule action record to the database
31
- // Note: the created field should be set manually before calling this function, and all the records belong to the same job should have the same created time.
32
- // So that the created time can be used to query the latest schedule action records of a job.
32
+ // Note: the scheduledAt field should be set manually before calling this function.
33
33
func (c * Client ) AddScheduleActionRecord (ctx context.Context , scheduleActionRecord model.ScheduleActionRecord ) (model.ScheduleActionRecord , errors.EdgeX ) {
34
34
if len (scheduleActionRecord .Id ) == 0 {
35
35
scheduleActionRecord .Id = uuid .New ().String ()
36
36
}
37
37
return addScheduleActionRecord (ctx , c .ConnPool , scheduleActionRecord )
38
38
}
39
39
40
+ // AddScheduleActionRecords adds multiple schedule action records to the database
41
+ func (c * Client ) AddScheduleActionRecords (ctx context.Context , scheduleActionRecords []model.ScheduleActionRecord ) ([]model.ScheduleActionRecord , errors.EdgeX ) {
42
+ records := make ([]model.ScheduleActionRecord , len (scheduleActionRecords ))
43
+ for _ , record := range scheduleActionRecords {
44
+ r , err := c .AddScheduleActionRecord (ctx , record )
45
+ if err != nil {
46
+ return nil , errors .NewCommonEdgeXWrapper (err )
47
+ }
48
+ records = append (records , r )
49
+ }
50
+ return records , nil
51
+ }
52
+
40
53
// AllScheduleActionRecords queries the schedule action records with the given range, offset, and limit
41
54
func (c * Client ) AllScheduleActionRecords (ctx context.Context , start , end int64 , offset , limit int ) ([]model.ScheduleActionRecord , errors.EdgeX ) {
42
55
var err errors.EdgeX
@@ -47,42 +60,32 @@ func (c *Client) AllScheduleActionRecords(ctx context.Context, start, end int64,
47
60
48
61
records , err := queryScheduleActionRecords (ctx , c .ConnPool , sqlQueryAllWithPaginationAndTimeRange (scheduleActionRecordTable ), time .UnixMilli (start ), time .UnixMilli (end ), offset , limit )
49
62
if err != nil {
50
- return nil , errors .NewCommonEdgeX (errors .KindDatabaseError , "failed to query all schedule action records" , err )
63
+ return nil , errors .NewCommonEdgeX (errors .Kind ( err ) , "failed to query all schedule action records" , err )
51
64
}
52
65
53
66
return records , nil
54
67
}
55
68
56
- // LatestScheduleActionRecords queries the latest schedule action records of all schedule jobs with the given offset and limit
57
- func (c * Client ) LatestScheduleActionRecords (ctx context.Context , offset , limit int ) ([]model.ScheduleActionRecord , errors.EdgeX ) {
58
- // Get all the job names
59
- jobNames , err := queryScheduleJobNames (ctx , c .ConnPool )
60
- if err != nil {
61
- return nil , errors .NewCommonEdgeXWrapper (err )
62
- }
63
-
69
+ // LatestScheduleActionRecordsByJobName queries the latest schedule action records by job name
70
+ func (c * Client ) LatestScheduleActionRecordsByJobName (ctx context.Context , jobName string ) ([]model.ScheduleActionRecord , errors.EdgeX ) {
64
71
sqlQueryLatestScheduleActionRecords := `
65
- SELECT id, job_name, action, status, scheduled_at, created
72
+ SELECT id, action_id, job_name, action, status, scheduled_at, created
66
73
FROM(
67
74
SELECT *
68
75
FROM (
69
76
SELECT *,
70
- RANK() OVER (PARTITION BY job_name ORDER BY created DESC) AS rnk
77
+ RANK() OVER (PARTITION BY job_name, action_id ORDER BY created DESC) AS rnk
71
78
FROM scheduler.schedule_action_record
72
- WHERE job_name = ANY($1::text[])
79
+ WHERE job_name = $1
73
80
) subquery
74
81
WHERE rnk = 1
75
82
)
76
- ORDER BY job_name, created DESC
77
- OFFSET $2
78
- LIMIT $3;
83
+ ORDER BY job_name, created DESC;
79
84
`
80
85
81
- // Pass the offset and limit here
82
- offset , limit = getValidOffsetAndLimit (offset , limit )
83
- records , err := queryScheduleActionRecords (ctx , c .ConnPool , sqlQueryLatestScheduleActionRecords , jobNames , offset , limit )
86
+ records , err := queryScheduleActionRecords (ctx , c .ConnPool , sqlQueryLatestScheduleActionRecords , jobName )
84
87
if err != nil {
85
- return nil , errors .NewCommonEdgeX (errors .KindDatabaseError , "failed to query latest schedule action records" , err )
88
+ return nil , errors .NewCommonEdgeX (errors .Kind ( err ) , "failed to query latest schedule action records" , err )
86
89
}
87
90
88
91
return records , nil
@@ -98,7 +101,7 @@ func (c *Client) ScheduleActionRecordsByStatus(ctx context.Context, status strin
98
101
99
102
records , err := queryScheduleActionRecords (ctx , c .ConnPool , sqlQueryAllByStatusWithPaginationAndTimeRange (scheduleActionRecordTable ), status , time .UnixMilli (start ), time .UnixMilli (end ), offset , limit )
100
103
if err != nil {
101
- return nil , errors .NewCommonEdgeX (errors .KindDatabaseError , fmt .Sprintf ("failed to query schedule action records by status %s" , status ), err )
104
+ return nil , errors .NewCommonEdgeX (errors .Kind ( err ) , fmt .Sprintf ("failed to query schedule action records by status %s" , status ), err )
102
105
}
103
106
104
107
return records , nil
@@ -114,7 +117,7 @@ func (c *Client) ScheduleActionRecordsByJobName(ctx context.Context, jobName str
114
117
115
118
records , err := queryScheduleActionRecords (ctx , c .ConnPool , sqlQueryAllByColWithPaginationAndTimeRange (scheduleActionRecordTable , jobNameCol ), jobName , time .UnixMilli (start ), time .UnixMilli (end ), offset , limit )
116
119
if err != nil {
117
- return nil , errors .NewCommonEdgeX (errors .KindDatabaseError , fmt .Sprintf ("failed to query schedule action records by job name %s" , jobName ), err )
120
+ return nil , errors .NewCommonEdgeX (errors .Kind ( err ) , fmt .Sprintf ("failed to query schedule action records by job name %s" , jobName ), err )
118
121
}
119
122
120
123
return records , nil
@@ -130,7 +133,7 @@ func (c *Client) ScheduleActionRecordsByJobNameAndStatus(ctx context.Context, jo
130
133
131
134
records , err := queryScheduleActionRecords (ctx , c .ConnPool , sqlQueryAllByColWithPaginationAndTimeRange (scheduleActionRecordTable , jobNameCol , statusCol ), jobName , status , time .UnixMilli (start ), time .UnixMilli (end ), offset , limit )
132
135
if err != nil {
133
- return nil , errors .NewCommonEdgeX (errors .KindDatabaseError , fmt .Sprintf ("failed to query schedule action records by job name %s and status %s" , jobName , status ), err )
136
+ return nil , errors .NewCommonEdgeX (errors .Kind ( err ) , fmt .Sprintf ("failed to query schedule action records by job name %s and status %s" , jobName , status ), err )
134
137
}
135
138
136
139
return records , nil
@@ -141,23 +144,6 @@ func (c *Client) ScheduleActionRecordTotalCount(ctx context.Context) (uint32, er
141
144
return getTotalRowsCount (ctx , c .ConnPool , sqlQueryCount (scheduleActionRecordTable ))
142
145
}
143
146
144
- // LatestScheduleActionRecordTotalCount returns the total count of all the latest schedule action records
145
- func (c * Client ) LatestScheduleActionRecordTotalCount (ctx context.Context ) (uint32 , errors.EdgeX ) {
146
- sqlQueryLatestScheduleActionRecordCount := `
147
- SELECT COUNT(*)
148
- FROM (
149
- SELECT *
150
- FROM (
151
- SELECT *,
152
- RANK() OVER (PARTITION BY job_name ORDER BY created DESC) AS rnk
153
- FROM scheduler.schedule_action_record
154
- )
155
- WHERE rnk = 1
156
- )
157
- `
158
- return getTotalRowsCount (ctx , c .ConnPool , sqlQueryLatestScheduleActionRecordCount )
159
- }
160
-
161
147
// ScheduleActionRecordCountByStatus returns the total count of the schedule action records by status
162
148
func (c * Client ) ScheduleActionRecordCountByStatus (ctx context.Context , status string ) (uint32 , errors.EdgeX ) {
163
149
return getTotalRowsCount (ctx , c .ConnPool , sqlQueryCountByCol (scheduleActionRecordTable , statusCol ), status )
@@ -190,13 +176,13 @@ func addScheduleActionRecord(ctx context.Context, connPool *pgxpool.Pool, schedu
190
176
191
177
_ , err = connPool .Exec (
192
178
ctx ,
193
- sqlInsert (scheduleActionRecordTable , idCol , jobNameCol , actionCol , statusCol , scheduledAtCol , createdCol ),
179
+ sqlInsert (scheduleActionRecordTable , idCol , actionIdCol , jobNameCol , actionCol , statusCol , scheduledAtCol ),
194
180
scheduleActionRecord .Id ,
181
+ copiedScheduleAction .GetBaseScheduleAction ().Id ,
195
182
scheduleActionRecord .JobName ,
196
183
actionJSONBytes ,
197
184
scheduleActionRecord .Status ,
198
- time .UnixMilli (scheduleActionRecord .ScheduledAt ).UTC (),
199
- time .UnixMilli (scheduleActionRecord .Created ).UTC ())
185
+ time .UnixMilli (scheduleActionRecord .ScheduledAt ).UTC ())
200
186
if err != nil {
201
187
return scheduleActionRecord , pgClient .WrapDBError ("failed to insert schedule action record" , err )
202
188
}
@@ -213,10 +199,11 @@ func queryScheduleActionRecords(ctx context.Context, connPool *pgxpool.Pool, sql
213
199
214
200
var scheduleActionRecords []model.ScheduleActionRecord
215
201
for rows .Next () {
202
+ var actionId string
216
203
var record model.ScheduleActionRecord
217
204
var created , scheduledAt time.Time
218
205
var actionJSONBytes []byte
219
- err := rows .Scan (& record .Id , & record .JobName , & actionJSONBytes , & record .Status , & scheduledAt , & created )
206
+ err := rows .Scan (& record .Id , & actionId , & record .JobName , & actionJSONBytes , & record .Status , & scheduledAt , & created )
220
207
if err != nil {
221
208
return nil , pgClient .WrapDBError ("failed to scan schedule action record" , err )
222
209
}
0 commit comments