Skip to content

Commit

Permalink
Merge pull request #38 from cesmii/feature/async-data-improvements
Browse files Browse the repository at this point in the history
Feature/async data improvements
  • Loading branch information
scott181182 authored Jun 3, 2024
2 parents 90dfff8 + 8cbf95f commit df9b4e7
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 49 deletions.
120 changes: 83 additions & 37 deletions frontend/composables/useAsyncEquipmentIds.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,93 @@
import { isNonNullish, unique } from "remeda";

import { GetOeeEquipmentTypesWithEquipmentIdsDocument } from "~/generated/graphql/operations";
import type { GetOeeEquipmentTypesWithEquipmentIdsQuery } from "~/generated/graphql/operations";
import { GetOeeEquipmentTypesWithEquipmentIdsDocument } from "~/generated/graphql/operations";



/*
* There are two possible structures for OEE equipment to descend from an "actual" piece of equipment:
*
* 1)
* - Physical Equipment
* - OEE Summary (type OEE Summary)
* - OEE Availability (type OEE Availability)
* - OEE Performance (type OEE Performance)
* - OEE Quality (type OEE Quality)
*
* 2)
* - Physical Equipment (type OEE Summary)
* - OEE Availability (type OEE Availability)
* - OEE Performance (type OEE Performance)
* - OEE Quality (type OEE Quality)
*
* We can differentiate based on `type` and presence of children.
*/





function deriveOeeEquipmentIds(
res: GetOeeEquipmentTypesWithEquipmentIdsQuery,
): string[] {
/** All equipment with OEE `equipmentType`. */
const oeeEquipment = unique(
res.equipmentTypes?.flatMap((et) => et.equipments) || [],
);

/** Set of equipment IDs with an OEE `equipmentType`. */
const oeeEquipmentIds = new Set(oeeEquipment.map((e) => e.id));

/** Equipment IDs that are the parent of at least one OEE Equipment. */
const oeeParentIds = unique(
oeeEquipment.map((eq) => eq.partOfId).filter(isNonNullish),
);

/**
* The OEE equipment IDs that have a child OEE equipment.
* This represents a root equipment following structure #2 (see above).
*/
const oeeEquipmentIdsWithOEEChildren = oeeParentIds
.filter(
(partOfId) => oeeEquipmentIds.has(partOfId),
);

const oeeParentIdsWithoutOEEChildren = unique(
oeeEquipment
.filter(
(eq) =>
// Exclude the parent of equipment who are counted under structure #2.
!oeeEquipmentIdsWithOEEChildren.includes(eq.id)
// Exclude the equipment who are counted under structure #2.
&& (!eq.partOfId || !oeeEquipmentIdsWithOEEChildren.includes(eq.partOfId)),
)
.map((eq) => eq.partOfId)
.filter(isNonNullish),
);

const equipmentIds = unique([
...oeeEquipmentIdsWithOEEChildren,
...oeeParentIdsWithoutOEEChildren,
]);

return equipmentIds;
}


/**
* A hook to retrieve all equipment with OEE metrics.
* Makes a couple GraphQL calls then transforms the returned equipment into a standard shape.
*/
export default function useAsyncEquipmentIds() {
return useAsyncQuery(GetOeeEquipmentTypesWithEquipmentIdsDocument,
return useAsyncQuery(
GetOeeEquipmentTypesWithEquipmentIdsDocument,
{},
"default",
{ errorPolicy: "ignore" }
, {
transform: (typeRes) => {
const oeeEquipment = unique(
typeRes.equipmentTypes?.flatMap((et) => et.equipments) || [],
);

const oeeEquipmentIds = new Set(oeeEquipment.map((e) => e.id));

const oeeParentIds = unique(
oeeEquipment.map((eq) => eq.partOfId).filter(isNonNullish),
);

const oeeEquipmentIdsWithOEEChildren = oeeParentIds
.filter(
(partOfId) => oeeEquipmentIds.has(partOfId),
);

const oeeParentIdsWithoutOEEChildren = unique(
oeeEquipment
.filter(
(eq) => !oeeEquipmentIdsWithOEEChildren.includes(eq.id) && (!eq.partOfId || !oeeEquipmentIdsWithOEEChildren.includes(eq.partOfId)),
)
.map((eq) => eq.partOfId)
.filter(isNonNullish),
);

const equipmentIds = computed(() => unique([
...oeeEquipmentIdsWithOEEChildren,
...oeeParentIdsWithoutOEEChildren,
]));

return equipmentIds.value;
},
});
{ errorPolicy: "ignore" },
{
transform: deriveOeeEquipmentIds,
},
);
}

28 changes: 18 additions & 10 deletions frontend/composables/useAsyncEquipmentWithOEE.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { VariablesOf } from "@graphql-typed-document-node/core";

import { GetEquipmentsDocument } from "~/generated/graphql/operations";
import { parseEquipmentWithOEE } from "~/lib/equipment";

Expand All @@ -12,22 +14,28 @@ export default function useAsyncEquipmentWithOEE() {
const endTime = new Date(startTime);
endTime.setDate(startTime.getDate() + 1);


const res = useAsyncQuery(GetEquipmentsDocument, {
// Making this `computed` so the query is reactive to changes in `equipmentIds`.
const variables = computed<VariablesOf<typeof GetEquipmentsDocument>>(() => ({
filter: {
id: { in: equipmentIds.value ?? [] },
},
startTime: startTime.toISOString(),
endTime: endTime.toISOString(),
}, "default", {
enabled: equipmentIds.value && equipmentIds.value.length > 0,
errorPolicy: "ignore",
}, {
watch: [ equipmentIds ],
transform: (eqRes) => {
return eqRes.equipments?.map(parseEquipmentWithOEE);
}));

const res = useAsyncQuery(
GetEquipmentsDocument,
variables,
"default",
{
errorPolicy: "ignore",
},
{
transform: (eqRes) => {
return eqRes.equipments?.map(parseEquipmentWithOEE);
},
},
});
);

// Compound status involving both queries.
const status = computed(() => idStatus.value === "success" ? res.status.value : idStatus.value);
Expand Down
18 changes: 16 additions & 2 deletions frontend/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,20 @@
text="There was an unexpected error fetching equipment. Please try again."
/>
</v-col>
<v-col
v-else-if="equipments && equipments.length === 0"
cols="12"
class="d-flex flex-column align-center ga-4"
>
<v-card class="text-center">
<v-card-title>
No Equipment Found
</v-card-title>
<v-card-subtitle>
Please try refreshing.
</v-card-subtitle>
</v-card>
</v-col>
<v-col v-else-if="equipments && equipments.length > 0" cols="12">
<v-row>
<v-col
Expand All @@ -30,10 +44,10 @@
</v-col>
</v-row>
</v-col>
<v-col v-else cols="auto">
<v-col v-else cols="12" class="d-flex flex-column align-center ga-4">
<v-card class="text-center">
<v-card-title>
No Equipment Found
Unable to Fetch Equipment
</v-card-title>
<v-card-subtitle>
Please try refreshing.
Expand Down

0 comments on commit df9b4e7

Please sign in to comment.