1
- import boto3
2
1
from boto3 .dynamodb .conditions import Key , Attr
3
2
4
3
def run_queries (table ):
@@ -29,14 +28,12 @@ def run_queries(table):
29
28
def run_query_1 (table ):
30
29
"""
31
30
Query 1: Get meetings by date and email
32
- - Key condition: PK1 = email AND SK1 BETWEEN date1 AND date2
33
- - Filter condition: Duration > 0
31
+ Key condition: PK1 = email AND SK1 BETWEEN date1 AND date2
32
+ Filter condition: Duration > 0
34
33
"""
35
- # Define query conditions using boto3 Key() and Attr()
36
34
key_condition = Key (
"PK1" ).
eq (
"[email protected] " )
& Key (
"SK1" ).
between (
"MS-2022-07-02" ,
"MS-2022-07-08" )
37
35
filter_condition = Attr ("Duration" ).gt ("0" )
38
36
39
- # Execute query
40
37
response = table .query (
41
38
IndexName = "GSI-1" ,
42
39
KeyConditionExpression = key_condition ,
@@ -67,11 +64,9 @@ def run_query_2(table):
67
64
Key condition: PK=employeeID SK between(date1, date2)
68
65
Filter condition: duration > 0
69
66
"""
70
- # Define query conditions using boto3 Key() and Attr()
71
67
key_condition = Key ("PK" ).eq ("E-emp_001" ) & Key ("SK" ).between ("MS-2022-07-02" , "MS-2022-07-08" )
72
68
filter_condition = Attr ("Duration" ).gt ("0" )
73
69
74
- # Execute query
75
70
response = table .query (
76
71
IndexName = "GSI-0" ,
77
72
KeyConditionExpression = key_condition ,
@@ -95,6 +90,7 @@ def run_query_2(table):
95
90
96
91
assert found_known_value_item
97
92
93
+
98
94
def run_query_3 (table ):
99
95
"""
100
96
Query 3: Get meetings by date and building/floor/room
@@ -105,12 +101,11 @@ def run_query_3(table):
105
101
However, one cannot use primary keys (partition nor sort) in a filter expression.
106
102
Instead, this query filters on the individual beacon attributes: building, floor, and room.
107
103
"""
108
- # Define query conditions using boto3 Key() and Attr()
109
104
key_condition = Key ("PK" ).eq ("B-SEA33" ) & Key ("SK" ).between ("MS-2022-07-02" , "MS-2022-07-08" )
110
105
filter_condition = (Attr ("Building" ).eq ("SEA33" ) &
111
106
Attr ("Floor" ).eq ("12" ) &
112
107
Attr ("Room" ).eq ("403" ))
113
- # Execute query
108
+
114
109
response = table .query (
115
110
IndexName = "GSI-0" ,
116
111
KeyConditionExpression = key_condition ,
@@ -132,15 +127,14 @@ def run_query_3(table):
132
127
133
128
assert found_known_value_item
134
129
130
+
135
131
def run_query_4 (table ):
136
132
"""
137
133
Query 4: Get employee data by email
138
134
Key condition: PK1=email SK1=employee ID
139
135
"""
140
- # Define query conditions using boto3 Key()
141
136
key_condition = Key (
"PK1" ).
eq (
"[email protected] " )
& Key (
"SK1" ).
eq (
"E-emp_001" )
142
137
143
- # Execute query
144
138
response = table .query (
145
139
IndexName = "GSI-1" ,
146
140
KeyConditionExpression = key_condition
@@ -162,15 +156,14 @@ def run_query_4(table):
162
156
163
157
assert found_known_value_item
164
158
159
+
165
160
def run_query_5 (table ):
166
161
"""
167
162
Query 5: Get meetings by email
168
163
Key condition: PK1=email SK1 > 30 days ago
169
164
"""
170
- # Define query conditions using boto3 Key()
171
165
key_condition = Key (
"PK1" ).
eq (
"[email protected] " )
& Key (
"SK1" ).
between (
"MS-" ,
"MS-2023-03-20" )
172
166
173
- # Execute query
174
167
response = table .query (
175
168
IndexName = "GSI-1" ,
176
169
KeyConditionExpression = key_condition
@@ -193,15 +186,14 @@ def run_query_5(table):
193
186
194
187
assert found_known_value_item
195
188
189
+
196
190
def run_query_6 (table ):
197
191
"""
198
192
Query 6: Get tickets by email
199
193
Key condition: PK1=email SK1 > 30 days ago
200
194
"""
201
- # Define query conditions using boto3 Key()
202
195
key_condition = Key (
"PK1" ).
eq (
"[email protected] " )
& Key (
"SK1" ).
lt (
"MS-2023-03-20" )
203
196
204
- # Execute query
205
197
response = table .query (
206
198
IndexName = "GSI-1" ,
207
199
KeyConditionExpression = key_condition
@@ -223,15 +215,14 @@ def run_query_6(table):
223
215
224
216
assert found_known_value_item
225
217
218
+
226
219
def run_query_7 (table ):
227
220
"""
228
221
Query 7: Get reservations by email
229
222
Key condition: PK1=organizeremail SK1 > 30 days ago
230
223
"""
231
- # Define query conditions using boto3 Key()
232
224
key_condition = Key (
"PK1" ).
eq (
"[email protected] " )
& Key (
"SK1" ).
lt (
"MS-2023-03-20" )
233
225
234
- # Execute query
235
226
response = table .query (
236
227
IndexName = "GSI-1" ,
237
228
KeyConditionExpression = key_condition
@@ -260,10 +251,8 @@ def run_query_8(table):
260
251
Query 8: Get time cards by email
261
252
Key condition: PK1=employeeemail SK1 > 30 days ago
262
253
"""
263
- # Define query conditions using boto3 Key()
264
254
key_condition = Key (
"PK1" ).
eq (
"[email protected] " )
& Key (
"SK1" ).
between (
"TC-" ,
"TC-2023-03-20" )
265
255
266
- # Execute query
267
256
response = table .query (
268
257
IndexName = "GSI-1" ,
269
258
KeyConditionExpression = key_condition
@@ -290,10 +279,8 @@ def run_query_9(table):
290
279
Query 9: Get employee info by employee ID
291
280
Key condition: PK1=employeeID SK starts with "E-"
292
281
"""
293
- # Define query conditions using boto3 Key()
294
282
key_condition = Key ("PK" ).eq ("E-emp_001" ) & Key ("SK" ).begins_with ("E-" )
295
283
296
- # Execute query
297
284
response = table .query (
298
285
IndexName = "GSI-0" ,
299
286
KeyConditionExpression = key_condition
@@ -314,16 +301,15 @@ def run_query_9(table):
314
301
315
302
assert found_known_value_item
316
303
304
+
317
305
def run_query_10 (table ):
318
306
"""
319
307
Query 10: Get employee info by email
320
308
Key condition: PK1=email
321
309
Filter condition: SK starts with "E-"
322
310
"""
323
- # Define query conditions using boto3 Key()
324
311
key_condition = Key (
"PK1" ).
eq (
"[email protected] " )
& Key (
"SK1" ).
begins_with (
"E-" )
325
312
326
- # Execute query
327
313
response = table .query (
328
314
IndexName = "GSI-1" ,
329
315
KeyConditionExpression = key_condition
@@ -350,10 +336,8 @@ def run_query_11(table):
350
336
Query 11: Get ticket history by ticket number
351
337
Key condition: PK=TicketNumber
352
338
"""
353
- # Define query conditions using boto3 Key()
354
339
key_condition = Key ("PK" ).eq ("T-ticket_001" )
355
340
356
- # Execute query
357
341
response = table .query (
358
342
IndexName = "GSI-0" ,
359
343
KeyConditionExpression = key_condition
@@ -382,11 +366,9 @@ def run_query_12(table):
382
366
Key condition: PK1=CreatorEmail
383
367
Filter condition: PK=TicketNumber
384
368
"""
385
- # Define query conditions using boto3 Key() and Attr()
386
369
key_condition = Key (
"PK1" ).
eq (
"[email protected] " )
387
370
filter_condition = Attr ("PK" ).eq ("T-ticket_001" )
388
371
389
- # Execute query
390
372
response = table .query (
391
373
IndexName = "GSI-1" ,
392
374
KeyConditionExpression = key_condition ,
@@ -415,11 +397,9 @@ def run_query_13(table):
415
397
Key condition: PK=AssigneeEmail
416
398
Filter condition: PK=ticketNumber
417
399
"""
418
- # Define query conditions using boto3 Key() and Attr()
419
400
key_condition = Key (
"PK2" ).
eq (
"[email protected] " )
420
401
filter_condition = Attr ("PK" ).eq ("T-ticket_001" )
421
402
422
- # Execute query
423
403
response = table .query (
424
404
IndexName = "GSI-2" ,
425
405
KeyConditionExpression = key_condition ,
@@ -447,7 +427,6 @@ def run_query_14(table):
447
427
Query 14: Get employees by city.building.floor.desk
448
428
Key condition: PK3=city SK3 begins_with(building.floor.desk)
449
429
"""
450
- # Define query conditions using boto3 Key()
451
430
key_condition = Key ("PK3" ).eq ("C-Seattle" ) & Key ("SK3" ).begins_with ("B-44~F-12~D-3" )
452
431
453
432
# Execute query with retries since GSIs don't update instantly
@@ -483,15 +462,14 @@ def run_query_14(table):
483
462
# Assert the value was found inside the loop
484
463
assert found_known_value_item
485
464
465
+
486
466
def run_query_15 (table ):
487
467
"""
488
468
Query 15: Get employees by manager email
489
469
Key condition: PK2 = ManagerEmail
490
470
"""
491
- # Define query conditions using boto3 Key()
492
471
key_condition = Key (
"PK2" ).
eq (
"[email protected] " )
493
472
494
- # Execute query
495
473
response = table .query (
496
474
IndexName = "GSI-2" ,
497
475
KeyConditionExpression = key_condition
@@ -520,10 +498,8 @@ def run_query_16(table):
520
498
Query 16: Get assigned tickets by assignee email
521
499
Key condition: PK2 = AssigneeEmail
522
500
"""
523
- # Define query conditions using boto3 Key()
524
501
key_condition = Key (
"PK2" ).
eq (
"[email protected] " )
525
502
526
- # Execute query
527
503
response = table .query (
528
504
IndexName = "GSI-2" ,
529
505
KeyConditionExpression = key_condition
@@ -555,10 +531,8 @@ def run_query_17(table):
555
531
is 2022-10-07T09:30:00, and that our sample ticket record
556
532
with TicketModTime=2022-10-07T14:32:25 will be returned.)
557
533
"""
558
- # Define query conditions using boto3 Key()
559
534
key_condition = Key ("PK3" ).eq ("S-3" ) & Key ("SK3" ).gt ("M-2022-10-07T09:30:00" )
560
535
561
- # Execute query
562
536
response = table .query (
563
537
IndexName = "GSI-3" ,
564
538
KeyConditionExpression = key_condition
@@ -587,11 +561,9 @@ def run_query_18(table):
587
561
Key condition: PK1 = Status, SK1 > StartDate
588
562
Filter condition: TargetDelivery < TargetDate
589
563
"""
590
- # Define query conditions using boto3 Key() and Attr()
591
564
key_condition = Key ("PK1" ).eq ("PSts-Pending" ) & Key ("SK1" ).gt ("PS-2022-01-01" )
592
565
filter_condition = Attr ("ProjectTarget" ).lt ("2025-01-01" )
593
566
594
- # Execute query
595
567
response = table .query (
596
568
IndexName = "GSI-1" ,
597
569
KeyConditionExpression = key_condition ,
@@ -619,10 +591,8 @@ def run_query_19(table):
619
591
Query 19: Get projects by name
620
592
Key condition: PK = ProjectName, SK = ProjectName
621
593
"""
622
- # Define query conditions using boto3 Key()
623
594
key_condition = Key ("PK" ).eq ("P-project_001" ) & Key ("SK" ).eq ("P-project_001" )
624
595
625
- # Execute query
626
596
response = table .query (
627
597
IndexName = "GSI-0" ,
628
598
KeyConditionExpression = key_condition
@@ -649,10 +619,8 @@ def run_query_20(table):
649
619
Query 20: Get Project History by date range (against timecard record)
650
620
Key condition: PK = ProjectName, SK between(date1, date2)
651
621
"""
652
- # Define query conditions using boto3 Key()
653
622
key_condition = Key ("PK" ).eq ("P-project_002" ) & Key ("SK" ).between ("TC-2022-01-01" , "TC-2023-01-01" )
654
623
655
- # Execute query
656
624
response = table .query (
657
625
IndexName = "GSI-0" ,
658
626
KeyConditionExpression = key_condition
@@ -681,11 +649,9 @@ def run_query_21(table):
681
649
Key condition: PK = ProjectName
682
650
Filter condition: role=rolename
683
651
"""
684
- # Define query conditions using boto3 Key() and Attr()
685
652
key_condition = Key ("PK" ).eq ("P-project_002" )
686
653
filter_condition = Attr ("Role" ).eq ("SDE3" )
687
654
688
- # Execute query
689
655
response = table .query (
690
656
IndexName = "GSI-0" ,
691
657
KeyConditionExpression = key_condition ,
@@ -713,10 +679,8 @@ def run_query_22(table):
713
679
Query 22: Get reservations by building ID
714
680
Key condition: PK = Building ID
715
681
"""
716
- # Define query conditions using boto3 Key()
717
682
key_condition = Key ("PK" ).eq ("B-SEA33" )
718
683
719
- # Execute query
720
684
response = table .query (
721
685
IndexName = "GSI-0" ,
722
686
KeyConditionExpression = key_condition
@@ -745,11 +709,9 @@ def run_query_23(table):
745
709
Key condition: PK = Building ID, SK between(date1, date2)
746
710
Filter condition: Duration > 0
747
711
"""
748
- # Define query conditions using boto3 Key() and Attr()
749
712
key_condition = Key ("PK" ).eq ("B-SEA33" ) & Key ("SK" ).between ("MS-2022-07-01" , "MS-2022-07-08" )
750
713
filter_condition = Attr ("Duration" ).gt ("0" )
751
714
752
- # Execute query
753
715
response = table .query (
754
716
IndexName = "GSI-0" ,
755
717
KeyConditionExpression = key_condition ,
0 commit comments