IG: drop telemetry, project component and application component

This commit is contained in:
Bas Leijdekkers
2015-05-10 12:50:07 +02:00
parent ddef5a2f92
commit 204d99dac0
10 changed files with 1 additions and 664 deletions
@@ -2728,15 +2728,4 @@
level="WARNING" implementationClass="com.siyeh.ig.visibility.LambdaParameterHidingMemberVariableInspection"/>
</extensions>
<application-components>
<component>
<implementation-class>com.siyeh.ig.InspectionGadgetsPlugin</implementation-class>
</component>
</application-components>
<project-components>
<component>
<implementation-class>com.siyeh.ig.InspectionGadgetsProjectComponent</implementation-class>
</component>
</project-components>
</idea-plugin>
@@ -1,5 +1,5 @@
/*
* Copyright 2003-2014 Dave Griffith, Bas Leijdekkers
* Copyright 2003-2015 Dave Griffith, Bas Leijdekkers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -18,7 +18,6 @@ package com.siyeh.ig;
import com.intellij.codeInsight.daemon.HighlightDisplayKey;
import com.intellij.codeInspection.BaseJavaBatchLocalInspectionTool;
import com.intellij.codeInspection.InspectionProfileEntry;
import com.intellij.codeInspection.LocalInspectionToolSession;
import com.intellij.codeInspection.ProblemsHolder;
import com.intellij.codeInspection.ex.InspectionProfileImpl;
import com.intellij.openapi.diagnostic.Logger;
@@ -30,7 +29,6 @@ import com.intellij.psi.PsiFile;
import com.intellij.ui.DocumentAdapter;
import com.intellij.util.ReflectionUtil;
import com.intellij.util.ui.UIUtil;
import com.siyeh.ig.telemetry.InspectionGadgetsTelemetry;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
@@ -47,7 +45,6 @@ public abstract class BaseInspection extends BaseJavaBatchLocalInspectionTool {
private static final Logger LOG = Logger.getInstance("#com.siyeh.ig.BaseInspection");
private String m_shortName = null;
private long timestamp = -1L;
@Override
@NotNull
@@ -188,30 +185,6 @@ public abstract class BaseInspection extends BaseJavaBatchLocalInspectionTool {
}
}
@Override
public void inspectionStarted(@NotNull LocalInspectionToolSession session, boolean isOnTheFly) {
super.inspectionStarted(session, isOnTheFly);
if (InspectionGadgetsTelemetry.isEnabled()) {
timestamp = System.currentTimeMillis();
}
}
@Override
public void inspectionFinished(@NotNull LocalInspectionToolSession session,
@NotNull ProblemsHolder problemsHolder) {
super.inspectionFinished(session, problemsHolder);
if (InspectionGadgetsTelemetry.isEnabled()) {
if (timestamp < 0L) {
LOG.warn("finish reported without corresponding start");
return;
}
final long end = System.currentTimeMillis();
final String displayName = getDisplayName();
InspectionGadgetsTelemetry.getInstance().reportRun(displayName, end - timestamp);
timestamp = -1L;
}
}
public static boolean isInspectionEnabled(@NonNls String shortName, PsiElement context) {
final InspectionProjectProfileManager profileManager = InspectionProjectProfileManager.getInstance(context.getProject());
final InspectionProfileImpl profile = (InspectionProfileImpl)profileManager.getInspectionProfile();
@@ -1,74 +0,0 @@
/*
* Copyright 2003-2010 Dave Griffith, Bas Leijdekkers
*
* 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.siyeh.ig.telemetry;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
public class InspectionGadgetsTelemetry {
private static final InspectionGadgetsTelemetry telemetry = new InspectionGadgetsTelemetry();
private static volatile boolean telemetryEnabled = false;
private final ConcurrentMap<String, InspectionRunTime> inspectionRunTimes =
new ConcurrentHashMap<String, InspectionRunTime>();
public List<InspectionRunTime> buildList() {
if (inspectionRunTimes.isEmpty()) {
return Collections.emptyList();
}
return new ArrayList<InspectionRunTime>(inspectionRunTimes.values());
}
public void reportRun(String inspectionID, long runTime) {
InspectionRunTime inspectionRunTime =
inspectionRunTimes.get(inspectionID);
if (inspectionRunTime == null) {
inspectionRunTime = new InspectionRunTime(inspectionID);
final InspectionRunTime oldValue =
inspectionRunTimes.putIfAbsent(inspectionID,
inspectionRunTime);
if (oldValue != null) {
inspectionRunTime = oldValue;
}
}
inspectionRunTime.addRunTime(runTime);
}
public void reset() {
inspectionRunTimes.clear();
}
public static boolean isEnabled() {
return telemetryEnabled;
}
public static void setEnabled(boolean enabled) {
telemetryEnabled = enabled;
if (telemetryEnabled) {
telemetry.reset();
}
}
@NotNull
public static InspectionGadgetsTelemetry getInstance() {
return telemetry;
}
}
@@ -1,57 +0,0 @@
/*
* Copyright 2010-2011 Bas Leijdekkers
*
* 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.siyeh.ig.telemetry;
class InspectionRunTime {
private final String inspectionName;
private long totalRunTime = 0L;
private int runCount = 0;
public InspectionRunTime(String inspectionName) {
this.inspectionName = inspectionName;
}
public void addRunTime(long runTime) {
synchronized (this) {
totalRunTime += runTime;
runCount++;
}
}
public double getAverageRunTime() {
synchronized (this) {
return (double)totalRunTime / (double)runCount;
}
}
public String getInspectionName() {
return inspectionName;
}
public int getRunCount() {
synchronized (this) {
return runCount;
}
}
public long getTotalRunTime() {
synchronized (this) {
return totalRunTime;
}
}
}
@@ -1,77 +0,0 @@
/*
* Copyright 2003-2011 Dave Griffith, Bas Leijdekkers
*
* 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.siyeh.ig;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.components.ApplicationComponent;
import com.intellij.openapi.util.registry.Registry;
import com.intellij.openapi.util.registry.RegistryValue;
import com.intellij.openapi.util.registry.RegistryValueListener;
import com.intellij.util.Consumer;
import com.siyeh.ig.telemetry.InspectionGadgetsTelemetry;
import org.jetbrains.annotations.NotNull;
@SuppressWarnings({"OverlyCoupledClass",
"OverlyCoupledMethod",
"OverlyLongMethod",
"ClassWithTooManyMethods"})
public class InspectionGadgetsPlugin implements ApplicationComponent {
public static boolean getUpToDateTelemetryEnabled(@NotNull final Consumer<Boolean> consumer, @NotNull Disposable disposable) {
final RegistryValue registryValue = Registry.get("inspectionGadgets.telemetry.enabled");
registryValue.addListener(new RegistryValueListener.Adapter() {
@Override
public void afterValueChanged(RegistryValue value) {
consumer.consume(Boolean.valueOf(value.asBoolean()));
}
}, disposable);
return registryValue.asBoolean();
}
public static InspectionGadgetsPlugin getInstance() {
return ApplicationManager.getApplication().getComponent(InspectionGadgetsPlugin.class);
}
@Override
public void disposeComponent() {}
@Override
@NotNull
public String getComponentName() {
return "InspectionGadgets";
}
@NotNull
public InspectionGadgetsTelemetry getTelemetry() {
return InspectionGadgetsTelemetry.getInstance();
}
@Override
public void initComponent() {
boolean telemetryEnabled = getUpToDateTelemetryEnabled(new Consumer<Boolean>() {
@Override
public void consume(Boolean value) {
boolean enabled = value.booleanValue();
InspectionGadgetsTelemetry.setEnabled(enabled);
}
}, ApplicationManager.getApplication());
InspectionGadgetsTelemetry.setEnabled(telemetryEnabled);
}
public boolean isTelemetryEnabled() {
return InspectionGadgetsTelemetry.isEnabled();
}
}
@@ -1,71 +0,0 @@
/*
* Copyright 2003-2010 Dave Griffith, Bas Leijdekkers
*
* 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.siyeh.ig;
import com.intellij.openapi.components.ProjectComponent;
import com.intellij.openapi.project.Project;
import com.intellij.util.Consumer;
import com.siyeh.ig.telemetry.InspectionGadgetsTelemetry;
import com.siyeh.ig.telemetry.TelemetryToolWindow;
import org.jetbrains.annotations.NotNull;
public class InspectionGadgetsProjectComponent implements ProjectComponent {
private final Project project;
public InspectionGadgetsProjectComponent(Project project) {
this.project = project;
}
@Override
public void projectOpened() {
final InspectionGadgetsPlugin inspectionGadgetsPlugin = InspectionGadgetsPlugin.getInstance();
final InspectionGadgetsTelemetry telemetry = inspectionGadgetsPlugin.getTelemetry();
final boolean telemetryEnabled = InspectionGadgetsPlugin.getUpToDateTelemetryEnabled(new Consumer<Boolean>() {
@Override
public void consume(Boolean value) {
final boolean telemetryEnabled = value.booleanValue();
if (telemetryEnabled) {
final TelemetryToolWindow toolWindow = new TelemetryToolWindow(telemetry);
toolWindow.register(project);
}
else {
TelemetryToolWindow.unregister(project);
}
}
}, project);
if (telemetryEnabled) {
final TelemetryToolWindow toolWindow = new TelemetryToolWindow(telemetry);
toolWindow.register(project);
}
}
@Override
public void projectClosed() {
TelemetryToolWindow.unregister(project);
}
@Override
@NotNull
public String getComponentName() {
return "InspectionGadgetsProjectComponent";
}
@Override
public void initComponent() {}
@Override
public void disposeComponent() {}
}
@@ -1,44 +0,0 @@
/*
* Copyright 2003-2010 Dave Griffith, Bas Leijdekkers
*
* 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.siyeh.ig.telemetry;
import com.intellij.CommonBundle;
import com.intellij.icons.AllIcons;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.siyeh.InspectionGadgetsBundle;
class ResetTelemetryAction extends AnAction {
private final InspectionGadgetsTelemetry telemetry;
private final TelemetryDisplay display;
ResetTelemetryAction(InspectionGadgetsTelemetry telemetry,
TelemetryDisplay display) {
super(CommonBundle.message("button.reset"),
InspectionGadgetsBundle.message(
"action.reset.telemetry.description"),
AllIcons.Actions.Reset);
this.telemetry = telemetry;
this.display = display;
}
@Override
public void actionPerformed(AnActionEvent event) {
telemetry.reset();
display.update(telemetry.buildList());
}
}
@@ -1,177 +0,0 @@
/*
* Copyright 2003-2011 Dave Griffith, Bas Leijdekkers
*
* 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.siyeh.ig.telemetry;
import com.intellij.ui.ScrollPaneFactory;
import com.intellij.ui.TableSpeedSearch;
import com.intellij.ui.table.JBTable;
import com.intellij.util.ui.ColumnInfo;
import com.intellij.util.ui.ListTableModel;
import com.siyeh.InspectionGadgetsBundle;
import javax.swing.*;
import java.text.NumberFormat;
import java.util.Comparator;
import java.util.List;
class TelemetryDisplay {
private final JScrollPane scrollPane;
private final ListTableModel<InspectionRunTime> tableModel;
public TelemetryDisplay(InspectionGadgetsTelemetry telemetry) {
tableModel = new ListTableModel<InspectionRunTime>(createColumns(),
telemetry.buildList(), 0);
final JTable table = new JBTable(tableModel);
new TableSpeedSearch(table);
scrollPane = ScrollPaneFactory.createScrollPane(table);
}
private static ColumnInfo[] createColumns() {
final Comparator<InspectionRunTime> nameComparator =
new Comparator<InspectionRunTime>() {
@Override
public int compare(InspectionRunTime runTime1,
InspectionRunTime runTime2) {
return runTime1.getInspectionName().compareToIgnoreCase(
runTime2.getInspectionName());
}
};
final Comparator<InspectionRunTime> runCountComparator =
new Comparator<InspectionRunTime>() {
@Override
public int compare(InspectionRunTime runTime1,
InspectionRunTime runTime2) {
return runTime1.getRunCount() - runTime2.getRunCount();
}
};
final Comparator<InspectionRunTime> totalRunTimeComparator =
new Comparator<InspectionRunTime>() {
@Override
public int compare(InspectionRunTime runTime1,
InspectionRunTime runTime2) {
final long totalRunTime1 = runTime1.getTotalRunTime();
final long totalRunTime2 = runTime2.getTotalRunTime();
if (totalRunTime1 < totalRunTime2) {
return -1;
}
else if (totalRunTime1 > totalRunTime2) {
return 1;
}
else {
return 0;
}
}
};
final Comparator<InspectionRunTime> averageRunTimeComparator =
new Comparator<InspectionRunTime>() {
@Override
public int compare(InspectionRunTime runTime1,
InspectionRunTime runTime2) {
final double averageRunTime1 =
runTime1.getAverageRunTime();
final double averageRunTime2 =
runTime2.getAverageRunTime();
if (averageRunTime1 < averageRunTime2) {
return -1;
}
else if (averageRunTime1 > averageRunTime2) {
return 1;
}
else {
return 0;
}
}
};
return new ColumnInfo[]{
new ColumnInfo<InspectionRunTime, String>(
InspectionGadgetsBundle.message(
"telemetry.table.column.inspection.name")) {
@Override
public String valueOf(InspectionRunTime inspectionRunTime) {
return inspectionRunTime.getInspectionName();
}
@Override
public Comparator<InspectionRunTime> getComparator() {
return nameComparator;
}
},
new ColumnInfo<InspectionRunTime, Integer>(
InspectionGadgetsBundle.message(
"telemetry.table.column.run.count")) {
@Override
public Integer valueOf(
InspectionRunTime inspectionRunTime) {
return Integer.valueOf(inspectionRunTime.getRunCount());
}
@Override
public Comparator<InspectionRunTime> getComparator() {
return runCountComparator;
}
},
new ColumnInfo<InspectionRunTime, Long>(
InspectionGadgetsBundle.message(
"telemetry.table.column.total.time")) {
@Override
public Long valueOf(InspectionRunTime inspectionRunTime) {
return Long.valueOf(
inspectionRunTime.getTotalRunTime());
}
@Override
public Comparator<InspectionRunTime> getComparator() {
return totalRunTimeComparator;
}
},
new ColumnInfo<InspectionRunTime, String>(
InspectionGadgetsBundle.message(
"telemetry.table.column.average.time")) {
private final NumberFormat format =
NumberFormat.getNumberInstance();
{
format.setMaximumFractionDigits(2);
format.setMinimumFractionDigits(2);
}
@Override
public String valueOf(InspectionRunTime inspectionRunTime) {
return format.format(inspectionRunTime.getAverageRunTime());
}
@Override
public Comparator<InspectionRunTime> getComparator() {
return averageRunTimeComparator;
}
}
};
}
public JComponent getContentPane() {
return scrollPane;
}
public void update(List<InspectionRunTime> inspectionRunTimes) {
tableModel.setItems(inspectionRunTimes);
}
}
@@ -1,82 +0,0 @@
/*
* Copyright 2003-2010 Dave Griffith, Bas Leijdekkers
*
* 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.siyeh.ig.telemetry;
import com.intellij.openapi.actionSystem.ActionManager;
import com.intellij.openapi.actionSystem.ActionToolbar;
import com.intellij.openapi.actionSystem.DefaultActionGroup;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.openapi.wm.ToolWindowAnchor;
import com.intellij.openapi.wm.ToolWindowManager;
import com.intellij.ui.JBColor;
import com.intellij.ui.content.Content;
import com.intellij.ui.content.ContentFactory;
import com.intellij.ui.content.ContentManager;
import com.siyeh.InspectionGadgetsBundle;
import org.jetbrains.annotations.NonNls;
import javax.swing.*;
import java.awt.*;
public class TelemetryToolWindow {
@NonNls
private static final String TOOL_WINDOW_ID = "IG Telemetry";
private final JPanel contentPanel;
public TelemetryToolWindow(InspectionGadgetsTelemetry telemetry) {
final TelemetryDisplay telemetryDisplay =
new TelemetryDisplay(telemetry);
final DefaultActionGroup toolbarGroup = new DefaultActionGroup();
toolbarGroup.add(
new UpdateTelemetryViewAction(telemetry, telemetryDisplay));
toolbarGroup.add(new ResetTelemetryAction(telemetry, telemetryDisplay));
final ActionManager actionManager = ActionManager.getInstance();
final ActionToolbar toolbar =
actionManager.createActionToolbar(TOOL_WINDOW_ID,
toolbarGroup, true);
contentPanel = new JPanel(new BorderLayout());
contentPanel.setBackground(JBColor.GRAY);
final JComponent toolbarComponent = toolbar.getComponent();
contentPanel.add(toolbarComponent, BorderLayout.NORTH);
final JComponent displayContentPane = telemetryDisplay.getContentPane();
contentPanel.add(displayContentPane, BorderLayout.CENTER);
}
public void register(Project project) {
final ToolWindowManager toolWindowManager =
ToolWindowManager.getInstance(project);
final ToolWindow toolWindow =
toolWindowManager.registerToolWindow(TOOL_WINDOW_ID, false,
ToolWindowAnchor.LEFT);
toolWindow.setTitle(InspectionGadgetsBundle.message(
"telemetry.toolwindow.title"));
final ContentManager contentManager = toolWindow.getContentManager();
final ContentFactory contentFactory = contentManager.getFactory();
final Content content = contentFactory.createContent(contentPanel,
"", true);
contentManager.addContent(content);
toolWindow.setAvailable(true, null);
}
public static void unregister(Project project) {
final ToolWindowManager toolWindowManager =
ToolWindowManager.getInstance(project);
toolWindowManager.unregisterToolWindow(TOOL_WINDOW_ID);
}
}
@@ -1,43 +0,0 @@
/*
* Copyright 2003-2010 Dave Griffith, Bas Leijdekkers
*
* 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.siyeh.ig.telemetry;
import com.intellij.CommonBundle;
import com.intellij.icons.AllIcons;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.siyeh.InspectionGadgetsBundle;
class UpdateTelemetryViewAction extends AnAction {
private final InspectionGadgetsTelemetry telemetry;
private final TelemetryDisplay telemetryDisplay;
UpdateTelemetryViewAction(InspectionGadgetsTelemetry telemetry,
TelemetryDisplay telemetryDisplay) {
super(CommonBundle.message("action.refresh"),
InspectionGadgetsBundle.message(
"action.telemetry.refresh.description"),
AllIcons.Actions.Refresh);
this.telemetry = telemetry;
this.telemetryDisplay = telemetryDisplay;
}
@Override
public void actionPerformed(AnActionEvent event) {
telemetryDisplay.update(telemetry.buildList());
}
}