+ Start a Discussion
bsil bsilbsil bsil 

login history

how to filter login history records based on login time.

my requirments is for ex: assum that we have 100 records for one user. out of that 10 recordes data like the below.

loginType,application,sourceip,logindate is same.
but the time is different.
in case if we have time difference is less than 5 minutes then we have to avoid all and consider one reccord.

acctually i am inserting this login history in to some custom object:

see the blow code:
global class loginCopybatch implements Database.Batchable<sObject> {     
     
    global set<Newloginhistoryobject__c> loginHistoryList = new set<Newloginhistoryobject__c>();
    global list<Newloginhistoryobject__c> loginHistoryList1 = new list<Newloginhistoryobject__c>();
    global Map <Id,User> userList = new Map<Id, User>();
    global list<user> users = new list<user>();
    global String[] newLoginHistoryIds;
    // created map on feb,03,2015
    global map<id,list<loginhistory>> loginhistorymap;
    // ends
    
    global loginCopybatch()
    {
       // feb, 03, 2015 
       loginhistorymap = new map<id,list<loginhistory>>();
       //ends
       users = [SELECT ID, FIRSTNAME,LASTNAME,EMAIL,UserRole.Name,username FROM USER];
       for(user u : users)
       {
           userList.put(u.id, u);
       }
       List<Newloginhistoryobject__c> newLoginHistoryIdsList = [select LoginHistoryID_Ref__c from Newloginhistoryobject__c where logindate__c = LAST_N_DAYS:10];
       newLoginHistoryIds = new String[newLoginHistoryIdsList.size()];
       Integer i=0;
       try
       {
          for(Newloginhistoryobject__c loginHistoryId : newLoginHistoryIdsList)
          {
              newLoginHistoryIds[i++] = (String)(loginHistoryId.LoginHistoryID_Ref__c);
          }
       }
        catch(Exception e)
        {
            System.debug('Exception1');
            throw e;
        }
 
    }
    
    global list<loginhistory> start(Database.BatchableContext BC) 
    {  
    
        try
        {   
           // string str = '005K0000002SQsE';
            String query = 'select id,userid,status,SourceIp,logintime,application,logintype from loginhistory where logintime = LAST_N_DAYS:7 and id not in :newLoginHistoryIds order by logintime asc';
           // String query = 'select id,userid,status,SourceIp,logintime,application,logintype from loginhistory where logintime = today and id not in :newLoginHistoryIds and userid =:str order by logintime asc';
            return Database.Query(query);
        }
        catch(Exception e)
        {
            throw e;
        }
    }
   
    global void execute(Database.BatchableContext BC, List<loginhistory> scope) 
    {
         try
         {
             system.debug('*****Before Map'+loginhistorymap);
             for(loginhistory rec : scope)
             {
                    // feb, 03, 2015
                    if(rec.application != 'Salesforce for Outlook'){
                        if(!loginhistorymap.keySet().contains(rec.userid)){
                            loginhistorymap.put(rec.userid, new List <loginhistory> {rec});
                        }
                        else{
                            List<loginhistory> oldList = loginhistorymap.get(rec.userid);
                            oldList.add(rec);
                            loginhistorymap.put(rec.userid, oldList);
                        }
                    }                     
                           
            }
            // feb, 03, 2015
            //integre count = 0;
            system.debug('*****After Map'+loginhistorymap);
            List <loginhistory> lh ;                          
                for(Id id : loginhistorymap.keySet()){
                    lh = new List <loginhistory>();  
                    lh.addAll(loginhistorymap.get(id));
                    system.debug('111111111111111111:' +lh.size());
                    integer lhsize = lh.size()-1;
                    //
                    Newloginhistoryobject__c   newLoginHistory1 = new Newloginhistoryobject__c ();  
                            newLoginHistory1.SourceIp__c = lh[lhsize].sourceIp;
                            newLoginHistory1.First_Name__c = userList.get(lh[lhsize].userId).firstname;
                            newLoginHistory1.LastName__c = userList.get(lh[lhsize].userId).LastName;
                            newLoginHistory1.Email__c = userList.get(lh[lhsize].userId).email;
                            newLoginHistory1.loginapplication__c = lh[lhsize].application;
                            newLoginHistory1.logindatetime__c = lh[lhsize].logintime;
                            newLoginHistory1.logindate__c = (lh[lhsize].logintime).date();
                            newLoginHistory1.logintype__c = lh[lhsize].logintype;
                            newLoginHistory1.LoginHistoryID_Ref__c = lh[lhsize].id;
                            newLoginHistory1.loginstatus__c = lh[lhsize].Status;
                            newLoginHistory1.userid__c = lh[lhsize].UserId;
                            newLoginHistory1.UserRole__c = userList.get(lh[lhsize].userId).UserRole.name;
                            newLoginHistory1.User_Name__c = userList.get(lh[lhsize].userId).id;  
                            //System.debug(loginHistoryList);
                            loginHistoryList.add(newLoginHistory1);
                            //
                    for(integer i = 0;i<lh.size();i++){
                        for(integer j = i+1;j<lh.size();j++){
                        system.debug('*****LH'+lh[i]);
                        system.debug('*****LH +1'+lh[j]);                        
                        if(lh[i].application == lh[j].application && lh[i].logintype == lh[j].logintype && lh[i].SourceIp == lh[j].SourceIp ){
                                                system.debug('*****ALL ARE EQUAL'); 
                            if(lh[i].logintime.date() == lh[i+1].logintime.date()){
                                                system.debug('*****SAME DATE');                                                             
                                Long dt1Long = lh[i].logintime.getTime();
                                Long dt2Long = lh[j].logintime.getTime();
                                Long milliseconds = dt2Long - dt1Long;
                                Long seconds = milliseconds / 1000;
                                long minutes = seconds / 60; 
                                 system.debug('*****MINUTES'+minutes); 
                                if(minutes < 5){
                                                                 system.debug('*****MINUTES < 5'+minutes);
                                    Newloginhistoryobject__c   newLoginHistory = new Newloginhistoryobject__c (); 
                                    newLoginHistory.SourceIp__c = lh[j].sourceIp;
                                    newLoginHistory.First_Name__c = userList.get(lh[j].userId).firstname;
                                    newLoginHistory.LastName__c = userList.get(lh[j].userId).LastName;
                                    newLoginHistory.Email__c = userList.get(lh[j].userId).email;
                                    newLoginHistory.loginapplication__c = lh[j].application;
                                    newLoginHistory.logindatetime__c = lh[j].logintime;
                                    newLoginHistory.logindate__c = (lh[j].logintime).date();
                                    newLoginHistory.logintype__c = lh[j].logintype;
                                    newLoginHistory.LoginHistoryID_Ref__c = lh[j].id;
                                    newLoginHistory.loginstatus__c = lh[j].Status;
                                    newLoginHistory.userid__c = lh[j].UserId;
                                    newLoginHistory.UserRole__c = userList.get(lh[j].userId).UserRole.name;
                                    newLoginHistory.User_Name__c = userList.get(lh[j].userId).id;  
                                    //System.debug(loginHistoryList);
                                    loginHistoryList.add(newLoginHistory);
                                    lh.remove(i);
                                }
                                else{
                                                                 system.debug('*****MINUTES > 5'+minutes); 
                                    Newloginhistoryobject__c   newLoginHistory = new Newloginhistoryobject__c ();  
                                    newLoginHistory.SourceIp__c = lh[i].sourceIp;
                                    newLoginHistory.First_Name__c = userList.get(lh[i].userId).firstname;
                                    newLoginHistory.LastName__c = userList.get(lh[i].userId).LastName;
                                    newLoginHistory.Email__c = userList.get(lh[i].userId).email;
                                    newLoginHistory.loginapplication__c = lh[i].application;
                                    newLoginHistory.logindatetime__c = lh[i].logintime;
                                    newLoginHistory.logindate__c = (lh[i].logintime).date();
                                    newLoginHistory.logintype__c = lh[i].logintype;
                                    newLoginHistory.LoginHistoryID_Ref__c = lh[i].id;
                                    newLoginHistory.loginstatus__c = lh[i].Status;
                                    newLoginHistory.userid__c = lh[i].UserId;
                                    newLoginHistory.UserRole__c = userList.get(lh[i].userId).UserRole.name;
                                    newLoginHistory.User_Name__c = userList.get(lh[i].userId).id;  
                                    //System.debug(loginHistoryList);
                                    loginHistoryList.add(newLoginHistory);
                                    
                                }                               
                            }
                            else{
                            system.debug('*****ALL ARE NOT EQUAL');
                            Newloginhistoryobject__c   newLoginHistory = new Newloginhistoryobject__c ();  
                            newLoginHistory.SourceIp__c = lh[i].sourceIp;
                            newLoginHistory.First_Name__c = userList.get(lh[i].userId).firstname;
                            newLoginHistory.LastName__c = userList.get(lh[i].userId).LastName;
                            newLoginHistory.Email__c = userList.get(lh[i].userId).email;
                            newLoginHistory.loginapplication__c = lh[i].application;
                            newLoginHistory.logindatetime__c = lh[i].logintime;
                            newLoginHistory.logindate__c = (lh[i].logintime).date();
                            newLoginHistory.logintype__c = lh[i].logintype;
                            newLoginHistory.LoginHistoryID_Ref__c = lh[i].id;
                            newLoginHistory.loginstatus__c = lh[i].Status;
                            newLoginHistory.userid__c = lh[i].UserId;
                            newLoginHistory.UserRole__c = userList.get(lh[i].userId).UserRole.name;
                            newLoginHistory.User_Name__c = userList.get(lh[i].userId).id;  
                            //System.debug(loginHistoryList);
                            loginHistoryList.add(newLoginHistory);
                        }
                              
                        }
                        else{
                            system.debug('*****ALL ARE NOT EQUAL');
                            Newloginhistoryobject__c   newLoginHistory = new Newloginhistoryobject__c ();  
                            newLoginHistory.SourceIp__c = lh[i].sourceIp;
                            newLoginHistory.First_Name__c = userList.get(lh[i].userId).firstname;
                            newLoginHistory.LastName__c = userList.get(lh[i].userId).LastName;
                            newLoginHistory.Email__c = userList.get(lh[i].userId).email;
                            newLoginHistory.loginapplication__c = lh[i].application;
                            newLoginHistory.logindatetime__c = lh[i].logintime;
                            newLoginHistory.logindate__c = (lh[i].logintime).date();
                            newLoginHistory.logintype__c = lh[i].logintype;
                            newLoginHistory.LoginHistoryID_Ref__c = lh[i].id;
                            newLoginHistory.loginstatus__c = lh[i].Status;
                            newLoginHistory.userid__c = lh[i].UserId;
                            newLoginHistory.UserRole__c = userList.get(lh[i].userId).UserRole.name;
                            newLoginHistory.User_Name__c = userList.get(lh[i].userId).id;  
                            //System.debug(loginHistoryList);
                            loginHistoryList.add(newLoginHistory);
                        }
                        }//second for loop
                    }//first forloop
                  // lh.clear(); 
                } //main for loop
            //ends
        }
         catch(Exception e)
         {
         }
         if(loginHistoryList.size() != null)
         {
             loginHistoryList1.addAll(loginHistoryList);
             insert loginHistoryList1;             
         }
         
    }   
    
}