db_connect.pl 2.84 KB
Newer Older
emepyc's avatar
emepyc committed
1 2
#!/usr/bin/env perl

3 4
=pod

5
 Copyright [1999-2015] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
nwillhoft's avatar
nwillhoft committed
6
 Copyright [2016-2021] EMBL-European Bioinformatics Institute
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.


=cut


emepyc's avatar
emepyc committed
24 25 26
use strict;
use warnings;

27 28 29 30
use Bio::EnsEMBL::Hive::Utils::Graph;
use Bio::EnsEMBL::Hive::DBSQL::DBAdaptor;
use Bio::EnsEMBL::Hive::DBSQL::SqlSchemaAdaptor;

emepyc's avatar
emepyc committed
31 32 33 34
use JSON;
use HTML::Template;

use lib ("./lib");
35 36
use msg;
use version_check;
emepyc's avatar
emepyc committed
37 38

my $json_url = shift @ARGV || '{"version":["53"],"url":["mysql://ensro@127.0.0.1:2912/mp12_compara_nctrees_74clean2"]}';
39
my @hive_config_files = ($ENV{GUIHIVE_BASEDIR}.'/config/hive_config.json', $ENV{EHIVE_ROOT_DIR}.'/hive_config.json');
emepyc's avatar
emepyc committed
40 41

# Input data
42
my $decoded_json = decode_json($json_url);
emepyc's avatar
emepyc committed
43 44

# Set up @INC and paths for static content
45
my $connection_template = $ENV{GUIHIVE_BASEDIR} . "static/connection_details.html";
emepyc's avatar
emepyc committed
46 47 48

my $response = msg->new();

49
my $dbConn = check_db_versions_match($decoded_json);
50

51 52
eval {
    my $graph = formAnalyses($dbConn);
53 54 55 56 57 58 59
    if ($graph) {
        my $html = formResponse($dbConn);
        $response->out_msg({"graph" => $graph, "html" => $html});
    } else {
        $response->err_msg('GraphViz failed to generate a diagram');
        $response->status("FAILED");
    }
60 61 62 63 64
};
if ($@) {
    $response->err_msg("I have problems retrieving data from the database:$@");
    $response->status("FAILED");
}
emepyc's avatar
emepyc committed
65 66 67 68 69 70 71 72 73 74 75 76

print $response->toJSON();

sub formResponse {
    my ($dbConn) = @_;
    my $info;

    $info->{db_name}   = $dbConn->dbc->dbname;
    $info->{host}      = $dbConn->dbc->host;
    $info->{port}      = $dbConn->dbc->port;
    $info->{driver}    = $dbConn->dbc->driver;
    $info->{username}  = $dbConn->dbc->username;
77
    $info->{hive_db_version} = get_hive_db_meta_key($dbConn, 'hive_sql_schema_version');
emepyc's avatar
emepyc committed
78
    $info->{hive_code_version} = get_hive_code_version();
79 80
    $info->{pipeline_name} = get_hive_db_meta_key($dbConn, 'hive_pipeline_name');
    $info->{hive_use_param_stack} = get_hive_db_meta_key($dbConn, 'hive_use_param_stack') ? 'Enabled' : 'Disabled';
emepyc's avatar
emepyc committed
81 82 83 84 85 86 87 88

    my $template = HTML::Template->new(filename => $connection_template);
    $template->param(%$info);
    return $template->output();
}

sub formAnalyses {
    my ($dbConn) = @_;
89
    my $graph = Bio::EnsEMBL::Hive::Utils::Graph->new($dbConn, @hive_config_files);
emepyc's avatar
emepyc committed
90 91 92 93 94 95
    my $graphviz = $graph->build();

    return $graphviz->as_svg;
}