summaryrefslogtreecommitdiffstats
path: root/reports/brpt/importscript.py
blob: 56c1def1dae97f810f4d57094910a75285e5fc01 (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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#! /usr/bin/env python
'''Imports statistics.xml and clients.xml files in to database backend for new statistics engine'''
__revision__ = '$Revision$'

import os, sys
try:    # Add this project to sys.path so that it's importable
    import settings # Assumed to be in the same directory.
except ImportError:
    sys.stderr.write("Failed to locate settings.py")
    sys.exit(1)

project_directory = os.path.dirname(settings.__file__)
project_name = os.path.basename(project_directory)
sys.path.append(os.path.join(project_directory, '..'))
project_module = __import__(project_name, '', '', [''])
sys.path.pop()
# Set DJANGO_SETTINGS_MODULE appropriately.
os.environ['DJANGO_SETTINGS_MODULE'] = '%s.settings' % project_name

from brpt.reports.models import Client, Interaction, Bad, Modified, Extra, Performance, Reason
from lxml.etree import XML, XMLSyntaxError
from sys import argv
from getopt import getopt, GetoptError
from datetime import datetime
from time import strptime, sleep

if __name__ == '__main__':

    try:
        opts, args = getopt(argv[1:], "hc:s:", ["help", "clients=", "stats="])
    except GetoptError, mesg:
        # print help information and exit:
        print "%s\nUsage:\nStatReports.py [-h] -c <clients-file> -s <statistics-file>" % (mesg) 
        raise SystemExit, 2
    for o, a in opts:
        if o in ("-h", "--help"):
            print "Usage:\nStatReports.py [-h] -c <clients-file> -s <statistics-file>"
            raise SystemExit
        if o in ("-c", "--clients"):
            clientspath = a
        if o in ("-s", "--stats"):
            statpath = a

    '''Reads Data & Config files'''
    try:
        statsdata = XML(open(statpath).read())
    except (IOError, XMLSyntaxError):
        print("StatReports: Failed to parse %s"%(statpath))
        raise SystemExit, 1
    try:
        clientsdata = XML(open(clientspath).read())
    except (IOError, XMLSyntaxError):
        print("StatReports: Failed to parse %s"%(clientspath))
        raise SystemExit, 1

    from django.db import connection, backend
    cursor = connection.cursor()

    cursor.execute("SELECT name, id from reports_client;")
    clients = {}
    [clients.__setitem__(a,b) for a,b in cursor.fetchall()]
    
    for node in statsdata.findall('Node'):
        name = node.get('name')
        if not clients.has_key(name):
            cursor.execute("INSERT INTO reports_client VALUES (NULL, %s, %s, NULL)", [datetime.now(),name])
            clients[name] = cursor.lastrowid
#            print("Client %s added to db"%name)
#        else:
#            print("Client %s already exists in db"%name)


    cursor.execute("SELECT client_id, timestamp, id from reports_interaction")
    interactions = cursor.fetchall()
    #interactions_slice = [x[0:2] for x in interactions]
    interactions_hash = {}
    [interactions_hash.__setitem__(str(x[0])+"-"+x[1].isoformat(),x[2]) for x in interactions]
    pingability = {}
    [pingability.__setitem__(n.get('name'),n.get('pingable',default='N')) for n in clientsdata.findall('Client')]

    cursor.execute("SELECT id, owner, current_owner, %s, current_group, perms, current_perms, status, current_status, %s, current_to, version, current_version, current_exists, current_diff from reports_reason"%(backend.quote_name("group"),backend.quote_name("to")))
    reasons = cursor.fetchall()

    cursor.execute("SELECT id, name, kind, reason_id from reports_bad")
    bad_things = cursor.fetchall()
    cursor.execute("SELECT id, name, kind, reason_id from reports_extra")
    extra_things = cursor.fetchall()
    cursor.execute("SELECT id, name, kind, reason_id from reports_modified")
    modified_things = cursor.fetchall()
    cursor.execute("SELECT id, metric, value from reports_performance")
    performance_things = cursor.fetchall()

    reasons_hash = {}
    [reasons_hash.__setitem__(tuple(n[1:]),n[0]) for n in reasons]
    for r in statsdata.findall('.//Bad/*')+statsdata.findall('.//Extra/*')+statsdata.findall('.//Modified/*'):
        arguments = [r.get('owner', default=""), r.get('current_owner', default=""),
                     r.get('group', default=""), r.get('current_group', default=""),
                     eval(r.get('perms', default="''")), eval(r.get('current_perms', default="''")),
                     r.get('status', default=""), r.get('current_status', default=""),
                     r.get('to', default=""), r.get('current_to', default=""),
                     r.get('version', default=""), r.get('current_version', default=""),
                     eval(r.get('current_exists', default="True").capitalize()), r.get('current_diff', default="")]
        if reasons_hash.has_key(tuple(arguments)):
            current_reason_id = reasons_hash[tuple(arguments)]
#            print("Reason already exists..... It's ID is: %s"%current_reason_id)
        else:
            cursor.execute("INSERT INTO reports_reason VALUES (NULL, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s);",
                           arguments)
            current_reason_id = cursor.lastrowid
            reasons.append([current_reason_id]+arguments)
            reasons_hash[tuple(arguments)] = current_reason_id
                               
#            print("Reason inserted with id %s"%current_reason_id)
    print "----------------REASONS SYNCED------------------"
    for node in statsdata.findall('Node'):
        name = node.get('name')
        try:
            pingability[name]
        except KeyError:
            pingabilty[name] = 'N'
        for statistics in node.findall('Statistics'):
            t = strptime(statistics.get('time'))
            timestamp = datetime(t[0],t[1],t[2],t[3],t[4],t[5])
            if interactions_hash.has_key(str(clients[name]) +"-"+ timestamp.isoformat()):
                current_interaction_id = interactions_hash[str(clients[name])+"-"+timestamp.isoformat()]
#                print("Interaction for %s at %s with id %s already exists"%(clients[name],
#                      datetime(t[0],t[1],t[2],t[3],t[4],t[5]),current_interaction_id))
            else:
                cursor.execute("INSERT INTO reports_interaction VALUES (NULL, %s, %s, %s, %s, %s, %s, %s, %s);",
                               [clients[name],
                                timestamp,
                                statistics.get('state', default="unknown"),
                                statistics.get('revision',default="unknown"),
                                statistics.get('client_version',default="unknown"),
                                pingability[name],
                                statistics.get('good',default="0"),
                                statistics.get('total',default="0")])
                current_interaction_id = cursor.lastrowid
                interactions.append([clients[name], timestamp, current_interaction_id])
                interactions_hash[str(clients[name])+"-"+timestamp.isoformat()] = current_interaction_id
#                print("Interaction for %s at %s with id %s INSERTED in to db"%(clients[name],
#                      timestamp, current_interaction_id))

            #insert bad children, extra children, modified children, performance items for this iteration of Client/Interaction

    #use that crazy query to update all the latest client_interaction records.
    
            
    connection._commit()
    #Clients are consistent
    for q in connection.queries:
        print q

    raise SystemExit, 0


    #-------------------------------
    for node in statsdata.findall('Node'):
        (client_rec, cr_created) = Client.objects.get_or_create(name=node.get('name'),
                                        defaults={'name': node.get('name'), 'creation': datetime.now()})

        for statistics in node.findall('Statistics'):
            t = strptime(statistics.get('time'))
            (interaction_rec, ir_created) = Interaction.objects.get_or_create(client=client_rec.id,
                                                timestamp=datetime(t[0],t[1],t[2],t[3],t[4],t[5]),
                                                defaults={'client':client_rec,
                                                          'timestamp':datetime(t[0],t[1],t[2],t[3],t[4],t[5]),
                                                          'state':statistics.get('state', default="unknown"),
                                                          'repo_revision':statistics.get('revision', default="unknown"),
                                                          'client_version':statistics.get('client_version'),
                                                          'goodcount':statistics.get('good', default="unknown"),
                                                          'totalcount':statistics.get('total', default="unknown")})
            for bad in statistics.findall('Bad'):
                for ele in bad.getchildren():
                    (reason_rec, rr_created) = Reason.objects.get_or_create(owner=ele.get('owner',default=''),
                                                        current_owner=ele.get('current_owner',default=''),
                                                        group=ele.get('group',default=''),
                                                        current_group=ele.get('current_group',default=''),
                                                        perms=ele.get('perms',default=''),
                                                        current_perms=ele.get('current_perms',default=''),
                                                        status=ele.get('status',default=''),
                                                        current_status=ele.get('current_status',default=''),
                                                        to=ele.get('to',default=''),
                                                        current_to=ele.get('current_to',default=''),
                                                        version=ele.get('version',default=''),
                                                        current_version=ele.get('current_version',default=''),
                                                        current_exists=ele.get('current_exists',default='True'),
                                                        current_diff=ele.get('current_diff',default=''),
                                                        defaults={'owner':ele.get('owner',default=''),
                                                                  'current_owner':ele.get('current_owner',default=''),
                                                                  'group':ele.get('group',default=''),
                                                                  'current_group':ele.get('current_group',default=''),
                                                                  'perms':ele.get('perms',default=''),
                                                                  'current_perms':ele.get('current_perms',default=''),
                                                                  'status':ele.get('status',default=''),
                                                                  'current_status':ele.get('current_status',default=''),
                                                                  'to':ele.get('to',default=''),
                                                                  'current_to':ele.get('current_to',default=''),
                                                                  'version':ele.get('version',default=''),
                                                                  'current_version':ele.get('current_version',default=''),
                                                                  'current_exists':ele.get('current_exists',default='True'),
                                                                  'current_diff':ele.get('current_diff',default='')})
                        

                    (ele_rec, er_created) = Bad.objects.get_or_create(name=ele.get('name'), kind=ele.tag,
                                                defaults={'name':ele.get('name'),
                                                          'kind':ele.tag,
                                                          'reason':reason_rec})

                    if not ele_rec in interaction_rec.bad_items.all():
                        interaction_rec.bad_items.add(ele_rec)

            for modified in statistics.findall('Modified'):
                for ele in modified.getchildren():
                    (reason_rec, rr_created) = Reason.objects.get_or_create(owner=ele.get('owner',default=''),
                                                        current_owner=ele.get('current_owner',default=''),
                                                        group=ele.get('group',default=''),
                                                        current_group=ele.get('current_group',default=''),
                                                        perms=ele.get('perms',default=''),
                                                        current_perms=ele.get('current_perms',default=''),
                                                        status=ele.get('status',default=''),
                                                        current_status=ele.get('current_status',default=''),
                                                        to=ele.get('to',default=''),
                                                        current_to=ele.get('current_to',default=''),
                                                        version=ele.get('version',default=''),
                                                        current_version=ele.get('current_version',default=''),
                                                        current_exists=ele.get('current_exists',default='True'),
                                                        current_diff=ele.get('current_diff',default=''),
                                                        defaults={'owner':ele.get('owner',default=''),
                                                                  'current_owner':ele.get('current_owner',default=''),
                                                                  'group':ele.get('group',default=''),
                                                                  'current_group':ele.get('current_group',default=''),
                                                                  'perms':ele.get('perms',default=''),
                                                                  'current_perms':ele.get('current_perms',default=''),
                                                                  'status':ele.get('status',default=''),
                                                                  'current_status':ele.get('current_status',default=''),
                                                                  'to':ele.get('to',default=''),
                                                                  'current_to':ele.get('current_to',default=''),
                                                                  'version':ele.get('version',default=''),
                                                                  'current_version':ele.get('current_version',default=''),
                                                                  'current_exists':ele.get('current_exists',default='True'),
                                                                  'current_diff':ele.get('current_diff',default='')})
                        

                    (ele_rec, er_created) = Modified.objects.get_or_create(name=ele.get('name'), kind=ele.tag,
                                                defaults={'name':ele.get('name'),
                                                          'kind':ele.tag,
                                                          'reason':reason_rec})
                    if not ele_rec in interaction_rec.modified_items.all():
                        interaction_rec.modified_items.add(ele_rec)

            for extra in statistics.findall('Extra'):
                for ele in extra.getchildren():
                    (reason_rec, rr_created) = Reason.objects.get_or_create(owner=ele.get('owner',default=''),
                                                        current_owner=ele.get('current_owner',default=''),
                                                        group=ele.get('group',default=''),
                                                        current_group=ele.get('current_group',default=''),
                                                        perms=ele.get('perms',default=''),
                                                        current_perms=ele.get('current_perms',default=''),
                                                        status=ele.get('status',default=''),
                                                        current_status=ele.get('current_status',default=''),
                                                        to=ele.get('to',default=''),
                                                        current_to=ele.get('current_to',default=''),
                                                        version=ele.get('version',default=''),
                                                        current_version=ele.get('current_version',default=''),
                                                        current_exists=ele.get('current_exists',default='True'),
                                                        current_diff=ele.get('current_diff',default=''),
                                                        defaults={'owner':ele.get('owner',default=''),
                                                                  'current_owner':ele.get('current_owner',default=''),
                                                                  'group':ele.get('group',default=''),
                                                                  'current_group':ele.get('current_group',default=''),
                                                                  'perms':ele.get('perms',default=''),
                                                                  'current_perms':ele.get('current_perms',default=''),
                                                                  'status':ele.get('status',default=''),
                                                                  'current_status':ele.get('current_status',default=''),
                                                                  'to':ele.get('to',default=''),
                                                                  'current_to':ele.get('current_to',default=''),
                                                                  'version':ele.get('version',default=''),
                                                                  'current_version':ele.get('current_version',default=''),
                                                                  'current_exists':ele.get('current_exists',default='True'),
                                                                  'current_diff':ele.get('current_diff',default='')})
                        

                    (ele_rec, er_created) = Extra.objects.get_or_create(name=ele.get('name'), kind=ele.tag,
                                                defaults={'name':ele.get('name'),
                                                          'kind':ele.tag,
                                                          'reason':reason_rec})
                        
                    if not ele_rec in interaction_rec.extra_items.all():
                        interaction_rec.extra_items.add(ele_rec)

            for times in statistics.findall('OpStamps'):
                for tags in times.items():
                    (time_rec, tr_created) = Performance.objects.get_or_create(metric=tags[0], value=tags[1],
                                                    defaults={'metric':tags[0],
                                                              'value':tags[1]})
                    if not ele_rec in interaction_rec.extra_items.all():
                        interaction_rec.performance_items.add(time_rec)