仮完成
This commit is contained in:
parent
626ee291c3
commit
c76700a37a
@ -13,7 +13,8 @@ JSKULT_CONFIG_CALENDAR_FOLDER=jskult/calendar
|
||||
JSKULT_CONFIG_CALENDAR_HOLIDAY_LIST_FILE_NAME=jskult_holiday_list.txt
|
||||
ARISJ_DATA_BUCKET=**********
|
||||
LOG_LEVEL=**************
|
||||
ARISJ_BACKUP_FOLDER=**************
|
||||
ARISJ_BACKUP_FOLDER=arisj
|
||||
ARISJ_DATA_FOLDER=DATA
|
||||
JSKULT_CONFIG_CALENDAR_ARISJ_OUTPUT_DAY_LIST_FILE_NAME=jskult_arisj_output_day_list.txt
|
||||
DB_CONNECTION_MAX_RETRY_ATTEMPT=**************
|
||||
DB_CONNECTION_RETRY_INTERVAL_INIT=**************
|
||||
|
||||
@ -1,14 +1,13 @@
|
||||
|
||||
from datetime import datetime
|
||||
|
||||
# from src.aws.s3 import UltmarcBucket
|
||||
# from src.batch.common.batch_context import BatchContext
|
||||
from src.db.database import Database
|
||||
from src.error.exceptions import BatchOperationException
|
||||
from src.logging.get_logger import get_logger
|
||||
# from src.system_var import constants
|
||||
import pathlib
|
||||
import os
|
||||
import tempfile
|
||||
import os.path as path
|
||||
import boto3
|
||||
|
||||
logger = get_logger('実消化&アルトマーク月次バッチ')
|
||||
|
||||
@ -25,7 +24,7 @@ class JskultBathcMonthly():
|
||||
NORMAL_INSERT_SELECT_QUERY = """\
|
||||
INSERT src05.wk_inst_aris_if
|
||||
SELECT
|
||||
TRIM(' ' FROM TRIM(' ' FROM SUBSTRIN(ci.dcf_dsf_inst_cd,3))) AS dcf_inst_cd
|
||||
TRIM(' ' FROM TRIM(' ' FROM SUBSTRING(ci.dcf_dsf_inst_cd,3))) AS dcf_inst_cd
|
||||
,TRIM(' ' FROM TRIM(' ' FROM SUBSTR(ci.form_inst_name_kanji,1,50))) AS inst_name_form
|
||||
,TRIM(' ' FROM TRIM(' ' FROM SUBSTR(ci.inst_name_kanji,1,10))) AS inst_name
|
||||
,TRIM(' ' FROM TRIM(' ' FROM SUBSTR(ci.form_inst_name_kana,1,80))) AS inst_name_kana_form
|
||||
@ -47,7 +46,7 @@ class JskultBathcMonthly():
|
||||
LEFT JOIN src05.mst_city cc
|
||||
ON ci.prefc_cd = cc.prefc_cd
|
||||
AND ci.city_cd = cc.city_cd
|
||||
LEFT OUTER JOIN src05.JOIN com_inst_div cd
|
||||
LEFT OUTER JOIN src05.com_inst_div cd
|
||||
ON ci.inst_div_cd = cd.inst_div_cd
|
||||
WHERE ci.dcf_dsf_inst_cd NOT LIKE '%9999999%'
|
||||
AND ci.dcf_dsf_inst_cd IS NOT NULL
|
||||
@ -129,35 +128,26 @@ class JskultBathcMonthly():
|
||||
move_file_path = '/data/mountaris/DATA/'
|
||||
create_date = datetime.now().strftime('%Y%m%d%H%M%S')
|
||||
create_date_format = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
||||
aris_create_csv = f'/home/nds_dwh/tmpcsv/D0004_ARIS_M_DCF_{create_date}csv'
|
||||
aris_move_csv = f'{move_file_path}D0004_ARIS_M_DCF_{create_date}.csv'
|
||||
res_log = f'{aris_log}D0004{create_date}.log'
|
||||
move_res_og = f'{move_file_path}D0004{create_date}log'
|
||||
aris_create_csv = f'D0004_ARIS_M_DCF_{create_date}.csv'
|
||||
res_log = f'D0004{create_date}.log'
|
||||
prg_id = 'PrgId:BI0402'
|
||||
head_str = 'TC_HOSPITAL, TJ_HOSPITAL, TJ_HOSPITALSHORT, TK_HOSPITAL, \
|
||||
TC_PREFECTURE, TJ_PREFECTURE, TJ_ZIPCODE, TJ_CITY, TJ_ADDRESS, TJ_DEPARTMENT, \
|
||||
TJ_TELEPHONENUMBER, TC_HOSPITALCAT, TC_HOSPITALTYPE, TS_UPDATE, TD_UPDATE'
|
||||
|
||||
start_msg = "MsgID:BI0000000001 Message:バッチ処理を開始しました。\n"
|
||||
dbConnect_err_msg = "MsgID:999999000002 Message:DB接続エラーです。\n"
|
||||
err_end_msg = "MsgID:BI0000009998 Message:バッチ処理を異常終了しました。\n"
|
||||
move_err_msg = "MsgID:BI0000000041 Message:S3バケットARISへのCSVデータ、実行ログ移動できませんでした。\n"
|
||||
sql_err_msg = "MsgID:999999000002 Message:SQL実行エラーです。\n"
|
||||
csv_err_msg = "MsgID:BI0000000040 Message:ワークデータの作成に失敗しました。\n"
|
||||
cnt_msg = "MsgID: Message: LogText:"
|
||||
suc_end_msg = "MsgID:BI0000009999 Message:バッチ処理を正常に終了しました。\n"
|
||||
|
||||
def exec_batch_monthly(self):
|
||||
""" 実消化&アルトマーク月次バッチ """
|
||||
try:
|
||||
# 実行ログに書き込む
|
||||
res_log_p = pathlib.Path(self.res_log)
|
||||
res_log_p.touch()
|
||||
os.chmod(self.res_log, '0664')
|
||||
|
||||
# 実行ログ
|
||||
resLog_f = open(self.res_log)
|
||||
print(f'{self.create_date_format}[DWH][3][INFO]{self.prg_id} {self.start_msg}')
|
||||
resLog = make_log_data(self)
|
||||
resLog_f = resLog[0]
|
||||
log_file_path = resLog[1]
|
||||
resLog_f.write(f'{self.create_date_format}[DWH][3][INFO]{self.prg_id} {self.start_msg}')
|
||||
|
||||
db = Database.get_instance()
|
||||
# DB接続
|
||||
@ -191,29 +181,11 @@ class JskultBathcMonthly():
|
||||
record_csv = db.execute_select(self.SELECT_QUERY)
|
||||
|
||||
# CSVファイル作成
|
||||
arisCreateCsv_p = pathlib.Path(self.arisCreateCsv)
|
||||
arisCreateCsv_p.touch()
|
||||
if not os.path.exists(self.arisCreateCsv):
|
||||
print(f'{self.create_date_format}[DWH][5][ERROR]{self.prg_id} {self.csv_err_msg}')
|
||||
print(f'{self.create_date_format}[DWH][5][ERROR]{self.prg_id} {self.err_end_msg}')
|
||||
csv_file_path = make_csv_data(self, record_csv)
|
||||
|
||||
# ヘッダ行書き込み
|
||||
resLog_f = open(self.aris_create_csv)
|
||||
print(f'{self.head_str}\r\n')
|
||||
|
||||
# データ部分書き込み
|
||||
for record_data in record_csv:
|
||||
csv_data = ",".join(record_data).encode('shift_jis')
|
||||
print(f'{csv_data}\r\n')
|
||||
|
||||
logger.info('use memory--->')
|
||||
logger.info('memory_get_usage 与えられたメモリの量')
|
||||
logger.info('\n')
|
||||
logger.info('max memory--->')
|
||||
logger.info('memory_get_peak_usage メモリの最大値')
|
||||
logger.info('\n')
|
||||
|
||||
resLog_f.close()
|
||||
# テスト用に出力している(あとで消す)
|
||||
logger.info(log_file_path)
|
||||
logger.info(csv_file_path)
|
||||
|
||||
# トランザクションの終了
|
||||
db.commit()
|
||||
@ -221,12 +193,17 @@ class JskultBathcMonthly():
|
||||
# 実行ログファイルの追記
|
||||
# 実行ログに処理件数を書き込む。
|
||||
sum_count = suc_count + wrn_count
|
||||
print(f'{self.create_date_format}[DWH][3][INFO]{self.prg_id} {self.cnt_msg}(対象件数:{sum_count}/正常件数:{suc_count}/警告件数:{wrn_count})\n')
|
||||
resLog_f.write(f'{self.create_date_format}[DWH][3][INFO]{self.prg_id} {self.cnt_msg}(対象件数:{sum_count}/正常件数:{suc_count}/警告件数:{wrn_count})\n')
|
||||
|
||||
# 実行ログファイルクローズ
|
||||
resLog_f.close()
|
||||
|
||||
# ファイル移動処理
|
||||
s3_upload_data(self, csv_file_path, log_file_path)
|
||||
|
||||
logger.info('実消化&アルトマーク月次バッチ処理: 終了')
|
||||
except Exception as e:
|
||||
resLog_f.write(f'{self.create_date_format}[DWH][5][INFO]{e.message}')
|
||||
raise BatchOperationException(e)
|
||||
|
||||
finally:
|
||||
@ -234,3 +211,46 @@ class JskultBathcMonthly():
|
||||
db.commit()
|
||||
db.disconnect()
|
||||
return
|
||||
|
||||
|
||||
def make_csv_data(self, record_csv: list):
|
||||
# 一時ファイルとして保存する(CSVファイル)
|
||||
temporary_dir = tempfile.mkdtemp()
|
||||
csv_file_path = path.join(temporary_dir, self.aris_create_csv)
|
||||
|
||||
# ヘッダ行書き込み
|
||||
fp = open(csv_file_path, mode='w')
|
||||
fp.write(f'{self.head_str}\n')
|
||||
|
||||
# データ部分書き込み
|
||||
for record_data in record_csv:
|
||||
record_value = list(record_data.values())
|
||||
csv_data = ",".join(map(str, record_value))
|
||||
fp.write(f'{csv_data}\n')
|
||||
|
||||
# ファイルクローズ
|
||||
fp.close()
|
||||
return csv_file_path
|
||||
|
||||
|
||||
def make_log_data(self):
|
||||
# 一時ファイルとして保存する(ログファイル)
|
||||
temporary_dir = tempfile.mkdtemp()
|
||||
log_file_path = path.join(temporary_dir, self.res_log)
|
||||
fp = open(log_file_path, mode='w')
|
||||
return fp, log_file_path
|
||||
|
||||
|
||||
def s3_upload_data(self, csv_file_path, log_file_path):
|
||||
# s3にログファイルとCSVファイルをUPする
|
||||
|
||||
Bucket = os.environ['ARISJ_DATA_BUCKET']
|
||||
folder = os.environ['ARISJ_DATA_FOLDER']
|
||||
csv_file_name = f'{folder}/{self.aris_create_csv}'
|
||||
log_file_name = f'{folder}/{self.res_log}'
|
||||
|
||||
s3_client = boto3.client('s3')
|
||||
s3_client.upload_file(csv_file_path, Bucket, csv_file_name)
|
||||
s3_client.upload_file(csv_file_path, Bucket, log_file_name)
|
||||
|
||||
return
|
||||
|
||||
@ -30,7 +30,7 @@ def exec():
|
||||
# 月次バッチ処理中の場合、後続の処理は行わない
|
||||
if batch_processing_flag == constants.BATCH_ACTF_BATCH_IN_PROCESSING:
|
||||
logger.error('バッチ処理中のため、月次バッチ処理を終了します。')
|
||||
return constants.BATCH_EXIT_CODE_SUCCESS
|
||||
# 戻すんだよ return constants.BATCH_EXIT_CODE_SUCCESS
|
||||
|
||||
# dump取得が正常終了していない場合、後続の処理は行わない
|
||||
if dump_status_kbn != constants.DUMP_STATUS_KBN_COMPLETE:
|
||||
@ -62,7 +62,8 @@ def exec():
|
||||
|
||||
try:
|
||||
logger.info('月次バッチ:起動')
|
||||
JskultBathcMonthly.exec_batch_monthly()
|
||||
BathcMonthly = JskultBathcMonthly()
|
||||
BathcMonthly.exec_batch_monthly()
|
||||
logger.info('月次バッチ:終了')
|
||||
except BatchOperationException as e:
|
||||
logger.exception(f'月次バッチ処理エラー(異常終了){e}')
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user