OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 import 'dart:async'; | 5 import 'dart:async'; |
6 import 'dart:collection'; | |
7 import 'dart:core'; | 6 import 'dart:core'; |
8 | 7 |
9 import 'package:analysis_server/protocol/protocol.dart'; | 8 import 'package:analysis_server/protocol/protocol.dart'; |
10 import 'package:analysis_server/protocol/protocol_constants.dart'; | 9 import 'package:analysis_server/protocol/protocol_constants.dart'; |
11 import 'package:analysis_server/protocol/protocol_generated.dart'; | 10 import 'package:analysis_server/protocol/protocol_generated.dart'; |
12 import 'package:analysis_server/src/analysis_server.dart'; | 11 import 'package:analysis_server/src/analysis_server.dart'; |
13 import 'package:analyzer/src/context/cache.dart'; | 12 import 'package:analyzer/src/dart/analysis/driver.dart'; |
14 import 'package:analyzer/src/context/context.dart'; | |
15 import 'package:analyzer/src/dart/analysis/driver.dart' as nd; | |
16 import 'package:analyzer/src/generated/engine.dart'; | |
17 import 'package:analyzer/src/generated/source.dart'; | |
18 import 'package:analyzer/src/generated/utilities_collection.dart'; | |
19 import 'package:analyzer/src/task/driver.dart'; | |
20 import 'package:analyzer/task/model.dart'; | |
21 | |
22 int _workItemCount(AnalysisContextImpl context) { | |
23 AnalysisDriver driver = context.driver; | |
24 List<WorkItem> items = driver.currentWorkOrder?.workItems; | |
25 return items?.length ?? 0; | |
26 } | |
27 | 13 |
28 /// Instances of the class [DiagnosticDomainHandler] implement a | 14 /// Instances of the class [DiagnosticDomainHandler] implement a |
29 /// [RequestHandler] that handles requests in the `diagnostic` domain. | 15 /// [RequestHandler] that handles requests in the `diagnostic` domain. |
30 class DiagnosticDomainHandler implements RequestHandler { | 16 class DiagnosticDomainHandler implements RequestHandler { |
31 /// The analysis server that is using this handler to process requests. | 17 /// The analysis server that is using this handler to process requests. |
32 final AnalysisServer server; | 18 final AnalysisServer server; |
33 | 19 |
34 /// Initialize a newly created handler to handle requests for the given | 20 /// Initialize a newly created handler to handle requests for the given |
35 /// [server]. | 21 /// [server]. |
36 DiagnosticDomainHandler(this.server); | 22 DiagnosticDomainHandler(this.server); |
37 | 23 |
38 /// Answer the `diagnostic.getDiagnostics` request. | 24 /// Answer the `diagnostic.getDiagnostics` request. |
39 Response computeDiagnostics(Request request) { | 25 Response computeDiagnostics(Request request) { |
40 List<ContextData> contexts = | 26 List<ContextData> contexts = |
41 server.driverMap.values.map(extractDataFromDriver).toList(); | 27 server.driverMap.values.map(extractDataFromDriver).toList(); |
42 return new DiagnosticGetDiagnosticsResult(contexts).toResponse(request.id); | 28 return new DiagnosticGetDiagnosticsResult(contexts).toResponse(request.id); |
43 } | 29 } |
44 | 30 |
45 /// Extract context data from the given [context]. | |
46 ContextData extractDataFromContext(AnalysisContext context) { | |
47 int explicitFiles = 0; | |
48 int implicitFiles = 0; | |
49 int workItems = 0; | |
50 Set<String> exceptions = new HashSet<String>(); | |
51 if (context is AnalysisContextImpl) { | |
52 workItems = _workItemCount(context); | |
53 var cache = context.analysisCache; | |
54 if (cache is AnalysisCache) { | |
55 Set<AnalysisTarget> countedTargets = new HashSet<AnalysisTarget>(); | |
56 MapIterator<AnalysisTarget, CacheEntry> iterator = cache.iterator(); | |
57 while (iterator.moveNext()) { | |
58 AnalysisTarget target = iterator.key; | |
59 if (countedTargets.add(target)) { | |
60 CacheEntry cacheEntry = iterator.value; | |
61 if (cacheEntry == null) { | |
62 throw new StateError( | |
63 "mutated cache key detected: $target (${target.runtimeType})")
; | |
64 } | |
65 if (target is Source) { | |
66 if (cacheEntry.explicitlyAdded) { | |
67 explicitFiles++; | |
68 } else { | |
69 implicitFiles++; | |
70 } | |
71 } | |
72 // Caught exceptions. | |
73 if (cacheEntry.exception != null) { | |
74 exceptions.add(cacheEntry.exception.toString()); | |
75 } | |
76 } | |
77 } | |
78 } | |
79 } | |
80 return new ContextData(context.name, explicitFiles, implicitFiles, | |
81 workItems, exceptions.toList()); | |
82 } | |
83 | |
84 /// Extract context data from the given [driver]. | 31 /// Extract context data from the given [driver]. |
85 ContextData extractDataFromDriver(nd.AnalysisDriver driver) { | 32 ContextData extractDataFromDriver(AnalysisDriver driver) { |
86 int explicitFileCount = driver.addedFiles.length; | 33 int explicitFileCount = driver.addedFiles.length; |
87 int knownFileCount = driver.knownFiles.length; | 34 int knownFileCount = driver.knownFiles.length; |
88 return new ContextData(driver.name, explicitFileCount, | 35 return new ContextData(driver.name, explicitFileCount, |
89 knownFileCount - explicitFileCount, driver.numberOfFilesToAnalyze, []); | 36 knownFileCount - explicitFileCount, driver.numberOfFilesToAnalyze, []); |
90 } | 37 } |
91 | 38 |
92 /// Answer the `diagnostic.getServerPort` request. | 39 /// Answer the `diagnostic.getServerPort` request. |
93 Future handleGetServerPort(Request request) async { | 40 Future handleGetServerPort(Request request) async { |
94 try { | 41 try { |
95 // Open a port (or return the existing one). | 42 // Open a port (or return the existing one). |
(...skipping 23 matching lines...) Expand all Loading... |
119 } | 66 } |
120 } | 67 } |
121 | 68 |
122 class MemoryCpuSample { | 69 class MemoryCpuSample { |
123 final DateTime time; | 70 final DateTime time; |
124 final double cpuPercentage; | 71 final double cpuPercentage; |
125 final int memoryKB; | 72 final int memoryKB; |
126 | 73 |
127 MemoryCpuSample(this.time, this.cpuPercentage, this.memoryKB); | 74 MemoryCpuSample(this.time, this.cpuPercentage, this.memoryKB); |
128 } | 75 } |
OLD | NEW |