View Javadoc
1   package org.kuali.ole.docstore.metrics.reindex;
2   
3   import org.json.simple.JSONObject;
4   import org.slf4j.Logger;
5   import org.slf4j.LoggerFactory;
6   
7   import java.io.IOException;
8   import java.io.StringWriter;
9   import java.util.ArrayList;
10  import java.util.LinkedHashMap;
11  import java.util.LinkedList;
12  import java.util.List;
13  
14  /**
15   * Created by IntelliJ IDEA.
16   * User: Pranitha
17   * Date: 6/27/12
18   * Time: 12:16 PM
19   * To change this template use File | Settings | File Templates.
20   */
21  public class ReIndexingStatus {
22      private static final Logger log = LoggerFactory.getLogger(ReIndexingStatus.class);
23      private List<ReIndexingDocTypeStatus> reIndTypStatusList = new ArrayList<ReIndexingDocTypeStatus>();
24  
25      private ReIndexingStatus() {
26  
27      }
28  
29      private static ReIndexingStatus reIndexingStatus = null;
30  
31      public static ReIndexingStatus getInstance() {
32          if (reIndexingStatus == null) {
33              reIndexingStatus = new ReIndexingStatus();
34          }
35          return reIndexingStatus;
36      }
37  
38      public void reset() {
39          reIndTypStatusList = new ArrayList<ReIndexingDocTypeStatus>();
40      }
41  
42      public void startDocType(String docCategory, String docType, String docFormat) {
43          ReIndexingDocTypeStatus reIndexingDocTypeStatus = new ReIndexingDocTypeStatus();
44          reIndexingDocTypeStatus.setDocCategory(docCategory);
45          reIndexingDocTypeStatus.setDocType(docType);
46          reIndexingDocTypeStatus.setDocFormat(docFormat);
47          reIndexingDocTypeStatus.setStatus("Started");
48          reIndTypStatusList.add(reIndexingDocTypeStatus);
49          //        reIndexingStatus.setReIndTypStatusList(reIndTypStatusList);
50      }
51  
52      public ReIndexingDocTypeStatus getDocTypeList() {
53          ReIndexingDocTypeStatus reIndexingDocTypeStatus = null;
54          if (reIndTypStatusList != null && reIndTypStatusList.size() > 0) {
55              reIndexingDocTypeStatus = reIndTypStatusList.get(reIndTypStatusList.size() - 1);
56          } else {
57              reIndexingDocTypeStatus = new ReIndexingDocTypeStatus();
58          }
59          return reIndexingDocTypeStatus;
60      }
61  
62      public List<ReIndexingDocTypeStatus> getReIndTypStatusList() {
63          return reIndTypStatusList;
64      }
65  
66      public void setReIndTypStatusList(List<ReIndexingDocTypeStatus> reIndTypStatusList) {
67          this.reIndTypStatusList = reIndTypStatusList;
68      }
69  
70      @Override
71      public String toString() {
72          StringBuilder sb = new StringBuilder();
73          sb.append("\"Doc Category\" \t" + "\"Doc Type \" \t" + "\"Doc Format\" \t" + "\"Type Status\" \t"
74                  + "\"Batch Load Time\" \t" + "\"Batch Start Time\" \t" + "\"Batch End Time\" \t"
75                  + "\"Batch Indexing Time\"\t" + "\"Records processed\"\t" + "\"Batch Total Time\" \t" +
76                    /* "\"Remaining Records\"\t" +*/ "\"BatchStatus\"\t");
77          for (ReIndexingDocTypeStatus bTS : reIndTypStatusList) {
78              sb.append("\n" + bTS.getDocCategory() + "\t");
79              sb.append(bTS.getDocType() + "\t");
80              sb.append(bTS.getDocFormat() + "\t");
81              sb.append(bTS.getStatus() + "\t");
82  
83              for (ReIndexingBatchStatus bS : bTS.getReIndBatStatusList()) {
84                  sb.append("\n" + "\t" + "\t" + "\t" + "\t" + bS.getBatchLoadTime() + "\t");
85                  sb.append(bS.getBatchStartTime() + "\t");
86                  sb.append(bS.getBatchEndTime() + "\t");
87                  sb.append(bS.getBatchIndexingTime() + "\t");
88                  sb.append(bS.getRecordsProcessed() + "\t");
89                  sb.append(bS.getBatchTotalTime() + "\t");
90                  //                sb.append(bS.getRecordsRemaining() + "\t");
91                  sb.append(bS.getStatus() + "\t");
92  
93              }
94          }
95          return sb.toString();
96      }
97  
98      public String getJsonString() {
99          StringWriter out = new StringWriter();
100         JSONObject obj = new JSONObject();
101         LinkedHashMap reindexMap = null;
102         LinkedList reindexList = new LinkedList();
103         for (ReIndexingDocTypeStatus bTS : reIndTypStatusList) {
104             reindexMap = getRebuildIndexMap(bTS.getDocCategory(), bTS.getDocType(), bTS.getDocFormat(), bTS.getStatus(),
105                     "", "", "", "", null, "", "");
106             reindexList.add(reindexMap);
107             for (ReIndexingBatchStatus bS : bTS.getReIndBatStatusList()) {
108                 reindexMap = getRebuildIndexMap("", "", "", "", bS.getBatchLoadTime(), bS.getBatchStartTime(),
109                         bS.getBatchEndTime(), bS.getBatchIndexingTime(),
110                         bS.getRecordsProcessed(), bS.getBatchTotalTime(), bS.getStatus());
111                 reindexList.add(reindexMap);
112             }
113         }
114         obj.put("rows", reindexList);
115         try {
116             obj.writeJSONString(out);
117         } catch (IOException e) {
118             log.error("Error occurred due to :", e);
119         }
120         return out.toString();
121     }
122 
123     public LinkedHashMap getRebuildIndexMap(String category, String type, String format, String typeStatus,
124                                             String batchLoadTime, String batchStartTime, String batchEndTime,
125                                             String batchIndexTime, Long recordsProcessed, String batchTotalTime,
126                                             String status) {
127         LinkedHashMap reindexMap = new LinkedHashMap();
128         reindexMap.put("category", category);
129         reindexMap.put("type", type);
130         reindexMap.put("format", format);
131         reindexMap.put("typeStatus", typeStatus);
132         reindexMap.put("batchLoadTime", batchLoadTime);
133         reindexMap.put("batchStartTime", batchStartTime);
134         reindexMap.put("batchEndTime", batchEndTime);
135         reindexMap.put("batchIndexTime", batchIndexTime);
136         reindexMap.put("recordsProcessed", recordsProcessed);
137         reindexMap.put("batchTotalTime", batchTotalTime);
138         reindexMap.put("status", status);
139 
140         return reindexMap;
141     }
142 
143 
144 }