Browse Source

REPORT-15314 103模板信息收集=>精确度提升

research/10.0
plough 6 years ago
parent
commit
db3996303e
  1. 2
      designer-base/src/main/java/com/fr/design/mainframe/DesktopCardPane.java
  2. 39
      designer-base/src/main/java/com/fr/design/mainframe/JTemplate.java
  3. 72
      designer-base/src/main/java/com/fr/design/mainframe/template/info/TimeConsumeTimer.java
  4. 62
      designer-base/src/test/java/com/fr/design/mainframe/template/info/TimeConsumeTimerTest.java

2
designer-base/src/main/java/com/fr/design/mainframe/DesktopCardPane.java

@ -35,6 +35,7 @@ public class DesktopCardPane extends BasicPane implements TargetModifiedListener
} }
DesignerFrameFileDealerPane.getInstance().setCurrentEditingTemplate(jt); DesignerFrameFileDealerPane.getInstance().setCurrentEditingTemplate(jt);
if (component != null) { if (component != null) {
component.onLostFocus();
remove(component); remove(component);
} }
add(component = jt, BorderLayout.CENTER); add(component = jt, BorderLayout.CENTER);
@ -42,6 +43,7 @@ public class DesktopCardPane extends BasicPane implements TargetModifiedListener
repaint(); repaint();
revalidate(); revalidate();
component.requestGridFocus(); component.requestGridFocus();
component.onGetFocus();
} }
protected JTemplate<?, ?> getSelectedJTemplate() { protected JTemplate<?, ?> getSelectedJTemplate() {

39
designer-base/src/main/java/com/fr/design/mainframe/JTemplate.java

@ -36,6 +36,7 @@ import com.fr.design.i18n.Toolkit;
import com.fr.design.layout.FRGUIPaneFactory; import com.fr.design.layout.FRGUIPaneFactory;
import com.fr.design.mainframe.template.info.TemplateInfoCollector; import com.fr.design.mainframe.template.info.TemplateInfoCollector;
import com.fr.design.mainframe.template.info.TemplateProcessInfo; import com.fr.design.mainframe.template.info.TemplateProcessInfo;
import com.fr.design.mainframe.template.info.TimeConsumeTimer;
import com.fr.design.mainframe.toolbar.ToolBarMenuDockPlus; import com.fr.design.mainframe.toolbar.ToolBarMenuDockPlus;
import com.fr.design.mainframe.toolbar.VcsScene; import com.fr.design.mainframe.toolbar.VcsScene;
import com.fr.design.menu.MenuDef; import com.fr.design.menu.MenuDef;
@ -78,7 +79,6 @@ import java.util.regex.Pattern;
public abstract class JTemplate<T extends BaseBook, U extends BaseUndoState<?>> extends TargetComponent<T> implements ToolBarMenuDockPlus, DesignerProxy { public abstract class JTemplate<T extends BaseBook, U extends BaseUndoState<?>> extends TargetComponent<T> implements ToolBarMenuDockPlus, DesignerProxy {
// TODO ALEX_SEP editingFILE这个属性一定要吗?如果非要不可,有没有可能保证不为null // TODO ALEX_SEP editingFILE这个属性一定要吗?如果非要不可,有没有可能保证不为null
private static final int PREFIX_NUM = 3000; private static final int PREFIX_NUM = 3000;
private static final int ONE_THOUSAND = 1000;
private FILE editingFILE = null; private FILE editingFILE = null;
// alex:初始状态为saved,这样不管是新建模板,还是打开模板,如果未做任何操作直接关闭,不提示保存 // alex:初始状态为saved,这样不管是新建模板,还是打开模板,如果未做任何操作直接关闭,不提示保存
private boolean saved = true; private boolean saved = true;
@ -92,7 +92,7 @@ public abstract class JTemplate<T extends BaseBook, U extends BaseUndoState<?>>
private static short currentIndex = 0;// 此变量用于多次新建模板时,让名字不重复 private static short currentIndex = 0;// 此变量用于多次新建模板时,让名字不重复
private DesignModelAdapter<T, ?> designModel; private DesignModelAdapter<T, ?> designModel;
private PreviewProvider previewType; private PreviewProvider previewType;
private long openTime = 0L; // 打开模板的时间点(包括新建模板) private TimeConsumeTimer consumeTimer = new TimeConsumeTimer();
public int resolution = ScreenResolution.getScreenResolution(); public int resolution = ScreenResolution.getScreenResolution();
public JTemplate() { public JTemplate() {
@ -100,7 +100,6 @@ public abstract class JTemplate<T extends BaseBook, U extends BaseUndoState<?>>
public JTemplate(T t, String defaultFileName) { public JTemplate(T t, String defaultFileName) {
this(t, new MemFILE(newTemplateNameByIndex(defaultFileName)), true); this(t, new MemFILE(newTemplateNameByIndex(defaultFileName)), true);
openTime = System.currentTimeMillis();
} }
public JTemplate(T t, FILE file) { public JTemplate(T t, FILE file) {
@ -123,10 +122,24 @@ public abstract class JTemplate<T extends BaseBook, U extends BaseUndoState<?>>
this.add(createCenterPane(), BorderLayout.CENTER); this.add(createCenterPane(), BorderLayout.CENTER);
this.undoState = createUndoState(); this.undoState = createUndoState();
designModel = createDesignModel(); designModel = createDesignModel();
// 如果不是新建模板,并且在收集列表中
if (!isNewFile && TemplateInfoCollector.getInstance().contains(t.getTemplateID())) { consumeTimer.setEnabled(shouldInitForCollectInfo(isNewFile));
openTime = System.currentTimeMillis();
} }
void onGetFocus() {
consumeTimer.start();
}
void onLostFocus() {
consumeTimer.stop();
}
private boolean shouldInitForCollectInfo(boolean isNewFile) {
if (isNewFile) {
return true;
}
// 不是新建模板,但是已经在收集列表中
return TemplateInfoCollector.getInstance().contains(template.getTemplateID());
} }
// 刷新右侧属性面板 // 刷新右侧属性面板
@ -139,9 +152,8 @@ public abstract class JTemplate<T extends BaseBook, U extends BaseUndoState<?>>
// 为收集模版信息作准备 // 为收集模版信息作准备
private void initForCollect() { private void initForCollect() {
generateTemplateId(); generateTemplateId();
if (openTime == 0) { consumeTimer.setEnabled(true);
openTime = System.currentTimeMillis(); consumeTimer.start();
}
} }
private void collectInfo() { // 执行收集操作 private void collectInfo() { // 执行收集操作
@ -149,16 +161,15 @@ public abstract class JTemplate<T extends BaseBook, U extends BaseUndoState<?>>
} }
private void collectInfo(String originID) { // 执行收集操作 private void collectInfo(String originID) { // 执行收集操作
if (openTime == 0) { // 旧模板,不收集数据 if (!consumeTimer.isEnabled()) {
return; return;
} }
long saveTime = System.currentTimeMillis(); // 保存模板的时间点
try { try {
long timeConsume = ((saveTime - openTime) / ONE_THOUSAND); // 制作模板耗时(单位:s) int timeConsume = consumeTimer.popTime();
TemplateInfoCollector.getInstance().collectInfo(template.getTemplateID(), originID, getProcessInfo(), (int)timeConsume); TemplateInfoCollector.getInstance().collectInfo(template.getTemplateID(), originID, getProcessInfo(), timeConsume);
} catch (Throwable th) { // 不管收集过程中出现任何异常,都不应该影响模版保存 } catch (Throwable th) { // 不管收集过程中出现任何异常,都不应该影响模版保存
} }
openTime = saveTime; // 更新 openTime,准备下一次计算 consumeTimer.start(); // 准备下一次计算
} }
public abstract TemplateProcessInfo<T> getProcessInfo(); public abstract TemplateProcessInfo<T> getProcessInfo();

72
designer-base/src/main/java/com/fr/design/mainframe/template/info/TimeConsumeTimer.java

@ -0,0 +1,72 @@
package com.fr.design.mainframe.template.info;
/**
* Created by plough on 2019/4/19.
*/
public class TimeConsumeTimer {
private static final int ONE_THOUSAND = 1000;
private enum State {
RUNNING, STOPPED
}
private int timeConsume; // 单位 s
private long startMS; // 单位 ms
private long stopMS;
private State state;
private boolean enabled;
public TimeConsumeTimer() {
reset();
}
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public void start() {
if (!isEnabled() || isRunning()) {
return;
}
startMS = System.currentTimeMillis();
state = State.RUNNING;
}
public void stop() {
if (!isEnabled() || !isRunning()) {
return;
}
stopMS = System.currentTimeMillis();
timeConsume += ((stopMS - startMS) / ONE_THOUSAND);
startMS = 0;
stopMS = 0;
state = State.STOPPED;
System.out.println("timeConsume now: " + timeConsume);
}
public int popTime() {
if (!isEnabled()) {
return 0;
}
stop();
int result = timeConsume;
reset();
return result;
}
private boolean isRunning() {
return state == State.RUNNING;
}
private void reset() {
timeConsume = 0;
startMS = 0;
stopMS = 0;
state = State.STOPPED;
}
}

62
designer-base/src/test/java/com/fr/design/mainframe/template/info/TimeConsumeTimerTest.java

@ -0,0 +1,62 @@
package com.fr.design.mainframe.template.info;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
/**
* Created by plough on 2019/4/19.
*/
public class TimeConsumeTimerTest {
@Test
public void testNotEnabled() throws InterruptedException {
TimeConsumeTimer consumeTimer = new TimeConsumeTimer();
consumeTimer.start();
Thread.sleep(1100);
consumeTimer.stop();
assertEquals(0, consumeTimer.popTime());
}
@Test
public void testEnabled() throws InterruptedException {
TimeConsumeTimer consumeTimer = new TimeConsumeTimer();
consumeTimer.setEnabled(true);
consumeTimer.start();
Thread.sleep(1100);
consumeTimer.stop();
assertEquals(1, consumeTimer.popTime());
assertEquals(0, consumeTimer.popTime());
}
@Test
public void testMultiTimes() throws InterruptedException {
TimeConsumeTimer consumeTimer = new TimeConsumeTimer();
consumeTimer.setEnabled(true);
consumeTimer.start();
Thread.sleep(1010);
consumeTimer.stop();
Thread.sleep(2000);
consumeTimer.start();
Thread.sleep(1010);
assertEquals(2, consumeTimer.popTime());
}
@Test
public void testStartMultiTime() throws InterruptedException {
TimeConsumeTimer consumeTimer = new TimeConsumeTimer();
consumeTimer.setEnabled(true);
consumeTimer.start();
Thread.sleep(1010);
consumeTimer.start();
Thread.sleep(1010);
consumeTimer.start();
Thread.sleep(1010);
assertEquals(3, consumeTimer.popTime());
}
}
Loading…
Cancel
Save