Skip to content

Commit

Permalink
Merge branch 'distributed' into 'main'
Browse files Browse the repository at this point in the history
Distributed

See merge request chanpreet/csci-5408-group-project-dpg9!9
  • Loading branch information
mikhona committed Apr 10, 2022
2 parents faa6164 + 5cbd8d1 commit 4405452
Show file tree
Hide file tree
Showing 43 changed files with 2,642 additions and 413 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ target/
.idea/
run-designite.bat
output-designite/
usr/
src/main/java/com/dal/distributed/constant/VMConstants.java
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"java.configuration.updateBuildConfiguration": "interactive"
}
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
<artifactId>commons-codec</artifactId>
<version>1.15</version>
</dependency>
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.55</version>
</dependency>
</dependencies>

<build>
Expand Down
205 changes: 205 additions & 0 deletions src/main/java/com/dal/distributed/analytics/Analytics.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
package com.dal.distributed.analytics;

import com.dal.distributed.constant.DataConstants;
import com.dal.distributed.constant.QueryRegex;
import com.dal.distributed.constant.VMConstants;
import com.dal.distributed.logger.Logger;
import com.dal.distributed.queryImpl.model.QueryLog;
import com.dal.distributed.utils.FileOperations;
import com.dal.distributed.utils.RemoteVmUtils;

import java.io.File;
import java.util.*;
import java.util.regex.Matcher;

