Debian Codegraph @

lib/Analizo/Batch/Output/DB.pm

raw
1 package Analizo::Batch::Output::DB;
2 use strict;
3 use warnings;
4 use parent qw( Analizo::Batch::Output );
5 use DBI;
6 use Digest::SHA qw(sha1_hex);
7
8 sub database($) {
9 my ($self) = @_;
10 my $db = $self->file || 'output.sqlite3';
11 if ($db =~ /^dbi:/) {
12 return $db;
13 } else {
14 return 'dbi:SQLite:' . $db;
15 }
16 }
17
18 sub push($$) {
19 my ($self, $job) = @_;
20
21 my $project_id = $self->_add_project($job->project_name);
22
23 my $developer_id = $self->_add_developer($job);
24
25 my $commit_id = $self->_add_commit($job, $project_id, $developer_id);
26
27 $self->_add_modules($job, $commit_id, $project_id);
28 }
29
30 sub flush($) {
31 my ($self) = @_;
32 $self->{dbh}->disconnect();
33 }
34
35 sub _find_row_id($$@) {
36 my ($self, $sql, @data) = @_;
37 my $statement_id = 'st_find_' . sha1_hex($sql); # is this SHA1 needed at all?
38
39 $self->{$statement_id} ||= $self->{dbh}->prepare($sql);
40 my $list = $self->{dbh}->selectall_arrayref($self->{$statement_id}, {}, @data);
41 if (scalar(@$list) == 0) {
42 return undef;
43 } else {
44 return $list->[0]->[0];
45 }
46 }
47
48 sub _add_project($$) {
49 my ($self, $project) = @_;
50 $self->{st_add_project} ||= $self->{dbh}->prepare('INSERT INTO projects (name) values(?)');
51
52 my $project_id = $self->_find_project($project);
53 if (! $project_id) {
54 $self->{st_add_project}->execute($project);
55 $project_id = $self->_find_project($project);
56 }
57
58 return $project_id;
59 }
60
61 sub _find_project($$) {
62 my ($self, $project) = @_;
63 return $self->_find_row_id('SELECT id from projects where name = ?', $project);
64 }
65
66 sub _add_developer($$) {
67 my ($self, $job) = @_;
68
69 my $metadata = $job->metadata_hashref();
70 my $name = $metadata->{author_name};
71 my $email = $metadata->{author_email};
72
73 # FIXME unstested
74 if (!$name || !$email) {
75 return undef;
76 }
77
78 $self->{st_add_developer} ||= $self->{dbh}->prepare('INSERT INTO developers (name,email) VALUES (?,?)');
79 my $developer_id = $self->_find_developer($name, $email);
80 if (! $developer_id) {
81 $self->{st_add_developer}->execute($name, $email);
82 $developer_id = $self->_find_developer($name, $email);
83 }
84
85 return $developer_id;
86 }
87
88 sub _find_developer($$$) {
89 my ($self, $name, $email) = @_;
90 return $self->_find_row_id('SELECT id FROM developers WHERE name = ? AND email = ?', $name, $email);
91 }
92
93 sub _add_commit($$$$) {
94 my ($self, $job, $project_id, $developer_id) = @_;
95 unless ($self->_find_row_id('SELECT id FROM commits where id = ?', $job->id)) {
96 my $metadata = $job->metadata_hashref;
97 my $previous_commit_id = $metadata->{previous_commit_id};
98 my $date = $metadata->{author_date};
99
100 my ($summary, $details) = ({}, {});
101 if ($job->metrics) {
102 ($summary, $details) = $job->metrics->data();
103 }
104 my ($metric_placeholders_sql, $metric_column_names_sql, @metric_values) = _metrics_sql($summary, _project_metric_columns());
105
106 $self->{st_insert_commit} ||= $self->{dbh}->prepare("INSERT INTO commits (id, project_id,developer_id,previous_commit_id,date,$metric_column_names_sql) VALUES(?,?,?,?,?,$metric_placeholders_sql)");
107 $self->{st_insert_commit}->execute($job->id, $project_id, $developer_id, $previous_commit_id, $date, @metric_values);
108 }
109 return $job->id;
110 }
111
112 sub _add_modules($$$$) {
113 my ($self, $job, $commit_id, $project_id) = @_;
114 my $metadata = $job->metadata_hashref();
115
116 my %module_versions = ();
117 if ($metadata->{files}) {
118 for my $file (keys(%{$metadata->{files}})) {
119 for my $module ($job->model->module_by_file($file)) {
120 unless($module_versions{$module}) {
121 my $module_id = $self->_add_module($module, $project_id);
122
123 my $module_files = $job->model->files($module);
124 my @file_ids = map { $metadata->{files}->{$_} } sort(@$module_files);
125
126 my $module_version_id = _module_version_id(@file_ids);
127
128 my $metrics = $job->metrics->metrics_for($module);
129
130 $self->_add_module_version($commit_id, $module_id, $module_version_id, %$metrics);
131 $module_versions{$module} = $module_version_id;
132 }
133 }
134 }
135 }
136
137 if ($metadata->{changed_files}) {
138 my %already_marked = ();
139 for my $file (keys(%{$metadata->{changed_files}})) {
140 for my $module ($job->model->module_by_file($file)) {
141 next if $already_marked{$module};
142 $already_marked{$module} = 1;
143
144 my $module_version_id = $module_versions{$module};
145
146 my $module_files = $job->model->files($module);
147 my @statuses = map { $metadata->{changed_files}->{$_} || 'K' } @$module_files;
148 my $statuses = join('', @statuses);
149
150 if ($statuses =~ /^A+$/) {
151 $self->_mark_as_added($commit_id, $module_version_id);
152 } elsif ($statuses !~ /^D+$/) {
153 $self->_mark_as_modified($commit_id, $module_version_id);
154 }
155 }
156 }
157 }
158 }
159
160 sub _add_module($$$) {
161 my ($self, $module, $project_id) = @_;
162
163 my $module_id = $self->_find_module($module, $project_id);
164 if (!$module_id) {
165 $self->{st_add_module} ||= $self->{dbh}->prepare('INSERT INTO modules (name, project_id) values (?,?)');
166 $self->{st_add_module}->execute($module, $project_id);
167 $module_id = $self->_find_module($module, $project_id);
168 }
169
170 return $module_id;
171 }
172
173 sub _find_module($$$) {
174 my ($self, $module, $project_id) = @_;
175 return $self->_find_row_id('SELECT id FROM modules WHERE name = ? AND project_id = ?', $module, $project_id);
176 }
177
178 sub _module_version_id {
179 my @file_ids = @_;
180 my $module_version_id;
181 if (scalar(@file_ids) == 1) {
182 $module_version_id = $file_ids[0];
183 } else {
184 $module_version_id = sha1_hex(join('', @file_ids));
185 }
186 return $module_version_id;
187 }
188
189 sub _add_module_version {
190 my ($self, $commit_id, $module_id, $module_version_id, %metrics) = @_;
191 my $module_version_already_exists = $self->_find_row_id('SELECT id from module_versions WHERE module_id = ? AND id = ?', $module_id, $module_version_id);
192 unless ($module_version_already_exists) {
193
194 my ($metric_placeholders_sql, $metric_column_names_sql, @metric_values) = _metrics_sql(\%metrics, _module_metric_columns());
195
196 my $statement = "INSERT INTO module_versions (id,module_id,$metric_column_names_sql) VALUES (?,?,$metric_placeholders_sql)";
197 $self->{st_add_module_version} ||= $self->{dbh}->prepare($statement);
198 $self->{st_add_module_version}->execute($module_version_id, $module_id, @metric_values);
199 }
200
201 $self->{st_link_commit_and_module_version} ||= $self->{dbh}->prepare('INSERT INTO commits_module_versions (commit_id,module_version_id) VALUES (?,?)');
202 $self->{st_link_commit_and_module_version}->execute($commit_id, $module_version_id);
203
204 return $module_version_id;
205 }
206
207 sub _mark_as_added($$$) {
208 my ($self, $commit_id, $module_version_id) = @_;
209 $self->{st_mark_as_added} ||= $self->{dbh}->prepare('UPDATE commits_module_versions SET added = 1 WHERE commit_id = ? AND module_version_id = ?');
210 $self->{st_mark_as_added}->execute($commit_id, $module_version_id);
211 }
212
213 sub _mark_as_modified($$$) {
214 my ($self, $commit_id, $module_version_id) = @_;
215 $self->{st_mark_as_modified} ||= $self->{dbh}->prepare('UPDATE commits_module_versions SET modified = 1 WHERE commit_id = ? AND module_version_id = ?');
216 $self->{st_mark_as_modified}->execute($commit_id, $module_version_id);
217 }
218
219 sub _metrics_sql($@) {
220 my ($metrics_hash, @metric_columns) = @_;
221 my $metric_placeholders_sql = join(',', map { '?' } @metric_columns);
222 my $metric_column_names_sql = join(',', @metric_columns);
223 my @metric_values = map { $metrics_hash->{$_} } @metric_columns;
224 return ($metric_placeholders_sql, $metric_column_names_sql, @metric_values)
225 }
226
227 # Initializes the database
228 #
229 # TODO the current approach of feeding DDL SQL statements directly to the
230 # database handle might not be good enough, since the SQL being written might
231 # not be portable to other databases than SQLite. It would be nice to have
232 # something similar to the rails migrations DSL here.
233 sub initialize($) {
234 my ($self) = @_;
235 $self->{dbh} = DBI->connect($self->database, undef, undef, { RaiseError => 1, InactiveDestroy => 1});
236 # assume that if there is a table called `analizo_metadata`, then the database was already initialized
237 if (!grep { $_ =~ /analizo_metadata/ } $self->{dbh}->tables()) {
238 for my $statement (ddl_statements()) {
239 $statement =~ s/\@\@MODULE_METRICS\@\@/_metric_columns_ddl(_module_metric_columns())/egm;
240 $statement =~ s/\@\@PROJECT_METRICS\@\@/_metric_columns_ddl(_project_metric_columns())/egm;
241 $statement =~ s/\@\@NUMERIC_AUTOINC_PK\@\@/_numeric_autoinc_pk($self->database)/egm;
242 $self->{dbh}->do($statement);
243 }
244 }
245 }
246
247 my $DDL_INITIALIZED = 0;
248 my @DDL_STATEMENTS = ();
249 sub ddl_statements($) {
250 if (!$DDL_INITIALIZED) {
251 my $sql = '';
252 while (my $line = <DATA>) {
253 $sql .= $line;
254 if ($line =~ /;\s*$/) {
255 # SQL statement is ready
256 CORE::push @DDL_STATEMENTS, $sql;
257 $sql = '';
258 }
259 }
260 $DDL_INITIALIZED = 1;
261 }
262 return @DDL_STATEMENTS;
263 }
264
265 use Analizo::Metrics;
266 use Analizo::Model;
267 my $__metric_columns = undef;
268 sub _metric_columns() {
269 if (!$__metric_columns) {
270 my $metrics = Analizo::Metrics->new(model => Analizo::Model->new);
271
272 my %module_metrics = $metrics->list_of_metrics();
273 my @module_metrics= keys(%module_metrics);
274
275 my %project_metrics = $metrics->list_of_global_metrics();
276 my @project_metrics = keys(%project_metrics);
277
278 $__metric_columns = {
279 module => \@module_metrics,
280 project => \@project_metrics,
281 };
282 }
283 return $__metric_columns;
284 }
285
286 sub _module_metric_columns() {
287 return @{ _metric_columns()->{module} };
288 }
289
290 sub _project_metric_columns() {
291 return @{ _metric_columns()->{project} };
292 }
293
294 sub _metric_columns_ddl {
295 my @columns = @_;
296 my $ddl = join(",\n ", map { "$_ REAL"} @columns);
297 return $ddl;
298 }
299
300 my %NUMERIC_AUTOINC_PK = (
301 'sqlite' => 'INTEGER PRIMARY KEY AUTOINCREMENT',
302 'pg' => 'SERIAL PRIMARY KEY',
303 );
304
305 sub _numeric_autoinc_pk($) {
306 my ($database) = @_;
307 my $dbtype = lc($database);
308 $dbtype =~ s/^dbi:(.*):.*/$1/;
309 my $sql = $NUMERIC_AUTOINC_PK{$dbtype};
310 die("Database $dbtype is not supported!") if (!defined($sql));
311 return $sql;
312 }
313
314 1;
315
316 __DATA__
317
318 /* Analizo DB output schema definition */
319
320 CREATE TABLE projects (
321 id @@NUMERIC_AUTOINC_PK@@,
322 name CHAR(250)
323 );
324
325 CREATE TABLE developers (
326 id @@NUMERIC_AUTOINC_PK@@,
327 name CHAR(250),
328 email CHAR(250)
329 );
330
331 CREATE TABLE commits (
332 id CHAR(40) PRIMARY KEY,
333 previous_commit_id CHAR(40),
334 project_id INTEGER,
335 date INTEGER,
336 developer_id INTEGER,
337 @@PROJECT_METRICS@@
338 );
339 CREATE INDEX commits_project_id ON commits (project_id);
340
341 CREATE TABLE modules (
342 id @@NUMERIC_AUTOINC_PK@@,
343 project_id INTEGER,
344 name CHAR(250)
345 );
346 CREATE UNIQUE INDEX modules_project_id_name ON modules(project_id, name);
347
348 CREATE TABLE module_versions (
349 id CHAR(40),
350 module_id INTEGER,
351 @@MODULE_METRICS@@,
352 PRIMARY KEY (id, module_id)
353 );
354
355 CREATE TABLE commits_module_versions (
356 id @@NUMERIC_AUTOINC_PK@@,
357 commit_id CHAR(40),
358 module_version_id CHAR(40),
359 modified INTEGER default 0,
360 added INTEGER default 0
361 );
362 CREATE INDEX commits_module_versions_commit_id ON commits_module_versions (commit_id);
363
364 CREATE TABLE analizo_metadata (
365 key CHAR(100),
366 value CHAR(100)
367 );