From 300fb952afb97f28bf6dec886995951a30613052 Mon Sep 17 00:00:00 2001 From: Martijn Date: Thu, 31 Oct 2024 14:03:26 +0100 Subject: [PATCH] feat(metrics): performance --- packages/vendure-plugin-metrics/CHANGELOG.md | 4 ++++ packages/vendure-plugin-metrics/package.json | 2 +- .../src/api/metrics/average-order-value.ts | 11 ++++++----- .../src/api/metrics/revenue-per-product.ts | 11 ++++++----- .../src/api/metrics/units-sold-metric.ts | 14 ++++++++------ 5 files changed, 25 insertions(+), 17 deletions(-) diff --git a/packages/vendure-plugin-metrics/CHANGELOG.md b/packages/vendure-plugin-metrics/CHANGELOG.md index 05bd93d5..5430d16f 100644 --- a/packages/vendure-plugin-metrics/CHANGELOG.md +++ b/packages/vendure-plugin-metrics/CHANGELOG.md @@ -1,3 +1,7 @@ +# 1.4.1 (2024-10-31) + +- Only fetch relations from DB when variants are passed in for better performance + # 1.4.0 (2024-10-31) - Added revenue (per variant) metric diff --git a/packages/vendure-plugin-metrics/package.json b/packages/vendure-plugin-metrics/package.json index d86fbaed..e2ec3ccb 100644 --- a/packages/vendure-plugin-metrics/package.json +++ b/packages/vendure-plugin-metrics/package.json @@ -1,6 +1,6 @@ { "name": "@pinelab/vendure-plugin-metrics", - "version": "1.4.0", + "version": "1.4.1", "description": "Vendure plugin measuring and visualizing e-commerce metrics", "author": "Martijn van de Brug ", "homepage": "https://pinelab-plugins.com/", diff --git a/packages/vendure-plugin-metrics/src/api/metrics/average-order-value.ts b/packages/vendure-plugin-metrics/src/api/metrics/average-order-value.ts index 3afb6f50..68717110 100644 --- a/packages/vendure-plugin-metrics/src/api/metrics/average-order-value.ts +++ b/packages/vendure-plugin-metrics/src/api/metrics/average-order-value.ts @@ -45,8 +45,6 @@ export class AverageOrderValueMetric implements MetricStrategy { .getRepository(ctx, Order) .createQueryBuilder('order') .leftJoin('order.channels', 'orderChannel') - .leftJoin('order.lines', 'orderLine') - .leftJoin('orderLine.productVariant', 'productVariant') .where(`orderChannel.id=:channelId`, { channelId: ctx.channelId }) .andWhere(`order.orderPlacedAt >= :from`, { from: from.toISOString(), @@ -58,9 +56,12 @@ export class AverageOrderValueMetric implements MetricStrategy { .limit(take); if (variants.length) { - query = query.andWhere(`productVariant.id IN(:...variantIds)`, { - variantIds: variants.map((v) => v.id), - }); + query = query + .leftJoin('order.lines', 'orderLine') + .leftJoin('orderLine.productVariant', 'productVariant') + .andWhere(`productVariant.id IN(:...variantIds)`, { + variantIds: variants.map((v) => v.id), + }); } const [items, totalOrders] = await query.getManyAndCount(); orders.push(...items); diff --git a/packages/vendure-plugin-metrics/src/api/metrics/revenue-per-product.ts b/packages/vendure-plugin-metrics/src/api/metrics/revenue-per-product.ts index 835a79bd..32d3002e 100644 --- a/packages/vendure-plugin-metrics/src/api/metrics/revenue-per-product.ts +++ b/packages/vendure-plugin-metrics/src/api/metrics/revenue-per-product.ts @@ -42,8 +42,6 @@ export class RevenuePerProduct implements MetricStrategy { .get(TransactionalConnection) .getRepository(ctx, OrderLine) .createQueryBuilder('orderLine') - .leftJoin('orderLine.productVariant', 'productVariant') - .addSelect(['productVariant.sku', 'productVariant.id']) .leftJoinAndSelect('orderLine.order', 'order') .leftJoin('order.channels', 'channel') .where(`channel.id=:channelId`, { channelId: ctx.channelId }) @@ -56,9 +54,12 @@ export class RevenuePerProduct implements MetricStrategy { .offset(skip) .limit(take); if (variants.length) { - query = query.andWhere(`productVariant.id IN(:...variantIds)`, { - variantIds: variants.map((v) => v.id), - }); + query = query + .leftJoin('orderLine.productVariant', 'productVariant') + .addSelect(['productVariant.sku', 'productVariant.id']) + .andWhere(`productVariant.id IN(:...variantIds)`, { + variantIds: variants.map((v) => v.id), + }); } const [items, totalItems] = await query.getManyAndCount(); lines.push(...items); diff --git a/packages/vendure-plugin-metrics/src/api/metrics/units-sold-metric.ts b/packages/vendure-plugin-metrics/src/api/metrics/units-sold-metric.ts index 7e36d9a8..f9944c4f 100644 --- a/packages/vendure-plugin-metrics/src/api/metrics/units-sold-metric.ts +++ b/packages/vendure-plugin-metrics/src/api/metrics/units-sold-metric.ts @@ -42,9 +42,8 @@ export class UnitsSoldMetric implements MetricStrategy { .get(TransactionalConnection) .getRepository(ctx, OrderLine) .createQueryBuilder('orderLine') - .leftJoin('orderLine.productVariant', 'productVariant') - .addSelect(['productVariant.sku', 'productVariant.id']) - .leftJoinAndSelect('orderLine.order', 'order') + .leftJoin('orderLine.order', 'order') + .addSelect(['order.id', 'order.orderPlacedAt']) .leftJoin('order.channels', 'channel') .where(`channel.id=:channelId`, { channelId: ctx.channelId }) .andWhere(`order.orderPlacedAt >= :from`, { @@ -56,9 +55,12 @@ export class UnitsSoldMetric implements MetricStrategy { .skip(skip) .take(take); if (variants.length) { - query = query.andWhere(`productVariant.id IN(:...variantIds)`, { - variantIds: variants.map((v) => v.id), - }); + query = query + .leftJoin('orderLine.productVariant', 'productVariant') + .addSelect(['productVariant.sku', 'productVariant.id']) + .andWhere(`productVariant.id IN(:...variantIds)`, { + variantIds: variants.map((v) => v.id), + }); } const [items, totalItems] = await query.getManyAndCount(); lines.push(...items);