Skip to content

Commit f66fffd

Browse files
feat: add routing trace seeding to seed-insights (calcom#27782)
* feat: add routing trace seeding to seed-insights and standalone seed script Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com> * chore: remove standalone seed-routing-trace.ts (now in seed-insights) Co-Authored-By: hariom@cal.com <hariombalhara@gmail.com> * fix: track skipped routing trace creates for accurate log output Co-Authored-By: unknown <> --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
1 parent 82a4780 commit f66fffd

1 file changed

Lines changed: 119 additions & 0 deletions

File tree

scripts/seed-insights.ts

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,124 @@ async function seedBookingAssignments() {
746746
});
747747
}
748748

749+
async function seedRoutingTraces() {
750+
const bookings = await prisma.booking.findMany({
751+
where: {
752+
assignmentReason: {
753+
some: {},
754+
},
755+
routedFromRoutingFormReponse: {
756+
isNot: null,
757+
},
758+
},
759+
select: {
760+
id: true,
761+
uid: true,
762+
assignmentReason: {
763+
select: {
764+
id: true,
765+
reasonEnum: true,
766+
},
767+
take: 1,
768+
},
769+
routedFromRoutingFormReponse: {
770+
select: {
771+
id: true,
772+
},
773+
},
774+
},
775+
take: 200,
776+
});
777+
778+
if (bookings.length === 0) {
779+
console.log(
780+
"No bookings with both assignment reasons and form responses found - skipping routing traces",
781+
);
782+
return;
783+
}
784+
785+
const departments = ["Engineering", "Sales", "Marketing", "Product", "Design"];
786+
const skills = ["JavaScript", "React", "Node.js", "Python", "Sales"];
787+
788+
const traceData = [];
789+
for (const booking of bookings) {
790+
const assignmentReason = booking.assignmentReason[0];
791+
const formResponse = booking.routedFromRoutingFormReponse;
792+
if (!assignmentReason || !formResponse) continue;
793+
794+
const now = Date.now();
795+
const isFallback =
796+
assignmentReason.reasonEnum ===
797+
AssignmentReasonEnum.ROUTING_FORM_ROUTING_FALLBACK;
798+
799+
const steps = isFallback
800+
? [
801+
{
802+
domain: "routing_form",
803+
step: "fallback_route_used",
804+
timestamp: now - 5000,
805+
data: {
806+
routeId: `route-fallback-${booking.id}`,
807+
routeName: "Fallback Route",
808+
},
809+
},
810+
]
811+
: [
812+
{
813+
domain: "routing_form",
814+
step: "route_matched",
815+
timestamp: now - 5000,
816+
data: {
817+
routeId: `route-${booking.id}`,
818+
routeName: `Route for booking ${booking.id}`,
819+
},
820+
},
821+
{
822+
domain: "routing_form",
823+
step: "attribute-logic-evaluated",
824+
timestamp: now - 4000,
825+
data: {
826+
routeId: `route-${booking.id}`,
827+
routeName: `Route for booking ${booking.id}`,
828+
routeIsFallback: false,
829+
attributeRoutingDetails: [
830+
{
831+
attributeName: "Department",
832+
attributeValue:
833+
departments[
834+
Math.floor(Math.random() * departments.length)
835+
],
836+
},
837+
{
838+
attributeName: "Skills",
839+
attributeValue:
840+
skills[Math.floor(Math.random() * skills.length)],
841+
},
842+
],
843+
},
844+
},
845+
];
846+
847+
traceData.push({
848+
trace: steps,
849+
bookingUid: booking.uid,
850+
formResponseId: formResponse.id,
851+
assignmentReasonId: assignmentReason.id,
852+
});
853+
}
854+
855+
let skipped = 0;
856+
if (traceData.length > 0) {
857+
for (const data of traceData) {
858+
await prisma.routingTrace.create({ data }).catch(() => {
859+
skipped++;
860+
});
861+
}
862+
}
863+
864+
console.log(`Created ${traceData.length - skipped} routing traces (${skipped} skipped due to conflicts)`);
865+
}
866+
749867
async function main() {
750868
// First find the organization we want to add insights to
751869
const organization = await prisma.team.findFirst({
@@ -1039,6 +1157,7 @@ async function main() {
10391157
}
10401158

10411159
await seedBookingAssignments();
1160+
await seedRoutingTraces();
10421161

10431162
// Create diverse payment scenarios for testing
10441163
await createDiversePaymentBookings(organization.id);

0 commit comments

Comments
 (0)