1 | /**
|
---|
2 | * StatsLogger.java
|
---|
3 | * Copyright (C) 2010 New Zealand Digital Library, http://expeditee.org
|
---|
4 | *
|
---|
5 | * This program is free software: you can redistribute it and/or modify
|
---|
6 | * it under the terms of the GNU General Public License as published by
|
---|
7 | * the Free Software Foundation, either version 3 of the License, or
|
---|
8 | * (at your option) any later version.
|
---|
9 | *
|
---|
10 | * This program is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | * GNU General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU General Public License
|
---|
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
17 | */
|
---|
18 |
|
---|
19 | package org.expeditee.stats;
|
---|
20 |
|
---|
21 | import java.io.BufferedWriter;
|
---|
22 | import java.io.File;
|
---|
23 | import java.io.FileWriter;
|
---|
24 | import java.io.IOException;
|
---|
25 | import java.lang.reflect.Method;
|
---|
26 |
|
---|
27 | import org.expeditee.gui.Frame;
|
---|
28 | import org.expeditee.gui.FrameIO;
|
---|
29 | import org.expeditee.items.Text;
|
---|
30 | import org.expeditee.settings.UserSettings;
|
---|
31 |
|
---|
32 | /**TODO make this threadsafe*/
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * This class is used to create all stats log files. Files are created with
|
---|
36 | * filename: Date-Time.stat<br>
|
---|
37 | * Where Date is in the format DDMMMYYYY and Time is in the format HHMM.
|
---|
38 | *
|
---|
39 | * @author mrww1
|
---|
40 | *
|
---|
41 | */
|
---|
42 | public class StatsLogger {
|
---|
43 |
|
---|
44 | private static String _filename = null;
|
---|
45 |
|
---|
46 | private static final String FRAMES_EDITED_FILENAME = "FramesEdited.log";
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Sets the path on disk that the statlog files will be created in.
|
---|
50 | *
|
---|
51 | * @param location
|
---|
52 | * The path on disk to create the log files in.
|
---|
53 | */
|
---|
54 | private static void Init() {
|
---|
55 | if (!org.expeditee.settings.UserSettings.LogStats.get())
|
---|
56 | return;
|
---|
57 |
|
---|
58 | File test = new File(org.expeditee.gui.FrameIO.STATISTICS_PATH);
|
---|
59 | if (!test.exists())
|
---|
60 | test.mkdir();
|
---|
61 |
|
---|
62 | _filename = Formatter.getDateTime() + ".stat";
|
---|
63 | _filename = _filename.replace(":","_");
|
---|
64 | }
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * Writes the current stats to a file.
|
---|
68 | *
|
---|
69 | */
|
---|
70 | public static void WriteStatsFile() {
|
---|
71 | if (!org.expeditee.settings.UserSettings.LogStats.get())
|
---|
72 | return;
|
---|
73 |
|
---|
74 | Init();
|
---|
75 |
|
---|
76 | String statsFrameset = UserSettings.StatisticsFrameset.get();
|
---|
77 | if (statsFrameset != null) {
|
---|
78 | try {
|
---|
79 | Frame statsFrame = null;
|
---|
80 | if (FrameIO.LoadFrame(statsFrameset + "0") == null) {
|
---|
81 | statsFrame = FrameIO.CreateNewFrameset(statsFrameset);
|
---|
82 | statsFrame.setTitle(Formatter.getDateTime());
|
---|
83 | } else {
|
---|
84 | statsFrame = FrameIO.CreateFrame(statsFrameset, Formatter
|
---|
85 | .getDateTime(), null);
|
---|
86 | }
|
---|
87 |
|
---|
88 | //Now check the attribute value pairs
|
---|
89 | for(Text t: statsFrame.getBodyTextItems(false)){
|
---|
90 | Method m = StatsFrame.getMethod(t.getText());
|
---|
91 | if(m != null){
|
---|
92 | String value = m.invoke(null, new Object[]{}).toString();
|
---|
93 | t.setText(t.getText() + ": " + value);
|
---|
94 | }
|
---|
95 | }
|
---|
96 | FrameIO.ForceSaveFrame(statsFrame);
|
---|
97 | } catch (Exception e) {
|
---|
98 | e.printStackTrace();
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | try {
|
---|
103 | BufferedWriter writer = new BufferedWriter(new FileWriter(
|
---|
104 | org.expeditee.gui.FrameIO.STATISTICS_PATH + _filename, true));
|
---|
105 |
|
---|
106 | writer.write(SessionStats.getCurrentStats());
|
---|
107 | writer.newLine();
|
---|
108 | writer.newLine();
|
---|
109 | writer.write(SessionStats.getItemStats());
|
---|
110 | writer.newLine();
|
---|
111 | writer.newLine();
|
---|
112 | writer.write(SessionStats.getEventStats());
|
---|
113 | writer.newLine();
|
---|
114 |
|
---|
115 | writer.flush();
|
---|
116 | writer.close();
|
---|
117 |
|
---|
118 | writer = new BufferedWriter(new FileWriter(
|
---|
119 | org.expeditee.gui.FrameIO.STATISTICS_PATH
|
---|
120 | + FRAMES_EDITED_FILENAME, true));
|
---|
121 | writer.write(SessionStats.getFramesEdited());
|
---|
122 | writer.newLine();
|
---|
123 |
|
---|
124 | writer.flush();
|
---|
125 | writer.close();
|
---|
126 | } catch (IOException ioe) {
|
---|
127 | ioe.printStackTrace();
|
---|
128 | }
|
---|
129 | }
|
---|
130 | }
|
---|