# Report generation

Four report generation scripts are defined in the AppConfig.xml document.

Two of the scripts are used to generate XML reports for Oracle Endeca Workbench and two generate HTML reports that can be viewed in a browser. All scripts share similar functionality, so only one is included below, with numbered steps indicating the actions performed at each point in the script.

      <script id="DailyReports">
        <bean-shell-script>
          <![CDATA[
        log.info("Starting daily Workbench report generation script.");

1. Obtain lock. The report generation script attempts to set a "`report_generator_lock`" flag in the EAC to serve as a lock or mutex. If the flag is already set, this step fails, ensuring that the report generator cannot be started more than once simultaneously, as the default report generators share input directories and working directories. The flag is removed in the case of an error or when the script completes successfully.

           if (LockManager.acquireLock("report_generator_lock")) {

2. Clean working directories. Clear any files in the report generator's input directory.

             // clean report gen input dir
             DailyReportGenerator.cleanInputDir();

3. Distribute configuration files to each server. A single copy of the Dgraph configuration files is distributed to each server specified in the configuration.

       DgraphCluster.copyDgraphConfigToDgraphServers();

4. Roll LogServer. If the LogServer is actively writing to a file and the file is required for the specified time range, the LogServer needs to be rolled in order to free up the log file. This code handles that test and invokes the roll administrative URL command on the LogServer, if necessary.

             // roll the logserver, if the report requires the active log file
             if (LogServer.isActive() &&
                 LogServer.yesterdayIncludesLatestLogFile()) {
               LogServer.callLogserverRollUrl();
             }

5. Retrieve logs for specified report. The LogServer identifies log files in its output directory that are required to generate a report for the requested date range. Those files are copied to the target directory configured for the LogServer. Note that this step could be modified to include retrieving logs from multiple LogServers, if more than one is deployed.

             // retrieve required log files for processing
             LogServer.copyYesterdayLogFilesToTargetDir();

6. Update Report Generator to the appropriate time range and output file name. Oracle Endeca Workbench requires reports to be named according to a time stamp convention. The Report Generator component’s provisioning is updated to specify the appropriate time range, time series and output filename. The output file path in the existing provisioning is updated to use the same path, but to use the date stamp as the filename. Files default to a “.xml” extension, though the component will attempt to retain a “.html” extension, if specified in the AppConfig.xml.

             // update report generator to the appropriate dates, time series
             // and to output a timestamped file, as required by Workbench
             DailyReportGenerator.updateProvisioningForYesterdayReport();

7. Archive logs. If one or more files were copied into the report generator's input directory, report generation will proceed. Start by archiving logs associated with the previous report generator execution.

             if (DailyReportGenerator.reportInputDirContainsFiles()) {
               // archive logs
               DailyReportGenerator.archiveLogDir();

8. Run report generator. Execute the report generation process.

               // generate report
               DailyReportGenerator.run();

9. Copy report to Oracle Endeca Workbench report directory. By default, Oracle Endeca Workbench reads reports from a directory in its workspace. Typically, the directory is [ENDECA_TOOLS_CONF]/reports/[appName]/daily or [Endeca_TOOLS_CONF]/reports/[appName]/weekly. Starting in Oracle Endeca Workbench 1.0.1, this location can be configured by provisioning a host named "`webstudio`" with a custom directory named "webstudio-report-dir." The Deployment Template provisions this directory and delivers generated reports to that location for Workbench to read. The report file (and associated charts) will be copied to this directory, as specified in the AppConfig.xml, which defaults to [appdir]/reports. Note that this step is not necessary for HTML reports, as those reports are not viewed in Oracle Endeca Workbench.

               // copy generated report and charts to web studio directory
               // defined in "webstudio" host and its "webstudio-report-dir"
               // directory
               reportHost = "webstudio";
               absDestDir = PathUtils.getAbsolutePath(webstudio.getWorkingDir(),
                   webstudio.getDirectory("webstudio-report-dir"));
               isDaily = true;
               DailyReportGenerator.copyReportToWebStudio(reportHost,
                   absDestDir, isDaily);
             }
             else {
               log.warning("No log files for report generator to process.");
             }

             LockManager.releaseLock("report_generator_lock");
             log.info("Finished daily Workbench report generation.");
           }
           else {
             log.warning("Failed to obtain lock.");
           }
             ]]>
           </bean-shell-script>
         </script>