How can I merge table inside an other table base on the value of a field?

70 Views Asked by At

I have a table like this:

╭─────┬────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│   0 │ ╭───┬────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮ │
│     │ │ 0 │ 2023-09-22 17:49:03.301                                                                                                        │ │
│     │ │ 1 │ INFO:     10.0.0.30:49398 - "GET ..." 200 OK                                                                                   │ │
│     │ ╰───┴────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │
│   1 │ ╭───┬────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮ │
│     │ │ 0 │ 2023-09-22 17:44:11.024                                                                                                        │ │
│     │ │ 1 │ INFO:     10.0.2.177:15974 - "GET ..." 404                                                                                     │ │
│     │ ╰───┴────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │
│   2 │ ╭───┬────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮ │
│     │ │ 0 │ 2023-09-22 17:44:11.024                                                                                                        │ │
│     │ │ 1 │ INFO:     10.0.0.30:47184 - "GET ..." 500                                                                                      │ │
│     │ ╰───┴────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │

How can I merge line 1 for each cells if the date from line 0 is the same? To add more context it's a display of AWS logs and I want to merge the lines that are from the same request (so with the same timestamp) to have a better view of the logs.

In the example above I would like to have a new table (or the previous one updated) to have something like this:

╭─────┬────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│   0 │ ╭───┬────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮ │
│     │ │ 0 │ 2023-09-22 17:49:03.301                                                                                                        │ │
│     │ │ 1 │ INFO:     10.0.0.30:49398 - "GET ..." 200 OK                                                                                   │ │
│     │ ╰───┴────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │
│   1 │ ╭───┬────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮ │
│     │ │ 0 │ 2023-09-22 17:44:11.024                                                                                                        │ │
│     │ │ 1 │ INFO:     10.0.2.177:15974 - "GET ..." 404                                                                                     │ │
|     | |   | INFO:     10.0.0.30:47184 - "GET ..." 500                                                                                      │ │
│     │ ╰───┴────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │
1

There are 1 best solutions below

0
pmf On

Assuming, the underlying data structure is two nested arrays of strings like below:

[
  ['2023-09-22 17:49:03.301', 'INFO:     10.0.0.30:49398 - "GET ..." 200 OK'], 
  ['2023-09-22 17:44:11.024', 'INFO:     10.0.2.177:15974 - "GET ..." 404'], 
  ['2023-09-22 17:44:11.024', 'INFO:     10.0.0.30:47184 - "GET ..." 500']
]

Then, you can use group-by to group the input array by their first items (the date lines) using column 0, then iterate over the groups using items to capture both their grouping key (the date) and the array of input values grouped together. For each, take the latter, skip over their first items (the common date lines) using skip with amout 1, and flatten out their contents. Finally, prepend the captured dates to each group:

group-by 0 | items {|date, logs|
  $logs | each {skip 1} | flatten | prepend $date
}
╭───┬──────────────────────────────────────────────────────╮
│ 0 │ ╭───┬──────────────────────────────────────────────╮ │
│   │ │ 0 │ 2023-09-22 17:49:03.301                      │ │
│   │ │ 1 │ INFO:     10.0.0.30:49398 - "GET ..." 200 OK │ │
│   │ ╰───┴──────────────────────────────────────────────╯ │
│ 1 │ ╭───┬────────────────────────────────────────────╮   │
│   │ │ 0 │ 2023-09-22 17:44:11.024                    │   │
│   │ │ 1 │ INFO:     10.0.2.177:15974 - "GET ..." 404 │   │
│   │ │ 2 │ INFO:     10.0.0.30:47184 - "GET ..." 500  │   │
│   │ ╰───┴────────────────────────────────────────────╯   │
╰───┴──────────────────────────────────────────────────────╯