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

View File

@ -2,7 +2,7 @@ Heap = require('heap')
Url = require('url')
module.exports = class WebRequestMonitor
constructor: ->
constructor: (@getSummaryId) ->
@_requests = {}
@_recentRequests = new Heap((a, b) -> a._startTime - b._startTime)
@_callbacks = []
@ -144,6 +144,8 @@ module.exports = class WebRequestMonitor
ongoingCount: 0
errorCount: 0
doneCount: 0
summary: {}
}
setTabRequestInfo: (status, req) ->
@ -160,28 +162,16 @@ module.exports = class WebRequestMonitor
info.requestCount++
info.requestStatus[req.requestId] = status
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
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) ->
conditions = Object.keys(domains).map (domain) -> {
conditionType: 'HostWildcardCondition'
pattern: '*.' + domain
pattern: domain
}
omegaTarget.addCondition(conditions, profileName).then ->
omegaTarget.state('lastProfileNameForCondition', profileName)
@ -237,6 +237,11 @@ module.controller 'PopupCtrl', ($scope, $window, $q, omegaTarget,
$scope.domainsForCondition = {}
$scope.requestInfoProvided = null
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.requestInfo = info
$scope.requestInfoProvided ?= (info?.domains.length > 0)
@ -272,5 +277,3 @@ module.controller 'PopupCtrl', ($scope, $window, $q, omegaTarget,
profileName: preselectedProfileNameForCondition
$scope.$watch 'rule.condition.conditionType', (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-submit='addConditionForDomains(domainsForCondition, profileForDomains)')
fieldset
legend
legend(ng-show='!!currentProfileCanAddRule')
| {{'popup_addConditionTo' | tr}}
= ' '
span.profile-inline
span(omega-profile-inline='currentProfile' options='availableProfiles' disp-name='dispNameFilter')
legend(ng-show='!currentProfileCanAddRule')
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')
label
input(type='checkbox' ng-model='domainsForCondition[domain.domain]')
span.label.label-warning {{domain.errorCount}}
=' *.{{domain.domain}}'
div.form-group
=' {{domain.domain}}'
div.form-group(ng-show='!!currentProfileCanAddRule')
label {{'options_resultProfileForSelectedDomains' | tr}}
div(omega-profile-select='validResultProfiles' ng-model='profileForDomains'
disp-name='dispNameFilter' options='availableProfiles')
div.condition-controls
button.btn.btn-default(type='button' ng-click='showRequestInfo = false')
| {{'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='lib/FileSaver/FileSaver.js')