Unverified Commit e70d4af7 authored by mduppanapudi-nisum-com's avatar mduppanapudi-nisum-com Committed by GitHub

Merge pull request #174 from nisum-inc/FEATURE/MT-167-PerformanceImprovement

Improve performance on click of search button
parents c22777b2 5275656e
...@@ -39,8 +39,7 @@ public class AttendanceController { ...@@ -39,8 +39,7 @@ public class AttendanceController {
@RequestParam("toDate") String toDate) throws MyTimeException { @RequestParam("toDate") String toDate) throws MyTimeException {
List<EmpLoginData> message = new ArrayList<>(); List<EmpLoginData> message = new ArrayList<>();
try { try {
message = userService.employeeLoginsBasedOnDate(id, fromDate, message = userService.employeeLoginsBasedOnDate(id, fromDate, toDate);
toDate);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
......
...@@ -2,7 +2,6 @@ package com.nisum.mytime.service; ...@@ -2,7 +2,6 @@ package com.nisum.mytime.service;
import java.sql.Connection; import java.sql.Connection;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
...@@ -30,35 +29,29 @@ public class AttendanceServiceImpl implements AttendanceService { ...@@ -30,35 +29,29 @@ public class AttendanceServiceImpl implements AttendanceService {
ProjectTeamMatesRepo projectTeamMatesRepo; ProjectTeamMatesRepo projectTeamMatesRepo;
@Override @Override
public List<AttendenceData> getAttendanciesReport(String reportDate, String shift) throws MyTimeException, SQLException { public List<AttendenceData> getAttendanciesReport(String reportDate, String shift) throws MyTimeException {
long start_ms = System.currentTimeMillis(); long start_ms = System.currentTimeMillis();
List<AttendenceData> listOfEmployees = getEmpsAttendenceByShiftWise(reportDate,shift); String reportDateInReqDateFormat = reportDate.replace("-", "/");
List<AttendenceData> listOfEmployees = getEmpsAttendenceByShiftWise(reportDateInReqDateFormat, shift);
MyTimeLogger.getInstance().info("Time Taken for " + (System.currentTimeMillis() - start_ms)); MyTimeLogger.getInstance().info("Time Taken for " + (System.currentTimeMillis() - start_ms));
return listOfEmployees; return listOfEmployees;
} }
private List<AttendenceData> getEmpsAttendenceByShiftWise(String reportDate,String shift) throws MyTimeException { private List<AttendenceData> getEmpsAttendenceByShiftWise(String reportDate, String shift) throws MyTimeException {
List<AttendenceData> listOfEmployees = new ArrayList<AttendenceData>(); List<AttendenceData> listOfEmployees = new ArrayList<AttendenceData>();
Optional<List<ProjectTeamMate>> list = findEmpIdsByShiftWise(shift); Optional<List<ProjectTeamMate>> list = findEmpIdsByShiftWise(shift);
if(list.isPresent()) { if(list.isPresent()) {
List<String> empIdList = list.get().stream() List<String> empIdList = list.get().stream().map(ProjectTeamMate::getEmployeeId).collect(Collectors.toList());
.map(ProjectTeamMate::getEmployeeId) if(null != empIdList && empIdList.size() > MyTimeUtils.INT_ZERO) {
.collect(Collectors.toList()); String query = buildSqlQuery(reportDate,empIdList.toString().substring(1, empIdList.toString().length()-1), MyTimeUtils.PRESENT);
listOfEmployees.addAll(getAttendenceData(query, MyTimeUtils.PRESENT));
if(null != empIdList && empIdList.size() == MyTimeUtils.INT_ZERO) {
return listOfEmployees; List<String> presentList = listOfEmployees.stream().map(AttendenceData :: getEmployeeId).collect(Collectors.toList());
} empIdList.removeAll(presentList);
String query = buildSqlQuery(reportDate,empIdList.toString().substring(1, empIdList.toString().length()-1),MyTimeUtils.PRESENT); if(empIdList.size() > MyTimeUtils.INT_ZERO) {
listOfEmployees.addAll(getAttendenceData(query)); query = buildSqlQuery(reportDate, empIdList.toString().substring(1, empIdList.toString().length()-1), MyTimeUtils.ABSENT);
listOfEmployees .addAll(getAttendenceData(query, MyTimeUtils.ABSENT));
List<String> presentList = listOfEmployees.stream().map(AttendenceData :: getEmployeeId).collect(Collectors.toList()); }
empIdList.removeAll(presentList);
if(empIdList.size()>0) {
query = buildSqlQuery(reportDate, empIdList.toString().substring(1, empIdList.toString().length()-1), MyTimeUtils.ABSENT);
listOfEmployees .addAll(getAttendenceData(query));
} }
} }
return listOfEmployees; return listOfEmployees;
...@@ -66,7 +59,6 @@ public class AttendanceServiceImpl implements AttendanceService { ...@@ -66,7 +59,6 @@ public class AttendanceServiceImpl implements AttendanceService {
private Optional<List<ProjectTeamMate>> findEmpIdsByShiftWise(String shift) { private Optional<List<ProjectTeamMate>> findEmpIdsByShiftWise(String shift) {
Optional<List<ProjectTeamMate>> list = null; Optional<List<ProjectTeamMate>> list = null;
if(MyTimeUtils.ALL.equalsIgnoreCase(shift)) { if(MyTimeUtils.ALL.equalsIgnoreCase(shift)) {
list = projectTeamMatesRepo.findByActiveAndShiftLikeOrderByEmployeeIdDesc( true, MyTimeUtils.SHIFT); list = projectTeamMatesRepo.findByActiveAndShiftLikeOrderByEmployeeIdDesc( true, MyTimeUtils.SHIFT);
}else { }else {
...@@ -75,46 +67,38 @@ public class AttendanceServiceImpl implements AttendanceService { ...@@ -75,46 +67,38 @@ public class AttendanceServiceImpl implements AttendanceService {
return list; return list;
} }
private String buildSqlQuery(String reportDate, String empIdsStr,String type) { private String buildSqlQuery(String reportDate, String empIdsStr, String type) {
String query = null; String query = null;
if(MyTimeUtils.PRESENT.equals(type)) { if(MyTimeUtils.PRESENT.equals(type)) {
query = MyTimeUtils.PRESENT_QUERY query = MyTimeUtils.PRESENT_QUERY.replace("<REPORTDATE>", reportDate)
.replace("<TYPE>", type)
.replace("<REPORTDATE>", reportDate)
.replace("<EMPIDS>",empIdsStr); .replace("<EMPIDS>",empIdsStr);
}else { }else {
query = MyTimeUtils.ABSENT_QUERY query = MyTimeUtils.ABSENT_QUERY.replace("<ABSENTLIST>", empIdsStr);
.replace("<ABSENTLIST>", empIdsStr);
} }
return query; return query;
} }
private List<AttendenceData> getAttendenceData(String query) throws MyTimeException { private List<AttendenceData> getAttendenceData(String query, String type) throws MyTimeException {
List<AttendenceData> listOfEmployees = null; List<AttendenceData> listOfEmployees = null;
try(Connection connection = dbConnection.getDBConnection();
try (Connection connection = dbConnection.getDBConnection();
Statement statement = connection.createStatement(); Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(query)) { ResultSet resultSet = statement.executeQuery(query)) {
AttendenceData attendData = null; AttendenceData attendData = null;
listOfEmployees = new ArrayList<AttendenceData>(); listOfEmployees = new ArrayList<AttendenceData>();
while (resultSet.next()) { while (resultSet.next()) {
attendData = new AttendenceData(); attendData = new AttendenceData();
attendData.setEmployeeId(resultSet.getString("EmployeeCode")); attendData.setEmployeeId(resultSet.getString("EmployeeCode"));
attendData.setEmployeeName(resultSet.getString("FirstName")); attendData.setEmployeeName(resultSet.getString("FirstName"));
attendData.setPresent(resultSet.getString("AttStatus")); attendData.setPresent(type);
listOfEmployees.add(attendData); listOfEmployees.add(attendData);
attendData = null; attendData = null;
} }
} catch (Exception e) {
}catch (Exception e) {
MyTimeLogger.getInstance().error("Exception occured due to : ", e); MyTimeLogger.getInstance().error("Exception occured due to : ", e);
throw new MyTimeException(e.getMessage()); throw new MyTimeException(e.getMessage());
} }
return listOfEmployees; return listOfEmployees;
} }
} }
...@@ -26,36 +26,32 @@ public class EmployeeDataService { ...@@ -26,36 +26,32 @@ public class EmployeeDataService {
@Autowired @Autowired
DbConnection dbConnection; DbConnection dbConnection;
public List<EmpLoginData> fetchEmployeeLoginsBasedOnDates(long employeeId, public List<EmpLoginData> fetchEmployeeLoginsBasedOnDates(long employeeId, String fromDate, String toDate) throws MyTimeException {
String fromDate, String toDate) throws MyTimeException {
long start_ms = System.currentTimeMillis(); long start_ms = System.currentTimeMillis();
String querys = null; String querys = null;
if (employeeId == 0) if (employeeId == 0) {
querys = MyTimeUtils.ABESENT_STATUS_QUERY + "'" + fromDate + "'" querys = MyTimeUtils.ABESENT_STATUS_QUERY + "'" + fromDate.replace("-", "/") + "'"
+ MyTimeUtils.ABESENT_STATUS_QUERY1 + "'" + toDate + "'" + MyTimeUtils.ABESENT_STATUS_QUERY1 + "'" + toDate.replace("-", "/") + "'"
+ MyTimeUtils.ABESENT_STATUS_QUERY3; + MyTimeUtils.ABESENT_STATUS_QUERY3;
else } else {
querys = MyTimeUtils.ABESENT_STATUS_QUERY + "'" + fromDate + "'" querys = MyTimeUtils.ABESENT_STATUS_QUERY + "'" + fromDate.replace("-", "/") + "'"
+ MyTimeUtils.ABESENT_STATUS_QUERY1 + "'" + toDate + "'" + MyTimeUtils.ABESENT_STATUS_QUERY1 + "'" + toDate.replace("-", "/") + "'"
+ MyTimeUtils.ABESENT_STATUS_QUERY2 + employeeId + MyTimeUtils.ABESENT_STATUS_QUERY2 + employeeId
+ MyTimeUtils.ABESENT_STATUS_QUERY3; + MyTimeUtils.ABESENT_STATUS_QUERY3;
}
int countHours = 0; int countHours = 0;
List<EmpLoginData> listOfEmpLoginData = new ArrayList<>(); List<EmpLoginData> listOfEmpLoginData = new ArrayList<>();
try (Connection connection = dbConnection.getDBConnection(); try (Connection connection = dbConnection.getDBConnection();
Statement statement = connection.createStatement(); Statement statement = connection.createStatement();
ResultSet resultSet = statement ResultSet resultSet = statement.executeQuery(querys.toString())) {
.executeQuery(querys.toString())) {
while (resultSet.next()) { while (resultSet.next()) {
EmpLoginData empLoginData = new EmpLoginData(); EmpLoginData empLoginData = new EmpLoginData();
empLoginData.setEmployeeId(resultSet.getString("EmployeeCode")); empLoginData.setEmployeeId(resultSet.getString("EmployeeCode"));
empLoginData.setEmployeeName(resultSet.getString("FirstName")); empLoginData.setEmployeeName(resultSet.getString("FirstName"));
empLoginData.setDateOfLogin( empLoginData.setDateOfLogin(resultSet.getDate("FirstLogin").toString());
resultSet.getDate("FirstLogin").toString()); empLoginData.setFirstLogin(resultSet.getTime("FirstLogin").toString());
empLoginData.setFirstLogin( empLoginData.setLastLogout(resultSet.getTime("LastLogin").toString());
resultSet.getTime("FirstLogin").toString());
empLoginData.setLastLogout(
resultSet.getTime("LastLogin").toString());
Time from = resultSet.getTime("FirstLogin"); Time from = resultSet.getTime("FirstLogin");
Time to = resultSet.getTime("LastLogin"); Time to = resultSet.getTime("LastLogin");
long milliseconds = to.getTime() - from.getTime(); long milliseconds = to.getTime() - from.getTime();
...@@ -64,31 +60,21 @@ public class EmployeeDataService { ...@@ -64,31 +60,21 @@ public class EmployeeDataService {
int minutes = (seconds % 3600) / 60; int minutes = (seconds % 3600) / 60;
seconds = (seconds % 3600) % 60; seconds = (seconds % 3600) % 60;
empLoginData.setTotalLoginTime( empLoginData.setTotalLoginTime(hours + ":" + minutes + ":" + seconds);
hours + ":" + minutes + ":" + seconds); empLoginData.setTotalLoginTime(hours + ":" + minutes + ":" + seconds);
empLoginData.setTotalLoginTime( Date d = MyTimeUtils.tdf.parse(empLoginData.getTotalLoginTime());
hours + ":" + minutes + ":" + seconds);
Date d = MyTimeUtils.tdf
.parse(empLoginData.getTotalLoginTime());
countHours += d.getTime(); countHours += d.getTime();
listOfEmpLoginData.add(empLoginData); listOfEmpLoginData.add(empLoginData);
} }
if (!listOfEmpLoginData.isEmpty()) { if (!listOfEmpLoginData.isEmpty()) {
listOfEmpLoginData.get(0).setTotalAvgTime(MyTimeUtils.tdf listOfEmpLoginData.get(0).setTotalAvgTime(MyTimeUtils.tdf.format(countHours / listOfEmpLoginData.size()));
.format(countHours / listOfEmpLoginData.size()));
} }
MyTimeLogger.getInstance().info(" Time Taken fecth Employee data based on Dates ::: " + (System.currentTimeMillis() - start_ms));
MyTimeLogger.getInstance()
.info(" Time Taken fecth Employee data based on Dates ::: "
+ (System.currentTimeMillis() - start_ms));
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
MyTimeLogger.getInstance().error(e.getMessage()); MyTimeLogger.getInstance().error(e.getMessage());
throw new MyTimeException(e.getMessage()); throw new MyTimeException(e.getMessage());
} }
return listOfEmpLoginData; return listOfEmpLoginData;
} }
} }
\ No newline at end of file
...@@ -114,10 +114,8 @@ public class UserServiceImpl implements UserService { ...@@ -114,10 +114,8 @@ public class UserServiceImpl implements UserService {
} }
@Override @Override
public List<EmpLoginData> employeeLoginsBasedOnDate(long id, public List<EmpLoginData> employeeLoginsBasedOnDate(long id, String fromDate, String toDate) throws MyTimeException {
String fromDate, String toDate) throws MyTimeException { return employeeDataBaseService.fetchEmployeeLoginsBasedOnDates(id, fromDate, toDate);
return employeeDataBaseService.fetchEmployeeLoginsBasedOnDates(id,
fromDate, toDate);
} }
@Override @Override
......
...@@ -28,14 +28,14 @@ public class MyTimeUtils { ...@@ -28,14 +28,14 @@ public class MyTimeUtils {
public final static String COLON = ":"; public final static String COLON = ":";
public final static String EMP_NAME_QUERY = "SELECT * FROM EMPLOYEES Where EMPLOYEECODE=?"; public final static String EMP_NAME_QUERY = "SELECT * FROM EMPLOYEES Where EMPLOYEECODE=?";
public final static String WORKING_EMPLOYEES = "SELECT * FROM EMPLOYEES WHERE EMPLOYEECODE NOT IN(SELECT UserId FROM DeviceLogs_12_2017 WHERE LogDate BETWEEN '2017-12-27 06:00:00' AND '2017-12-27 11:00:00') AND STATUS='Working'"; //public final static String WORKING_EMPLOYEES = "SELECT * FROM EMPLOYEES WHERE EMPLOYEECODE NOT IN(SELECT UserId FROM DeviceLogs_12_2017 WHERE LogDate BETWEEN '2017-12-27 06:00:00' AND '2017-12-27 11:00:00') AND STATUS='Working'";
public final static String QUERY = "SELECT * FROM DeviceLogs_"; //public final static String QUERY = "SELECT * FROM DeviceLogs_";
public final static String USERID_QUERY = "SELECT USERID FROM DeviceLogs_"; //public final static String USERID_QUERY = "SELECT USERID FROM DeviceLogs_";
public final static String WHERE_COND = " WHERE LogDate between '"; //public final static String WHERE_COND = " WHERE LogDate between '";
public final static String AND_COND = " AND '"; public final static String AND_COND = " AND '";
public final static String SINGLE_QUOTE = "'"; public final static String SINGLE_QUOTE = "'";
public final static String ABESENT_QUERY = "SELECT * FROM EMPLOYEES WHERE EMPLOYEECODE NOT IN("; //public final static String ABESENT_QUERY = "SELECT * FROM EMPLOYEES WHERE EMPLOYEECODE NOT IN(";
public final static String ABESENT_QUERY1 = ") AND STATUS='Working' AND EMPLOYEECODE NOT LIKE 'del%' "; //public final static String ABESENT_QUERY1 = ") AND STATUS='Working' AND EMPLOYEECODE NOT LIKE 'del%' ";
public final static String ABESENT = "Absent"; public final static String ABESENT = "Absent";
public final static String EMAIL_ID = "emailId"; public final static String EMAIL_ID = "emailId";
...@@ -80,26 +80,16 @@ public class MyTimeUtils { ...@@ -80,26 +80,16 @@ public class MyTimeUtils {
+ "MIN(tr.aDateTime) AS FirstLogin,MAX(tr.aDateTime) AS LastLogin\n" + + "MIN(tr.aDateTime) AS FirstLogin,MAX(tr.aDateTime) AS LastLogin\n" +
"from Transactions as tr,EmployeeMaster as emp\n" + "from Transactions as tr,EmployeeMaster as emp\n" +
"where tr.EmployeemasterID=emp.EmployeeMasterID and \n" + "where tr.EmployeemasterID=emp.EmployeeMasterID and \n" +
"replace(convert(varchar,tr.aDateTime, 111), '/', '-')>= "; "convert(varchar,tr.aDateTime, 111) >= ";
public final static String ABESENT_STATUS_QUERY1 =" and \n" +
"replace(convert(varchar,tr.aDateTime, 111), '/', '-')<= ";
public final static String ABESENT_STATUS_QUERY2 =" and emp.EmployeeCode=";
public final static String ABESENT_STATUS_QUERY3 =" group by convert(varchar,tr.aDateTime, 111),emp.EmployeeCode,emp.FirstName";
public final static String ATTENDANCE_PRESENT_REPORT_QUERY ="select distinct emp.EmployeeCode,emp.FirstName,'P' as AttStatus\n" + public final static String ABESENT_STATUS_QUERY1 =" and convert(varchar,tr.aDateTime, 111) <= ";
" FROM Transactions as tr,EmployeeMaster as emp where tr.EmployeemasterID=emp.EmployeeMasterID and \n" + public final static String ABESENT_STATUS_QUERY2 =" and emp.EmployeeCode = ";
" replace(convert(varchar,tr.aDateTime, 111), '/', '-')= "; public final static String ABESENT_STATUS_QUERY3 =" group by convert(varchar,tr.aDateTime, 111), emp.EmployeeCode, emp.FirstName";
public final static String UNION=" Union "; public final static String UNION=" Union ";
public final static String ATTENDANCE_ABSENT_REPORT_QUERY= "select distinct M.EmployeeCode,M.FirstName,'A' as AttStatus from [smartiSCC].[dbo].[EmployeeMaster] M \n" +
"where M.EmployeeCode not in \n" +
" (select distinct emp.EmployeeCode \n" +
" FROM Transactions as tr,EmployeeMaster as emp \n" +
" where tr.EmployeemasterID=emp.EmployeeMasterID and \n" +
" replace(convert(varchar,tr.aDateTime, 111), '/', '-')=";
public final static String PRESENT_QUERY = "SELECT DISTINCT Emp.EmployeeCode, Emp.FirstName FROM Transactions AS Tr, EmployeeMaster AS Emp WHERE Tr.EmployeemasterID = Emp.EmployeeMasterID AND CONVERT(VARCHAR,Tr.aDateTime, 111) = '<REPORTDATE>' AND Emp.EmployeeCode IN (<EMPIDS>)";
public final static String PRESENT_QUERY = "SELECT DISTINCT Emp.EmployeeCode,Emp.FirstName,'<TYPE>' AS AttStatus FROM Transactions AS Tr,EmployeeMaster AS Emp WHERE Tr.EmployeemasterID = Emp.EmployeeMasterID AND REPLACE(CONVERT(VARCHAR,Tr.aDateTime, 111), '/', '-')= '<REPORTDATE>' AND Emp.EmployeeCode IN (<EMPIDS>)"; public final static String ABSENT_QUERY = "SELECT [EmployeeCode], [FirstName] FROM [EmployeeMaster] WHERE [EmployeeCode] IN(<ABSENTLIST>)";
public final static String ABSENT_QUERY = "SELECT [EmployeeCode], [FirstName],'A' AS AttStatus FROM [smartiSCC].[dbo].[EmployeeMaster] WHERE [EmployeeCode] IN(<ABSENTLIST>)";
public final static String PRESENT = "P"; public final static String PRESENT = "P";
public final static String ABSENT = "A"; public final static String ABSENT = "A";
......
...@@ -39,6 +39,8 @@ spring.mvc.favicon.enabled = false ...@@ -39,6 +39,8 @@ spring.mvc.favicon.enabled = false
#MS SQL configuration #MS SQL configuration
myTime.data.mssqldb.driver=com.microsoft.sqlserver.jdbc.SQLServerDriver myTime.data.mssqldb.driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
myTime.data.mssqldb.url=jdbc:sqlserver://10.3.45.105:1433;databaseName=smartiSCC #myTime.data.mssqldb.url=jdbc:sqlserver://10.3.45.105:1433;databaseName=smartiSCC
myTime.data.mssqldb.url=jdbc:sqlserver://10.3.45.218:1433;databaseName=smartiSCC
myTime.data.mssqldb.username=sa myTime.data.mssqldb.username=sa
myTime.data.mssqldb.password=nisum@123 #myTime.data.mssqldb.password=nisum@123
\ No newline at end of file myTime.data.mssqldb.password=admin@123
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment