summaryrefslogtreecommitdiffstats
path: root/autobuild.rb
blob: c52986cc8bc3c8c50686de667508383c24b46cf5 (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
#!/usr/bin/ruby

require 'fileutils'
require 'rubygems'
require 'sqlite3'

def create_tables(db)
    #db.execute "CREATE TABLE IF NOT EXISTS Packages(Id INTEGER PRIMARY KEY, Name TEXT, Version TEXT, Release TEXT, UNIQUE (Name, Version, Release))"
    db.execute "CREATE TABLE IF NOT EXISTS Packages(Id INTEGER PRIMARY KEY, Name TEXT, UNIQUE (Name))"
    db.execute "CREATE TABLE IF NOT EXISTS Runs(Id INTEGER PRIMARY KEY, Start INTEGER, End INTEGER, UNIQUE(Start))"
    db.execute "CREATE TABLE IF NOT EXISTS ResultValues(Id INTEGER PRIMARY KEY, Name TEXT, UNIQUE(Name))"
    db.execute "CREATE TABLE IF NOT EXISTS Results(Id INTEGER PRIMARY KEY, Package INTEGER, Run INTEGER, Result INTEGER,
		FOREIGN KEY(Package) REFERENCES Packages(Id),
		FOREIGN KEY(Run) REFERENCES Run(Id),
		FOREIGN KEY(Result) REFERENCES ResultValues(Id),
                UNIQUE(Package, Run, Result))"
    db.execute "CREATE TABLE IF NOT EXISTS Diffs(Run INTEGER PRIMARY KEY, New INTEGER, Fixed INTEGER, NotFixed INTEGER)"
    db.execute "CREATE TABLE IF NOT EXISTS Attributes(Id INTEGER PRIMARY KEY, Package INTEGER, Run INTEGER, Attr TEXT,
        FOREIGN KEY(Package) REFERENCES Packages(Id),
        FOREIGN KEY(Run) REFERENCES Run(Id),
        UNIQUE(Package, Run))"
end

def get_or_add_resultvalue(db, result)
    db.execute "INSERT OR IGNORE INTO ResultValues(Name) VALUES('#{result}')"
    return db.execute("SELECT Id FROM ResultValues WHERE Name = '#{result}'")[0][0]
end

def get_or_add_package(db, name, version, release)
    #db.execute "INSERT OR IGNORE INTO Packages(Name, Version, Release) VALUES('#{name}','#{version}','#{release}')"
    #return db.execute("SELECT Id FROM Packages WHERE Name = '#{name}' AND Version = '#{version}' AND Release = '#{release}'")[0][0]
    db.execute "INSERT OR IGNORE INTO Packages(Name) VALUES('#{name}')"
    return db.execute("SELECT Id FROM Packages WHERE Name = '#{name}'")[0][0]
end

