-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaws_check_reservation.py
539 lines (461 loc) · 20.7 KB
/
aws_check_reservation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
import subprocess
import json
import traceback
import datetime
from itertools import groupby
import collections
import argparse
import logging
import os
# 引数設定
parser = argparse.ArgumentParser(description='カレンダー実予約数チェック')
parser.add_argument('-p', '--profile',
default='lsc-dev-shibazaki',
help='AWS profile名')
parser.add_argument('--surveyCalendars',
default='lsc-shibazaki-survey-static-SurveyCalendars-FI2FQDEAO8M6',
help='例:lsc-dev-#####-survey-static-SurveyCalendars-############')
parser.add_argument('--surveyResults',
default='lsc-shibazaki-survey-static-SurveyResults-174JYZI0EURAP',
help='例:lsc-dev-#####-survey-static-SurveyResults-############')
parser.add_argument('--surveyId',
default='',
help='例:d7cf####-3a##-40##-9e##-e1ac09######',
nargs='*'
)
parser.add_argument('--categoryId',
default='category#007385',
help='例:category#000000')
parser.add_argument('--log',
default='diff_calendar.log',
help='ログ名')
args = parser.parse_args()
# 引数/定数定義
table_name_survey_results = args.surveyResults
table_name_survey_calendars = args.surveyCalendars
survey_ids = args.surveyId
category_id = args.categoryId
profile_name = args.profile
QUOTAS = 'quotas'
RESERVATION_COUNTS = 'reservationCounts'
VALUE = 'value'
SURVEY_ID = 'surveyId'
PARTITION_KEY = 'partitionKey'
CHECK = 'check'
USER_ID = 'userId'
GRATER_THAN_RESULTS = 'カレンダー予約数 > 帳票予約数'
GRATER_THAN_CALENDARS = 'カレンダー予約数 < 帳票予約数'
TAG_1 = 'tag1'
TAG_2 = 'tag2'
TAG_3 = 'tag3'
# ログ設定
logger = logging.getLogger("logger") #logger名loggerを取得
current_path = os.path.dirname(__file__)
file_name = f'{profile_name}_{category_id}_{args.log}'
file_path = os.path.join(current_path, file_name)
handler = logging.FileHandler(filename=file_path, encoding='utf-8') #ファイル出力
logger.setLevel(logging.DEBUG) #loggerとしてはDEBUGで
handler.setFormatter(logging.Formatter("%(asctime)s,%(message)s"))
logger.addHandler(handler)
def run_aws_cli_command(command):
result = subprocess.Popen(command, stdout=subprocess.PIPE).communicate()[0]
try:
# 実行PCがwindows(デフォルトの文字コードがcp932)の場合
result = result.decode('cp932')
except UnicodeDecodeError:
# 実行PCがmac(デフォルトの文字コードがutf-8)の場合
result = result.decode('utf-8')
return result
def get_items(data):
try:
parsed_data = json.loads(data)
items = parsed_data.get('Items')
return items
except:
traceback.print_exc()
logger.info('【Error】: データの取得ができておりません。DB名、surveyId、categoryId、profile名に間違いがないことを確認してください。')
def count_list_element(list):
counter = collections.Counter(list)
return counter.most_common()
def get_calendar_id(category_id):
"""
categoryIdを元に検索対象のcalendarIdを取得
:calendar_id 引数のcategoryIdを元に取得されたcalendarId
:return calendar_id. type is string.
"""
search_attribute_values = json.dumps({
':pkey': {
"S": 'categories'
},
':skey': {
'S': category_id
}
})
command = []
command.append('aws')
command.append('dynamodb')
command.append('query')
command.append('--table-name')
command.append(table_name_survey_calendars)
command.append('--key-condition-expression')
command.append('partitionKey=:pkey and sortKey=:skey')
command.append('--expression-attribute-values')
command.append(search_attribute_values)
command.append('--profile')
command.append(profile_name)
try:
# コマンドを実行/Itemsを取得
result = run_aws_cli_command(command)
items = get_items(result)
if len(items) > 0:
calendar_id = items[0]['calendarId']['S']
return calendar_id
else:
return None
except:
traceback.print_exc()
return None
def get_category_tag(category_id):
"""
categoryIdを元に検索対象のtag1 ~ 3を取得
"""
search_attribute_values = json.dumps({
':pkey': {
"S": 'categories'
},
':skey': {
'S': category_id
}
})
command = []
command.append('aws')
command.append('dynamodb')
command.append('query')
command.append('--table-name')
command.append(table_name_survey_calendars)
command.append('--key-condition-expression')
command.append('partitionKey=:pkey and sortKey=:skey')
command.append('--expression-attribute-values')
command.append(search_attribute_values)
command.append('--profile')
command.append(profile_name)
try:
# コマンドを実行/Itemsを取得
result = run_aws_cli_command(command)
items = get_items(result)
if len(items) > 0:
tag1 = items[0][TAG_1]['S'].encode('utf-8').decode('utf-8')
tag2 = items[0][TAG_2]['S'].encode('utf-8').decode('utf-8')
tag3 = items[0][TAG_3]['S'].encode('utf-8').decode('utf-8')
return f'{tag1} > {tag2} > {tag3}'
else:
return None
except:
traceback.print_exc()
return None
def get_reservation_info(calendar_id):
"""
SurveyCalendarsからcalendarIdを元に現在の予約数(reservationCounts)・予約の限度数(quotas)・予約日(date)を取得
:calendar_id 引数のcategoryIdを元に取得されたcalendarId
:return dictionary.
{
"quotas": {
date: {
coma: int
}
},
"reservationCounts": {
date: {
coma: int
}
}
}
"""
search_attribute_values = json.dumps({
':pkey': {
"S": calendar_id
}
})
command = []
command.append('aws')
command.append('dynamodb')
command.append('query')
command.append('--table-name')
command.append(table_name_survey_calendars)
command.append('--key-condition-expression')
command.append('partitionKey=:pkey')
command.append('--expression-attribute-values')
command.append(search_attribute_values)
command.append('--profile')
command.append(profile_name)
try:
# コマンドを実行/Itemsを取得
result = run_aws_cli_command(command)
items = get_items(result)
return_value = {
QUOTAS: {},
RESERVATION_COUNTS: {}
}
# そのまま返すと分かりにくいので整形する
for item in items:
quotas = item[QUOTAS]['M']
reservation_counts = item[RESERVATION_COUNTS]['M']
date = item['date']['S']
arranged_quotas = {}
arranged_reservation = {}
for coma, value in quotas.items():
arranged_quotas[coma] = int(value['N'])
for coma, value in reservation_counts.items():
arranged_reservation[coma] = int(value['N'])
return_value[QUOTAS][date] = arranged_quotas
return_value[RESERVATION_COUNTS][date] = arranged_reservation
return return_value
except:
traceback.print_exc()
def get_survey_ids():
"""
categoryIdを元に、予約データのsurveyIdを取得する
"""
command = []
command.append('aws')
command.append('dynamodb')
command.append('scan')
command.append('--table-name')
command.append(table_name_survey_results)
command.append('--profile')
command.append(profile_name)
return_value = []
try:
result = run_aws_cli_command(command)
items = get_items(result)
for item in items:
value = None
survey_id = None
if VALUE in item and 'S' in item[VALUE]:
value = item[VALUE]['S']
if not category_id in value:
continue
else:
continue
if SURVEY_ID in item\
and 'S' in item[SURVEY_ID]\
and item[SURVEY_ID]['S'] != SURVEY_ID:
survey_id = item[SURVEY_ID]['S']
else:
continue
if value and survey_id and not survey_id in return_value:
return_value.append(survey_id)
except:
traceback.print_exc()
finally:
logger.info('surveyId:'+ str(return_value))
return return_value
def get_reservation_record():
"""
SurveyResultsから実予約のレコードを検索・返却
:return reservation_records for SurveyResults. type is list.
"""
start_datetime = datetime.datetime.now().strftime('%Y年%m月%d日 %H:%M:%S')
logger.info(f'検索開始日時 : {start_datetime}')
logger.info('検索中...')
items = []
reservation_records = []
global survey_ids
# surveyIdが引数で入力されていなかった場合
if len(survey_ids) == 0:
survey_ids = get_survey_ids()
if len(survey_ids) != 0:
for survey_id in survey_ids:
# surveyId毎にレコードを取得する
search_attribute_values = json.dumps({
":pkey": {
"S": survey_id
},
":skey": {
"S": survey_id
}
})
command = []
command.append('aws')
command.append('dynamodb')
command.append('query')
command.append('--table-name')
command.append(table_name_survey_results)
command.append('--index-name')
command.append('surveyId-partitionKey-index')
command.append('--key-condition-expression')
command.append('surveyId=:pkey and begins_with(partitionKey, :skey)')
command.append('--expression-attribute-values')
command.append(search_attribute_values)
command.append('--profile')
command.append(profile_name)
try:
# コマンドを実行/Itemsを取得
result = run_aws_cli_command(command)
items.extend(get_items(result))
except:
traceback.print_exc()
try:
if len(items) > 0:
# partitionKey毎にソート
items.sort(key=lambda item: item[PARTITION_KEY]['S'])
# partitionKey毎にグループ化してループ
for pkey, items in groupby(items, key=lambda item: item[PARTITION_KEY]['S']):
# 値渡しでitemsをコピーしておく
copy_items = [item for item in items]
# checkの値を取り出す
check_list = []
for item in copy_items:
# checkに値が保存されていない場合エラーになるのでバリデーション
if 'check' in item:
check_list.append(item[CHECK]['S'])
# checkが混在している場合はキャンセルもしくは取り消しされたpartitionKeyとする = 予約数にカウントしない
check_counted = count_list_element(check_list)
if len(check_counted) > 1:
continue
# checkがキャンセル,取り消し以外のレコードを取得
for item in copy_items:
# キーエラーにならないようにバリデーション
if CHECK in item and 'S' in item[CHECK]:
check = item['check']['S']
# キーエラーにならないようにバリデーション
if VALUE in item and 'S' in item['value']:
value = item['value']['S']
if check != 'キャンセル' and check != '取り消し' and category_id in value:
reservation_records.append(item)
except:
traceback.print_exc()
finally:
if len(reservation_records) == 0:
return None
else:
return reservation_records
def comparsion_reservation():
"""
実予約数とsurveyCalendarの予約数を比較する
"""
count_greater_than_results = 0
count_greater_than_calendars = 0
greater_than_results_list = []
greater_than_calendars_list = []
category_tags = get_category_tag(category_id)
try:
# 各日付・コマ毎の予約数を整形する
reservations_for_survey_results = {}
diff_info = {}
reservation_records = get_reservation_record()
separate = '|'
if reservation_records:
# partitionKey毎にソート
reservation_records.sort(key=lambda record: record[PARTITION_KEY]['S'])
# partitionKey毎にグループ化してループ
for pkey, records in groupby(reservation_records, key=lambda record: record[PARTITION_KEY]['S']):
# 値渡し
copy_records = [record for record in records]
user_id = None
# userIdの取得
for record in copy_records:
if 'userId' in record and 'S' in record['userId'] and user_id == None:
user_id = record['userId']['S']
break
for record in copy_records:
# 予約データかどうかを判別
if 'check' in record and 'S' in record['check']:
if VALUE in record and 'S' in record[VALUE]:
# valueの例:category#000000_0|20210522|1
# categoryId_0|予約日|コマ という形式
value = record['value']['S']
else:
value = None
if value and separate in value and category_id in value:
# '|'で区切る
split_value = value.split(separate)
date = split_value[1]
coma = split_value[2]
# keyErrorにならないようにvalidation
if not date in reservations_for_survey_results:
reservations_for_survey_results[date] = {
coma: 0
}
if not date in diff_info:
diff_info[date] = {
coma: []
}
# keyErrorにならないようにvalidation
if date in reservations_for_survey_results\
and not coma in reservations_for_survey_results[date]:
reservations_for_survey_results[date][coma] = 0
if date in diff_info\
and not coma in diff_info[date]:
diff_info[date][coma] = []
# 実予約数を増やす
reservations_for_survey_results[date][coma] += 1
# 実予約の接種券番号を追加
diff_info[date][coma].append({
PARTITION_KEY: pkey,
USER_ID: user_id
})
# SurveyCalendarsから予約数と予約上限数を取得
calendar_id = get_calendar_id(category_id)
if calendar_id:
reservations_for_survey_calendars = get_reservation_info(calendar_id)
current_reservations = reservations_for_survey_calendars[RESERVATION_COUNTS]
# SurveyResultsの予約数と、SurveyCalendarsの予約数が一致しない場合はログに表示する
input('SurveyResultsの予約数とSurveyCalendarsの予約数が一致しないデータをログファイルに保存します。Enterを押して下さい。 > ')
for date in reservations_for_survey_results.keys():
diff_reservations = {}
if not date in current_reservations:
# キーエラーにならないようにバリデーション
current_reservations[date] = {}
if date in current_reservations:
# 日付単位で一致しているか判断
if reservations_for_survey_results[date] != current_reservations[date]:
# コマ単位で一致しているか判断
for coma in reservations_for_survey_results[date]:
# キーエラーにならないようにバリデーション
if not coma in current_reservations[date]:
current_reservations[date][coma] = 0
if reservations_for_survey_results[date][coma] != current_reservations[date][coma]:
diff_reservations = {
'date': date,
'coma':coma,
'calendarCounts': current_reservations[date][coma],
'resultsCounts': reservations_for_survey_results[date][coma],
}
# どちらが大きいかを比較する
if reservations_for_survey_results[date][coma] < current_reservations[date][coma]:
count_greater_than_results += 1
greater_than_results_list.append(diff_reservations)
elif reservations_for_survey_results[date][coma] > current_reservations[date][coma]:
count_greater_than_calendars += 1
greater_than_calendars_list.append(diff_reservations)
else:
logger.info('calendarIdが取得できませんでした。カレンダーが存在しない可能性があります。')
logger.info('検索が完了しました。')
logger.info(f'{category_id}:{category_tags}')
logger.info(f'{GRATER_THAN_RESULTS}:{count_greater_than_results}件')
if count_greater_than_results > 0:
greater_than_results_list.sort(key=lambda data: (data['date'], data['coma']))
for data in greater_than_results_list:
date = data['date']
coma = data['coma']
calendar_counts = data['calendarCounts']
results_counts = data['resultsCounts']
log_string = f'{date}:{coma} カレンダー予約数:帳票予約数 = {calendar_counts}:{results_counts}'
logger.info(log_string)
logger.info(f'{GRATER_THAN_CALENDARS}:{count_greater_than_calendars}件')
if count_greater_than_calendars > 0:
greater_than_calendars_list.sort(key=lambda data: (data['date'], data['coma']))
for data in greater_than_calendars_list:
date = data['date']
coma = data['coma']
calendar_counts = data['calendarCounts']
results_counts = data['resultsCounts']
log_string = f'{date}:{coma} カレンダー予約数:帳票予約数 = {calendar_counts}:{results_counts}'
logger.info(log_string)
except:
traceback.print_exc()
finally:
end_datetime = datetime.datetime.now().strftime('%Y年%m月%d日 %H:%M:%S')
logger.info(f'検索終了日時 : {end_datetime}')
comparsion_reservation()