简体   繁体   中英

Group logging entries if time span overlap and less than 30 minutes?

I have log entries for a single user that exist in the following format:

[unique id], [start time],[ end time]

So, in the following example entries:

1,1100,1200
2,1030,1130
3,1420,1500
4,1519,1700

Find Sessions ie, group log entries into as 'sessions'. The conditions to determine a session are:

  1. If two entries time span overlap, then they belong to the same session.
  2. Or if not overlapping, but gap in between < 30, then they belong to the same session.

Example: Output should be like:

Session 1: 1, 2
Session 2: 3, 4

Logic what I am thinking is:

  • Parse the string and load it in "LogEntries" class.
  • Sort "entries" collection basis on "startTime". I have "LogEntries" class implemented "Comparable" interface.
  • Now iterate "entries" collection and get the required output. Output will be a list of string where each string will be comma separated.

I came up with below code but I am confuse on how to work on point 3 logic above.

  private static List<String> groupSessions(List<String> inputs) {
    List<String> output = new ArrayList<>();
    List<LogEntries> entries = new ArrayList<>();
    for (String input : inputs) {
      String[] arr = input.split(",");
      LogEntries entry =
          new LogEntries(Integer.parseInt(arr[0]), Integer.parseInt(arr[1]),
              Integer.parseInt(arr[2]));
      entries.add(entry);
    }

    // sort it basis on startTime
    Collections.sort(entries);

    // now iterate the entries list - this is where I am confuse
    for (int i = 0; i < entries.size(); i++) {
      // do some stuff
    }

    return output;
  }

Some thoughts:

  • you are representing your timestamps as int/Integer values. That allows for simple sorting, but will make later computations (like getting the delta between two timestamps harder). You could consider creating a distinct class to represent these hour:minute values.
  • for solving your task: start by doing that on a piece of paper. Take your input example and start by sorting that list based on the start times.
  • looking at the sorted timestamps, look at the first entry. Obviously, that must be the begin of a session. Now you simply look at the end time of that first entry and the start time of the subsequent entry. Overlap? Then session one contuines to the end time of the second entry. No overlap, then you compute "start time (second) - end time first". Smaller than 30 minutes? Session continues, so you compare against the next end time again. Otherwise, a session ended, and the next entry is the begin of the next session. Repeat.

Long story short: you have to first develop the algorithm that tells you how to determine sessions. Then you turn that sequence of instructions into code. The key is to first conceptually dissect the big problem into its smallest parts and to then see how to bring them together.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM