Filtering out schedule data to display

Viewed 45

Hello I'm trying to filter out data that shouldn't be on this schedule schedule

I currently have this function that has the filter data

       const formatData = (input: any) => {
    const data: any = _.uniqBy(input.Value, 'Activity Id');
    const formatDate = (text: any) =>
      moment(text as string).format('YYYY-MM-DD');
    // Data being returned from thirdparty isn't filtering out certain data
    const filtered = data.filter(
      (e: any) =>
        (formatDate(e['Start Date ']) <= formatDate(query.Start_Date) &&
          formatDate(e['End Date']) >= formatDate(query.End_Date)) ||
        (formatDate(e['Start Date ']) >= formatDate(query.Start_Date) &&
          formatDate(e['End Date']) <= formatDate(query.End_Date)) ||
        (formatDate(e['Start Date ']) >= formatDate(query.Start_Date) &&
          formatDate(e['Start Date ']) <= formatDate(query.End_Date))
    );
    return { Value: filtered };
  };

The filter isn't correct I'm trying to figure simple way of filtering out dates that aren't between those two query req.query.end and start

This sample of the data set

 {
  Value: [
    {
      "Activity Id": "05005413-05",
      "Activity Name": "UNIT 1  ASBESTOS REMOVAL; PERMITS (CSP, FSI, HWP)",
      "Start Date ": "2020-11-09 07:00:00.0",
      "End Date": "2021-05-17 15:00:00.0",
      "Work Group Name": "PSCF",
      "Unit ": "01",
      Status: "READY",
      "Work Pln Factor": ", ",
    },
    {
      "Activity Id": "B2119",
      "Activity Name": "Begin Work Week 2119",
      "Start Date ": "2021-05-10 00:00:00.0",
      "End Date": "2021-05-10 00:00:00.0",
      "Work Pln Factor": ", ",
    },
    {
      "Activity Id": "B2118",
      "Activity Name": "Begin Work Week 2118",
      "Start Date ": "2021-05-03 00:00:00.0",
      "End Date": "2021-05-03 00:00:00.0",
      "Work Pln Factor": ", ",
    },
    {
      "Activity Id": "04901693-05",
      "Activity Name": "NEW LPRM IV TEST RE-SET PM FOR NEXT CYCLE.",
      "Start Date ": "2021-04-23 01:00:00.0",
      "End Date": "2021-04-23 03:00:00.0",
      "Unit ": "02",
      Status: "PLAN",
      "Work Pln Factor": ", ",
    },
    {
      "Activity Id": "04901693-04",
      "Activity Name": "20C037           IV CURVES ON SELECTED U2 LPRM'S",
      "Start Date ": "2021-04-22 07:00:00.0",
      "End Date": "2021-04-22 15:00:00.0",
      "Work Group Name": "PMI3",
      "Unit ": "02",
      Status: "PLAN",
      "Work Pln Factor": ", ",
    },
    {
      "Activity Id": "B2116",
      "Activity Name": "Begin Work Week 2116",
      "Start Date ": "2021-04-19 00:00:00.0",
      "End Date": "2021-04-19 00:00:00.0",
      "Work Pln Factor": ", ",
    },
    {
      "Activity Id": "B2121",
      "Activity Name": "Begin Work Week 2121",
      "Start Date ": "2021-05-24 00:00:00.0",
      "End Date": "2021-05-24 00:00:00.0",
      "Work Pln Factor": ", ",
    },
    {
      "Activity Id": "05005413-35",
      "Activity Name": "SCAFFOLD SUPPORT TEAM - CARPENTERS & LABORERS",
      "Start Date ": "2020-11-09 06:00:00.0",
      "End Date": "2021-06-03 16:00:00.0",
      "Work Group Name": "PSCF",
      "Unit ": "01",
      Status: "READY",
      "Work Pln Factor": ", ",
    },
    {
      "Activity Id": "04912309-35",
      "Activity Name": "SCAFFOLD SUPPORT TEAM - CARPENTERS & LABORERS",
      "Start Date ": "2020-11-09 06:00:00.0",
      "End Date": "2021-06-30 16:00:00.0",
      "Work Group Name": "PSCF",
      "Unit ": "01",
      Status: "READY",
      "Work Pln Factor": ", ",
    },
    {
      "Activity Id": "04224121-04",
      "Activity Name": "REBUILD CAT ID 0011698252 AND RETURN TO STORES.",
      "Start Date ": "2021-05-03 07:00:00.0",
      "End Date": "2021-05-05 15:00:00.0",
      "Work Group Name": "PME1",
      "Unit ": "03",
      Status: "READY",
      "Work Pln Factor": ", ",
    },
  ];
}

