Permalink
Browse files

Add historical data/graph on stock holding page.

  • Loading branch information...
1 parent a90852b commit 95a0ed0fb7d0dd8e0fa66019d1e4b9805dd01af8 @ajablonski committed Nov 15, 2012
Showing with 314 additions and 1 deletion.
  1. BIN nodata.png
  2. +115 −0 plot_stock.pl
  3. +35 −1 stock.pl
  4. +164 −0 stock_data_access.pm
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
@@ -0,0 +1,115 @@
+#!/usr/bin/perl -w
+
+
+use strict;
+use CGI qw(:standard);
+use DBI;
+use Time::ParseDate;
+use Date::Parse;
+use Date::Format;
+use user;
+use File::Copy;
+
+BEGIN {
+ $ENV{PORTF_DBMS}="oracle";
+ $ENV{PORTF_DB}="cs339";
+ $ENV{PORTF_DBUSER}="amj650";
+ $ENV{PORTF_DBPASS}="z40wkjgIK";
+
+ unless ($ENV{BEGIN_BLOCK}) {
+ use Cwd;
+ $ENV{ORACLE_BASE}="/raid/oracle11g/app/oracle/product/11.2.0.1.0";
+ $ENV{ORACLE_HOME}=$ENV{ORACLE_BASE}."/db_1";
+ $ENV{ORACLE_SID}="CS339";
+ $ENV{LD_LIBRARY_PATH}=$ENV{ORACLE_HOME}."/lib";
+ $ENV{BEGIN_BLOCK} = 1;
+ $ENV{GDFONTPATH}="/usr/share/fonts/liberation";
+ $ENV{GNUPLOT_DEFAULT_GDFONT}="LiberationSans-Regular";
+ exec 'env',cwd().'/'.$0,@ARGV;
+ }
+};
+
+use stock_data_access;
+
+my $type = param('type');
+my $symbol = param('symbol');
+my $start = param('start');
+my $end = param('end');
+
+if (defined(param('start'))) {
+ $start = str2time(param('start'));
+} else {
+ $start = 0;
+}
+
+if (defined(param('end'))) {
+ $end = str2time(param('end'));
+} else {
+ $end = time;
+}
+
+if (!defined($type)) {
+ $type = 'plot';
+}
+
+my $sqlString = "";
+$sqlString .= "SELECT timestamp, close FROM ".GetStockPrefix()."StocksDaily ";
+$sqlString .= " WHERE symbol=rpad(:1,16) AND timestamp BETWEEN :2 AND :3";
+$sqlString .= " UNION ";
+$sqlString .= "SELECT timestamp, close FROM $netID.newstocksdaily ";
+$sqlString .= " WHERE symbol=rpad(:1,16) AND timestamp BETWEEN :2 AND :3";
+my @rows;
+eval {
+ @rows = ExecStockSQL("2D",$sqlString,$symbol, $start, $end);
+};
+my $error = $@;
+
+if ($type eq "plot") {
+ print header(-type => 'image/png', -expires => '-1h' );
+ if (@rows != 0) {
+# This is how to drive gnuplot to produce a plot
+# The basic idea is that we are going to send it commands and data
+# at stdin, and it will print the graph for us to stdout
+#
+#
+ open(GNUPLOT,"| gnuplot") or die "Cannot run gnuplot";
+
+ print GNUPLOT "set term png\n"; # we want it to produce a PNG
+ print GNUPLOT "set output\n"; # output the PNG to stdout
+ print GNUPLOT "set xdata time\n";
+ print GNUPLOT "set timefmt \"%s\"\n";
+ print GNUPLOT "set format x \"\%m/\%y\"\n";
+ print GNUPLOT "plot '-' using 1:2 with linespoints\n"; # feed it data to plot
+ foreach my $r (@rows) {
+ print GNUPLOT $r->[0], "\t", $r->[1], "\n";
+ }
+ print GNUPLOT "e\n"; # end of data
+
+#
+# Here gnuplot will print the image content
+#
+
+ close(GNUPLOT);
+ } else {
+ binmode STDOUT;
+ copy "./nodata.png", \*STDOUT;
+ }
+} else {
+ print header(-type => 'text/html', -expires => '-1h' );
+ print start_html;
+ print "<table border=1>";
+ print th(["Date", "Price"]);
+ foreach my $r (@rows) {
+ print Tr(
+ td([
+ time2str("%D", $r->[0]),
+ $r->[1]
+ ]),
+ ), "\n";
+ }
+ print "</table>";
+ print end_html;
+}
+
+
+
View
@@ -7,7 +7,7 @@
use user;
use common;
use DBI;
-
+use Date::Format;
require "sql.pl";
redirectIfNotLoggedIn();
@@ -16,6 +16,17 @@
my $symbol = param("stock");
my $portID = param("portID");
+my $start = param('start');
+my $end = param('end');
+
+if (!$start) {
+ $start = time2str("%Y-%m-%d", 0);
+}
+
+if (!$end) {
+ $end = time2str("%Y-%m-%d", time);
+}
+
print header(-cookies=>\@cookies),
start_html( -title=>"Stock View",
@@ -50,9 +61,32 @@
$error = $@;
print $error if $error;
+print "<div class=\"container\">";
+
print h1("$symbol");
print h2("Number of shares: $stockInfo[0]");
+print img({src=>"plot_stock.pl?symbol=$symbol&type=plot&start=$start&end=$end"});
+
+
+print start_form({-class=>"form-inline"}),
+ hidden(-name=>"type", -value=>"plot", -override=>1),
+ hidden(-name=>"portID", -value=>$portID, -override=>1),
+ hidden(-name=>"stock", -value=>$symbol, -override=>1),
+ "Start date", '<input type="date" name="start">', br,
+ "End date", '<input type="date" name="end">', br,
+ submit,
+ end_form;
+print "</div>";
+
+
+print "<div class=\"sidebar\">";
+print iframe({src=>"plot_stock.pl?symbol=$symbol&type=text&start=$start&end=$end",
+ width=>"250 px", height=>"100%"
+ });
+print "</div>";
+
+
print '<script src="http://twitter.github.com/bootstrap/assets/js/jquery.js" /> </script>', "\n",
'<script src="http://twitter.github.com/bootstrap/assets/js/bootstrap-collapse.js"> </script>', "\n",
'<script src="http://twitter.github.com/bootstrap/assets/js/bootstrap-button.js"> </script>', "\n",
View
@@ -0,0 +1,164 @@
+package stock_data_access;
+
+use Data::Dumper;
+
+require Exporter;
+
+@ISA=qw(Exporter);
+@EXPORT=qw(ExecStockSQL GetStockPrefix);
+
+
+
+my ($dbms,$user,$pass,$db);
+
+use DBI;
+
+sub GetStockPrefix {
+ if ($dbms eq "oracle" || $dbms eq "mysql") {
+ return "$db.";
+ }
+ if ($dbms eq "cassandra") {
+ return "";
+ }
+}
+
+#
+# @list or $string = ExecStockSQL($type, $querystring @fill);
+#
+# Executes a SQL statement given in $querystring
+#
+# $type is "ROW" => returns first row in list
+# $type is "COL" => returns first column in list
+# $type is "2D" or undef => returns list of row lists (list of listrefs)
+# $type is "TEXT" => Returns string output, rows delimited by \n, cols by \t
+#
+# @fill are the fillers for positional parameters in $querystring
+#
+# ExecStockSQL executes "die" on failure.
+#
+sub ExecStockSQL {
+ my ($type, $querystring, @fill) = @_;
+
+ my $dbh;
+
+ if ($dbms eq "oracle") {
+ $dbh = DBI->connect("DBI:Oracle:",$user,$pass);
+ } elsif ($dbms eq "mysql") {
+ $dbh = DBI->connect("DBI:mysql:$db",$user,$pass);
+ } elsif ($dbms eq "cassandra") {
+ return ExecStockCQL(@_);
+ } else {
+ die "Unknown DBMS \"$dbms\"\n";
+ }
+
+ if (not $dbh) {
+ die "Can't connect to database because of ".$DBI::errstr;
+ }
+
+ my $sth = $dbh->prepare($querystring);
+
+ if (not $sth) {
+ my $errstr="Can't prepare $querystring because of ".$DBI::errstr;
+ $dbh->disconnect();
+ die $errstr;
+ }
+
+ if (not $sth->execute(@fill)) {
+ my $errstr="Can't execute $querystring with fill (".join(",",map {"'$_'"} @fill).") because of ".$DBI::errstr;
+ $dbh->disconnect();
+ die $errstr;
+ }
+
+ my @data;
+
+ # One row of output
+ if (defined $type and $type eq "ROW") {
+ @data=$sth->fetchrow_array();
+ $sth->finish();
+ $dbh->disconnect();
+ return @data;
+ }
+
+ my @ret;
+
+ # multirow or single column output or strings
+ while (@data=$sth->fetchrow_array()) {
+ push @ret, [@data];
+ }
+
+ # single column
+ if (defined $type and $type eq "COL") {
+ @data = map {$_->[0]} @ret;
+ $sth->finish();
+ $dbh->disconnect();
+ return @data;
+ }
+
+ $sth->finish();
+ $dbh->disconnect();
+
+ # TEXT
+ if (defined $type and $type eq "TEXT") {
+ return join("\n",
+ map {join("\t",@{$_}) } @ret )."\n";
+ }
+
+ if (!defined $type or $type eq "2D") {
+ return @ret;
+ }
+
+ die "Unknown type \"$type\" requested\n";
+}
+
+
+#
+# @list = ExecStockCQL($type, $querystring);
+#
+# Executes a CQL statement given in $querystring
+#
+# $type is "ROW" => returns first row in list
+# $type is "COL" => returns first column in list
+# $type is "2D" or undef => returns list of row lists (list of listrefs)
+# $type is "TEXT" => Returns output, rows delimited by \n, cols by \t
+#
+# ExecStockCQL executes "die" on failure.
+#
+sub ExecStockCQL {
+ my ($type, $querystring, @fill) = @_;
+
+ die "Cassandra Queuries Not Currently Supported\n";
+}
+
+
+
+BEGIN {
+ $dbms = $ENV{'PORTF_DBMS'};
+ $user = $ENV{'PORTF_DBUSER'};
+ $pass = $ENV{'PORTF_DBPASS'};
+ $db = $ENV{'PORTF_DB'};
+
+ (defined $db and defined $user and defined $pass and defined $dbms) or die "Environment variables (PORTF_DBMS, PORTF_DBUSER, PORTF_DBPASS, PORTF_DB) not set properly\n";
+
+ if (!($dbms eq "oracle") && !($dbms eq "mysql")) {
+ die "Database \"$dbms\" is not currently support\n";
+ }
+
+ if ($dbms eq "oracle") {
+ unless ($ENV{BEGIN_BLOCK}) {
+ # Let the insanity roll...
+ use Cwd;
+ $ENV{ORACLE_BASE}="/raid/oracle11g/app/oracle/product/11.2.0.1.0";
+ $ENV{ORACLE_HOME}=$ENV{ORACLE_BASE}."/db_1";
+ $ENV{ORACLE_SID}="CS339";
+ $ENV{LD_LIBRARY_PATH}=$ENV{ORACLE_HOME}."/lib";
+ $ENV{BEGIN_BLOCK} = 1;
+ if ($0 =~ /\//) {
+ #path given...
+ exec $0,@ARGV;
+ } else {
+ # not a path, use env to find it
+ exec "env", $0, @ARGV;
+ }
+ }
+ }
+}

0 comments on commit 95a0ed0

Please sign in to comment.