public class Analytics {
Logger logger = Logger.instance();

public void analyze(Scanner scanner) throws Exception {
while (true) {
logger.info("\nPlease choose any one of the following options:");
logger.info("1. Analytics");
logger.info("2. Exit");
String input = scanner.nextLine();

switch (input) {
case "1":
logger.info("Please input a query for the selected option: ");
String query = scanner.nextLine();
analyzeQueries(query);
break;
case "2":
break;
default:
logger.error("Please choose a valid input!");
}
if ("2".equals(input))
break;
}
}

private void analyzeQueries(String queryString) throws Exception {
String[] query = queryString.split(" ");
if (query[1].contains("queries")) {
countQueries(queryString);
} else if (query[1].contains("update")) {
countOperationType("UPDATE");
} else if (query[1].contains("insert")) {
countOperationType("INSERT");
} else if (query[1].contains("delete")) {
countOperationType("DELETE");
} else if (query[1].contains("select")) {
countOperationType("SELECT");
} else if (query[1].contains("create")) {
countOperationType("CREATE TABLE");
}
}

private void countOperationType(String operation) throws Exception {
List<QueryLog> queryLogs = getQueryLogFileInformation();
HashMap<String, Integer> tableMap = new HashMap<>();
for (QueryLog queryLog : queryLogs) {
if (queryLog.getOperation().equalsIgnoreCase(operation)) {
if (null != tableMap.get(queryLog.getTableName())) {
tableMap.put(queryLog.getTableName(), tableMap.get(queryLog.getTableName()) + 1);
} else {
tableMap.put(queryLog.getTableName(), 1);
}
}
}

Boolean flag = Boolean.FALSE;
for (Map.Entry<String, Integer> user : tableMap.entrySet()) {
logger.info("Total " + user.getValue() + " " + operation + " operation(s) are performed on " + user.getKey());
flag = Boolean.TRUE;
}

if (!flag) {
logger.info("No " + operation + " queries found.");
}
}

private void countQueries(String queryString) throws Exception {
String [] query = queryString.split(" ");
Matcher matcher = QueryRegex.countQueriesAnalytics.matcher(queryString);
if (matcher.find()) {
String userName = matcher.group(1).trim();
String databaseName = matcher.group(2).replace(";","");
countQueriesForUser(userName, databaseName);
} else if (query.length == 2) {
countTotalQueries();
}
}

private void countTotalQueries() throws Exception {
List<QueryLog> queryLogs = getQueryLogFileInformation();
HashMap<String, Integer> userMap = new HashMap<>();
for (QueryLog queryLog : queryLogs) {
String key = queryLog.getSubmittedBy() + "|" + queryLog.getDatabaseName();
if (null != userMap.get(key)) {
userMap.put(key, userMap.get(key) + 1);
} else {
userMap.put(key, 1);
}
}

Boolean flag = Boolean.FALSE;
for (Map.Entry<String, Integer> user : userMap.entrySet()) {
String[] details = user.getKey().split("\\|");
if (null != details[1] && !details[1].equalsIgnoreCase("null")) {
logger.info("User " + details[0] + " submitted " + user.getValue() + " query(s) for " + details[1]);
}
flag = Boolean.TRUE;
}

if (!flag) {
logger.info("No queries found");
}
}

private void countQueriesForUser(String userName, String databaseName) throws Exception {
List<QueryLog> queryLogs = getQueryLogFileInformation();
HashMap<String, Integer> userMap = new HashMap<>();
for (QueryLog queryLog : queryLogs) {
if (queryLog.getDatabaseName().equalsIgnoreCase(databaseName) &&
queryLog.getSubmittedBy().equalsIgnoreCase(userName)) {
if (null != userMap.get(queryLog.getSubmittedBy())) {
userMap.put(queryLog.getSubmittedBy(), userMap.get(queryLog.getSubmittedBy()) + 1);
} else {
userMap.put(queryLog.getSubmittedBy(), 1);
}
}
}

Boolean flag = Boolean.FALSE;
for (Map.Entry<String, Integer> user : userMap.entrySet()) {
logger.info("User " + user.getKey() + " submitted " + user.getValue() + " query(s) for " + databaseName);
flag = Boolean.TRUE;
}

if (!flag) {
logger.info("No queries found");
}
}

private List<QueryLog> getQueryLogFileInformation() throws Exception {
String queryLogFile = FileOperations.readFileContent(
new File(DataConstants.LOGS_FILE_LOCATION + DataConstants.QUERY_LOG_FILE_NAME));

String remoteQueryLogFile = RemoteVmUtils.readFileContent(VMConstants.projectPath + DataConstants.LOGS_FILE_LOCATION + DataConstants.QUERY_LOG_FILE_NAME);
StringBuilder sb = new StringBuilder()
.append(queryLogFile)
.append(remoteQueryLogFile);

Matcher matcher = QueryRegex.valueBetweenQuotes.matcher(sb.toString());
List<QueryLog> queryLogList = new ArrayList<>();

//JSON to Java Object List
while (matcher.find()) {
if (matcher.group().equalsIgnoreCase("QueryLog")) {
QueryLog queryLog = new QueryLog();
while (matcher.find()) {
if (matcher.group().equalsIgnoreCase("flag")) {
matcher.find();
if (matcher.find()) {
queryLog.setFlag(matcher.group());
}
} else if (matcher.group().equalsIgnoreCase("query")) {
matcher.find();
if (matcher.find()) {
queryLog.setQuery(matcher.group());
}
} else if (matcher.group().equalsIgnoreCase("operation")) {
matcher.find();
if (matcher.find()) {
queryLog.setOperation(matcher.group());
}
} else if (matcher.group().equalsIgnoreCase("submissionTimestamp")) {
matcher.find();
if (matcher.find()) {
queryLog.setSubmissionTimestamp(matcher.group());
}
} else if (matcher.group().equalsIgnoreCase("submittedBy")) {
matcher.find();
if (matcher.find()) {
queryLog.setSubmittedBy(matcher.group());
}
} else if (matcher.group().equalsIgnoreCase("tableName")) {
matcher.find();
if (matcher.find()) {
queryLog.setTableName(matcher.group());
}
} else if (matcher.group().equalsIgnoreCase("databaseName")) {
matcher.find();
if (matcher.find()) {
queryLog.setDatabaseName(matcher.group());
}
break;
}
}
queryLogList.add(queryLog);
}
}
return queryLogList;
}
}
26 changes: 25 additions & 1 deletion src/main/java/com/dal/distributed/authentication/Login.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.dal.distributed.authentication;