So if the start time if start=23-JUN-2021 end=19-APR-2021 this was be the expected returned result

 {
  Value: [
    {
      "Activity Id": "05005413-05",
      "Activity Name": "UNIT 1  ASBESTOS REMOVAL; PERMITS (CSP, FSI, HWP)",
      "Start Date ": "2020-11-09 07:00:00.0",
      "End Date": "2021-05-17 15:00:00.0",
      "Work Group Name": "PSCF",
      "Unit ": "01",
      Status: "READY",
      "Work Pln Factor": ", ",
    },
    {
      "Activity Id": "B2119",
      "Activity Name": "Begin Work Week 2119",
      "Start Date ": "2021-05-10 00:00:00.0",
      "End Date": "2021-05-10 00:00:00.0",
      "Work Pln Factor": ", ",
    },

    {
      "Activity Id": "B2116",
      "Activity Name": "Begin Work Week 2116",
      "Start Date ": "2021-04-19 00:00:00.0",
      "End Date": "2021-04-19 00:00:00.0",
      "Work Pln Factor": ", ",
    },
    {
      "Activity Id": "05005413-35",
      "Activity Name": "SCAFFOLD SUPPORT TEAM - CARPENTERS & LABORERS",
      "Start Date ": "2020-11-09 06:00:00.0",
      "End Date": "2021-06-03 16:00:00.0",
      "Work Group Name": "PSCF",
      "Unit ": "01",
      Status: "READY",
      "Work Pln Factor": ", ",
    },
    {
      "Activity Id": "04912309-35",
      "Activity Name": "SCAFFOLD SUPPORT TEAM - CARPENTERS & LABORERS",
      "Start Date ": "2020-11-09 06:00:00.0",
      "End Date": "2021-06-30 16:00:00.0",
      "Work Group Name": "PSCF",
      "Unit ": "01",
      Status: "READY",
      "Work Pln Factor": ", ",
    },
  ];
}

This is the function that handles displaying the bars in the frontend

  const timeLine = (data: any, day: string) => {
    return (data || []).map((activity: any) => {
      const startMoment = moment(activity['Start Date ']);
      const endMoment = moment(activity['End Date']);
      const dayStart = moment(day, 'MMM D');
      const duration = 
 moment.duration(endMoment.diff(startMoment));
      let hours = Math.abs(duration.asHours());
      if (hours === 1 || hours === 0) hours = 3;
      let offSetX = 
 moment.duration(startMoment.diff(dayStart)).asHours() + 1;
      const result = {
        numberOfUnits: hours === 0 ? 1 : hours,
        offSetX,
        unitColor: getUnitColor(activity['Unit '], userPlant)
      };
      return result;
    });
  };
1 Answers

I don't have an answer yet but here's his code running if anyone wants to try it. It seems like the code is filtering correctly based on the filter logic.

  • It includes items that start before the defined start date and end date after the defined end date. (outside)

  • It includes items that start and end are between the defined start and defined end date (inside)

  • It includes items that start after the defined start date but before the defined end date. (start within)

