Skip to content

Commit 0299f6f

Browse files
committed
profiler should support http urls as data source #798
1 parent 096a433 commit 0299f6f

File tree

4 files changed

+767
-1
lines changed

4 files changed

+767
-1
lines changed

build-test.xml

+1
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@
110110
<include name="**/*.json"/>
111111
<include name="**/*.xlf"/>
112112
<include name="**/*.xliff"/>
113+
<include name="**/*.html"/>
113114
</patternset>
114115

115116
<copy toDir="@{dest}">

src/fr/adrienbrault/idea/symfony2plugin/profiler/ProfilerUtil.java

+77-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,29 @@
11
package fr.adrienbrault.idea.symfony2plugin.profiler;
22

3+
import com.intellij.lang.html.HTMLLanguage;
34
import com.intellij.openapi.project.Project;
5+
import com.intellij.psi.PsiElement;
6+
import com.intellij.psi.PsiFileFactory;
7+
import com.intellij.psi.impl.source.html.HtmlFileImpl;
8+
import com.intellij.psi.util.PsiTreeUtil;
9+
import com.intellij.psi.xml.XmlTag;
410
import fr.adrienbrault.idea.symfony2plugin.Symfony2ProjectComponent;
11+
import fr.adrienbrault.idea.symfony2plugin.profiler.dict.ProfilerRequest;
12+
import org.apache.commons.lang.StringUtils;
13+
import org.jetbrains.annotations.NotNull;
514
import org.jetbrains.annotations.Nullable;
6-
15+
import java.io.BufferedReader;
716
import java.io.File;
17+
import java.io.IOException;
18+
import java.io.InputStreamReader;
19+
import java.net.URL;
20+
import java.net.URLConnection;
21+
import java.nio.charset.StandardCharsets;
22+
import java.util.ArrayList;
23+
import java.util.Collection;
24+
import java.util.Collections;
25+
import java.util.List;
26+
import java.util.stream.Collectors;
827

928
public class ProfilerUtil {
1029

@@ -24,4 +43,61 @@ public static File findProfilerCsv(Project project) {
2443
return null;
2544
}
2645

46+
@Nullable
47+
public static String getProfilerIndexViaHttp(@NotNull String url) {
48+
URLConnection conn;
49+
try {
50+
conn = new URL("http://127.0.0.1:8000/_profiler/empty/search/results?ip=&limit=10").openConnection();
51+
} catch (IOException e) {
52+
return null;
53+
}
54+
55+
try (BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8))) {
56+
return reader.lines().collect(Collectors.joining("\n"));
57+
} catch (IOException e) {
58+
return null;
59+
}
60+
}
61+
62+
/**
63+
* Extract "table.search-results tbody tr td"
64+
* We dont have complete xpath inside with html support to use internal html parser
65+
*/
66+
@NotNull
67+
public static Collection<ProfilerRequest> createRequestsFromIndexHtml(@NotNull Project project, @NotNull String html) {
68+
HtmlFileImpl htmlFile = (HtmlFileImpl) PsiFileFactory.getInstance(project).createFileFromText(HTMLLanguage.INSTANCE, html);
69+
70+
PsiElement[] results = PsiTreeUtil.collectElements(htmlFile, psiElement ->
71+
psiElement instanceof XmlTag && "table".equals(((XmlTag) psiElement).getName()) && "search-results".equals(((XmlTag) psiElement).getAttributeValue("id"))
72+
);
73+
74+
if(results.length == 0) {
75+
return Collections.emptyList();
76+
}
77+
78+
XmlTag tbody = ((XmlTag) results[0]).findFirstSubTag("tbody");
79+
if(tbody == null) {
80+
return Collections.emptyList();
81+
}
82+
83+
Collection<ProfilerRequest> requests = new ArrayList<>();
84+
for (XmlTag tr : tbody.findSubTags("tr")) {
85+
List<String> values = new ArrayList<>();
86+
for (XmlTag td : tr.findSubTags("td")) {
87+
values.add(StringUtils.trim(stripHtmlTags(td.getText().replaceAll("\\n", " "))).replace("\\s+", " "));
88+
}
89+
90+
requests.add(
91+
new ProfilerRequest(new String[] {values.get(5), "", values.get(2), values.get(3), values.get(4)}, new ProfilerIndex(new File("aa")))
92+
);
93+
}
94+
95+
return requests;
96+
}
97+
98+
@NotNull
99+
private static String stripHtmlTags(@NotNull String text)
100+
{
101+
return text.replaceAll("<[^>]*>", "");
102+
}
27103
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package fr.adrienbrault.idea.symfony2plugin.tests.profiler;
2+
3+
import com.intellij.psi.PsiFile;
4+
import fr.adrienbrault.idea.symfony2plugin.profiler.ProfilerUtil;
5+
import fr.adrienbrault.idea.symfony2plugin.profiler.dict.ProfilerRequest;
6+
import fr.adrienbrault.idea.symfony2plugin.tests.SymfonyLightCodeInsightFixtureTestCase;
7+
8+
import java.io.File;
9+
import java.util.Collection;
10+
11+
/**
12+
* @author Daniel Espendiller <daniel@espendiller.net>
13+
* @see fr.adrienbrault.idea.symfony2plugin.profiler.ProfilerUtil
14+
*/
15+
public class ProfilerUtilTest extends SymfonyLightCodeInsightFixtureTestCase {
16+
public void setUp() throws Exception {
17+
super.setUp();
18+
19+
}
20+
21+
protected String getTestDataPath() {
22+
return new File(this.getClass().getResource("fixtures").getFile()).getAbsolutePath();
23+
}
24+
25+
/**
26+
* @see ProfilerUtil#createRequestsFromIndexHtml
27+
*/
28+
public void testCreateRequestsFromIndexHtml() {
29+
PsiFile psiFile = myFixture.configureByFile("profiler-index.html");
30+
Collection<ProfilerRequest> requests = ProfilerUtil.createRequestsFromIndexHtml(getProject(), psiFile.getText());
31+
32+
ProfilerRequest request = requests.iterator().next();
33+
34+
assertEquals("a9eaab", request.getHash());
35+
assertEquals("GET", request.getMethod());
36+
assertEquals("http://127.0.0.1:8000/_profiler/search/results?ip=&amp;limit=10", request.getUrl());
37+
}
38+
}

0 commit comments

Comments
 (0)