# Find interesting attributes about the package. Right now, this is just interesting
# dependencies.
def find_attributes(build_dir)
    attr = {}
    rpm_list_files = Dir.glob(build_dir + '/rpm_qa.*.log')
    rpm_list_file = rpm_list_files[0]
    if rpm_list_file then
        File.open(rpm_list_file, 'r') {|f|
            f.each_line{|l|
                if l !~ /^(.*)-([^-]*)-([^-]*)/ then
                    puts "Skipping " + l
                    next
                end
                name = $1
                if name =~ /^((bwbasic)|(yabasic)|(mono-basic))$/ then
                    attr[:lang_basic] = true
                end
                if name =~ /^mono$/ then
                    attr[:lang_mono] = true
                end
                if name =~ /^(erlang)-compiler$/ then
                    attr[:lang_erlang] = true
                end
                if name =~ /^gcc-g(fortran)$/ then
                    attr[:lang_fortran] = true
                end
                if name =~ /^groovy$/ then
                    attr[:lang_groovy] = true
                end
                if name =~ /^ghc$/ then
                    attr[:lang_haskell] = true
                end
                if name =~ /^(java)|(gcc-java)/ then
                    attr[:lang_java] = true
                end
                if name =~ /^c(lisp)-devel$/ then
                    attr[:lang_lisp] = true
                end
                if name =~ /^lib(64)?lua-devel$/ then
                    attr[:lang_lua] = true
                end
                if name =~ /^gcc-objc$/ then
                    attr[:lang_objc] = true
                end
                if name =~ /^(ocaml)-compiler$/ then
                    attr[:lang_ocaml] = true
                end
                if name =~ /^(nodejs)|(uglify-js)|(slimit)|(yuicompressor)$/ then
                    attr[:lang_javascript] = true
                end
                if name =~ /^fpc(-src)?$/ then
                    attr[:lang_pascal] = true
                end
                if name =~ /^(perl)/ then
                    # A bunch of perl modules are installed in every installation,
                    # so ignore those.
                    if name !~ /^((perl)|(perl-base)|(perl-Config-IniFiles)|(perl-Exporter-Tiny)|(perl-File-Slurp)|(perl-File-Sync)|(perl-Filesys-Df)|(perl-JSON)|(perl-libintl-perl)|(perl-List-MoreUtils)|(perl-Locale-gettext)|(perl-MDK-Common)|(perl-MDV-Distribconf)|(perl-MDV-Packdrakeng)|(perl-Module-ScanDeps)|(perl-Time-ZoneInfo)|(perl-URPM)|(perl-XML-LibXML)|(perl-XML-NamespaceSupport)|(perl-XML-SAX)|(perl-XML-SAX-Base)|(perl-YAML)|(perl-YAML-Tiny))$/ then
                        attr[:lang_perl] = true
                    end
                end
                if name =~ /^((php)-devel)|(lib(64)?php_common)/ then
                    attr[:lang_php] = true
                end
                if name =~ /^((python3?-setuptools)|(lib(64)?python3?-devel))$/ then
                    attr[:lang_python] = true
                end
                if name =~ /^((ruby)-devel)|(lib(64)?ruby[0-9])/ then
                    attr[:lang_ruby] = true
                end
                if name =~ /^(tcl)$/ then
                    attr[:lang_tcl] = true
                end

                # Check for a few build systems, too
                if name =~ /^(ant)$/ then
                    attr[:build_ant] = true
                end
                if name =~ /^(cmake)$/ then
                    attr[:build_cmake] = true
                end
                if name =~ /^(maven)$/ then
                    attr[:build_maven] = true
                end
                if name =~ /^(scons)$/ then
                    attr[:build_scons] = true
                end
            }
        }
    end

    attr_line = ''
    attr.keys.sort.each do |key|
        attr_line = attr_line + ' ' + key.to_s
    end
    attr_line = attr_line.strip()
    return attr_line
end
    
def insert_run(db, status_file)
    t_start = Date.parse(File.basename(File.dirname(status_file))).strftime('%s')
    puts "Inserting data for run #{t_start} (#{status_file})"
    t_end = File.mtime(status_file).to_i
    db.execute "INSERT INTO Runs(Start, End) VALUES(#{t_start}, #{t_end})"
    run_id = db.last_insert_row_id
    File.open(status_file, 'r') {|f|
        db.transaction
        f.each_line{|l|
            if l !~ /^(.*)-([^-]*)-([^-]*).src.rpm: (.*)$/ then
                puts l
                next
            end
            name = $1
            version = $2
            release = $3
            result = $4
            if result == 'rejected' then
                    next
            end
            result_id = get_or_add_resultvalue(db, result)
            package_id = get_or_add_package(db, name, version, release)
            puts name
            db.execute "INSERT INTO Results(Package, Run, Result) VALUES(#{package_id}, #{run_id}, #{result_id})"

            # Store some interesting attributes about the build
            attr = find_attributes(File.dirname(status_file) + '/' + name + '-' + version + '-' + release + '.src.rpm')
            #puts "\t\t\tAttributes: " + attr
            if attr then
                db.execute "INSERT INTO Attributes(Package, Run, Attr) VALUES(#{package_id}, #{run_id}, '#{attr}')"
            end

        }
        db.commit
    }
end

def drop_run(db, r)
    t_start = Date.parse(r).strftime('%s')
    run_id = db.execute("SELECT Id FROM Runs WHERE Start='#{t_start}'")[0][0]
    puts "Dropping run #{t_start}"
    db.execute "DELETE FROM Results WHERE Run = #{run_id}"
    db.execute "DELETE FROM Runs WHERE Id = #{run_id}"
end

def diff_runs(db, r1, r2)
    (newly_broken, fixed, still_broken) = (db.execute "SELECT New, Fixed, NotFixed FROM DIffs WHERE Run = #{r2}")[0]
    if newly_broken.nil? then
	    ok_id = get_or_add_resultvalue(db, 'ok')
	    not_on_this_arch_id = get_or_add_resultvalue(db, 'not_on_this_arch')
	    failure_query = "Result NOT IN (#{ok_id}, #{not_on_this_arch_id})"
	    newly_broken = (db.execute "SELECT count(Id) FROM Results WHERE Run = #{r2} AND #{failure_query} AND Package NOT IN (SELECT Package FROM Results WHERE Run = #{r1} AND #{failure_query})")[0][0]
	    fixed = (db.execute "SELECT count(Results.Id) FROM Results,Packages WHERE Run = #{r2} AND Package = Packages.Id AND (Result = #{ok_id} OR Result = #{not_on_this_arch_id}) AND Name IN (SELECT Name FROM Results,Packages WHERE Run = #{r1} AND #{failure_query} AND Package = Packages.Id)")[0][0]
	    still_broken = (db.execute "SELECT count(Id) FROM Results WHERE Run = #{r2} AND #{failure_query} AND Package IN (SELECT Package FROM Results WHERE Run = #{r1} AND #{failure_query})")[0][0]
	    db.execute "INSERT INTO Diffs(Run, New, Fixed, NotFixed) VALUES(#{r2},#{newly_broken},#{fixed},#{still_broken})"
    end
    { 'new' => newly_broken, 'fixed' => fixed, 'not fixed' => still_broken }
