summaryrefslogtreecommitdiffstats
path: root/packages_poc_activity_stats.py
blob: a5b46bab9942d7ac6c4d88d696972833ae8a5bc8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env python -tt
#-*- coding: utf-8 -*-

#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License along
#    with this program; if not, write to the Free Software Foundation, Inc.,
#    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

"""
This program loads the picle generated by packages_poc_activity and outputs
some stats.

Dependencies:
* python-requests
"""

import json
import pickle
import requests


def get_number_current_poc(username):
    """ Retrieve the current number of POC a specified username has.
    """
    url = 'http://209.132.184.188/api/packager/stats/%s' % username
    req = requests.get(url)
    try:
        data = json.loads(req.text)
    except Exception, err:
        print 'ERROR for user %s' % username
        print 'ERROR:', err.message
        print req.url
        return 0

    cnt = 0
    if 'devel' in data:
        cnt = data['devel']['point of contact']
    else:
        print 'ERROR for user %s' % username
        print 'Wrongly formed json'
    return cnt


def main():
    """ Reads the pickle file and output some stats. """
    stream = open('poc_activity.data.pickle', 'rb')
    data =pickle.load(stream)
    stream.close()

    pkger_stats = {}

    loose_pkg = {}
    win_pkg = {}
    for pkg in data:
        poc = data[pkg]['poc']
        if poc not in data[pkg]['active_poc']:

            if poc not in pkger_stats:
                pkger_stats[poc] = get_number_current_poc(poc)

            if poc in loose_pkg:
                loose_pkg[data[pkg]['poc']] += 1
            else:
                loose_pkg[data[pkg]['poc']] = 1

            for user in data[pkg]['active_poc']:
                if user not in pkger_stats:
                    pkger_stats[user] = get_number_current_poc(user)

                if user in win_pkg:
                    win_pkg[user] += 1
                else:
                    win_pkg[user] = 1

    # invert pkg: number of packages won/lost into: number of package won/lost: packagers
    lost_pkger = {}
    lost_pkger_pc = {}
    for pkger in loose_pkg:
        if pkger not in pkger_stats:
            pkger_stats[pkger] = get_number_current_poc(pkger)

        if loose_pkg[pkger] in lost_pkger:
            lost_pkger[loose_pkg[pkger]].append(pkger)
        else:
            lost_pkger[loose_pkg[pkger]] = [pkger]

        if float(pkger_stats[pkger]) != 0:
            pcent = loose_pkg[pkger] / float(pkger_stats[pkger]) * 100
            if pcent in lost_pkger_pc:
                lost_pkger_pc[pcent].append(pkger)
            else:
                lost_pkger_pc[pcent] = [pkger]


    win_pkger = {}
    win_pkger_pc = {}
    for pkger in win_pkg:
        if pkger not in pkger_stats:
            pkger_stats[pkger] = get_number_current_poc(pkger)

        if win_pkg[pkger] in win_pkger:
            win_pkger[win_pkg[pkger]].append(pkger)
        else:
            win_pkger[win_pkg[pkger]] = [pkger]

        if float(pkger_stats[pkger]) != 0:
            pcent = win_pkg[pkger] / float(pkger_stats[pkger]) * 100
            if pcent in win_pkger_pc:
                win_pkger_pc[pcent].append(pkger)
            else:
                win_pkger_pc[pcent] = [pkger]

    avg_loose = sum(loose_pkg.values())/ float(len(loose_pkg.values()))
    avg_win = sum(win_pkg.values())/ float(len(win_pkg.values()))

    print '%s packagers loose at least one package' % len(loose_pkg)
    print 'In average they loose %s packages' % avg_loose

    print '%s packagers win at least one package' % len(win_pkg)
    print 'In average they win %s packages' % avg_win

    print "\n\nTop 10 packagers loosing packages:"
    for key in sorted(lost_pkger.keys(), reverse=True)[:10]:
        print "   %s loose %s packages : %s -> %s" %(', '.join(
                lost_pkger[key]), key, pkger_stats[lost_pkger[key][0]],
                (pkger_stats[lost_pkger[key][0]] - key ))

    print "\n\nTop 10 packagers winning packages:"
    for key in sorted(win_pkger.keys(), reverse=True)[:10]:
        print "   %s win %s packages : %s -> %s" %(', '.join(
                win_pkger[key]), key, pkger_stats[win_pkger[key][0]],
                (pkger_stats[win_pkger[key][0]] + key))

    print "\n\nTop 10 packagers loosing packages (in %):"
    for key in sorted(lost_pkger_pc.keys(), reverse=True)[:10]:
        print "   %s loose %s%% packages" %(', '.join(lost_pkger_pc[key]), key)

    print "\n\nTop 10 packagers winning packages (in %):"
    for key in sorted(win_pkger_pc.keys(), reverse=True)[:10]:
        print "   %s win %s%% packages" %(', '.join(win_pkger_pc[key]), key)


    print "\n\n Writing file"

    stream = open('poc_stats_pkgers_win.csv', 'w')
    stream.write("# packagers, # pkgs win\n")
    for key in sorted(win_pkger.keys(), reverse=True):
        stream.write("%s, %s\n" % (len(win_pkger[key]), key))
    stream.close()

    stream = open('poc_stats_pkgers_lost.csv', 'w')
    stream.write("# packagers, # pkgs lost\n")
    for key in sorted(lost_pkger.keys(), reverse=True):
        stream.write("%s, %s\n" % (len(lost_pkger[key]), key))
    stream.close()

    stream = open('poc_stats_pkgers_win_pc.csv', 'w')
    stream.write("# packagers, # pkgs win (in %)\n")
    for key in sorted(win_pkger_pc.keys(), reverse=True):
        stream.write("%s, %s\n" % (len(win_pkger_pc[key]), key))
    stream.close()

    stream = open('poc_stats_pkgers_lost_pc.csv', 'w')
    stream.write("# packagers, # pkgs lost (in %)\n")
    for key in sorted(lost_pkger_pc.keys(), reverse=True):
        stream.write("%s, %s\n" % (len(lost_pkger_pc[key]), key))
    stream.close()

if __name__ == '__main__':
    main()