Skip to main content
summaryrefslogtreecommitdiffstats
blob: 27f7cfe42f54d4606333c7b34c148e8ec0de8960 (plain) (blame)
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
283
284
285
286
287
288
289
290
291
292
293
294
295
/*******************************************************************************
 * Copyright (c) 2004, 2009 Tasktop Technologies and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Meghan Allen - initial API and implementation
 *******************************************************************************/

package org.eclipse.mylyn.internal.monitor.usage;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.IJobChangeEvent;
import org.eclipse.core.runtime.jobs.IJobChangeListener;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.mylyn.commons.core.StatusHandler;
import org.eclipse.mylyn.internal.monitor.core.collection.IUsageCollector;
import org.eclipse.mylyn.internal.monitor.usage.editors.UsageStatsEditorInput;
import org.eclipse.mylyn.internal.monitor.usage.editors.UsageSummaryReportEditorPart;
import org.eclipse.mylyn.monitor.core.InteractionEvent;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;

/**
 * Job that performs the rollover of the monitor interaction history log file (modelled after
 * org.eclipse.mylyn.internal.tasks.ui.util.TaskDataExportJob). Overwrites destination if exists!
 * 
 * @author Meghan Allen
 */
public class MonitorFileRolloverJob extends Job implements IJobChangeListener {

	private static final String JOB_LABEL = Messages.MonitorFileRolloverJob_Mylyn_Monitor_Log_Rollover;

	// XXX: needs to be the same as NAME_DATA_DIR in org.eclipse.mylyn.tasks.ui.TasksUIPlugin
	private static final String NAME_DATA_DIR = ".mylyn"; //$NON-NLS-1$

	private static final String DIRECTORY_MONITOR_BACKUP = "monitor"; //$NON-NLS-1$

	private static final String ZIP_EXTENSION = ".zip"; //$NON-NLS-1$

	private List<IUsageCollector> collectors = null;

	private ReportGenerator generator = null;

	private IEditorInput input = null;

	private boolean forceSyncForTesting = false;

	public static final String BACKUP_FILE_SUFFIX = "monitor-log"; //$NON-NLS-1$

	private final StudyParameters studyParameters;

	public MonitorFileRolloverJob(List<IUsageCollector> collectors, StudyParameters studyParameters) {
		super(JOB_LABEL);
		this.collectors = collectors;
		this.studyParameters = studyParameters;
	}

	@SuppressWarnings("deprecation")
	private String getYear(InteractionEvent event) {
		return "" + (event.getDate().getYear() + 1900); //$NON-NLS-1$
	}

	public void forceSyncForTesting(boolean forceSync) {
		this.forceSyncForTesting = forceSync;
	}

	private String getMonth(int month) {
		switch (month) {
		case 0:
			return "01"; //$NON-NLS-1$
		case 1:
			return "02"; //$NON-NLS-1$
		case 2:
			return "03"; //$NON-NLS-1$
		case 3:
			return "04"; //$NON-NLS-1$
		case 4:
			return "05"; //$NON-NLS-1$
		case 5:
			return "06"; //$NON-NLS-1$
		case 6:
			return "07"; //$NON-NLS-1$
		case 7:
			return "08"; //$NON-NLS-1$
		case 8:
			return "09"; //$NON-NLS-1$
		case 9:
			return "10"; //$NON-NLS-1$
		case 10:
			return "11"; //$NON-NLS-1$
		case 11:
			return "12"; //$NON-NLS-1$
		default:
			return ""; //$NON-NLS-1$

		}
	}

	public static String getZippedMonitorFileDirPath() {
		return ResourcesPlugin.getWorkspace().getRoot().getLocation().toString() + File.separatorChar + NAME_DATA_DIR
				+ File.separatorChar + DIRECTORY_MONITOR_BACKUP;
	}

