1

I need to count the points of a chart for a duration picked by an user.

My chart is about logs, we need to know how many users are connected for every specified minutes while a specific duration. We don't want to use too many queries, so I work essentially with lists.

Dates are stored in String format as "yyyy-MM-dd HH:mm:ss".

My code works but is really too long to load:

    old=ofy().load().type(Log.class).filter("site in",selectedSites).filter("license in",ofy().load().type(License.class).filter("name", name)).filter("date_out >=",datemin).filter("date_out <=",datemax).list();

    Duration duration = new Duration(firstIn, lastDateOut); //Duration between two dates choosen by the user
    int dimension=(int) ((duration.getStandardMinutes())/divideBy); //Number of abscissa points in the chart
    DateTime[] dates=new DateTime[dimension+1]; //Init of the dates 
    int[] counts=new int[dimension+1]; //Init of the count table (count of logged users at the date
    DateTime transfert=firstIn; //First date 


    for(int i=0;i<=dimension;i++){
        counts[i]=0;
        dates[i]=transfert.minusSeconds(transfert.getSecondOfMinute());
        transfert=transfert.plusMinutes(divideBy);
        for(Log log:old){
            if((StaticMethods.toDateTime(log.getDate_in()).minusSeconds(StaticMethods.toDateTime(log.getDate_in()).getSecondOfMinute()).equals(dates[i]))
                ||((StaticMethods.toDateTime(log.getDate_in()).minusSeconds(StaticMethods.toDateTime(log.getDate_in()).getSecondOfMinute()).isBefore(dates[i]))
                        &&(StaticMethods.toDateTime(log.getDate_out()).minusSeconds(StaticMethods.toDateTime(log.getDate_out()).getSecondOfMinute()).isAfter(dates[i])))
                ||(StaticMethods.toDateTime(log.getDate_out()).minusSeconds(StaticMethods.toDateTime(log.getDate_out()).getSecondOfMinute()).equals(dates[i]))
            ){
                counts[i]++;
            }
        }
        GraphData nw=new GraphData(dates[i].toDate(), counts[i]);
    }

I want to know if there is a possible less loading time (have read this and I need to know if there's similar way for approximate values).

Community
  • 1
  • 1
Pauline
  • 35
  • 6

1 Answers1

0

you should order your data by Date_out value first, in ascending order. Also you don't need tables for Dates and Counts, try the following instead

Duration duration = new Duration(firstIn, lastDateOut); //Duration between two dates choosen by the user
int dimension=(int) ((duration.getStandardMinutes())/divideBy); //Number of abscissa points in the chart
DateTime transfert=firstIn; //First date
DateTime currentDate=transfert; // X-Axis Date
int currentCount=0; // Y-Axis LoggedUser Count

//Log data
DateTime log_DateIn;
DateTime log_DateOut;

for(int i=0;i<=dimension;i++)
{
   currentDate = transfert;
   currentCount = 0;
   transfert = transfert.plusMinutes(divideBy);

   for(Log log:old)
   {
      // We store our dates into variables, that way we avoid doing the time conversion twice
      log_DateIn = StaticMethods.toDateTime(log.getDate_in()).minusSeconds(StaticMethods.toDateTime(log.getDate_in()).getSecondOfMinute());

      log_DateOut = StaticMethods.toDateTime(log.getDate_out()).minusSeconds(StaticMethods.toDateTime(log.getDate_out()).getSecondOfMinute());

      // Since we made the list ordered by DateOut, we are sure that if the stored DateOut is greater than transfert we can't find another user, that way we can break the for without parsing each log
      if(log_DateOut.isAfter(transfert))
         break;

      // Now we can do checks to see if we need to increase the count
      // We just check if the DateIn is between the currentDate and currentDate + divideBy (transfert)
      if(log_DateIn.isAfter(currentDate) && log_DateIn.isBefore(transfert))
         currentCount++;
      // Same thing for DateOut
      else if(log_DateOut.isAfter(currentDate) && log_DateOut.isBefore(transfert))
         currentCount++;
      // Now we need both cases
      else if(log_DateIn.isBefore(currentDate) && log_DateOut.isAfter(transfert))
         currentCount++;
      // Equalities
      else if(log_DateIn.equals(currentDate) || log_DateIn.equals(transfert) || log_DateOut.equals(currentDate) || log_DateOut.equals(transfert))
         currentCount++;
   }

   GraphData nw = new GraphData(currentDate, currentCount);
}
Chaussette
  • 26
  • 4