2019-05-08 08:46:58 +02:00
|
|
|
#!/usr/bin/env perl
|
|
|
|
# =======================================================================================
|
|
|
|
#
|
|
|
|
# Author: Jan Eitzinger (je), jan.eitzinger@fau.de
|
|
|
|
# Copyright (c) 2019 RRZE, University Erlangen-Nuremberg
|
|
|
|
#
|
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
# of this software and associated documentation files (the "Software"), to deal
|
|
|
|
# in the Software without restriction, including without limitation the rights
|
|
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
# copies of the Software, and to permit persons to whom the Software is
|
|
|
|
# furnished to do so, subject to the following conditions:
|
|
|
|
#
|
|
|
|
# The above copyright notice and this permission notice shall be included in all
|
|
|
|
# copies or substantial portions of the Software.
|
|
|
|
#
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
# SOFTWARE.
|
|
|
|
#
|
|
|
|
# =======================================================================================
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
use utf8;
|
|
|
|
|
|
|
|
use File::Slurp;
|
|
|
|
use Data::Dumper;
|
|
|
|
use JSON::MaybeXS qw(encode_json decode_json);
|
|
|
|
use DBI;
|
|
|
|
|
|
|
|
my $database = $ARGV[0];
|
|
|
|
my $basedir = $ARGV[1];
|
|
|
|
|
|
|
|
my %attr = (
|
|
|
|
PrintError => 1,
|
|
|
|
RaiseError => 1
|
|
|
|
);
|
|
|
|
|
|
|
|
my $dbh = DBI->connect(
|
|
|
|
"DBI:SQLite:dbname=$database", "", "", \%attr)
|
|
|
|
or die "Could not connect to database: $DBI::errstr";
|
|
|
|
|
|
|
|
my $sth_select_all = $dbh->prepare(qq{
|
|
|
|
SELECT id, user_id, project_id
|
|
|
|
FROM job;
|
|
|
|
});
|
|
|
|
|
|
|
|
my $sth_update_job = $dbh->prepare(qq{
|
|
|
|
UPDATE job
|
|
|
|
SET user_id = ?,
|
2019-05-09 11:40:47 +02:00
|
|
|
project_id = ?
|
2019-05-08 08:46:58 +02:00
|
|
|
WHERE id=?;
|
|
|
|
});
|
|
|
|
|
|
|
|
my $user_index = 0; my $project_index = 0;
|
|
|
|
my %user_lookup; my %project_lookup;
|
2019-05-09 11:40:47 +02:00
|
|
|
my %user_group;
|
2019-05-08 08:46:58 +02:00
|
|
|
my %row;
|
2019-05-09 11:40:47 +02:00
|
|
|
|
|
|
|
# build lookups
|
|
|
|
$sth_select_all->execute;
|
|
|
|
$sth_select_all->bind_columns( \( @row{ @{$sth_select_all->{NAME_lc} } } ));
|
2019-05-08 08:46:58 +02:00
|
|
|
|
|
|
|
while ($sth_select_all->fetch) {
|
2019-05-09 11:40:47 +02:00
|
|
|
my $user_id = $row{'user_id'};
|
|
|
|
my $project_id = $row{'project_id'};
|
2019-05-09 07:18:28 +02:00
|
|
|
|
|
|
|
if ( not exists $user_lookup{$user_id}) {
|
|
|
|
$user_index++;
|
|
|
|
$user_lookup{$user_id} = $user_index;
|
2019-05-09 11:40:47 +02:00
|
|
|
$user_group{$user_id} = $project_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( not exists $project_lookup{$project_id}) {
|
|
|
|
$project_index++;
|
|
|
|
$project_lookup{$project_id} = $project_index;
|
2019-05-09 07:18:28 +02:00
|
|
|
}
|
|
|
|
}
|
2019-05-08 08:46:58 +02:00
|
|
|
|
2019-05-09 11:40:47 +02:00
|
|
|
write_file("user-conversion.json", encode_json \%user_lookup);
|
|
|
|
write_file("project-conversion.json", encode_json \%project_lookup);
|
|
|
|
print "$user_index total users\n";
|
|
|
|
print "$project_index total projects\n";
|
|
|
|
|
|
|
|
# convert database
|
|
|
|
$sth_select_all->execute;
|
|
|
|
$sth_select_all->bind_columns( \( @row{ @{$sth_select_all->{NAME_lc} } } ));
|
|
|
|
|
|
|
|
while ($sth_select_all->fetch) {
|
|
|
|
my $user_id = 'user_'.$user_lookup{$row{'user_id'}};
|
|
|
|
my $project_id = 'project_'.$project_lookup{$row{'project_id'}};
|
|
|
|
|
|
|
|
# print "$row{'id'}: $user_id - $project_id\n";
|
|
|
|
|
|
|
|
$sth_update_job->execute(
|
|
|
|
$user_id,
|
|
|
|
$project_id,
|
|
|
|
$row{'id'}
|
|
|
|
);
|
|
|
|
}
|
2019-05-08 08:46:58 +02:00
|
|
|
|
2019-05-09 11:40:47 +02:00
|
|
|
# convert job meta file
|
2019-05-08 08:46:58 +02:00
|
|
|
opendir my $dh, $basedir or die "can't open directory: $!";
|
|
|
|
while ( readdir $dh ) {
|
|
|
|
chomp;
|
|
|
|
next if $_ eq '.' or $_ eq '..';
|
|
|
|
|
|
|
|
my $jobID = $_;
|
|
|
|
my $jobmeta_json = read_file("$basedir/$jobID/meta.json");
|
|
|
|
my $job = decode_json $jobmeta_json;
|
2019-05-09 11:40:47 +02:00
|
|
|
|
|
|
|
my $user = $job->{'user_id'};
|
|
|
|
my $project;
|
|
|
|
|
|
|
|
if ( exists $user_lookup{$user}) {
|
|
|
|
$project = $user_group{$user};
|
|
|
|
$user = 'user_'.$user_lookup{$user};
|
2019-05-08 08:46:58 +02:00
|
|
|
} else {
|
2019-05-09 11:40:47 +02:00
|
|
|
die "$user not in lookup hash!\n";
|
2019-05-08 08:46:58 +02:00
|
|
|
}
|
2019-05-09 11:40:47 +02:00
|
|
|
|
|
|
|
if ( exists $project_lookup{$project}) {
|
|
|
|
$project = 'project_'.$project_lookup{$project};
|
|
|
|
} else {
|
|
|
|
die "$project not in lookup hash!\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
$job->{user_id} = $user;
|
|
|
|
$job->{project_id} = $project;
|
|
|
|
$jobmeta_json = encode_json $job;
|
|
|
|
# print "$jobmeta_json\n";
|
|
|
|
write_file("$basedir/$jobID/meta.json", $jobmeta_json);
|
2019-05-08 08:46:58 +02:00
|
|
|
}
|
|
|
|
closedir $dh or die "can't close directory: $!";
|
|
|
|
|
2019-05-09 11:40:47 +02:00
|
|
|
$dbh->disconnect;
|