package com.jetbrains.python.statistics;

import com.intellij.internal.statistic.AbstractApplicationUsagesCollector;
import com.intellij.internal.statistic.CollectUsagesException;
import com.intellij.internal.statistic.beans.GroupDescriptor;
import com.intellij.internal.statistic.beans.UsageDescriptor;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.projectRoots.Sdk;
import com.jetbrains.python.packaging.PyPIPackageUtil;
import com.jetbrains.python.packaging.PyPackageManagerImpl;
import com.jetbrains.python.packaging.PyRequirement;
import com.jetbrains.python.sdk.PythonSdkType;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.*;

/**
 * @author yole
 */
public class PyPackageUsagesCollector extends AbstractApplicationUsagesCollector {
  private static final String GROUP_ID = "py-packages";

  @NotNull
  @Override
  public Set<UsageDescriptor> getProjectUsages(@NotNull Project project) throws CollectUsagesException {
    Set<UsageDescriptor> result = new HashSet<UsageDescriptor>();
    for(Module m: ModuleManager.getInstance(project).getModules()) {
      Sdk pythonSdk = PythonSdkType.findPythonSdk(m);
      if (pythonSdk != null) {
        List<PyRequirement> requirements = PyPackageManagerImpl.getRequirements(m);
        if (requirements != null) {
          Collection<String> packages;
          try {
            packages = new HashSet<String>(PyPIPackageUtil.INSTANCE.getPackageNames());
          }
          catch (IOException e) {
            continue;
          }
          for (PyRequirement requirement : requirements) {
            String name = requirement.getName();
            if (packages.contains(name)) {
              result.add(new UsageDescriptor(name, 1));
            }
          }
        }
      }
    }
    return result;
  }

  @NotNull
  @Override
  public GroupDescriptor getGroupId() {
    return GroupDescriptor.create(GROUP_ID);
  }
}

@@ -289,32 +289,92 @@
 sageDescriptor;%0A
+import com.intellij.openapi.application.ApplicationManager;%0A
 import com.intel
@@ -1144,16 +1144,22 @@
 n %7B%0A    
+final 
 Set%3CUsag
@@ -1220,16 +1220,22 @@
     for(
+final 
 Module m
@@ -1364,32 +1364,161 @@
 nSdk != null) %7B%0A
+        ApplicationManager.getApplication().runReadAction(new Runnable() %7B%0A          @Override%0A          public void run() %7B%0A    
         List%3CPyR
@@ -1589,24 +1589,28 @@
 m);%0A        
+    
 if (requirem
@@ -1635,16 +1635,20 @@
         
+ 
+   
 Collecti
@@ -1682,14 +1682,22 @@
     
+    
 try %7B%0A
+    
     
@@ -1776,24 +1776,28 @@
 geNames());%0A
+    
           %7D%0A
@@ -1806,16 +1806,20 @@
         
+    
 catch (I
@@ -1850,18 +1850,22 @@
     
-continue;%0A
+    return;%0A  
     
@@ -1862,34 +1862,40 @@
 rn;%0A            
-%7D%0A
+  %7D%0A    
           for (P
@@ -1949,16 +1949,20 @@
         
+   
+ 
 String n
@@ -1998,24 +1998,28 @@
             
+    
 if (packages
@@ -2051,16 +2051,20 @@
         
+  
+  
 result.a
@@ -2093,24 +2093,58 @@
 (name, 1));%0A
+                %7D%0A              %7D%0A
             
@@ -2162,24 +2162,26 @@
  %7D%0A        %7D
+);
 %0A      %7D%0A   

read action when collecting package usages (PY-8723)