2

I'm trying to filter a list of processes and group together processes with the same name. It works however the grouped processes are duplicated and stay as their own.

I have tried messing with how it does it, checking if it already exists in the object, and a dozen other things.

You can see the raw data I'm trying to process here

Here is the code I have so far:

var seen = {};
data = data.filter(async function (entry) {
  var previous;
  if (seen.hasOwnProperty(entry[1].name)) {
    previous = seen[entry[1].name];
    var temp = JSON.parse(JSON.stringify(entry[1]));

    if (!previous[1]["grouped"]) {
      previous[1]["grouped"] = true;
      previous[1].pid = "";
      previous[1].ppid = "";
      previous[1].ctime = "";
      previous[1].elapsed = "";
      previous[1].timestamp = "";

      previous[1]["subps"] = [];
      previous[1]["subps"].push(temp);
    }

    previous[1]["memory"] += entry[1].memory;
    previous[1]["cpu"] += entry[1].cpu;
    previous[1]["subps"].push(temp);
    return false;
  } else {
    seen[entry[1].name] = entry;
    return true;
  }
});
console.log(JSON.stringify(data, null, "  "));

Here is the output from said code. Here is the expected output.

Either I'm missing something really simple or I'm just stupid. Probably both.

ps. I have no idea what to title this.

4
  • hi, so what was the expected output vs actual? Commented Jan 7, 2022 at 3:31
  • @jspcal Sorry about that, I edited the question to include the expected output. Commented Jan 7, 2022 at 3:36
  • Can you use lodash library ? Commented Jan 7, 2022 at 4:31
  • @MayurVaghasiya How would that help? I took a look at it and it seems to just help with performance and tidbits. Commented Jan 7, 2022 at 4:35

1 Answer 1

1

The filter will not return your expected output, because it'll always return a data object from an existing array, not the one you created.

var data = [
  [
    "0",
    {
      cpu: 0,
      memory: 400150528,
      ppid: 4,
      pid: 3612,
      ctime: 8594,
      elapsed: 10784310,
      timestamp: 1641346226906,
      name: "Memory Compression",
    },
  ],
  [
    "1",
    {
      cpu: 0.2,
      memory: 336556032,
      ppid: 3012,
      pid: 1312,
      ctime: 61844,
      elapsed: 637176,
      timestamp: 1641346226906,
      name: "chrome.exe",
    },
  ],
  [
    "2",
    {
      cpu: 0.4,
      memory: 285069312,
      ppid: 7772,
      pid: 13172,
      ctime: 404125,
      elapsed: 2139440,
      timestamp: 1641346226906,
      name: "Code.exe",
    },
  ],
  [
    "3",
    {
      cpu: 6.6375,
      memory: 284061696,
      ppid: 5516,
      pid: 6636,
      ctime: 452234,
      elapsed: 4046397,
      timestamp: 1641346226906,
      name: "Discord.exe",
    },
  ],
  [
    "4",
    {
      cpu: 35.725,
      memory: 274665472,
      ppid: 15328,
      pid: 15232,
      ctime: 804718,
      elapsed: 1998606,
      timestamp: 1641346226906,
      name: "node.exe",
    },
  ],
  [
    "5",
    {
      cpu: 0,
      memory: 236437504,
      ppid: 14508,
      pid: 22016,
      ctime: 41828,
      elapsed: 316914,
      timestamp: 1641346226906,
      name: "Code.exe",
    },
  ],
  [
    "6",
    {
      cpu: 0.5875,
      memory: 231018496,
      ppid: 7772,
      pid: 14508,
      ctime: 53750,
      elapsed: 2130762,
      timestamp: 1641346226906,
      name: "Code.exe",
    },
  ],
  [
    "7",
    {
      cpu: 0,
      memory: 199610368,
      ppid: 2536,
      pid: 10656,
      ctime: 32469,
      elapsed: 10165205,
      timestamp: 1641346226906,
      name: "pia-client.exe",
    },
  ],
  [
    "8",
    {
      cpu: 0.975,
      memory: 193478656,
      ppid: 2536,
      pid: 3012,
      ctime: 144406,
      elapsed: 5764764,
      timestamp: 1641346226906,
      name: "chrome.exe",
    },
  ],
  [
    "9",
    {
      cpu: 0,
      memory: 186900480,
      ppid: 1104,
      pid: 8076,
      ctime: 8672,
      elapsed: 10140626,
      timestamp: 1641346226906,
      name: "SearchApp.exe",
    },
  ],
];

const result = data.reduce((seen, [_, entry]) => {
  if (seen.hasOwnProperty(entry.name)) {
    var previous = seen[entry.name] ?? {};
    // var temp = JSON.parse(JSON.stringify(entry));
    if (!previous["grouped"]) {
      previous["grouped"] = true;
      previous.pid = "";
      previous.ppid = "";
      previous.ctime = "";
      previous.elapsed = "";
      previous.timestamp = "";
      previous["subps"] = []; // For duplicate entries add here also, added in expected output previous["subps"] = [entry]
    }
    previous["memory"] += entry.memory;
    previous["cpu"] += entry.cpu;
    previous["subps"].push(entry);
  } else {
    seen[entry.name] = entry;
  }
  return seen;
}, {});
console.log(JSON.stringify(Object.values(result), null, "  "));

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.