aboutsummaryrefslogtreecommitdiff
path: root/steuermann/report.py
blob: 1bf4f6aa74919f02b028cefa5d019e3de5b22fb0 (plain) (blame)
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
'''
prototype of report

'''
import subprocess
import time
import sys

try :
    import CStringIO as StringIO
except ImportError :
    import StringIO as StringIO

#####

def c_d_fn(x,depth) :
    if x.depth  >= depth :
        return
    x.depth = depth
    depth = depth + 1
    for y in x.children :
        c_d_fn(y,depth)
    

def compute_depths(nodes) :
    for x in nodes :
        x = nodes[x]
        x.depth = 0
        x.in_recurse = 0
        x.parents = [ ]
        x.children = [ ]
    for x in nodes :
        x = nodes[x]
        for y in x.precursors :
            if not x in y.children :
                y.children.append(x)
    for x in nodes :
        c_d_fn(nodes[x],1)
        
#####

def compute_table(nodes) :
    # find the depths
    compute_depths(nodes)

    # sort the nodes by depth
    l = [ x.split(':') for x in nodes ]
    l = [ [ x[0] ] + x[1].split('/') for x in l ]
    l = [ [ x[1], x[2], x[0] ] for x in l ]
    l = sorted(l)

    # table_content is a list of nodes in each table
    table_content = { }

    # table_hosts is a list of hosts in each table
    table_hosts = { }

    # table_depth is how deep the deepest row of each table is
    table_depth = { }

    for x in nodes  :
        host, table = x.split(':')
        table, cmd = table.split('/')

        table_content[table] = table_content.get(table,[]) + [ x ]

        table_hosts  [table] = table_hosts  .get(table,[]) + [ host ]

        if table_depth.get(table,0) < nodes[x].depth :
            table_depth[table] = nodes[x].depth

    for x in table_hosts :
        table_hosts[x] = list(set(table_hosts[x]))

    return table_content, table_hosts, table_depth

#####

# html of one table

def html_table(nodes, table, host_list ) :
    s=StringIO.StringIO()

    # this is all the nodes in this table
    pat = ':%s/' % table
    l = [ x for x in nodes if pat in x ]

    # d[x] is the max depth of command x
    d = { }
    for x in l :
        depth = nodes[x].depth
        x = x.split('/')[1]
        if d.get(x,0) < depth :
            d[x] = depth

    # this is the order of the rows of the table
    cmd_order = sorted( [ (d[x], x) for x in d ] )

    # this is the table
    s.write( "<table border=1>" )

    # heading
    s.write( "<tr> <td>&nbsp;</td> " )
    for host in host_list :
        s.write( "<th>%s</th>" % host )
    s.write( "</tr>\n" )

    # loop over the commands in the order they appear
    for depth, cmd in cmd_order :
        s.write( "<tr>\n\t<td>%s/%s</td>\n"%(table,cmd) )
        for host in host_list :
            name = host + ':' + table + '/' + cmd
            if name in nodes :
                s.write( "\t<td class=%%(class!%s)s> %%(text!%s)s </td>\n"%(name,name) )
            else :
                s.write( "\t<td class=nothing> . </td>\n" )
        s.write( "</tr>\n" )
    s.write( "</table>" )
    return s.getvalue()

class struct(object): 
    pass

#####

def main() :
    import sqlite3
    db = sqlite3.connect('sr.db')

    c = db.cursor()
    c.execute('select run from runs')
    for (run,) in c :

        all = [ ]
        c1 = db.cursor()
        c1.execute('select host, tablename, cmd, depth, status, start_time, end_time, notes from status where run = ? ',(run,) )
        for x in c1 :
            n = struct()
            n.host, n.tablename, n.cmd, n.depth, n.status, n.start_time, n.ent_time, n.notes = x
            all.append(n)

        print "RUN",run
        for x in all :
            print x.host, x.tablename, x.cmd, x.status

main()