Refactor WebRequestMonitor#summarizeError, improving efficiency.

This commit is contained in:
FelisCatus 2015-02-27 21:09:07 +08:00
parent 0ae801e716
commit 61856d5eae
4 changed files with 36 additions and 37 deletions

View File

@ -196,7 +196,8 @@ class ChromeOptions extends OmegaTarget.Options
@_monitorWebRequests = enabled @_monitorWebRequests = enabled
if enabled and not @_requestMonitor? if enabled and not @_requestMonitor?
@_tabRequestInfoPorts = {} @_tabRequestInfoPorts = {}
@_requestMonitor = new WebRequestMonitor() wildcardForReq = (req) -> OmegaPac.wildcardForUrl(req.url)
@_requestMonitor = new WebRequestMonitor(wildcardForReq)
@_requestMonitor.watchTabs (tabId, info) => @_requestMonitor.watchTabs (tabId, info) =>
return unless @_monitorWebRequests return unless @_monitorWebRequests
if info.errorCount > 0 if info.errorCount > 0
@ -210,8 +211,10 @@ class ChromeOptions extends OmegaTarget.Options
else if info.badgeSet else if info.badgeSet
info.badgeSet = false info.badgeSet = false
chrome.browserAction.setBadgeText(text: '', tabId: tabId) chrome.browserAction.setBadgeText(text: '', tabId: tabId)
@_tabRequestInfoPorts[tabId]?.postMessage( @_tabRequestInfoPorts[tabId]?.postMessage({
@_requestMonitor.summarizeErrors(info, OmegaPac.getBaseDomain)) errorCount: info.errorCount
summary: info.summary
})
chrome.runtime.onConnect.addListener (port) => chrome.runtime.onConnect.addListener (port) =>
return unless port.name == 'tabRequestInfo' return unless port.name == 'tabRequestInfo'
@ -222,8 +225,10 @@ class ChromeOptions extends OmegaTarget.Options
@_tabRequestInfoPorts[tabId] = port @_tabRequestInfoPorts[tabId] = port
info = @_requestMonitor.tabInfo[tabId] info = @_requestMonitor.tabInfo[tabId]
if info if info
summ = @_requestMonitor.summarizeErrors info, OmegaPac.getBaseDomain port.postMessage({
port.postMessage(summ) errorCount: info.errorCount
summary: info.summary
})
port.onDisconnect.addListener => port.onDisconnect.addListener =>
delete @_tabRequestInfoPorts[tabId] if tabId? delete @_tabRequestInfoPorts[tabId] if tabId?

View File

@ -2,7 +2,7 @@ Heap = require('heap')
Url = require('url') Url = require('url')
module.exports = class WebRequestMonitor module.exports = class WebRequestMonitor
constructor: -> constructor: (@getSummaryId) ->
@_requests = {} @_requests = {}
@_recentRequests = new Heap((a, b) -> a._startTime - b._startTime) @_recentRequests = new Heap((a, b) -> a._startTime - b._startTime)
@_callbacks = [] @_callbacks = []
@ -144,6 +144,8 @@ module.exports = class WebRequestMonitor
ongoingCount: 0 ongoingCount: 0
errorCount: 0 errorCount: 0
doneCount: 0 doneCount: 0
summary: {}
} }
setTabRequestInfo: (status, req) -> setTabRequestInfo: (status, req) ->
@ -160,28 +162,16 @@ module.exports = class WebRequestMonitor
info.requestCount++ info.requestCount++
info.requestStatus[req.requestId] = status info.requestStatus[req.requestId] = status
info[@eventCategory[status] + 'Count']++ info[@eventCategory[status] + 'Count']++
id = @getSummaryId?(req)
if id?
if @eventCategory[status] == 'error'
if @eventCategory[oldStatus] != 'error'
summaryItem = info.summary[id]
if not summaryItem?
summaryItem = info.summary[id] = {errorCount: 0}
summaryItem.errorCount++
else if @eventCategory[oldStatus] == 'error'
summaryItem = info.summary[id]
summaryItem.errorCount-- if summaryItem?
for callback in @_tabCallbacks for callback in @_tabCallbacks
callback(req.tabId, info, req, status) callback(req.tabId, info, req, status)
summarizeErrors: (info, domainOfHost) ->
domains = []
domainInfoByName = {}
for reqId, req of info.requests
if @eventCategory[info.requestStatus[reqId]] == 'error'
domain = Url.parse(req.url).hostname
domain = domainOfHost(domain) if domainOfHost
domainInfo = domainInfoByName[domain]
if not domainInfo
domainInfo = domainInfoByName[domain] = {
domain: domain
errorCount: 0
type: 'other'
}
domains.push(domainInfo)
domainInfo.errorCount++
domainInfo.type = req.type
domains.sort (a, b) -> b.errorCount - a.errorCount
return {
errorCount: info.errorCount
domains: domains
}

View File

