Skip to content

Commit

Permalink
Added a method in DBConnection that generates the SQL to compute the …
Browse files Browse the repository at this point in the history
…number of seconds between two timestamps
  • Loading branch information
muffato committed Sep 5, 2018
1 parent 9bc9704 commit 8420a24
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 17 deletions.
7 changes: 1 addition & 6 deletions modules/Bio/EnsEMBL/Hive/DBSQL/AnalysisStatsAdaptor.pm
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,8 @@ sub default_table_name {

sub default_input_column_mapping {
my $self = shift @_;
my $driver = $self->dbc->driver();
return {
'when_updated' => {
'mysql' => "UNIX_TIMESTAMP()-UNIX_TIMESTAMP(when_updated) seconds_since_when_updated ",
'sqlite' => "strftime('%s','now')-strftime('%s',when_updated) seconds_since_when_updated ",
'pgsql' => "EXTRACT(EPOCH FROM CURRENT_TIMESTAMP - when_updated) seconds_since_when_updated ",
}->{$driver},
'when_updated' => $self->dbc->_interval_seconds_sql('when_updated') . ' seconds_since_when_updated',
};
}

Expand Down
33 changes: 33 additions & 0 deletions modules/Bio/EnsEMBL/Hive/DBSQL/DBConnection.pm
Original file line number Diff line number Diff line change
Expand Up @@ -407,5 +407,38 @@ sub requires_write_access {
}


=head2 _interval_seconds_sql
Argument[1] : String. Name of the column for the start of the interval
Argument[2] : String, optional. Name of the column for the end of the interval
Example : $self->dbc->_interval_seconds_sql();
Description : Returns an SQL expression to compute the number of seconds betwen both columns.
If the second column name is missing, compute the number of seconds until now instead.
Returntype : String
Exceptions : none
Caller : general
Status : Stable
=cut

sub _interval_seconds_sql {
my ($self, $column_from, $column_to) = @_;

my $driver = $self->driver();

$column_to ||= {
'mysql' => '',
'sqlite' => q{'now'},
'pgsql' => 'CURRENT_TIMESTAMP',
}->{$driver};

return {
'mysql' => "UNIX_TIMESTAMP($column_to)-UNIX_TIMESTAMP($column_from)",
'sqlite' => "strftime('%s',$column_to)-strftime('%s',$column_from)",
'pgsql' => "EXTRACT(EPOCH FROM $column_to - $column_from)",
}->{$driver};
}


1;

13 changes: 2 additions & 11 deletions modules/Bio/EnsEMBL/Hive/Queen.pm
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,8 @@ sub default_table_name {

sub default_input_column_mapping {
my $self = shift @_;
my $driver = $self->dbc->driver();
return {
'when_submitted' => {
'mysql' => "UNIX_TIMESTAMP()-UNIX_TIMESTAMP(when_submitted) seconds_since_when_submitted ",
'sqlite' => "strftime('%s','now')-strftime('%s',when_submitted) seconds_since_when_submitted ",
'pgsql' => "EXTRACT(EPOCH FROM CURRENT_TIMESTAMP - when_submitted) seconds_since_when_submitted ",
}->{$driver},
'when_submitted' => $self->dbc->_interval_seconds_sql('when_submitted') . ' seconds_since_when_submitted',
};
}

Expand Down Expand Up @@ -661,11 +656,7 @@ sub fetch_overdue_workers {

$overdue_secs = 3600 unless(defined($overdue_secs));

my $constraint = "status!='DEAD' AND (when_checked_in IS NULL OR ".{
'mysql' => "(UNIX_TIMESTAMP()-UNIX_TIMESTAMP(when_checked_in)) > $overdue_secs",
'sqlite' => "(strftime('%s','now')-strftime('%s',when_checked_in)) > $overdue_secs",
'pgsql' => "EXTRACT(EPOCH FROM CURRENT_TIMESTAMP - when_checked_in) > $overdue_secs",
}->{ $self->dbc->driver }.' )';
my $constraint = "status!='DEAD' AND (when_checked_in IS NULL OR ". $self->dbc->_interval_seconds_sql('when_checked_in') . " > $overdue_secs)";

return $self->fetch_all( $constraint );
}
Expand Down

0 comments on commit 8420a24

Please sign in to comment.