|
| 1 | +package fr.adrienbrault.idea.symfony2plugin.profiler; |
| 2 | + |
| 3 | +import com.intellij.util.containers.ContainerUtil; |
| 4 | +import fr.adrienbrault.idea.symfony2plugin.profiler.collector.LocalDefaultDataCollector; |
| 5 | +import fr.adrienbrault.idea.symfony2plugin.profiler.collector.LocalMailCollector; |
| 6 | +import fr.adrienbrault.idea.symfony2plugin.profiler.dict.LocalProfilerRequest; |
| 7 | +import fr.adrienbrault.idea.symfony2plugin.profiler.dict.ProfilerRequestInterface; |
| 8 | +import fr.adrienbrault.idea.symfony2plugin.profiler.reader.ReverseFileLineReader; |
| 9 | +import fr.adrienbrault.idea.symfony2plugin.profiler.utils.ProfilerUtil; |
| 10 | +import org.apache.commons.lang.StringUtils; |
| 11 | +import org.jetbrains.annotations.NotNull; |
| 12 | +import org.jetbrains.annotations.Nullable; |
| 13 | + |
| 14 | +import java.io.BufferedReader; |
| 15 | +import java.io.File; |
| 16 | +import java.io.FileReader; |
| 17 | +import java.io.IOException; |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.Collection; |
| 20 | +import java.util.List; |
| 21 | +import java.util.concurrent.Callable; |
| 22 | + |
| 23 | +/** |
| 24 | + * @author Daniel Espendiller <daniel@espendiller.net> |
| 25 | + */ |
| 26 | +public class LocalProfilerIndex implements ProfilerIndexInterface { |
| 27 | + @NotNull |
| 28 | + private File file; |
| 29 | + |
| 30 | + @Nullable |
| 31 | + private String baseUrl; |
| 32 | + |
| 33 | + public LocalProfilerIndex(@NotNull File file) { |
| 34 | + this.file = file; |
| 35 | + } |
| 36 | + |
| 37 | + public LocalProfilerIndex(@NotNull File file, @Nullable String baseUrl) { |
| 38 | + this.file = file; |
| 39 | + this.baseUrl = baseUrl; |
| 40 | + } |
| 41 | + |
| 42 | + @NotNull |
| 43 | + public List<ProfilerRequestInterface> getRequests() { |
| 44 | + List<String> lines = new ArrayList<>(); |
| 45 | + |
| 46 | + try { |
| 47 | + // empty line and end of line need +1 |
| 48 | + ContainerUtil.addAll(lines, new ReverseFileLineReader(this.file, "UTF-8", 11).readLines()); |
| 49 | + } catch (IOException ignored) { |
| 50 | + } |
| 51 | + |
| 52 | + Collection<Callable<ProfilerRequestInterface>> callable = new ArrayList<>(); |
| 53 | + |
| 54 | + // build thread callable collection |
| 55 | + lines.stream().filter(StringUtils::isNotBlank).forEachOrdered(line -> { |
| 56 | + String[] split = line.split(","); |
| 57 | + if (split.length < 6) { |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + callable.add(new MyProfilerRequestBuilderCallable(split)); |
| 62 | + }); |
| 63 | + |
| 64 | + return ProfilerUtil.getProfilerRequestCollectorDecorated(callable, 15); |
| 65 | + } |
| 66 | + |
| 67 | + @Nullable |
| 68 | + @Override |
| 69 | + public String getUrlForRequest(@NotNull ProfilerRequestInterface request) { |
| 70 | + if(this.baseUrl != null) { |
| 71 | + return this.baseUrl + "/" + StringUtils.stripStart(request.getProfilerUrl(), "/"); |
| 72 | + } |
| 73 | + |
| 74 | + return ProfilerUtil.getBaseProfilerUrlFromRequest(request.getProfilerUrl()); |
| 75 | + } |
| 76 | + |
| 77 | + @NotNull |
| 78 | + private String getPath(@NotNull String hash) { |
| 79 | + String[] hashSplit = hash.split("(?<=\\G.{2})"); |
| 80 | + return hashSplit[2] + "/" + hashSplit[1] + "/" + hash; |
| 81 | + } |
| 82 | + |
| 83 | + @Nullable |
| 84 | + private File getFile(@NotNull String hash) { |
| 85 | + String path = this.getPath(hash); |
| 86 | + |
| 87 | + File file = new File(this.file.getParentFile().getAbsolutePath() + "/" + path); |
| 88 | + if(!file.exists()) { |
| 89 | + return null; |
| 90 | + } |
| 91 | + |
| 92 | + return file; |
| 93 | + } |
| 94 | + |
| 95 | + @Nullable |
| 96 | + private String getContentForHash(@NotNull String hash) { |
| 97 | + File file = this.getFile(hash); |
| 98 | + if(file == null) { |
| 99 | + return null; |
| 100 | + } |
| 101 | + |
| 102 | + StringBuilder content = new StringBuilder(); |
| 103 | + |
| 104 | + try { |
| 105 | + BufferedReader in = new BufferedReader(new FileReader(file)); |
| 106 | + String str; |
| 107 | + while ((str = in.readLine()) != null) { |
| 108 | + content.append(str); |
| 109 | + } |
| 110 | + in.close(); |
| 111 | + } catch (IOException ignored) { |
| 112 | + } |
| 113 | + |
| 114 | + return content.toString(); |
| 115 | + } |
| 116 | + |
| 117 | + private class MyProfilerRequestBuilderCallable implements Callable<ProfilerRequestInterface> { |
| 118 | + private final String[] split; |
| 119 | + |
| 120 | + MyProfilerRequestBuilderCallable(String[] split) { |
| 121 | + this.split = split; |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public ProfilerRequestInterface call() throws Exception { |
| 126 | + String content = getContentForHash(split[0]); |
| 127 | + if(content == null) { |
| 128 | + return new LocalProfilerRequest(split); |
| 129 | + } |
| 130 | + |
| 131 | + return new LocalProfilerRequest( |
| 132 | + split, |
| 133 | + new LocalDefaultDataCollector(content), |
| 134 | + new LocalMailCollector(content) |
| 135 | + ); |
| 136 | + } |
| 137 | + } |
| 138 | +} |
0 commit comments