Permalink
Browse files

Improve quote daemon to run less frequently, update when new data ava…

…ilable for today
  • Loading branch information...
1 parent 6fad4de commit e5a6516edcc9415d336f7d6c621cff881e3f3eee @ajablonski committed Nov 17, 2012
Showing with 70 additions and 47 deletions.
  1. +24 −5 quote.pl
  2. +1 −1 quote_daemon.c
  3. +45 −41 sql.pl
View
@@ -8,7 +8,6 @@
use Date::Format;
require "sql.pl";
-close STDOUT;
open(ERRORLOG, ">>", "quote.errlog") or die "CANNOT OPEN FILE";
my $sqlString = "";
@@ -28,6 +27,11 @@
$insertSQLstring .= "INSERT INTO $netID.newstocksdaily (symbol, timestamp, high, low, close, open, volume)";
$insertSQLstring .= " VALUES(?, ?, ?, ?, ?, ?, ?)";
+my $updateSQLstring = "";
+$updateSQLstring .= "UPDATE $netID.newstocksdaily ";
+$updateSQLstring .= " SET high=?, low=?, open=?, close=?, volume=? ";
+$updateSQLstring .= " WHERE symbol=? AND timestamp=?";
+
if ($#ARGV>=0) {
@symbols=@ARGV;
} else {
@@ -45,15 +49,30 @@
$symbol =~ s/\s+$//;
if (!defined($quotes{$symbol,"success"})) {
} else {
+# eval {
+# ExecSQL($dbuser, $dbpasswd, $insertSQLstring, undef,
+# $symbol,
+# str2time($quotes{$symbol, "date"}),
+# $quotes{$symbol, "high"},
+# $quotes{$symbol, "low"},
+# $quotes{$symbol, "open"},
+# $quotes{$symbol, "close"},
+# $quotes{$symbol, "volume"}
+# );
+# };
+# $error = $@;
+#
+# print ERRORLOG time2str('%c', time), $error, "\n" if $error;
+
eval {
- ExecSQL($dbuser, $dbpasswd, $insertSQLstring, undef,
- $symbol,
- str2time($quotes{$symbol, "date"}),
+ ExecSQL($dbuser, $dbpasswd, $updateSQLstring, undef,
$quotes{$symbol, "high"},
$quotes{$symbol, "low"},
$quotes{$symbol, "open"},
$quotes{$symbol, "close"},
- $quotes{$symbol, "volume"}
+ $quotes{$symbol, "volume"},
+ $symbol,
+ str2time($quotes{$symbol, "date"})
);
};
$error = $@;
View
@@ -37,7 +37,7 @@ int main()
close(STDERR_FILENO);
while (1) {
system("./quote.pl");
- sleep(60);
+ sleep(900); // Update every 15 minutes
}
exit(EXIT_SUCCESS);
View
86 sql.pl
@@ -20,49 +20,53 @@ sub SQLdebug {
}
sub ExecSQL {
- my ($user, $passwd, $querystring, $type, @fill) =@_;
- my $dbh = DBI->connect("DBI:Oracle:",$user,$passwd);
- if (not $dbh) {
- # if the connect failed, record the reason and then die.
- die "Can't connect to database because of ".$DBI::errstr;
- }
- my $sth = $dbh->prepare($querystring);
- if (not $sth) {
- # If prepare failed, then record reason to sqloutput and then die
- my $errstr="Can't prepare $querystring because of ".$DBI::errstr;
- $dbh->disconnect();
- die $errstr;
- }
- if (not $sth->execute(@fill)) {
- # if exec failed, record to sqlout and die.
- my $errstr="Can't execute $querystring with fill (".join(",",map {"'$_'"} @fill).") because of ".$DBI::errstr;
- $dbh->disconnect();
- die $errstr;
- }
- # The rest assumes that the data will be forthcoming.
- my @data;
- if (defined $type and $type eq "ROW") {
- @data=$sth->fetchrow_array();
- $sth->finish();
- if ($debug) {push @sqloutput, MakeTable("debug_sqloutput","ROW",undef,@data);}
- $dbh->disconnect();
- return @data;
- }
- my @ret;
- while (@data=$sth->fetchrow_array()) {
- push @ret, [@data];
- }
- if (defined $type and $type eq "COL") {
- @data = map {$_->[0]} @ret;
+ my ($user, $passwd, $querystring, $type, @fill) =@_;
+ my $dbh = DBI->connect("DBI:Oracle:",$user,$passwd);
+ if (not $dbh) {
+# if the connect failed, record the reason and then die.
+ die "Can't connect to database because of ".$DBI::errstr;
+ }
+ my $sth = $dbh->prepare($querystring);
+ if (not $sth) {
+# If prepare failed, then record reason to sqloutput and then die
+ my $errstr="Can't prepare $querystring because of ".$DBI::errstr;
+ $dbh->disconnect();
+ die $errstr;
+ }
+ if (not $sth->execute(@fill)) {
+# if exec failed, record to sqlout and die.
+ my $errstr="Can't execute $querystring with fill (".join(",",map {"'$_'"} @fill).") because of ".$DBI::errstr;
+ $dbh->disconnect();
+ die $errstr;
+ }
+# The rest assumes that the data will be forthcoming.
+ my @data;
+ if (defined $type and $type eq "ROW") {
+ @data=$sth->fetchrow_array();
+ $sth->finish();
+ if ($debug) {push @sqloutput, MakeTable("debug_sqloutput","ROW",undef,@data);}
+ $dbh->disconnect();
+ SQLdebug("Row", @data);
+ return @data;
+ }
+ my @ret;
+ while (@data=$sth->fetchrow_array()) {
+ SQLdebug("undef", @data);
+ push @ret, [@data];
+ }
+ if (defined $type and $type eq "COL") {
+ @data = map {$_->[0]} @ret;
+ $sth->finish();
+ if ($debug) {push @sqloutput, MakeTable("debug_sqloutput","COL",undef,@data);}
+ $dbh->disconnect();
+ SQLdebug("COL", @data);
+ return @data;
+ }
$sth->finish();
- if ($debug) {push @sqloutput, MakeTable("debug_sqloutput","COL",undef,@data);}
+ if ($debug) {push @sqloutput, MakeTable("debug_sql_output","2D",undef,@ret);}
$dbh->disconnect();
- return @data;
- }
- $sth->finish();
- if ($debug) {push @sqloutput, MakeTable("debug_sql_output","2D",undef,@ret);}
- $dbh->disconnect();
- return @ret;
+ SQLdebug(@ret);
+ return @ret;
}
sub BuySellStock {

0 comments on commit e5a6516

Please sign in to comment.