From 54a78c3fdea0727dfa483e30461a6fd8a33b5ed4 Mon Sep 17 00:00:00 2001 From: Melody Chen <34081368+melodychn@users.noreply.github.com> Date: Fri, 4 Sep 2020 15:18:52 -0700 Subject: [PATCH] cleanup + update parts of README (#78) --- README.md | 47 ++++++++++++------- .../com/google/blackswan/mock/Constant.java | 4 ++ .../mock/SimpleAnomalyGenerator.java | 12 ++--- .../mock/SimpleRelatedDataGenerator.java | 6 ++- dispatch.yaml | 7 ++- 5 files changed, 48 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 1b31957..05f5c9e 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,6 @@ # GreySwan -## Config frontend project -Run `yarn` in `./frontend` to install dependencies. - -## Run locally +## Run project locally ``` // From frontend directory yarn local @@ -12,6 +9,34 @@ mvn appengine:run // From python directory python3.7 main.py ``` +*See `key.json` section if you don't already have the key locally. + +## Project set up +### Frontend +1) Install Node v10.21.0. +``` +nvm install v10.21.0 +nvm list // Make sure v10.21.0 is included. +nvm use 10.21.0 +``` +2) Make sure you have `yarn` installed. +3) Run `yarn` in `./frontend` to install dependencies. +### Java Backend +1) Follow [instructions](https://cloud.google.com/sdk/docs/downloads-apt-ge) to install gCloud SDK. +2) Run `gcloud init` in root directory of our repository. Provide `greyswan` as gCloud project ID. +3) Install Maven locally. +``` +sudo apt update +sudo apt install maven +mvn -version // Check if maven is installed. +``` +4) Make sure you have Java 8 installed locally. If not, follow instructions [here](https://www.oracle.com/java/technologies/javase/javase-jdk8-downloads.html). +### Python Microservice +1) Make sure you have Python 3.7 and pip3 installed. +2) To install dependencies: +``` +pip3 install -r requirements.txt +``` ## Testing ``` @@ -21,18 +46,8 @@ mvn test yarn test ``` -## Setting up and running Python code -To install dependencies: (Only need to run once.) -``` -pip3 install -r requirements.txt -``` -To run server locally (make sure you're in `python` directory and `key.json` is present): -``` -python3.7 main.py -``` - -## Deploy to gcloud -Only need to do for first time. If you're using cloud shell, should not need following commands. +## Deploy to gCloud +Only need to do for first time in project root directory. If you're using cloud shell, should not need following commands. ``` gcloud init gcloud config set project greyswan diff --git a/backend/src/main/java/com/google/blackswan/mock/Constant.java b/backend/src/main/java/com/google/blackswan/mock/Constant.java index cf9ee8c..c659d10 100644 --- a/backend/src/main/java/com/google/blackswan/mock/Constant.java +++ b/backend/src/main/java/com/google/blackswan/mock/Constant.java @@ -22,6 +22,10 @@ /** Constants used throughout the blackswan mock classes. */ public final class Constant { + // Only set to false, when you're pushing to Github or do + // not have access to key.json. + public static final boolean USE_CLOUD_STORAGE = false; + // Constants used to filenames. private static final String FILE_DELIMITER = "-"; private static final String FILE_END = "--data.csv"; diff --git a/backend/src/main/java/com/google/blackswan/mock/SimpleAnomalyGenerator.java b/backend/src/main/java/com/google/blackswan/mock/SimpleAnomalyGenerator.java index 56c26b2..77246df 100644 --- a/backend/src/main/java/com/google/blackswan/mock/SimpleAnomalyGenerator.java +++ b/backend/src/main/java/com/google/blackswan/mock/SimpleAnomalyGenerator.java @@ -41,7 +41,8 @@ public class SimpleAnomalyGenerator implements AnomalyGenerator { private static final int THRESHOLD = 13; private static final int NUM_POINTS = 5; /** Ideally the FileSystem should be injected. */ - private static final FileSystem FILE_SYSTEM = LocalFileSystem.createSystem(); + private static final FileSystem FILE_SYSTEM = Constant.USE_CLOUD_STORAGE ? + CloudFileSystem.createSystem() : LocalFileSystem.createSystem(); private final ImmutableList anomalies; private final DataInfo topic; @@ -50,8 +51,6 @@ public class SimpleAnomalyGenerator implements AnomalyGenerator { public static SimpleAnomalyGenerator createGenerator(DataInfo topic) { return new SimpleAnomalyGenerator( topic, - // For [push] to git, always use LocalFileSystem, as CloudFileSystem will fail - // unit test without access to key.json. FILE_SYSTEM.getDataAsStream(topic), THRESHOLD, NUM_POINTS @@ -101,10 +100,6 @@ private ImmutableList generateAnomalies(int threshold, int numDataPoint .collect(ImmutableList.toImmutableList()); } - /** - * TODO: Depending on size of future data, alter algorithm of finding - * associated data points. - */ private Anomaly createAnomalyFromDataPoint(Timestamp time, int numDataPoints) { // Convert keys into ArrayList. @@ -133,7 +128,8 @@ private Anomaly createAnomalyFromDataPoint(Timestamp time, int numDataPoints) { listKeys.get(firstDataPointIndex), listKeys.get(lastDataPointIndex)); - return new Anomaly(time, topic.getMetricName(), topic.getDimensionName(), dataPoints, relatedDataList); + return new Anomaly(time, topic.getMetricName(), topic.getDimensionName(), + dataPoints, relatedDataList); } } diff --git a/backend/src/main/java/com/google/blackswan/mock/SimpleRelatedDataGenerator.java b/backend/src/main/java/com/google/blackswan/mock/SimpleRelatedDataGenerator.java index 2172568..0e7e245 100644 --- a/backend/src/main/java/com/google/blackswan/mock/SimpleRelatedDataGenerator.java +++ b/backend/src/main/java/com/google/blackswan/mock/SimpleRelatedDataGenerator.java @@ -36,8 +36,10 @@ public class SimpleRelatedDataGenerator implements RelatedDataGenerator { private static final String CONFIG_USERNAME = "catyu@"; /** Ideally should be injected. Currently, putting as class variable. */ - private static final FileSystem FILE_SYSTEM = LocalFileSystem.createSystem(); - private static final SimpleRelatedDataGenerator INSTANCE = new SimpleRelatedDataGenerator(); + private static final FileSystem FILE_SYSTEM = Constant.USE_CLOUD_STORAGE ? + CloudFileSystem.createSystem() : LocalFileSystem.createSystem(); + private static final SimpleRelatedDataGenerator INSTANCE + = new SimpleRelatedDataGenerator(); private Multimap relatedDataMap; private Map> csvDataCache; diff --git a/dispatch.yaml b/dispatch.yaml index 4d07027..525328e 100644 --- a/dispatch.yaml +++ b/dispatch.yaml @@ -1,10 +1,13 @@ # Rules for dispatch file... dispatch: -# Route the urls that point to the Java backend's API calls +# Route the urls that point to the Java backend's API calls. - url: "*/api/v1/*" service: backend - url: "*/blackswan/*" service: backend -# Route all other urls to the React.js frontend +# Route the urls that point to Python backend's API calls. + - url: "*/python/*" + service: python +# Route all other urls to the React.js frontend. - url: "*/*" service: default