	@Override
	@SuppressWarnings("deprecation")
	public IStatus run(final IProgressMonitor progressMonitor) {

		progressMonitor.beginTask(JOB_LABEL, IProgressMonitor.UNKNOWN);

		final File monitorFile = UiUsageMonitorPlugin.getDefault().getMonitorLogFile();
		InteractionEventLogger logger = UiUsageMonitorPlugin.getDefault().getInteractionLogger();

		logger.stopMonitoring();

		List<InteractionEvent> events = logger.getHistoryFromFile(monitorFile);
		progressMonitor.worked(1);

		int nowMonth = Calendar.getInstance().get(Calendar.MONTH);
		if (events.size() > 0 && events.get(0).getDate().getMonth() != nowMonth) {
			int currMonth = events.get(0).getDate().getMonth();

			String fileName = getYear(events.get(0)) + "-" + getMonth(currMonth) + "-" + BACKUP_FILE_SUFFIX; //$NON-NLS-1$ //$NON-NLS-2$

			File dir = new File(getZippedMonitorFileDirPath());

			if (!dir.exists()) {
				dir.mkdirs();
			}
			try {
				File currBackupZipFile = new File(dir, fileName + ZIP_EXTENSION);
				if (!currBackupZipFile.exists()) {
					currBackupZipFile.createNewFile();
				}
				ZipOutputStream zipFileStream;

				zipFileStream = new ZipOutputStream(new FileOutputStream(currBackupZipFile));
				zipFileStream.putNextEntry(new ZipEntry(UiUsageMonitorPlugin.getDefault().getMonitorLogFile().getName()));

				for (InteractionEvent event : events) {
					int monthOfCurrEvent = event.getDate().getMonth();
					if (monthOfCurrEvent == currMonth) {
						// put in curr zip
						String xml = logger.writeLegacyEvent(event);

						zipFileStream.write(xml.getBytes());

					} else if (monthOfCurrEvent != nowMonth) {
						// we are finished backing up currMonth, but now need to
						// start backing up monthOfCurrEvent
						progressMonitor.worked(1);
						zipFileStream.closeEntry();
						zipFileStream.close();

						fileName = getYear(event) + "-" + getMonth(monthOfCurrEvent) + "-" + BACKUP_FILE_SUFFIX; //$NON-NLS-1$ //$NON-NLS-2$
						currBackupZipFile = new File(dir, fileName + ZIP_EXTENSION);
						if (!currBackupZipFile.exists()) {

							currBackupZipFile.createNewFile();

						}
						zipFileStream = new ZipOutputStream(new FileOutputStream(currBackupZipFile));
						zipFileStream.putNextEntry(new ZipEntry(UiUsageMonitorPlugin.getDefault()
								.getMonitorLogFile()
								.getName()));
						currMonth = monthOfCurrEvent;
						String xml = logger.writeLegacyEvent(event);
						zipFileStream.write(xml.getBytes());
					} else if (monthOfCurrEvent == nowMonth) {
						// if these events are from the current event, just put
						// them back in the current log (first clear the log,
						// since we are putting them all back)

						logger.clearInteractionHistory(false);
						logger.interactionObserved(event);
					}
				}
				zipFileStream.closeEntry();
				zipFileStream.close();
			} catch (IOException e) {
				StatusHandler.fail(new Status(IStatus.ERROR, UiUsageMonitorPlugin.ID_PLUGIN,
						studyParameters.getStudyName() + "Mylyn monitor log rollover failed", e)); //$NON-NLS-1$
			}

		}
		progressMonitor.worked(1);
		logger.startMonitoring();

		generator = new ReportGenerator(UiUsageMonitorPlugin.getDefault().getInteractionLogger(), collectors, this,
				forceSyncForTesting);

		progressMonitor.worked(1);
		final List<File> files = new ArrayList<File>();

		files.add(monitorFile);
		progressMonitor.done();

		Display.getDefault().asyncExec(new Runnable() {

			public void run() {
				// ignore

				if (!forceSyncForTesting) {
					input = new UsageStatsEditorInput(files, generator, studyParameters);
					try {

						IWorkbenchPage page = UiUsageMonitorPlugin.getDefault()
								.getWorkbench()
								.getActiveWorkbenchWindow()
								.getActivePage();

						if (input != null) {
							page.openEditor(input, UsageSummaryReportEditorPart.ID);
						}

					} catch (PartInitException e) {
						StatusHandler.fail(new Status(IStatus.ERROR, UiUsageMonitorPlugin.ID_PLUGIN,
								"Could not show usage summary", e)); //$NON-NLS-1$
					}

				}
			}
		});

		return Status.OK_STATUS;
	}

	public void aboutToRun(IJobChangeEvent event) {
		// ignore

	}

	public void awake(IJobChangeEvent event) {
		// ignore

	}

	public void done(IJobChangeEvent event) {
		PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
			public void run() {
				try {
					final IWorkbenchPage page = UiUsageMonitorPlugin.getDefault()
							.getWorkbench()
							.getActiveWorkbenchWindow()
							.getActivePage();
					if (page == null) {
						return;
					}

					if (input != null) {
						page.openEditor(input, UsageSummaryReportEditorPart.ID);
					}

				} catch (PartInitException e) {
					StatusHandler.fail(new Status(IStatus.ERROR, UiUsageMonitorPlugin.ID_PLUGIN,
							"Could not show usage summary", e)); //$NON-NLS-1$
				}
			}
		});

	}

	public void running(IJobChangeEvent event) {
		// ignore

	}

	public void scheduled(IJobChangeEvent event) {
		// ignore

	}

	public void sleeping(IJobChangeEvent event) {
		// ignore

	}

}

Back to the top