|
| 1 | +"""LHCb command for bookkeeping report file generation based on the XMLSummary and the XML catalog.""" |
| 2 | + |
| 3 | +import os |
| 4 | + |
| 5 | +from DIRAC.Workflow.Utilities.Utils import getStepCPUTimes |
| 6 | +from LHCbDIRAC.BookkeepingSystem.Client.BookkeepingClient import BookkeepingClient |
| 7 | +from LHCbDIRAC.Core.Utilities.ProductionData import constructProductionLFNs |
| 8 | +from LHCbDIRAC.Core.Utilities.XMLSummaries import XMLSummary |
| 9 | +from LHCbDIRAC.Workflow.Modules.BookkeepingReport import ( |
| 10 | + _generate_xml_object, |
| 11 | + _generateInputFiles, |
| 12 | + _generateOutputFiles, |
| 13 | + _prepare_job_info, |
| 14 | + _process_time, |
| 15 | +) |
| 16 | +from LHCbDIRAC.Workflow.Modules.ModulesUtilities import getNumberOfProcessorsToUse |
| 17 | + |
| 18 | +from dirac_cwl.core.exceptions import WorkflowProcessingException |
| 19 | + |
| 20 | +from .core import PostProcessCommand |
| 21 | +from .utils import prepare_lhcb_workflow_commons |
| 22 | + |
| 23 | + |
| 24 | +class BookeepingReport(PostProcessCommand): |
| 25 | + """Generates a bookkeeping report file based on the XMLSummary and the pool XML catalog.""" |
| 26 | + |
| 27 | + def execute(self, job_path, **kwargs): |
| 28 | + """Execute the command. |
| 29 | +
|
| 30 | + :param job_path: Path to the job working directory. |
| 31 | + :param kwargs: Additional keyword arguments. |
| 32 | + """ |
| 33 | + # Obtain Workflow Commons |
| 34 | + workflow_commons_path = kwargs.get("workflow-commons-path", os.path.join(job_path, "workflow_commons.json")) |
| 35 | + |
| 36 | + workflow_commons = prepare_lhcb_workflow_commons( |
| 37 | + workflow_commons_path, |
| 38 | + extra_mandatory_values=[ |
| 39 | + "bk_step_id", |
| 40 | + ], |
| 41 | + extra_default_values={ |
| 42 | + "bookkeeping_LFNs": [], |
| 43 | + "size": {}, |
| 44 | + "md5": {}, |
| 45 | + "guid": {}, |
| 46 | + "sim_description": "NoSimConditions", |
| 47 | + }, |
| 48 | + ) |
| 49 | + |
| 50 | + if not workflow_commons["step_status"]["OK"]: |
| 51 | + return |
| 52 | + |
| 53 | + # Setup variables |
| 54 | + start_time = workflow_commons.get("start_time", None) |
| 55 | + |
| 56 | + cpu_times = {} |
| 57 | + if start_time: |
| 58 | + cpu_times["StartTime"] = start_time |
| 59 | + if "start_stats" in workflow_commons: |
| 60 | + cpu_times["StartStats"] = workflow_commons["start_stats"] |
| 61 | + |
| 62 | + exectime, cputime = getStepCPUTimes(cpu_times) |
| 63 | + |
| 64 | + number_of_processors = getNumberOfProcessorsToUse( |
| 65 | + workflow_commons["job_id"], workflow_commons["max_number_of_processors"] |
| 66 | + ) |
| 67 | + |
| 68 | + bk_client = BookkeepingClient() |
| 69 | + |
| 70 | + parameters = { |
| 71 | + "PRODUCTION_ID": workflow_commons["production_id"], |
| 72 | + "JOB_ID": workflow_commons["prod_job_id"], |
| 73 | + "configVersion": workflow_commons["config_version"], |
| 74 | + "outputList": workflow_commons["outputs"], |
| 75 | + "configName": workflow_commons["config_name"], |
| 76 | + "outputDataFileMask": workflow_commons["output_data_file_mask"], |
| 77 | + } |
| 78 | + |
| 79 | + if "bookkeeping_LFNs" in workflow_commons and "production_output_data" in workflow_commons: |
| 80 | + bk_lfns = workflow_commons["bookkeeping_LFNs"] |
| 81 | + |
| 82 | + if not isinstance(bk_lfns, list): |
| 83 | + bk_lfns = [i.strip() for i in bk_lfns.split(";")] |
| 84 | + |
| 85 | + else: |
| 86 | + result = constructProductionLFNs(parameters, bk_client) |
| 87 | + if not result["OK"]: |
| 88 | + raise WorkflowProcessingException("Could not create production LFNs") |
| 89 | + |
| 90 | + bk_lfns = result["Value"]["BookkeepingLFNs"] |
| 91 | + |
| 92 | + ldate, ltime, ldatestart, ltimestart = _process_time(start_time) |
| 93 | + |
| 94 | + # Obtain XMLSummary |
| 95 | + if "xml_summary_path" in workflow_commons: |
| 96 | + xf_o = XMLSummary(workflow_commons["xml_summary_path"]) |
| 97 | + else: |
| 98 | + xf_o = _generate_xml_object( |
| 99 | + workflow_commons["cleaned_application_name"], |
| 100 | + workflow_commons["production_id"], |
| 101 | + workflow_commons["prod_job_id"], |
| 102 | + workflow_commons["command_number"], |
| 103 | + workflow_commons["command_id"], |
| 104 | + ) |
| 105 | + |
| 106 | + info_dict = { |
| 107 | + "exectime": exectime, |
| 108 | + "cputime": cputime, |
| 109 | + "numberOfProcessors": number_of_processors, |
| 110 | + "production_id": workflow_commons["production_id"], |
| 111 | + "jobID": workflow_commons["job_id"], |
| 112 | + "siteName": workflow_commons["site_name"], |
| 113 | + "jobType": workflow_commons["job_type"], |
| 114 | + "applicationName": workflow_commons["application_name"], |
| 115 | + "applicationVersion": workflow_commons["application_version"], |
| 116 | + "numberOfEvents": workflow_commons["number_of_events"], |
| 117 | + } |
| 118 | + |
| 119 | + # Generate job_info object |
| 120 | + job_info = _prepare_job_info( |
| 121 | + info_dict, |
| 122 | + ldatestart, |
| 123 | + ltimestart, |
| 124 | + ldate, |
| 125 | + ltime, |
| 126 | + xf_o, |
| 127 | + workflow_commons["inputs"], |
| 128 | + workflow_commons["command_id"], |
| 129 | + workflow_commons["bk_step_id"], |
| 130 | + bk_client, |
| 131 | + workflow_commons["config_name"], |
| 132 | + workflow_commons["config_version"], |
| 133 | + ) |
| 134 | + |
| 135 | + # Add input files to job_info |
| 136 | + _generateInputFiles(job_info, bk_lfns, workflow_commons["inputs"]) |
| 137 | + |
| 138 | + # Add output files to job_info |
| 139 | + _generateOutputFiles( |
| 140 | + job_info, |
| 141 | + bk_lfns, |
| 142 | + workflow_commons["event_type"], |
| 143 | + workflow_commons["application_name"], |
| 144 | + xf_o, |
| 145 | + workflow_commons["outputs"], |
| 146 | + workflow_commons["inputs"], |
| 147 | + ) |
| 148 | + |
| 149 | + # Generate SimulationConditions |
| 150 | + if workflow_commons["application_name"] == "Gauss": |
| 151 | + job_info.simulation_condition = workflow_commons["sim_description"] |
| 152 | + |
| 153 | + # Convert job_info object to XML |
| 154 | + doc = job_info.to_xml() |
| 155 | + |
| 156 | + # Write to file |
| 157 | + bfilename = f"bookkeeping_{workflow_commons['command_id']}.xml" |
| 158 | + with open(bfilename, "wb") as bfile: |
| 159 | + bfile.write(doc) |
0 commit comments