aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/edu/brown/cs/student/term/DatabaseQuerier.java25
-rw-r--r--src/main/java/edu/brown/cs/student/term/Main.java96
-rw-r--r--src/main/java/edu/brown/cs/student/term/ProfitCalculation.java4
-rw-r--r--src/main/java/edu/brown/cs/student/term/hub/Holder.java7
-rw-r--r--src/main/java/edu/brown/cs/student/term/hub/HubSearch.java68
-rw-r--r--src/main/java/edu/brown/cs/student/term/hub/LinkMapper.java15
-rw-r--r--src/main/java/edu/brown/cs/student/term/hub/SuspicionRanker.java49
-rw-r--r--src/main/java/edu/brown/cs/student/term/repl/Command.java8
-rw-r--r--src/main/java/edu/brown/cs/student/term/repl/REPL.java1
-rw-r--r--src/main/java/edu/brown/cs/student/term/repl/commands/SetupCommand.java28
10 files changed, 129 insertions, 172 deletions
diff --git a/src/main/java/edu/brown/cs/student/term/DatabaseQuerier.java b/src/main/java/edu/brown/cs/student/term/DatabaseQuerier.java
index f4be9b3..688270f 100644
--- a/src/main/java/edu/brown/cs/student/term/DatabaseQuerier.java
+++ b/src/main/java/edu/brown/cs/student/term/DatabaseQuerier.java
@@ -7,7 +7,7 @@ import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
-public class DatabaseQuerier{
+public class DatabaseQuerier {
private static Connection conn = null;
//TODO: Be prepared to overhaul this to account for IDs
@@ -49,10 +49,19 @@ public class DatabaseQuerier{
while(rs.next()){
stocks.add(rs.getString(1));
}
+
+ rs.close();
+ prep.close();
return stocks;
}
- //TODO: Fill these in
+ /**
+ * Gets all the trades in by stock and buy type, ordered by time
+ * @param startDate - the start date of these trades
+ * @param endDate - the end date of these trades
+ * @return a list of list of trades as specified above
+ * @throws SQLException - if something goes wrong with connection
+ */
public List<List<Trade>> getAllTradesByStock(Instant startDate, Instant endDate) throws SQLException {
List<List<Trade>> allTrades = new ArrayList<>();
List<String> stocks = getRecentStocks(startDate, endDate);
@@ -64,6 +73,15 @@ public class DatabaseQuerier{
return allTrades;
}
+ /**
+ * Gets a single stock's list of trades for that time period (either buy or sell)
+ * @param stock - string name of the stock to get the trades for
+ * @param isBuy - integer whether it's a buy or sell
+ * @param startDate - an Instant representing the start of the time period
+ * @param endDate - an Instant representing the end of the time period
+ * @return - a list of trades for that stock
+ * @throws SQLException - if issue getting connection
+ */
public List<Trade> getTradeByStock(String stock, int isBuy, Instant startDate, Instant endDate) throws SQLException{
List<Trade> trades = new ArrayList<>();
@@ -84,6 +102,9 @@ public class DatabaseQuerier{
rs.getDouble(8)));
}
+ rs.close();
+ prep.close();
+
return trades;
}
}
diff --git a/src/main/java/edu/brown/cs/student/term/Main.java b/src/main/java/edu/brown/cs/student/term/Main.java
index 0a70b91..5ca40e8 100644
--- a/src/main/java/edu/brown/cs/student/term/Main.java
+++ b/src/main/java/edu/brown/cs/student/term/Main.java
@@ -1,9 +1,11 @@
package edu.brown.cs.student.term;
+import com.google.common.collect.ImmutableMap;
import edu.brown.cs.student.term.hub.Holder;
import edu.brown.cs.student.term.hub.HubSearch;
import edu.brown.cs.student.term.hub.LinkMapper;
import edu.brown.cs.student.term.profit.StockHolding;
+import edu.brown.cs.student.term.hub.SuspicionRanker;
import edu.brown.cs.student.term.repl.Command;
import edu.brown.cs.student.term.repl.REPL;
import edu.brown.cs.student.term.repl.commands.LoadCommand;
@@ -11,30 +13,13 @@ import edu.brown.cs.student.term.repl.commands.SetupCommand;
import joptsimple.OptionParser;
import joptsimple.OptionSet;
-import java.sql.Connection;
-import java.sql.Date;
-import java.sql.DriverManager;
-import java.sql.SQLException;
-import java.sql.Statement;
import java.time.Instant;
+import java.sql.Date;
import java.util.HashMap;
-import java.io.*;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
-
-import joptsimple.OptionParser;
-import joptsimple.OptionSet;
import spark.*;
import spark.template.freemarker.FreeMarkerEngine;
-
-import java.io.BufferedReader;
-import java.io.InputStreamReader;
-
-import com.google.common.collect.ImmutableMap;
-
import freemarker.template.Configuration;
//fix
@@ -42,46 +27,19 @@ import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
-import java.sql.ResultSet;
-import java.util.ArrayList;
import java.util.List;
import java.util.Map;
-import java.util.*;
-import com.google.common.collect.ImmutableMap;
import com.google.gson.Gson;
-import joptsimple.OptionParser;
-import joptsimple.OptionSet;
-import spark.ExceptionHandler;
-import spark.ModelAndView;
-import spark.QueryParamsMap;
-import spark.Request;
-import spark.Response;
-import spark.Route;
-import spark.Filter;
-import spark.Spark;
-import spark.TemplateViewRoute;
-import spark.template.freemarker.FreeMarkerEngine;
-
-import freemarker.template.Configuration;
-
import org.json.JSONObject;
-import java.sql.Connection;
-import java.sql.DriverManager;
-import java.sql.SQLException;
-import java.sql.Statement;
-
/**
* The Main class of our project. This is where execution begins.
*/
public final class Main {
- // TODO: fix temproary solution
public static JSONObject xmlLinks = null;
-
-
private static final Gson GSON = new Gson();
private static final int DEFAULT_PORT = 4567;
@@ -110,6 +68,9 @@ public final class Main {
if (options.has("gui")) {
runSparkServer((int) options.valueOf("port"));
+ //will auto connect to correct db when running gui!
+ SetupCommand setConnection = new SetupCommand();
+ setConnection.run(new String[]{"data/trades.sqlite3"});
}
HashMap<String, Command> commandHashMap = new HashMap<>();
@@ -157,31 +118,36 @@ public final class Main {
return "OK";
});
-
-
Spark.before((request, response) -> response.header("Access-Control-Allow-Origin", "*"));
- Spark.post("/data", new DataHandler());
+ Spark.post("/data", new SuspicionRankHandler());
Spark.post("/profit", new ProfitQueryHandler());
}
-
- private static class DataHandler implements Route {
+ /**
+ * Gets the list of holders with id, name, and suspicion rank
+ */
+ private static class SuspicionRankHandler implements Route {
@Override
+ /**
+ * Expects that the request will contain two longs that are the start/end
+ * dates for the suspicion rank to run on as epoch time in milliseconds
+ */
public Object handle(Request request, Response response) throws Exception {
- String str = request.body();
- xmlLinks = new JSONObject(str); //this is all the filedAt times and xml files
-
+ //String str = request.body();
+ //xmlLinks = new JSONObject(str); //this is all the filedAt times and xml files
try {
- DatabaseQuerier db = new DatabaseQuerier("data/trades.sqlite3");
- LinkMapper lm = new LinkMapper(db);
-
- Instant start = Instant.parse("2021-03-30T05:00:00.00Z");
- //12 am on 3/28 in UTC
- Instant end = Instant.parse("2021-04-12T05:00:00.00Z");
- lm.makeFollowerLinks(start, end);
- HubSearch hub = new HubSearch(lm);
- Map<Holder, Double> him = hub.runHubSearch(start, end);
- return GSON.toJson(him);
+ DatabaseQuerier db = SetupCommand.getDq();
+ SuspicionRanker ranker = new SuspicionRanker(db);
+
+ JSONObject data = new JSONObject(request.body());
+
+ long startMilli = data.getLong("start");
+ long endMilli = data.getLong("end");
+ Instant start = Instant.ofEpochMilli(startMilli);
+ Instant end = Instant.ofEpochMilli(endMilli);
+ List<Holder> suspiciousHolders = ranker.getSuspicionScoreList(start, end);
+ Map<String, Object> variables = ImmutableMap.of("holders", suspiciousHolders);
+ return GSON.toJson(variables);
} catch (Exception e) {
System.out.println("DBQuerier Test, couldn't connect to db???");
return "Error";
@@ -223,7 +189,9 @@ public final class Main {
res.put("SP500", (1 + sp500PercentGain) * profit.getMoneyInput());
res.put("percentSP500", 100 * sp500PercentGain);
return GSON.toJson(res);
+
}
+
}
/**
@@ -242,6 +210,4 @@ public final class Main {
res.body(stacktrace.toString());
}
}
-
-
} \ No newline at end of file
diff --git a/src/main/java/edu/brown/cs/student/term/ProfitCalculation.java b/src/main/java/edu/brown/cs/student/term/ProfitCalculation.java
index 28f7c79..88b4b04 100644
--- a/src/main/java/edu/brown/cs/student/term/ProfitCalculation.java
+++ b/src/main/java/edu/brown/cs/student/term/ProfitCalculation.java
@@ -362,6 +362,10 @@ public class ProfitCalculation {
while (rs.next()) {
int id = rs.getInt("holder_id");
this.person = rs.getString("holder_name");
+ //TODO: Temporary fix for the moneyinput divide by 0 error
+ if(moneyInput == 0){
+ moneyInput = 1;
+ }
profitMap.put(id, this.calculateGains() / moneyInput);
}
} catch (SQLException throwables) {
diff --git a/src/main/java/edu/brown/cs/student/term/hub/Holder.java b/src/main/java/edu/brown/cs/student/term/hub/Holder.java
index abe59df..66def5e 100644
--- a/src/main/java/edu/brown/cs/student/term/hub/Holder.java
+++ b/src/main/java/edu/brown/cs/student/term/hub/Holder.java
@@ -5,6 +5,7 @@ import java.util.Objects;
public class Holder {
private int id;
private String name;
+ private double suspicionScore;
public Holder(int id, String name){
this.id = id;
@@ -15,6 +16,12 @@ public class Holder {
return id;
}
+ public void setSuspicionScore(double sus){
+ this.suspicionScore = sus;
+ }
+
+ public double getSuspicionScore(){return suspicionScore;}
+
public String getName() {
return name;
}
diff --git a/src/main/java/edu/brown/cs/student/term/hub/HubSearch.java b/src/main/java/edu/brown/cs/student/term/hub/HubSearch.java
index 4d5755c..ccefeef 100644
--- a/src/main/java/edu/brown/cs/student/term/hub/HubSearch.java
+++ b/src/main/java/edu/brown/cs/student/term/hub/HubSearch.java
@@ -11,12 +11,10 @@ public class HubSearch {
LinkMapper mapper;
Map<Holder, Set<Holder>> followerToLeaderMap = new HashMap<>();
- //TODO: Make this just take in a map from holder -> set of holder
public HubSearch(LinkMapper mapper){
this.mapper = mapper;
}
- //TODO: reevaluate this basic version and tweak to customize
public Map<Holder, Double> runHubSearch(Instant start, Instant end){
followerToLeaderMap = mapper.makeFollowerLinks(start, end);
int numHolders = followerToLeaderMap.size();
@@ -54,15 +52,9 @@ public class HubSearch {
//dampening factor
double damp = 0.15;
/*
- In normal page rank:
- links is the pages that k links to, if k links to j, then
- we want to add some weight to j from k, but that
- weight is diluted by the number of links that k has
- in general because we don't want to count a bunch of links
- from a page as highly as one targeted link from one page to another
In Hub Search
- leader = j, follower = k, if follower links to (follows) leader,
+ we have the leader and follower if follower links to (follows) leader,
then we want to add some weight from follower to leader,
but should that weight be diluted by the number of people
who followed leader or the number of people who follower followed
@@ -87,62 +79,4 @@ public class HubSearch {
return sum <= 0.001;
}
-
-
-
-
-
-
-
-
-
- /*
- def pageRank(pList: List[Page]): mutable.HashMap[Int, Double] = {
- val n = pList.length
- val weights = new Array[Double](n)
- val r = new Array[Double](n)
- val rPrime = Array.fill[Double](n)(1.0 / n)
-
- while (!distance(r, rPrime)) {
- Array.copy(rPrime, 0, r, 0, n)
- for (j <- 0 to n - 1) {
- rPrime(j) = 0
- for (k <- 0 to n - 1) {
- val wjk = getWeight(pList(j), pList(k), n)
- rPrime(j) = (rPrime(j) + (wjk * r(k)))
- }
- }
- }
-
- val pageRank = new mutable.HashMap[Int, Double]()
-
- for (i <- 0 to rPrime.length - 1) {
- pageRank.put(pList(i).getID, rPrime(i))
- }
- pageRank
- }
-
- def getWeight(j: Page, k: Page, n: Int): Double = {
-
- val links = k.getLinks
- val nk = links.size
-
- if (links.contains(j.getTitle.toLowerCase)) {
- ((0.15 / n) + ((1 - 0.15) * (1.0 / nk)))
- } else if (nk == 0) {
- ((0.15 / n) + ((1 - 0.15) * (1.0 / n)))
- } else {
- (0.15 / n)
- }
- }
-
- def distance(r1: Array[Double], r2: Array[Double]): Boolean = {
- var sum = 0.0
- for (i <- 0 to r1.length - 1) {
- sum = sum + Math.pow((r1(i) - r2(i)), 2)
- }
- sum <= .001
-
- }
- */
}
diff --git a/src/main/java/edu/brown/cs/student/term/hub/LinkMapper.java b/src/main/java/edu/brown/cs/student/term/hub/LinkMapper.java
index 8bc8cf9..b490ea1 100644
--- a/src/main/java/edu/brown/cs/student/term/hub/LinkMapper.java
+++ b/src/main/java/edu/brown/cs/student/term/hub/LinkMapper.java
@@ -21,7 +21,7 @@ public class LinkMapper {
/**
- * Returns the follower => leaders map as is, does not update it
+ * Returns the follower to leaders map as is, does not update it
* @return person to follower map
*/
public Map<Holder, Set<Holder>> getFollowerToLeaders() {
@@ -29,9 +29,11 @@ public class LinkMapper {
}
/**
- * The links between people and their followers who made the same trade
+ * This links a person and the set of people whose trades they followed
* in the same time period
- * @return the newly updated link map
+ * @param start - instant representing start of time period to look at
+ * @param end - instant representing end of time period to look at
+ * @return - the newly updated link map
*/
public Map<Holder, Set<Holder>> makeFollowerLinks(Instant start, Instant end){
if(databaseQuerier != null){
@@ -54,9 +56,10 @@ public class LinkMapper {
return followerToLeaders;
}
-
- //TODO: Try to create both leader => follower and follower => leader map at once
- //only if necessary tho!
+ /**
+ * Converts a single trade list into entries in the follower to leader map
+ * @param tradeList - a list of trades for a single stock (either buy or sell)
+ */
private void convertTradeListToFollowerMap(List<Trade> tradeList){
List<Holder> holderList = new ArrayList<>();
diff --git a/src/main/java/edu/brown/cs/student/term/hub/SuspicionRanker.java b/src/main/java/edu/brown/cs/student/term/hub/SuspicionRanker.java
index a1196d8..dd959f0 100644
--- a/src/main/java/edu/brown/cs/student/term/hub/SuspicionRanker.java
+++ b/src/main/java/edu/brown/cs/student/term/hub/SuspicionRanker.java
@@ -1,4 +1,53 @@
package edu.brown.cs.student.term.hub;
+import edu.brown.cs.student.term.DatabaseQuerier;
+import edu.brown.cs.student.term.ProfitCalculation;
+
+import java.time.Instant;
+import java.sql.Date;
+import java.util.*;
+
public class SuspicionRanker {
+
+ DatabaseQuerier querier;
+ public SuspicionRanker(DatabaseQuerier db){
+ this.querier = db;
+ }
+
+ public <K, V extends Comparable<V>> V getMaxOfMap(Map<K, V> map) {
+ Map.Entry<K, V> maxEntry = Collections.max(map.entrySet(), Map.Entry.comparingByValue());
+ System.out.println(maxEntry);
+ System.out.println(maxEntry.getValue());
+ return maxEntry.getValue();
+ }
+
+ public List<Holder> getSuspicionScoreList(Instant start, Instant end){
+ List<Holder> suspicionList = new ArrayList<>();
+ try {
+ LinkMapper lm = new LinkMapper(querier);
+ HubSearch hub = new HubSearch(lm);
+ Map<Holder, Double> holderToHubScore = hub.runHubSearch(start, end);
+
+ ProfitCalculation pc = new ProfitCalculation(DatabaseQuerier.getConn(), "",
+ new Date(start.toEpochMilli()),
+ new Date(end.toEpochMilli()));
+
+ Map<Integer, Double> profitMap = pc.getProfitMap();
+
+ double profitMax = getMaxOfMap(profitMap);
+
+ double hubMax = getMaxOfMap(holderToHubScore);
+
+ for(Holder guy: holderToHubScore.keySet()){
+ double normalizedProfitScore = profitMap.get(guy.getId()) / profitMax;
+ double normalizedHubScore = holderToHubScore.get(guy) / hubMax;
+ double suspicionScore = normalizedHubScore * 0.6 + normalizedProfitScore * 0.4;
+ guy.setSuspicionScore(suspicionScore);
+ suspicionList.add(guy);
+ }
+ } catch (Exception e) {
+ System.out.println("ERROR: Could not connect to database querier");
+ }
+ return suspicionList;
+ }
}
diff --git a/src/main/java/edu/brown/cs/student/term/repl/Command.java b/src/main/java/edu/brown/cs/student/term/repl/Command.java
index 056c7df..b110644 100644
--- a/src/main/java/edu/brown/cs/student/term/repl/Command.java
+++ b/src/main/java/edu/brown/cs/student/term/repl/Command.java
@@ -1,13 +1,13 @@
package edu.brown.cs.student.term.repl;
/**
- * Interface implemented by all five StarsCommand commands.
- * Implemented by StarsCommand, NaiveNeighbors, NaiveRadiusCommand, RadiusCommand, NeighborsCommand
+ * Interface implemented by all the commands that the REPL is responsible for
*/
public interface Command {
/**
- * Main run method for every command.
- * @param args arguments for the command
+ * The function that the command calls when run from the REPL
+ * @param args - the arguments for the command
+ * @return a string representation of the output, or error message
*/
String run(String[] args);
}
diff --git a/src/main/java/edu/brown/cs/student/term/repl/REPL.java b/src/main/java/edu/brown/cs/student/term/repl/REPL.java
index a1d5c23..5381d45 100644
--- a/src/main/java/edu/brown/cs/student/term/repl/REPL.java
+++ b/src/main/java/edu/brown/cs/student/term/repl/REPL.java
@@ -16,6 +16,7 @@ public class REPL {
/**
* Constructor for a REPL object.
+ * @param userCommands - map of string to Command object for REPL to run
*/
public REPL(HashMap<String, Command> userCommands) {
commands = userCommands;
diff --git a/src/main/java/edu/brown/cs/student/term/repl/commands/SetupCommand.java b/src/main/java/edu/brown/cs/student/term/repl/commands/SetupCommand.java
index e19117c..a6ef1f0 100644
--- a/src/main/java/edu/brown/cs/student/term/repl/commands/SetupCommand.java
+++ b/src/main/java/edu/brown/cs/student/term/repl/commands/SetupCommand.java
@@ -34,34 +34,6 @@ public class SetupCommand implements Command {
System.out.println(error);
return error;
}
-
- try{
- /** Just for testing purposes **/
- //12 am on 3/12 in UTC
- Instant start = Instant.parse("2021-03-12T05:00:00.00Z");
- //12 am on 3/27 in UTC
- Instant end = Instant.parse("2021-03-27T05:00:00.00Z");
-
- System.out.println(end.toEpochMilli());
- System.out.println(start.getEpochSecond());
-
- ZonedDateTime zdt = ZonedDateTime.ofInstant(start, ZoneId.of("America/New_York"));
- System.out.println(zdt.toString());
- List<List<Trade>> trades = dq.getAllTradesByStock(start, end);
-
- int sum = 0;
- for(List<Trade> t: trades){
- System.out.println(t);
- sum += t.size();
- }
-
- System.out.println("num of trades: " + sum);
-
-
- } catch(Exception e){
- e.printStackTrace();
- }
-
return error;
}