public async Task <ActionResult> References(string projectId, string symbolId, string projectScope = null) { try { Requests.LogRequest(this); var definitionResult = await((IStorage)Storage).GetDefinitionsAsync(this.GetSearchRepos(), projectId, symbolId); var definitionSpan = definitionResult?.FirstOrDefault()?.Span; var definition = definitionSpan?.Definition; var symbolName = definition?.ShortName ?? symbolId; Responses.PrepareResponse(Response); var referencesResult = await Storage.GetReferencesToSymbolAsync( this.GetSearchRepos(), new Symbol() { ProjectId = projectId, Id = SymbolId.UnsafeCreateWithValue(symbolId), }.SetProjectScope(projectScope)); if (referencesResult.Entries.Count != 0) { if (definition != null) { if (projectScope == null) { var relatedDefinitions = await Storage.GetRelatedDefinitions(this.GetSearchRepos(), definition.Id.Value, definition.ProjectId); referencesResult.RelatedDefinitions = relatedDefinitions; } else { var definitionReferences = await Storage.GetReferencesToSymbolAsync( this.GetSearchRepos(), new Symbol() { ProjectId = projectId, Id = SymbolId.UnsafeCreateWithValue(symbolId), Kind = nameof(ReferenceKind.Definition) }); referencesResult.Entries.InsertRange(0, definitionReferences.Entries); } } referencesResult.SymbolName = symbolName; return(PartialView((object)GenerateReferencesHtml(referencesResult))); } return(PartialView((object)$"No references to project {projectId} and symbol {symbolId} found.")); } catch (Exception ex) { return(Responses.Exception(ex)); } }
private static void ComputeMissingProjects(ElasticsearchStorage storage) { storage.UpdateProjectsAsync(force: true).GetAwaiter().GetResult(); var projectsResponse = storage.Provider.GetProjectsAsync(projectKind: nameof(ProjectKind.Source)).GetAwaiter().GetResult(); var projectMap = storage.Projects; var projects = projectsResponse.Result; HashSet <string> processedReferences = new HashSet <string>(); HashSet <string> hasProjectInfoProjects = new HashSet <string>(StringComparer.OrdinalIgnoreCase); HashSet <string> missingProjects = new HashSet <string>(StringComparer.OrdinalIgnoreCase); int i = 0; foreach (var project in projects) { Console.WriteLine($"Processing {project.Id} ({i++}/{projects.Count})"); foreach (var referencedProject in project.ReferencedProjects) { if (!processedReferences.Add(referencedProject.ProjectId)) { continue; } if (!projectMap.ContainsKey(referencedProject.ProjectId) && !referencedProject.ProjectId.EndsWith(".Fakes", StringComparison.OrdinalIgnoreCase)) { if (missingProjects.Add(referencedProject.ProjectId)) { Console.WriteLine("Missing: " + referencedProject.ProjectId); } } var references = storage.GetReferencesToSymbolAsync(new Symbol() { ProjectId = referencedProject.ProjectId, Kind = nameof(ReferenceKind.Definition) }, 1).GetAwaiter().GetResult(); if (references.Total == 0 && !referencedProject.ProjectId.EndsWith(".Fakes", StringComparison.OrdinalIgnoreCase)) { if (missingProjects.Add(referencedProject.ProjectId)) { Console.WriteLine("Missing total: " + referencedProject.ProjectId); } } } } File.WriteAllLines("AllProjects.txt", projects.OrderBy(p => p.Id, StringComparer.OrdinalIgnoreCase).Select( p => $"{p.Id} (Has Definitions: {!missingProjects.Contains(p.Id)})")); File.WriteAllLines("MissingProjects.txt", missingProjects.OrderBy(p => p, StringComparer.OrdinalIgnoreCase)); return; }
static void Search() { ElasticsearchStorage storage = new ElasticsearchStorage(elasticSearchServer); string[] repos = new string[] { repoName.ToLowerInvariant() }; string line = null; Console.WriteLine("Please enter symbol short name: "); while ((line = Console.ReadLine()) != null) { if (line.Contains("`")) { //results = storage.SearchAsync(repos, line, classification: null).Result; var results = ((ElasticsearchStorage)storage).TextSearchAsync(repos, line.TrimStart('`')).Result; Console.WriteLine($"Found {results.Count} matches"); foreach (var result in results) { Console.WriteLine($"{result.File} ({result.Span.LineNumber}, {result.Span.LineSpanStart})"); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine($"{result.ReferringFilePath} in '{result.ReferringProjectId}'"); Console.ForegroundColor = ConsoleColor.Gray; var bsf = storage.GetBoundSourceFileAsync(result.ReferringProjectId, result.ReferringFilePath).Result; if (!string.IsNullOrEmpty(result.Span.LineSpanText)) { Console.Write(result.Span.LineSpanText.Substring(0, result.Span.LineSpanStart)); Console.ForegroundColor = ConsoleColor.Yellow; Console.Write(result.Span.LineSpanText.Substring(result.Span.LineSpanStart, result.Span.Length)); Console.ForegroundColor = ConsoleColor.Gray; Console.WriteLine(result.Span.LineSpanText.Substring(result.Span.LineSpanStart + result.Span.Length)); } } } else if (line.Contains("|")) { var parts = line.Split('|'); var symbolId = SymbolId.UnsafeCreateWithValue(parts[0]); var projectId = parts[1]; var results = storage.GetReferencesToSymbolAsync(repos, new Symbol() { Id = symbolId, ProjectId = projectId }).GetAwaiter().GetResult(); var relatedDefinitions = storage.GetRelatedDefinitions(repos, symbolId.Value, projectId).GetAwaiter().GetResult(); var definition = results.Entries .Where(e => e.Span.Reference.ReferenceKind == nameof(ReferenceKind.Definition)) .Select(e => e.Span.Reference) .FirstOrDefault(); if (definition != null) { var relatedDefs = storage.Provider.GetRelatedDefinitions(repos, definition.Id.Value, definition.ProjectId) .GetAwaiter().GetResult(); } Console.WriteLine($"Found {results.Total} matches"); foreach (var result in results.Entries) { Console.WriteLine($"{result.File} ({result.Span.LineNumber}, {result.Span.LineSpanStart})"); if (!string.IsNullOrEmpty(result.Span.LineSpanText)) { Console.Write(result.Span.LineSpanText.Substring(0, result.Span.LineSpanStart)); Console.ForegroundColor = ConsoleColor.Yellow; Console.Write(result.Span.LineSpanText.Substring(result.Span.LineSpanStart, result.Span.Length)); Console.ForegroundColor = ConsoleColor.Gray; Console.WriteLine(result.Span.LineSpanText.Substring(result.Span.LineSpanStart + result.Span.Length)); } } if (results.Entries.Count != 0) { var result = results.Entries[0]; Console.WriteLine($"Retrieving source file {result.ReferringFilePath} in {result.ReferringProjectId}"); var stopwatch = Stopwatch.StartNew(); var sourceFile = storage.GetBoundSourceFileAsync(repos, result.ReferringProjectId, result.ReferringFilePath).GetAwaiter().GetResult(); var elapsed = stopwatch.Elapsed; Console.WriteLine($"Retrieved source file in {elapsed.TotalMilliseconds} ms"); Console.WriteLine($"Source file has { sourceFile?.ClassificationSpans.Count ?? -1 } classifications"); if (sourceFile.ClassificationSpans != null) { ConcurrentDictionary <string, int> classificationCounters = new ConcurrentDictionary <string, int>(); foreach (var cs in sourceFile.ClassificationSpans) { classificationCounters.AddOrUpdate(cs.Classification, 1, (k, v) => v + 1); } foreach (var counter in classificationCounters) { Console.WriteLine($"Source file has {counter.Value} {counter.Key} classifications"); } } Console.WriteLine($"Source file has { sourceFile?.References.Count ?? -1 } references"); Console.WriteLine($"Source file has { sourceFile?.Definitions.Count ?? -1 } definitions"); } } else { //results = storage.SearchAsync(repos, line, classification: null).Result; var results = storage.SearchAsync(repos, line, null).Result; Console.WriteLine($"Found {results.Total} matches"); foreach (var result in results.Entries) { Console.WriteLine($"{result.File} ({result.Span.LineNumber}, {result.Span.LineSpanStart})"); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine($"{result.Symbol.Id}|{result.Symbol.ProjectId}"); Console.ForegroundColor = ConsoleColor.Gray; var symbol = result.Symbol; int index = result.DisplayName.IndexOf(symbol.ShortName); if (index >= 0) { result.Span.LineSpanText = symbol.DisplayName; result.Span.LineSpanStart = index; result.Span.Length = symbol.ShortName.Length; } if (!string.IsNullOrEmpty(result.Span.LineSpanText)) { Console.Write(result.Span.LineSpanText.Substring(0, result.Span.LineSpanStart)); Console.ForegroundColor = ConsoleColor.Yellow; Console.Write(result.Span.LineSpanText.Substring(result.Span.LineSpanStart, result.Span.Length)); Console.ForegroundColor = ConsoleColor.Gray; Console.WriteLine(result.Span.LineSpanText.Substring(result.Span.LineSpanStart + result.Span.Length)); } } } } }