db_connect.pl 3 KB
Newer Older
Miguel Pignatelli's avatar
Miguel Pignatelli committed
1 2 3 4
#!/usr/bin/env perl

=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
Miguel Pignatelli's avatar
Miguel Pignatelli committed
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

 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


use strict;
use warnings;

use Bio::EnsEMBL::Hive::Utils::Graph;
use Bio::EnsEMBL::Hive::DBSQL::DBAdaptor;
use Bio::EnsEMBL::Hive::DBSQL::SqlSchemaAdaptor;

use JSON;
use HTML::Template;

use lib ("./lib");
use msg;
use version_check;

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');
Miguel Pignatelli's avatar
Miguel Pignatelli committed
40 41

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

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

my $response = msg->new();

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

$dbConn->load_collections();
Miguel Pignatelli's avatar
Miguel Pignatelli committed
52 53

eval {
54
    my $graph = formAnalyses($dbConn);
55 56 57 58 59 60 61
    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");
    }
Miguel Pignatelli's avatar
Miguel Pignatelli committed
62 63
};
if ($@) {
64
    $response->err_msg("I have problems retrieving data from the database:$@");
Miguel Pignatelli's avatar
Miguel Pignatelli committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78
    $response->status("FAILED");
}

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;
79
    $info->{hive_db_version} = get_hive_db_meta_key($dbConn, 'hive_sql_schema_version');
Miguel Pignatelli's avatar
Miguel Pignatelli committed
80
    $info->{hive_code_version} = get_hive_code_version();
81 82 83
    $info->{pipeline_name} = get_hive_db_meta_key($dbConn, 'hive_pipeline_name');
    $info->{hive_auto_rebalance_semaphores} = get_hive_db_meta_key($dbConn, 'hive_auto_rebalance_semaphores') ? 'Enabled' : 'Disabled';
    $info->{hive_use_param_stack} = get_hive_db_meta_key($dbConn, 'hive_use_param_stack') ? 'Enabled' : 'Disabled';
Miguel Pignatelli's avatar
Miguel Pignatelli committed
84 85 86 87 88 89 90 91

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

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

    return $graphviz->as_svg;
}