end

def report(db)
    resultvalues = db.execute("SELECT Name FROM ResultValues").flatten
    runs = db.execute("SELECT Id, Start FROM Runs ORDER BY Start ASC")
    results = {}
    prev_run = 0
    runs.each{|r|
	run_id = r[0]
	results[run_id] = {}
        data = db.execute "SELECT count(Results.Id), ResultValues.Name FROM Results, ResultValues WHERE Results.Result = ResultValues.Id and Run = #{run_id} GROUP BY ResultValues.Name"
	total = 0
	success = 0
	failure = 0
	data.each{|d|
            results[run_id][d[1]] = d[0]
	    total += d[0]
	    if (d[1] == "ok")
                success += d[0]
	    elsif (d[1] == "build_failure" || d[1] == "missing_dep" || d[1] == "install_deps_failure" || d[1] == "recreate_srpm_failure")
		failure += d[0]
	    end
	    results[run_id]['success'] = success
	    results[run_id]['failure'] = failure
	    results[run_id]['total'] = total
	    results[run_id]['success_rate'] = (1000*success/(success+failure)).round.to_f/10
	}
	results[run_id].merge!(diff_runs(db, prev_run, run_id))
	prev_run = run_id
    }
    results
end

def export_data(db, target_dir)	
    resultvalues = db.execute("SELECT Name FROM ResultValues").flatten
    runs = db.execute("SELECT Id, Start FROM Runs ORDER BY Start DESC")
    results = report(db)
    File.open(target_dir + "/data.js", 'w') {|df|    
    df.puts "var data = { cols: [ {id: 'A', label: 'Date', type: 'date'},"
    col = 'A'
    resultvalues.each{|v| df.puts "{id: '#{col.next!}', label: '#{v}', type: 'number'}," }
    df.puts "{id: '#{col.next!}', label: 'Total', type: 'number'},"
    df.puts "{id: '#{col.next!}', label: 'Success Rate', type: 'number'},"
    df.puts "{id: '#{col.next!}', label: 'Newly Broken', type: 'number'},"
    df.puts "{id: '#{col.next!}', label: 'Fixed', type: 'number'},"
    df.puts "{id: '#{col.next!}', label: 'Not Fixed', type: 'number'},"
    df.puts  "],"
    df.puts  "rows: ["
    runs.each{|r|
	result = results[r[0]]
	date_val = "new Date(#{DateTime.strptime(r[1].to_s, '%s').strftime('%Y, %m-1, %d')}, 0, 0, 0)"
	date_str = DateTime.strptime(r[1].to_s, '%s').strftime('%Y-%m-%d')
	df.print "{c:[{v: #{date_val}, f: '<a href=\"results.php?run=#{date_str}\">#{date_str}</a>'},"
        resultvalues.each{|v| df.print "{v: #{result[v] || 0}},"}
	df.print "{v: #{result['total']}},"
	df.print "{v: #{result['success_rate']}, f: '#{result['success_rate']}%'},"
	df.print "{v: #{result['new']}},"
	df.print "{v: #{result['fixed']}},"
	df.print "{v: #{result['not fixed']}},"
	df.puts "]},"
    }
    df.puts "]}"
    }
    FileUtils.cp("autobuild.db", target_dir)
end

def finish
	# get date of finished one
	# olddate = readlink current latest
	# change link to point to newdate
	# cleanlogs.sh olddate newdate
	# insert newdate
end

begin 
    db = SQLite3::Database.open "autobuild.db"
    create_tables(db)
    if (ARGV[0] == 'insert') then
        insert_run(db, ARGV[1])
    elsif (ARGV[0] == 'report')
        export_data(db, ARGV[1])
    elsif (ARGV[0] == 'drop')
        drop_run(db, ARGV[1])
    end
rescue SQLite3::Exception => e 
    
    puts "Exception occured"
    puts e
    
ensure
    db.close if db
end