const input = {
  Value: [{
      "Activity Id": "05005413-05",
      "Activity Name": "UNIT 1  ASBESTOS REMOVAL; PERMITS (CSP, FSI, HWP)",
      "Start Date ": "2020-11-09 07:00:00.0",
      "End Date": "2021-05-17 15:00:00.0",
      "Work Group Name": "PSCF",
      "Unit ": "01",
      Status: "READY",
      "Work Pln Factor": ", ",
    },
    {
      "Activity Id": "B2119",
      "Activity Name": "Begin Work Week 2119",
      "Start Date ": "2021-05-10 00:00:00.0",
      "End Date": "2021-05-10 00:00:00.0",
      "Work Pln Factor": ", ",
    },
    {
      "Activity Id": "B2118",
      "Activity Name": "Begin Work Week 2118",
      "Start Date ": "2021-05-03 00:00:00.0",
      "End Date": "2021-05-03 00:00:00.0",
      "Work Pln Factor": ", ",
    },
    {
      "Activity Id": "04901693-05",
      "Activity Name": "NEW LPRM IV TEST RE-SET PM FOR NEXT CYCLE.",
      "Start Date ": "2021-04-23 01:00:00.0",
      "End Date": "2021-04-23 03:00:00.0",
      "Unit ": "02",
      Status: "PLAN",
      "Work Pln Factor": ", ",
    },
    {
      "Activity Id": "04901693-04",
      "Activity Name": "20C037           IV CURVES ON SELECTED U2 LPRM'S",
      "Start Date ": "2021-04-22 07:00:00.0",
      "End Date": "2021-04-22 15:00:00.0",
      "Work Group Name": "PMI3",
      "Unit ": "02",
      Status: "PLAN",
      "Work Pln Factor": ", ",
    },
    {
      "Activity Id": "B2116",
      "Activity Name": "Begin Work Week 2116",
      "Start Date ": "2021-04-19 00:00:00.0",
      "End Date": "2021-04-19 00:00:00.0",
      "Work Pln Factor": ", ",
    },
    {
      "Activity Id": "B2121",
      "Activity Name": "Begin Work Week 2121",
      "Start Date ": "2021-05-24 00:00:00.0",
      "End Date": "2021-05-24 00:00:00.0",
      "Work Pln Factor": ", ",
    },
    {
      "Activity Id": "05005413-35",
      "Activity Name": "SCAFFOLD SUPPORT TEAM - CARPENTERS & LABORERS",
      "Start Date ": "2020-11-09 06:00:00.0",
      "End Date": "2021-06-03 16:00:00.0",
      "Work Group Name": "PSCF",
      "Unit ": "01",
      Status: "READY",
      "Work Pln Factor": ", ",
    },
    {
      "Activity Id": "04912309-35",
      "Activity Name": "SCAFFOLD SUPPORT TEAM - CARPENTERS & LABORERS",
      "Start Date ": "2020-11-09 06:00:00.0",
      "End Date": "2021-06-30 16:00:00.0",
      "Work Group Name": "PSCF",
      "Unit ": "01",
      Status: "READY",
      "Work Pln Factor": ", ",
    },
    {
      "Activity Id": "04224121-04",
      "Activity Name": "REBUILD CAT ID 0011698252 AND RETURN TO STORES.",
      "Start Date ": "2021-05-03 07:00:00.0",
      "End Date": "2021-05-05 15:00:00.0",
      "Work Group Name": "PME1",
      "Unit ": "03",
      Status: "READY",
      "Work Pln Factor": ", ",
    }
  ]
}

 const query = {
 Start_Date:"23-JUN-2021",
 End_Date: "19-APR-2021"};


 const formatData = (input: any) => {
    const data: any = _.uniqBy(input.Value, 'Activity Id');
    const formatDate = (text) => moment(new Date(text)).format('YYYY-MM-DD');
    
  // Data being returned from thirdparty isn't filtering out certain data
  const filtered = data.filter(
    (e: any) => {
    console.log(e["Activity Id"]);
    console.log("query.start", formatDate(query.Start_Date), "query.end", formatDate(query.End_Date));
    console.log("input.start", formatDate(e["Start Date "]), "input.end", formatDate(e["End Date"]));

    
    const outsideOfDates = (formatDate(e['Start Date ']) <= formatDate(query.Start_Date) && formatDate(e['End Date']) <= formatDate(query.End_Date));
    const insideOfDates = (formatDate(e['Start Date ']) >= formatDate(query.Start_Date) && formatDate(e['End Date']) <= formatDate(query.End_Date));
    const startWithin = (formatDate(e['Start Date ']) >= formatDate(query.Start_Date) && formatDate(e['Start Date ']) <= formatDate(query.End_Date));
    
    console.log("out", outsideOfDates, "| in", insideOfDates, "| startwithin", startWithin);
    console.log("passes???", outsideOfDates || insideOfDates || startWithin);
    console.log("+++");        
    return outsideOfDates || insideOfDates || startWithin;

    }
  );
  return {
    Value: filtered
  };
};

console.log(formatData(input));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

Related