|
| 1 | +import shutil |
| 2 | +import traceback |
| 3 | +from glob import glob |
| 4 | +from os import makedirs |
| 5 | +from os.path import dirname, join, basename, exists |
| 6 | + |
| 7 | +import pandas as pd |
| 8 | +import migrate.versioning.api as api |
| 9 | +from mlcomp.utils.io import zip_folder |
| 10 | + |
| 11 | +from mlcomp.db.enums import LogStatus, ComponentType |
| 12 | + |
| 13 | +from mlcomp import SA_CONNECTION_STRING, REPORT_FOLDER, LOG_FOLDER, DB_TYPE, \ |
| 14 | + CONFIG_FOLDER, ROOT_FOLDER, DATA_FOLDER, MODEL_FOLDER, TASK_FOLDER, \ |
| 15 | + DB_FOLDER, TMP_FOLDER |
| 16 | +from mlcomp.utils.misc import now, to_snake |
| 17 | + |
| 18 | +from mlcomp.db.providers import DagProvider, LogProvider |
| 19 | + |
| 20 | + |
| 21 | +def statuses(folder: str = None): |
| 22 | + rows = [] |
| 23 | + |
| 24 | + folder_status = 'OK' |
| 25 | + folder_comment = '' |
| 26 | + |
| 27 | + folders = [ |
| 28 | + ROOT_FOLDER, |
| 29 | + DATA_FOLDER, |
| 30 | + MODEL_FOLDER, |
| 31 | + TASK_FOLDER, |
| 32 | + LOG_FOLDER, |
| 33 | + CONFIG_FOLDER, |
| 34 | + DB_FOLDER, |
| 35 | + REPORT_FOLDER, |
| 36 | + TMP_FOLDER |
| 37 | + ] |
| 38 | + for f in folders: |
| 39 | + if not exists(f): |
| 40 | + folder_status = 'ERROR' |
| 41 | + folder_comment = f'folder {f} does not exist' |
| 42 | + |
| 43 | + files = [ |
| 44 | + join(CONFIG_FOLDER, '.env') |
| 45 | + ] |
| 46 | + for f in files: |
| 47 | + if not exists(f): |
| 48 | + folder_status = 'ERROR' |
| 49 | + folder_comment = f'file {f} does not exist' |
| 50 | + |
| 51 | + rows.append({ |
| 52 | + 'name': 'Folders', |
| 53 | + 'status': folder_status, |
| 54 | + 'comment': folder_comment |
| 55 | + }) |
| 56 | + |
| 57 | + database_status = 'OK' |
| 58 | + database_comment = f'DB_TYPE = {DB_TYPE}' |
| 59 | + try: |
| 60 | + provider = DagProvider() |
| 61 | + provider.count() |
| 62 | + except Exception: |
| 63 | + database_status = 'ERROR' |
| 64 | + database_comment += ' ' + traceback.format_exc() |
| 65 | + |
| 66 | + rows.append({ |
| 67 | + 'name': 'Database', |
| 68 | + 'status': database_status, |
| 69 | + 'comment': database_comment |
| 70 | + }) |
| 71 | + |
| 72 | + if database_status == 'OK': |
| 73 | + migrate_status = 'OK' |
| 74 | + |
| 75 | + repository_folder = join(dirname(__file__), 'migration') |
| 76 | + repository_version = api.version(repository_folder) |
| 77 | + |
| 78 | + db_version = api.db_version(SA_CONNECTION_STRING, repository_folder) |
| 79 | + |
| 80 | + if db_version != repository_version: |
| 81 | + migrate_status = 'ERROR' |
| 82 | + migrate_comment = f'Repository version = {repository_version} ' \ |
| 83 | + f'Db version = {db_version}' |
| 84 | + else: |
| 85 | + migrate_comment = f'version: {db_version}' |
| 86 | + |
| 87 | + rows.append({ |
| 88 | + 'name': 'Migrate', |
| 89 | + 'status': migrate_status, |
| 90 | + 'comment': migrate_comment |
| 91 | + }) |
| 92 | + |
| 93 | + df = pd.DataFrame(rows) |
| 94 | + |
| 95 | + if folder is not None: |
| 96 | + print('Statuses:') |
| 97 | + print(df) |
| 98 | + |
| 99 | + df.to_csv(join(folder, 'statuses.csv'), index=False) |
| 100 | + |
| 101 | + return df |
| 102 | + |
| 103 | + |
| 104 | +def check_statuses(): |
| 105 | + stats = statuses() |
| 106 | + failed = stats[stats['status'] != 'OK'] |
| 107 | + if failed.shape[0] > 0: |
| 108 | + print('There are errors in statuses') |
| 109 | + for row in stats.itertuples(): |
| 110 | + print(f'name: {row.name} status' |
| 111 | + f' {row.status} comment {row.comment}') |
| 112 | + |
| 113 | + import time |
| 114 | + time.sleep(0.01) |
| 115 | + |
| 116 | + raise Exception('There are errors in statuses. ' |
| 117 | + 'Please check them above') |
| 118 | + |
| 119 | + |
| 120 | +def logs(folder: str = None): |
| 121 | + log_provider = LogProvider() |
| 122 | + errors = log_provider.last(count=1000, levels=[LogStatus.Error.value]) |
| 123 | + service_components = [ComponentType.Supervisor.value, |
| 124 | + ComponentType.API.value, |
| 125 | + ComponentType.WorkerSupervisor.value] |
| 126 | + services = log_provider.last(count=1000, components=service_components) |
| 127 | + logs = errors + services |
| 128 | + |
| 129 | + rows = [] |
| 130 | + for l, _ in logs: |
| 131 | + rows.append({ |
| 132 | + 'status': to_snake(LogStatus(l.level).name), |
| 133 | + 'component': to_snake(ComponentType(l.component).name), |
| 134 | + 'time': l.time, |
| 135 | + 'message': l.message, |
| 136 | + }) |
| 137 | + df = pd.DataFrame(rows) |
| 138 | + df.to_csv(join(folder, 'logs_db.csv'), index=False) |
| 139 | + |
| 140 | + if folder is not None: |
| 141 | + for file in glob(join(LOG_FOLDER, '*')): |
| 142 | + shutil.copy(file, join(folder, basename(file))) |
| 143 | + print('logs formed') |
| 144 | + return df |
| 145 | + |
| 146 | + |
| 147 | +def create_report(): |
| 148 | + print('*** Report Start ***') |
| 149 | + print() |
| 150 | + |
| 151 | + folder = join(REPORT_FOLDER, f'{now()}'.split('.')[0]) |
| 152 | + makedirs(folder, exist_ok=True) |
| 153 | + |
| 154 | + statuses(folder) |
| 155 | + |
| 156 | + print() |
| 157 | + |
| 158 | + logs(folder) |
| 159 | + |
| 160 | + print() |
| 161 | + |
| 162 | + zip_path = folder + '.zip' |
| 163 | + zip_folder(folder, dst=zip_path) |
| 164 | + |
| 165 | + print('Report path', zip_path) |
| 166 | + print() |
| 167 | + |
| 168 | + print('*** Report End ***') |
| 169 | + |
| 170 | + |
| 171 | +__all__ = ['check_statuses', 'create_report'] |
0 commit comments