P15GEN2\59518
2024-05-29 d4210c7c4b04abde20037ea8aa0f54ef8a2649aa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package foundation.monitor;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Stack;
 
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
 
import foundation.json.IJSONProvider;
import foundation.json.IJSONWriter;
import foundation.translator.Translator;
 
public class Progressor implements Iterable<MessageLine>, IJSONProvider {
    
    private static Logger logger;
    private String taskName;
    private Stack<Phase> phaseStack;
    private List<MessageLine> messageList;
    private Phase priorPhase;
 
    private long max;
    private long min;
    private long step;
    private long position;
    private long stepBuffer;
    private String stepMessage;
    private boolean debug;
    
 
    static {
        logger = LogManager.getLogger(Progressor.class);
    }
    
    public Progressor(String taskName) {
        super();
        this.taskName = taskName;
    }
 
    public Progressor() {
        messageList = new ArrayList<MessageLine>(100);
        phaseStack = new Stack<Phase>();
 
        max = 0;
        min = 0;
        step = 0;
        position = 0;
        stepBuffer = 0;
        stepMessage = "0%--------0/0";
        
        debug = true;
    }
 
    public void clear() {
        priorPhase = null;
        phaseStack.clear();
        clearMessages();
        clearStep();
    }
 
    public void clearStep() {
        max = 0;
        min = 0;
        step = 0;
        position = 0;
        stepBuffer = 0;
        stepMessage = "0%--------0/0";
    }
 
    public void stepIt() {
        if (position < max) {
            position++;
            stepBuffer++;
 
            if (stepBuffer >= step) {
                double percent = position * 100 / max;
                stepBuffer = 0;
                stepMessage = Math.round(percent) + "%" + "--------" + position + "/" + max;
            }
        }
        else {
            stepMessage = "100%" + "--------" + max + "/" + max;
        }
    }
 
    public void stepIt(int aStep) {
        if (position < max) {
            position = Math.min(position + aStep, max);
            stepBuffer = stepBuffer + aStep;
 
            if (stepBuffer >= step) {
                double percent = position * 100 / max;
                stepBuffer = 0;
                stepMessage = Math.round(percent) + "%" + "--------" + position + "/" + max;
            }
        }
        else {
            stepMessage = "100%" + "--------" + max + "/" + max;
        }
    }
 
    public long getMax() {
        return max;
    }
 
    public void setMax(long max) {
        clearStep();
        this.max = max;
    }
 
    public long getMin() {
        return min;
    }
 
    public void setMin(int min) {
        this.min = min;
    }
 
    public String getStepMsg() {
        return stepMessage;
    }
 
    public void newTask(String name) {
        taskName = name;
    
        clear();
    
        Date datetime = new Date();
        String text = "【" + name + "】 开始... " + Translator.toString(datetime);
        
        appendLine(text);
    }
 
    public void endTask() {
        String text = "【" + taskName + "】 结束";
        appendLine(text);
    }
 
    public void newPhase(String code, String phaseName) {
        Phase phase = new Phase(code, phaseName);
        phase.setNo(priorPhase, phaseStack);
 
        String indent = "";
        for (int i = 0; i < phaseStack.size(); i++) {
            indent = indent + "    ";
        }
 
        String text = indent + phase.getNoString() + phaseName;
        appendLine(text);
 
        phaseStack.push(phase);
        priorPhase = null;
    }
    
    public void newPhase(String phaseName) {
        Phase phase = new Phase(null, phaseName);
        phase.setNo(priorPhase, phaseStack);
 
        String indent = "";
        for (int i = 0; i < phaseStack.size(); i++) {
            indent = indent + "    ";
        }
 
        String text = indent + phase.getNoString() + phaseName;
        appendLine(text);
 
        phaseStack.push(phase);
        priorPhase = null;
    }
 
    public void endPhase() {
        clearStep();
        priorPhase = phaseStack.pop();
    }
 
    public void clearMessages() {
        messageList.clear();
    }
 
    public void appendLine(String message) {
        int stackSize = phaseStack.size();
        
        //1.
        MessageLine line = new MessageLine(stackSize, message);
        messageList.add(line);
        
        //2.
        if (debug) {
            String indent = "";
            for (int i = 0; i <= stackSize; i++) {
                indent = indent + "    ";
            }
            
            String text = indent + message;
            logger.debug(text);
        }
    }
 
    public void appendInlineMessage(String message) {
        if (message != null) {
            MessageLine line = null;
            int stackSize = phaseStack.size();
            
            if (messageList.isEmpty()) {
                line =  new MessageLine(stackSize, message);
                messageList.add(line);
            }
            else {
                 int max = messageList.size() - 1;
                 line = messageList.get(max);
                 line.appendValue(message);
            }
        }
        
        if (debug) {
            logger.debug(message);
        }
    }
 
    public void setStepName(String step) {
        MessageLine line = new MessageLine(phaseStack.size(), step);
        messageList.add(line);
    }
 
    public List<MessageLine> getMessageList() {
        return messageList;
    }
 
    public void terminate(String error) {
        MessageLine line = new MessageLine(phaseStack.size(), error);
        messageList.add(line);
    }
 
    public Boolean isEmpty() {
        return messageList.size() == 0;
    }
 
    public List<Phase> getCurrentPhases() {
        List<Phase> resultList = new ArrayList<Phase>();
    
        for (Phase phase : phaseStack) {
            resultList.add(phase);
        }
    
        return resultList;
    }
 
    public String getTaskName() {
        return taskName;
    }
 
    public void setStepLength(int batch_Write) {
        // TODO Auto-generated method stub
        
    }
    
    public void setDebug(boolean debug) {
        this.debug = debug;
    }
 
    @Override
    public Iterator<MessageLine> iterator() {
        return messageList.iterator();
    }
 
    @Override
    public void writeJSON(IJSONWriter writer) {
        writer.beginArray();
        
        for (MessageLine line: messageList) {
            writer.beginObject();
            writer.write("placeHolder", line.getPlaceHolder());
            writer.write("value", line.getValue());
            writer.endObject();
        }
        
        writer.endArray();
    }
 
}