Skip to content

Commit

Permalink
feat(metrics): performance
Browse files Browse the repository at this point in the history
  • Loading branch information
martijnvdbrug committed Oct 31, 2024
1 parent 8e7e53c commit 300fb95
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 17 deletions.
4 changes: 4 additions & 0 deletions packages/vendure-plugin-metrics/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/vendure-plugin-metrics/package.json
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>",
"homepage": "https://pinelab-plugins.com/",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ export class AverageOrderValueMetric implements MetricStrategy<Order> {
.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(),
Expand All @@ -58,9 +56,12 @@ export class AverageOrderValueMetric implements MetricStrategy<Order> {
.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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ export class RevenuePerProduct implements MetricStrategy<OrderLine> {
.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 })
Expand All @@ -56,9 +54,12 @@ export class RevenuePerProduct implements MetricStrategy<OrderLine> {
.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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@ export class UnitsSoldMetric implements MetricStrategy<OrderLine> {
.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`, {
Expand All @@ -56,9 +55,12 @@ export class UnitsSoldMetric implements MetricStrategy<OrderLine> {
.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);
Expand Down

0 comments on commit 300fb95

Please sign in to comment.