import com.dal.distributed.logger.model.EventLog;
import com.dal.distributed.main.OperationsMenu;
import com.dal.distributed.constant.AuthConstants;
import com.dal.distributed.logger.Logger;
Expand All @@ -16,27 +17,41 @@ public class Login {

public static Logger logger = Logger.instance();

public void flow(Scanner sc) throws IOException {
public void flow(Scanner sc) throws Exception {
logger.info("For login, please provide your userId and press enter");
EventLog loginEvent = new EventLog();
loginEvent.setLogType("LOGIN");
String userId = sc.nextLine();
if (userId == null || userId.isEmpty()) {
loginEvent.setSuccess(false);
loginEvent.setUserId(userId);
EventLog.logEvent(loginEvent);
logger.info("Please type something before enter!");
return;
}
logger.info("Please provide your password and press enter");
String password = sc.nextLine();
if (password == null || password.isEmpty()) {
loginEvent.setSuccess(false);
loginEvent.setUserId(userId);
EventLog.logEvent(loginEvent);
logger.error("Password can't be empty!");
return;
}
Optional<UserRegistration> userOpt = AuthFileUtils.readUserDetails(AuthConstants.USER_DETAILS_FILE_LOCATION, getHashedValue(userId));
if(!userOpt.isPresent()) {
loginEvent.setSuccess(false);
loginEvent.setUserId(userId);
EventLog.logEvent(loginEvent);
logger.error("Either userId/password is not correct");
return;
}
UserRegistration user = userOpt.get();
String hashedPassword = getHashedValue(password);
if (!hashedPassword.equals(user.getPassword())) {
loginEvent.setSuccess(false);
loginEvent.setUserId(userId);
EventLog.logEvent(loginEvent);
logger.error("Either userId/password is not correct");
return;
}
Expand All @@ -46,13 +61,22 @@ public void flow(Scanner sc) throws IOException {
logger.info(securityQuestion.getQuestion());
String securingAnsByUser = sc.nextLine();
if (securingAnsByUser == null || securingAnsByUser.isEmpty()) {
loginEvent.setSuccess(false);
loginEvent.setUserId(userId);
EventLog.logEvent(loginEvent);
logger.error("Your security answer can't be empty!");
return;
}
if (!securingAnsByUser.equals(securityQuestion.getAnswer())) {
loginEvent.setSuccess(false);
loginEvent.setUserId(userId);
EventLog.logEvent(loginEvent);
logger.info("Invalid answer please try again!");
return;
}
loginEvent.setSuccess(true);
loginEvent.setUserId(userId);
EventLog.logEvent(loginEvent);
logger.info("You are successfully logged in!!");
OperationsMenu operationsMenu = new OperationsMenu();
operationsMenu.displayOperationsMenu(userId, sc);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.dal.distributed.logger.Logger;
import com.dal.distributed.authentication.model.SecurityQuestions;
import com.dal.distributed.authentication.model.UserRegistration;
import com.dal.distributed.logger.model.EventLog;
import org.apache.commons.codec.digest.DigestUtils;

import java.util.ArrayList;
Expand All @@ -24,39 +25,52 @@ public class Registration {

public void registerUser() {
Scanner sc = new Scanner(System.in);
EventLog registerEvent = new EventLog();
registerEvent.setLogType("REGISTRATION");

String userId;
String password;

logger.info("Enter a UserID containing 5 to 15 characters: ");
userId = sc.nextLine();
registerEvent.setUserId(userId);
Boolean userIdValid = performUserIdValidations(userId);
if (!userIdValid) {
registerEvent.setSuccess(false);
EventLog.logEvent(registerEvent);
return;
}

logger.info("Enter a password");
password = sc.nextLine();
Boolean isPasswordValid = performPasswordValidations(password);
if (!isPasswordValid) {
registerEvent.setSuccess(false);
EventLog.logEvent(registerEvent);
return;
}

logger.info(AuthConstants.SECURITY_QUESTION_1);
final String securityAnswerOne = sc.nextLine();
if (!validateSecurityInput(securityAnswerOne)) {
registerEvent.setSuccess(false);
EventLog.logEvent(registerEvent);
return;
}

logger.info(AuthConstants.SECURITY_QUESTION_2);
final String securityAnswerTwo = sc.nextLine();
if (!validateSecurityInput(securityAnswerTwo)) {
registerEvent.setSuccess(false);
EventLog.logEvent(registerEvent);
return;
}

logger.info(AuthConstants.SECURITY_QUESTION_3);
final String securityAnswerThree = sc.nextLine();
if (!validateSecurityInput(securityAnswerThree)) {
registerEvent.setSuccess(false);
EventLog.logEvent(registerEvent);
return;
}

Expand All @@ -73,7 +87,8 @@ public void registerUser() {

AuthFileUtils file = new AuthFileUtils();
file.writeUserDetails(AuthConstants.USER_DETAILS_FILE_LOCATION, user.toString());

registerEvent.setSuccess(true);
EventLog.logEvent(registerEvent);
logger.info("Registration completed successfully!!! You can now access the system with userID and Password.");
}

Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/dal/distributed/constant/AuthConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ public final class AuthConstants {

public static final String USER_DETAILS_FILE_LOCATION = "usr/dpg9/authentication/User_Profile/";

public static final String AUTHENTICATION_FOLDER = "usr/dpg9/authentication/";

public static final String AUTHENTICATION_FILE = "User_Profile";

public static final String SUCCESS = "SUCCESS";

public static final String FAILURE = "FAILURE";

}
4 changes: 4 additions & 0 deletions src/main/java/com/dal/distributed/constant/DataConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ public class DataConstants {
public static final String QUERY_LOGS_FILE_LOCATION = "usr/dpg9/logs/";

public static final String QUERY_LOG_FILE_NAME = "query_logs";

public static final String EVENT_LOG_FILE_NAME = "event_logs";

public static final String FILE_FORMAT = ".psv";
}
Loading

0 comments on commit 4405452

Please sign in to comment.