001    /*
002     * ReportWriter.java
003     *
004     * Created on October 25, 2004, 3:56 PM
005     *
006     * This file is part of the STAR Scheduler.
007     * Copyright (c) 2003-2006 STAR Collaboration - Brookhaven National Laboratory
008     *
009     * STAR Scheduler is free software; you can redistribute it and/or modify
010     * it under the terms of the GNU General Public License as published by
011     * the Free Software Foundation; either version 2 of the License, or
012     * (at your option) any later version.
013     *
014     * STAR Scheduler is distributed in the hope that it will be useful,
015     * but WITHOUT ANY WARRANTY; without even the implied warranty of
016     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
017     * GNU General Public License for more details.
018     *
019     * You should have received a copy of the GNU General Public License
020     * along with STAR Scheduler; if not, write to the Free Software
021     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
022     */
023    
024    package gov.bnl.star.offline.scheduler.util.persistent;
025    
026    import java.io.File;
027    import java.io.FileOutputStream;
028    import java.io.InputStreamReader;
029    import java.io.PrintStream;
030    import java.util.Date;
031    import java.util.List;
032    
033    import gov.bnl.star.offline.scheduler.request.Request;
034    import gov.bnl.star.offline.scheduler.Job;
035    
036    /** Writes documentation about the state of jobs.
037     * @author  Levente Hajdu
038     */
039    public class ReportWriter {
040        
041        String jobIDBase = "";
042        String reportText ="";
043        
044        /** Creates a new instance of ReportWriter */
045        public ReportWriter() {
046            
047        }
048        
049        public void addToReportText(String text){ reportText = reportText.concat(text + "\n"); }
050        
051        public void writeReport(Request request){
052            jobIDBase = request.getID();
053            List jobs = request.getJobs();
054            addToReportText(gov.bnl.star.offline.scheduler.SchedulerCommandLine.getProgramName()); //write out the version being used
055            addToReportText("File generated at : " + (new Date()).toString()); //write out the time the file was written
056            addToReportText("This file is a detailed report of SUMS meta job " + jobIDBase + " and its "+ jobs.size() +" processes.\n");
057            
058    
059            //////////////////////////////////////////// write the jobs table //////////////////////////////////
060            TablePrinter table = new TablePrinter();
061            table.addRow("ID"); // make col for job id
062            for(int nJob = 0; nJob != jobs.size(); nJob++){
063                Job job = (Job) jobs.get(nJob);
064                table.addToRow(job.getJobID());
065            }
066            
067            table.addRow("Queue"); // make col for job Queue
068            for(int nJob = 0; nJob != jobs.size(); nJob++){
069                Job job = (Job) jobs.get(nJob);
070                table.addToRow(job.getQueue());
071            }
072            
073            table.addRow("Files"); // make col for job Queue
074            for(int nJob = 0; nJob != jobs.size(); nJob++){
075                Job job = (Job) jobs.get(nJob);
076                table.addToRow(job.getInput().size());
077            }
078            
079            table.addRow("estRunTime"); // make col for job Time
080            for(int nJob = 0; nJob != jobs.size(); nJob++){
081                Job job = (Job) jobs.get(nJob);
082                double jobTimeLimit = (((double) job.getInput().size()) / request.getFilesPerHour()) * 60;
083                if((job.getInput().size() == 0)&&(request.getFilesPerHour() != Double.POSITIVE_INFINITY))
084                        jobTimeLimit = 60 / request.getFilesPerHour(); //If the job has a FilesPerHour but no file, then the FilesPerHour is the time the job takes
085                        table.addToRow(String.valueOf(jobTimeLimit) + "min");    
086            }
087            
088            table.addRow("Target"); // make col for job Target
089            for(int nJob = 0; nJob != jobs.size(); nJob++){
090                Job job = (Job) jobs.get(nJob);
091                table.addToRow(job.getTarget() );
092            }
093            
094            table.addRow("SubmitTime"); // make col for job SubmitTime
095            for(int nJob = 0; nJob != jobs.size(); nJob++){
096                Job job = (Job) jobs.get(nJob);
097                if(request.getSimulation())table.addToRow("n/a (Simulation)");
098                else table.addToRow(job.getTimeOfDispatch()); 
099            }
100            
101            table.addRow("Dispatcher"); // make col for Successful
102            for(int nJob = 0; nJob != jobs.size(); nJob++){
103                Job job = (Job) jobs.get(nJob);
104                String str = "";
105                try {
106                   str = job.getAssociatedDispatcher().toString().substring(1 + job.getAssociatedDispatcher().toString().lastIndexOf("."),job.getAssociatedDispatcher().toString().lastIndexOf("@"));
107                } catch (Exception e) {
108                   System.out.println("ERROR in ReportWriter: "+e.toString());
109                }
110                table.addToRow( str );
111            }
112            
113                          
114            table.addRow("Successful"); // make col for Successful
115            for(int nJob = 0; nJob != jobs.size(); nJob++){
116                Job job = (Job) jobs.get(nJob);
117                String yn = "NO";
118                if(job.getDispatchSuccessful()) yn = "YES";        
119                if(request.getSimulation()) yn = "n/a (Simulation)";
120                table.addToRow(yn);
121            }
122            
123            table.addRow("DispatchTime"); // make col for Successful
124            for(int nJob = 0; nJob != jobs.size(); nJob++){
125                //Job job = (Job) jobs.get(nJob);
126                table.addToRow(String.valueOf(((Job) jobs.get(nJob)).getDispatchTime()).concat("ms"));
127            }
128            
129             
130            
131            
132            
133            
134            addToReportText("Jobs table :");
135            addToReportText(table.getTable());
136            table.clearTable();
137            ///////////////////////////////////////////////////////// End of jobs table ///////////////////////////////////////
138            
139            
140            //Write out the reports of request object
141            addToReportText("Request object report :");
142            addToReportText(request.GetReportText() + "\n\n");
143            
144            //Write out the reports of all the job objects
145            for(int i = 0; i != jobs.size()  ; i++ ){
146                Job job = (Job) jobs.get(i);
147                addToReportText("Job object " + job.getJobID() + " report :");
148                addToReportText(job.GetReportText());
149            }
150            
151            
152            
153            addToReportText("End of Report");
154            
155            
156            //Write out a report file.
157            try {
158                String filename = "sched"+jobIDBase+".report";
159                PrintStream inputFileList = new PrintStream(new FileOutputStream(new File(filename),true));
160                inputFileList.println(reportText);
161                System.out.println("Wrote scheduling report to : " + filename);
162            } 
163            catch (Exception e) {
164                System.out.println("Couldn't create the report file.\n" + e.toString());
165            } 
166            
167        }
168        
169    }