summaryrefslogtreecommitdiff
path: root/platform/platform-impl/src/com/intellij/diagnostic/SubmitPerformanceReportAction.java
blob: 88798381f8a846479bb91dad76a9ebbb6832b72b (plain)
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
/*
 * Copyright 2000-2013 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.diagnostic;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.application.ApplicationInfo;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.PathManager;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.progress.Task;
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.util.SystemProperties;
import com.intellij.util.io.ZipUtil;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.zip.ZipOutputStream;

/**
 * @author yole
 */
public class SubmitPerformanceReportAction extends AnAction implements DumbAware {
  private final DateFormat myDateFormat = new SimpleDateFormat("yyyyMMdd-HHmmss");

  private static final String MESSAGE_TITLE = "Submit Performance Report";

  public void actionPerformed(final AnActionEvent e) {
    String reportFileName = "perf_" + ApplicationInfo.getInstance().getBuild().asString() + "_" +
                            SystemProperties.getUserName() + "_" + myDateFormat.format(new Date()) + ".zip";
    final File reportPath = new File(SystemProperties.getUserHome(), reportFileName);
    final File logDir = new File(PathManager.getLogPath());
    final Project project = e.getData(CommonDataKeys.PROJECT);

    final boolean[] archiveCreated = new boolean[1];
    final boolean completed = ProgressManager.getInstance().runProcessWithProgressSynchronously(new Runnable() {
      public void run() {
        try {
          ZipOutputStream zip = new ZipOutputStream(new FileOutputStream(reportPath));
          ZipUtil.addDirToZipRecursively(zip, reportPath, logDir, "", new FileFilter() {
            public boolean accept(final File pathname) {
              ProgressManager.checkCanceled();

              if (logDir.equals(pathname.getParentFile())) {
                return pathname.getPath().contains("threadDumps");
              }
              return true;
            }
          }, null);
          zip.close();
          archiveCreated[0] = true;
        }
        catch (final IOException ex) {
          ApplicationManager.getApplication().invokeLater(new Runnable() {
            public void run() {
              Messages.showErrorDialog(project, "Failed to create performance report archive: " + ex.getMessage(), MESSAGE_TITLE);
            }
          });
        }
      }
    }, "Collecting Performance Report data", true, project);

    if (!completed ||
        !archiveCreated[0]) {
      return;
    }

    int rc = Messages.showYesNoDialog(project, "The performance report has been saved to\n" + reportPath +
                                               "\n\nWould you like to submit it to JetBrains?", MESSAGE_TITLE,
                                      Messages.getQuestionIcon());
    if (rc == Messages.YES) {
      ProgressManager.getInstance().run(new Task.Backgroundable(project, "Uploading Performance Report") {
        public void run(@NotNull final ProgressIndicator indicator) {
          final String error = uploadFileToFTP(reportPath, "ftp.intellij.net", ".uploads", indicator);
          if (error != null) {
            ApplicationManager.getApplication().invokeLater(new Runnable() {
              public void run() {
                Messages.showErrorDialog(error, MESSAGE_TITLE);
              }
            });
          }
        }
      });
    }
  }

  @Nullable
  private static String uploadFileToFTP(final File reportPath, @NonNls final String ftpSite, @NonNls final String directory,
                                        final ProgressIndicator indicator) {
    FTPClient ftp = new FTPClient();
    ftp.setConnectTimeout(30 * 1000);
    try {
      indicator.setText("Connecting to server...");
      ftp.connect(ftpSite);
      indicator.setText("Connected to server");

      if (!ftp.login("anonymous", "anonymous@jetbrains.com")) {
        return "Failed to login";
      }
      indicator.setText("Logged in");

      // After connection attempt, you should check the reply code to verify
      // success.
      int reply = ftp.getReplyCode();

      if (!FTPReply.isPositiveCompletion(reply)) {
        ftp.disconnect();
        return "FTP server refused connection: " + reply;
      }
      if (!ftp.changeWorkingDirectory(directory)) {
        return "Failed to change directory";
      }

      // else won't work behind FW
      ftp.enterLocalPassiveMode();

      if (!ftp.setFileType(FTPClient.BINARY_FILE_TYPE)) {
        return "Failed to switch to binary mode";
      }

      indicator.setText("Transferring (" + StringUtil.formatFileSize(reportPath.length()) + ")");
      FileInputStream readStream = new FileInputStream(reportPath);
      try {
        if (!ftp.storeFile(reportPath.getName(), readStream)) {
          return "Failed to upload file";
        }
      } catch (IOException e) {
        return "Error during transfer: " + e.getMessage();
      }
      finally {
        readStream.close();
      }
      ftp.logout();
      return null;
    }
    catch (IOException e) {
      return "Failed to upload: " + e.getMessage();
    }
    finally {
      if (ftp.isConnected()) {
        try {
          ftp.disconnect();
        }
        catch (IOException ioe) {
          // do nothing
        }
      }
    }
  }
}