001package org.kuali.ole.docstore.metrics.reindex;
002
003import org.json.simple.JSONObject;
004import org.slf4j.Logger;
005import org.slf4j.LoggerFactory;
006
007import java.io.IOException;
008import java.io.StringWriter;
009import java.util.ArrayList;
010import java.util.LinkedHashMap;
011import java.util.LinkedList;
012import java.util.List;
013
014/**
015 * Created by IntelliJ IDEA.
016 * User: Pranitha
017 * Date: 6/27/12
018 * Time: 12:16 PM
019 * To change this template use File | Settings | File Templates.
020 */
021public class ReIndexingStatus {
022    private static final Logger log = LoggerFactory.getLogger(ReIndexingStatus.class);
023    private List<ReIndexingDocTypeStatus> reIndTypStatusList = new ArrayList<ReIndexingDocTypeStatus>();
024
025    private ReIndexingStatus() {
026
027    }
028
029    private static ReIndexingStatus reIndexingStatus = null;
030
031    public static ReIndexingStatus getInstance() {
032        if (reIndexingStatus == null) {
033            reIndexingStatus = new ReIndexingStatus();
034        }
035        return reIndexingStatus;
036    }
037
038    public void reset() {
039        reIndTypStatusList = new ArrayList<ReIndexingDocTypeStatus>();
040    }
041
042    public void startDocType(String docCategory, String docType, String docFormat) {
043        ReIndexingDocTypeStatus reIndexingDocTypeStatus = new ReIndexingDocTypeStatus();
044        reIndexingDocTypeStatus.setDocCategory(docCategory);
045        reIndexingDocTypeStatus.setDocType(docType);
046        reIndexingDocTypeStatus.setDocFormat(docFormat);
047        reIndexingDocTypeStatus.setStatus("Started");
048        reIndTypStatusList.add(reIndexingDocTypeStatus);
049        //        reIndexingStatus.setReIndTypStatusList(reIndTypStatusList);
050    }
051
052    public ReIndexingDocTypeStatus getDocTypeList() {
053        ReIndexingDocTypeStatus reIndexingDocTypeStatus = null;
054        if (reIndTypStatusList != null && reIndTypStatusList.size() > 0) {
055            reIndexingDocTypeStatus = reIndTypStatusList.get(reIndTypStatusList.size() - 1);
056        } else {
057            reIndexingDocTypeStatus = new ReIndexingDocTypeStatus();
058        }
059        return reIndexingDocTypeStatus;
060    }
061
062    public List<ReIndexingDocTypeStatus> getReIndTypStatusList() {
063        return reIndTypStatusList;
064    }
065
066    public void setReIndTypStatusList(List<ReIndexingDocTypeStatus> reIndTypStatusList) {
067        this.reIndTypStatusList = reIndTypStatusList;
068    }
069
070    @Override
071    public String toString() {
072        StringBuilder sb = new StringBuilder();
073        sb.append("\"Doc Category\" \t" + "\"Doc Type \" \t" + "\"Doc Format\" \t" + "\"Type Status\" \t"
074                + "\"Batch Load Time\" \t" + "\"Batch Start Time\" \t" + "\"Batch End Time\" \t"
075                + "\"Batch Indexing Time\"\t" + "\"Records processed\"\t" + "\"Batch Total Time\" \t" +
076                  /* "\"Remaining Records\"\t" +*/ "\"BatchStatus\"\t");
077        for (ReIndexingDocTypeStatus bTS : reIndTypStatusList) {
078            sb.append("\n" + bTS.getDocCategory() + "\t");
079            sb.append(bTS.getDocType() + "\t");
080            sb.append(bTS.getDocFormat() + "\t");
081            sb.append(bTS.getStatus() + "\t");
082
083            for (ReIndexingBatchStatus bS : bTS.getReIndBatStatusList()) {
084                sb.append("\n" + "\t" + "\t" + "\t" + "\t" + bS.getBatchLoadTime() + "\t");
085                sb.append(bS.getBatchStartTime() + "\t");
086                sb.append(bS.getBatchEndTime() + "\t");
087                sb.append(bS.getBatchIndexingTime() + "\t");
088                sb.append(bS.getRecordsProcessed() + "\t");
089                sb.append(bS.getBatchTotalTime() + "\t");
090                //                sb.append(bS.getRecordsRemaining() + "\t");
091                sb.append(bS.getStatus() + "\t");
092
093            }
094        }
095        return sb.toString();
096    }
097
098    public String getJsonString() {
099        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}