@ -168,7 +168,7 @@ module.controller 'PopupCtrl', ($scope, $window, $q, omegaTarget,
$scope.addConditionForDomains = (domains, profileName) -> $scope.addConditionForDomains = (domains, profileName) ->
conditions = Object.keys(domains).map (domain) -> { conditions = Object.keys(domains).map (domain) -> {
conditionType: 'HostWildcardCondition' conditionType: 'HostWildcardCondition'
pattern: '*.' + domain pattern: domain
} }
omegaTarget.addCondition(conditions, profileName).then -> omegaTarget.addCondition(conditions, profileName).then ->
omegaTarget.state('lastProfileNameForCondition', profileName) omegaTarget.state('lastProfileNameForCondition', profileName)
@ -237,6 +237,11 @@ module.controller 'PopupCtrl', ($scope, $window, $q, omegaTarget,
$scope.domainsForCondition = {} $scope.domainsForCondition = {}
$scope.requestInfoProvided = null $scope.requestInfoProvided = null
omegaTarget.setRequestInfoCallback (info) -> omegaTarget.setRequestInfoCallback (info) ->
info.domains = []
for own domain, domainInfo of info.summary
domainInfo.domain = domain
info.domains.push(domainInfo)
info.domains.sort (a, b) -> b.errorCount - a.errorCount
$scope.$apply -> $scope.$apply ->
$scope.requestInfo = info $scope.requestInfo = info
$scope.requestInfoProvided ?= (info?.domains.length > 0) $scope.requestInfoProvided ?= (info?.domains.length > 0)
@ -272,5 +277,3 @@ module.controller 'PopupCtrl', ($scope, $window, $q, omegaTarget,
profileName: preselectedProfileNameForCondition profileName: preselectedProfileNameForCondition
$scope.$watch 'rule.condition.conditionType', (type) -> $scope.$watch 'rule.condition.conditionType', (type) ->
$scope.rule.condition.pattern = conditionSuggestion[type] $scope.rule.condition.pattern = conditionSuggestion[type]
else
$scope.requestInfoProvided = false

View File

@ -136,26 +136,27 @@ html(lang='en' ng-app='omegaPopup' ng-controller='PopupCtrl' ng-csp)
ng-style='{display: showRequestInfo ? "block" : "none"}' ng-style='{display: showRequestInfo ? "block" : "none"}'
ng-submit='addConditionForDomains(domainsForCondition, profileForDomains)') ng-submit='addConditionForDomains(domainsForCondition, profileForDomains)')
fieldset fieldset
legend legend(ng-show='!!currentProfileCanAddRule')
| {{'popup_addConditionTo' | tr}} | {{'popup_addConditionTo' | tr}}
= ' ' = ' '
span.profile-inline span.profile-inline
span(omega-profile-inline='currentProfile' options='availableProfiles' disp-name='dispNameFilter') span(omega-profile-inline='currentProfile' options='availableProfiles' disp-name='dispNameFilter')
legend(ng-show='!currentProfileCanAddRule')
p.text-warning {{'popup_requestErrorWarning' | tr}} p.text-warning {{'popup_requestErrorWarning' | tr}}
p.help-block {{'popup_requestErrorAddCondition' | tr}} p.help-block(ng-show='!!currentProfileCanAddRule') {{'popup_requestErrorAddCondition' | tr}}
.checkbox(ng-repeat='domain in requestInfo.domains') .checkbox(ng-repeat='domain in requestInfo.domains')
label label
input(type='checkbox' ng-model='domainsForCondition[domain.domain]') input(type='checkbox' ng-model='domainsForCondition[domain.domain]')
span.label.label-warning {{domain.errorCount}} span.label.label-warning {{domain.errorCount}}
=' *.{{domain.domain}}' =' {{domain.domain}}'
div.form-group div.form-group(ng-show='!!currentProfileCanAddRule')
label {{'options_resultProfileForSelectedDomains' | tr}} label {{'options_resultProfileForSelectedDomains' | tr}}
div(omega-profile-select='validResultProfiles' ng-model='profileForDomains' div(omega-profile-select='validResultProfiles' ng-model='profileForDomains'
disp-name='dispNameFilter' options='availableProfiles') disp-name='dispNameFilter' options='availableProfiles')
div.condition-controls div.condition-controls
button.btn.btn-default(type='button' ng-click='showRequestInfo = false') button.btn.btn-default(type='button' ng-click='showRequestInfo = false')
| {{'dialog_cancel' | tr}} | {{'dialog_cancel' | tr}}
button.btn.btn-primary(type='submit') {{'popup_addCondition' | tr}} button.btn.btn-primary(type='submit' ng-show='!!currentProfileCanAddRule') {{'popup_addCondition' | tr}}
script(src='js/log_error.js') script(src='js/log_error.js')
script(src='lib/FileSaver/FileSaver.js') script(src='lib/FileSaver/FileSaver.js')