tests_from_entry()   F
last analyzed

Complexity

Conditions 11

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
dl 0
loc 35
rs 3.1764
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
F test_from_entry() 0 24 10

How to fix   Complexity   

Complexity

Complex classes like tests_from_entry() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
from fundraising_ab_tests.fundraising_experiment import FrTest
2
from process.globals import get_config
3
4
5
def tests_from_entry(entry):
6
    '''
7
    Returns a tuple, (test ended, test begun)
8
9
    Start and end times are fudged quite a bit, all we care about
10
    for now is defining broad enough criteria to capture most of
11
    the related contributions.
12
    '''
13
    def test_from_entry(edge):
14
        if (edge == 'begin' and 'enabled' in entry['added'] and int(entry['added']['enabled'])) or (edge == 'end' and 'enabled' in entry['removed'] and int(entry['removed']['enabled'])):
15
            # irrelevant
16
            return False
17
18
        # FIXME: assuming this is a banner test
19
        banners = entry[edge]['banners']
20
        if hasattr(banners, 'keys'):
21
            banners = banners.keys()
22
23
        start = entry[edge]['start']
24
        if edge == 'end':
25
            start = entry['timestamp']
26
27
        end = entry[edge]['end']
28
        if edge == 'begin':
29
            end = entry['timestamp']
30
31
        return FrTest(
32
            type="banner",
33
            campaign=entry['campaign'],
34
            banners=banners,
35
            start=start,
36
            end=end
37
        )
38
39
    return (test_from_entry('begin'), test_from_entry('end'), )
40
41
42
def get_relevant_events():
43
    from mediawiki.centralnotice.api import get_campaign_logs
44
    from mediawiki.centralnotice import time_util
45
46
    def is_relevant(entry):
47
        '''
48
        This change enabled/disabled a test; or an enabled test has been mutated.
49
        '''
50
        if 'enabled' in entry['added'] or entry['begin']['enabled'] is 1:
51
            return True
52
53
    config = get_config()
54
    logs = get_campaign_logs(since=time_util.str_time_offset(days=-config.centralnotice_history_days))
55
    return [tests_from_entry(e) for e in reversed(logs) if is_relevant(e)]
56