isaozler / grafana-shift-selector

The shift selector allows you to adjust the time range of your grafana dashboard to one specific shift or a range of shifts.
Apache License 2.0
6 stars 6 forks source link

Trigger for one of the warnings seems to be misconfigured #26

Closed mazurp03 closed 2 months ago

mazurp03 commented 7 months ago

Hi, One of the warnings in the latest Shift Selector version seems to be too sensitive. Here is the code fragment that I'm talking about:

useEffect(() => {
    if (shiftValues.length && shiftOptions?.options && !closedAlerts.includes(1)) {
      const shiftGroupUUIDs: { [key: string]: number } = shiftValues.reduce((res: any, { text }: { text: string }) => {
        const [, shiftGroupUUID] = text.split('|');
        return {
          ...res,
          [shiftGroupUUID]: res[shiftGroupUUID] ? ++res[shiftGroupUUID] : 1,
        };
      }, {});
      const shiftOptionsCount = Object.values(shiftGroupUUIDs).reduce((acc: number, d: number): number => acc + d, 0);

      if (shiftOptionsCount !== shiftOptions.options.length) {
        if (shiftOptions.options.length > 1) {
          return setAlertHandler({
            id: 1,
            type: 'brandWarning',
            text:
              'Warning! You are missing shifts because some shiftnames are not unique. Please make sure all shifts have unique names!',
          });
        } else {
          return setAlertHandler({
            id: 1,
            type: 'brandWarning',
            text: 'Warning! No shifts available. Please check your shift options variable.',
          });
        }
      }

      if (alerts.find(({ id }) => id === 1)) {
        resetAlert(1);
      }
    }
  }, [closedAlerts, alerts, shiftOptions, shiftValues, resetAlert, setAlertHandler]);

Let's focus on the first "if" condition. In case of more than one shift group configured in the database, this warning triggers all the time. For example, I have more than one instance of Grafana with completely different shiftGroupUUID's. For each shiftGroupUUID I have 3 shifts configured with the name e.g. "morning", "afternoon", "night". Of course, there should be something that will secure us from duplicating shift names in the same shiftGroupUUID (that's important), but for me there is a big discrepancy between the description of this warning and the condition that must be met.

There should be some function/effect that will check whether shift names are unique in the same shiftGroupUUID. For now, this "if" condition checks if there is more than one shiftGroupUUID (if I understand this code fragment correctly) and then if it meets the condition in which there is more than one - warning is triggered with a description, which for me doesn't indicate the checked condition.

isaozler commented 6 months ago

Hi @mazurp03, I tested it with the following data. Are you getting this issue with the latest 0.1.4 release? I'm asking because I am not getting any warnings about duplicate shifts in different shift groups.

image
{
    "static": {
        "shifts": [
            {
                "group": "Group 1 eg. Summer",
                "group_uuid": "uuid_1",
                "uuid": "shift_uuid_11",
                "label": "Morning Custom",
                "startTime": "06:01:00",
                "endTime": "14:00:00",
                "order": 1
            },
            {
                "group": "Group 1 eg. Summer",
                "group_uuid": "uuid_1",
                "uuid": "shift_uuid_12",
                "label": "Afternoon Custom",
                "startTime": "14:01:00",
                "endTime": "19:37:00",
                "order": 2
            },
            {
                "group": "Group 1 eg. Summer",
                "group_uuid": "uuid_1",
                "uuid": "shift_uuid_13",
                "label": "Night Custom",
                "startTime": "19:38:00",
                "endTime": "06:00:00",
                "order": 3
            },
            {
                "group": "Group 2 eg. Winter",
                "group_uuid": "uuid_2",
                "uuid": "shift_uuid_21",
                "label": "Morning Custom",
                "startTime": "06:01:00",
                "endTime": "14:00:00",
                "order": 1
            },
            {
                "group": "Group 2 eg. Winter",
                "group_uuid": "uuid_2",
                "uuid": "shift_uuid_22",
                "label": "Afternoon Custom",
                "startTime": "14:01:00",
                "endTime": "19:37:00",
                "order": 2
            },
            {
                "group": "Group 2 eg. Winter",
                "group_uuid": "uuid_2",
                "uuid": "shift_uuid_23",
                "label": "Night Custom",
                "startTime": "19:38:00",
                "endTime": "06:00:00",
                "order": 3
            }
        ]
    }
}
fatbasstard commented 3 months ago

Hi @isaozler ,

To jump on this, got the same warning. Using the Filter setup. When a line is selected the actual data returns a unique dataset. But without it there is indeed multiple values with the same name.

The validation appears to not take the filter into account correctly?

isaozler commented 3 months ago

Hi @isaozler ,

To jump on this, got the same warning. Using the Filter setup. When a line is selected the actual data returns a unique dataset. But without it there is indeed multiple values with the same name.

The validation appears to not take the filter into account correctly?

I see, will check this 👍

fatbasstard commented 3 months ago

@isaozler Found something already?

isaozler commented 2 months ago

@mazurp03 Could you check this branch to see if this fixes your issue?

https://github.com/isaozler/grafana-shift-selector/tree/fix/v10

isaozler commented 2 months ago

With the release of 0.1.6, you can retrieve all shifts without being filtered by Grafana.

Firstly, you should concatenate the shift labels with the shift group. https://github.com/isaozler/grafana-shift-selector/assets/1378630/5c4204a3-ef6c-40d5-b5a3-73eba997d5b5

Secondly, the trim feature should be enabled in the data mapper props panel. https://github.com/isaozler/grafana-shift-selector/assets/1378630/ebfeeeee-91a5-4245-97a5-107abc865aa3