-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #236 from Pinelab-studio/feat/metric-custom-strategy
Metrics: Reintroduce custom strategies to display custom metrics in dashboard
- Loading branch information
Showing
24 changed files
with
1,674 additions
and
1,024 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
// TODO set correct version number + date and the changes you've made connected to the PR. See this example for the correct format: https://github.com/Pinelab-studio/pinelab-vendure-plugins/blob/main/packages/vendure-plugin-invoices/CHANGELOG.md | ||
# 1.1.0 (2023-09-076) | ||
|
||
- Reintroduced custom strategies and using the new Chartist charts ([#236](https://github.com/Pinelab-studio/pinelab-vendure-plugins/pull/236)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "@pinelab/vendure-plugin-metrics", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "Vendure plugin measuring and visualizing e-commerce metrics", | ||
"author": "Martijn van de Brug <[email protected]>", | ||
"homepage": "https://pinelab-plugins.com/", | ||
|
@@ -24,6 +24,7 @@ | |
"generate": "graphql-codegen" | ||
}, | ||
"dependencies": { | ||
"chartist-plugin-tooltips-updated": "^1.0.0", | ||
"date-fns": "^2.29.3" | ||
}, | ||
"gitHead": "476f36da3aafea41fbf21c70774a30306f1d238f" | ||
|
73 changes: 73 additions & 0 deletions
73
packages/vendure-plugin-metrics/src/api/metric-strategy.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { | ||
AdvancedMetricSummaryInput, | ||
AdvancedMetricType, | ||
} from '../ui/generated/graphql'; | ||
import { RequestContext, Injector, ProductVariant } from '@vendure/core'; | ||
/** | ||
* GroupId is used to group datapoints. For example 'product1', so that the plugin can find all datapoints for that product; | ||
*/ | ||
export interface NamedDatapoint { | ||
legendLabel: string; | ||
value: number; | ||
} | ||
|
||
export interface MetricStrategy<T> { | ||
code: string; | ||
/** | ||
* We need to know if the chart should format your metrics as | ||
* numbers/amounts or as currency | ||
*/ | ||
metricType: AdvancedMetricType; | ||
|
||
/** | ||
* Title to display on the chart. | ||
* Ctx can be used to localize the title | ||
*/ | ||
getTitle(ctx: RequestContext): string; | ||
|
||
/** | ||
* Should return the date to sort by. This value is used to determine in what month the datapoint should be displayed. | ||
* For example `order.orderPlacedAt` when you are doing metrics for Orders. | ||
* By default `creeatedAt` is used | ||
*/ | ||
getSortableField?(entity: T): Date; | ||
|
||
/** | ||
* Load your entities for the given time frame here. | ||
* A client can optionally supply variants as input, which means metrics should be shown for the selected variants only | ||
* | ||
* Keep performance and object size in mind: | ||
* | ||
* Entities are cached in memory, so only return data you actually use in your calculateDataPoint function | ||
* | ||
* This function is executed in the main thread when a user views its dashboard, | ||
* so try not to fetch objects with many relations | ||
*/ | ||
loadEntities( | ||
ctx: RequestContext, | ||
injector: Injector, | ||
from: Date, | ||
to: Date, | ||
variants: ProductVariant[] | ||
): Promise<T[]>; | ||
|
||
/** | ||
* Calculate the aggregated datapoint for the given data. | ||
* E.g. the sum of all given data, or the average. | ||
* | ||
* Return multiple datapoints for a multi line chart. | ||
* The name will be used as legend on the chart. | ||
* | ||
* @example | ||
* // Number of products sold | ||
* [ | ||
* {name: 'product1', value: 10 }, | ||
* {name: 'product2', value: 16 } | ||
* ] | ||
*/ | ||
calculateDataPoints( | ||
ctx: RequestContext, | ||
entities: T[], | ||
variants: ProductVariant[] | ||
): NamedDatapoint[]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,22 @@ | ||
import { Args, Query, Resolver } from '@nestjs/graphql'; | ||
import { Allow, Ctx, Permission, RequestContext } from '@vendure/core'; | ||
import { MetricsService } from './metrics.service'; | ||
import { | ||
AdvancedMetricSummary, | ||
AdvancedMetricSummaryInput, | ||
AdvancedMetricType, | ||
} from '../ui/generated/graphql'; | ||
import { MetricsService } from './metrics.service'; | ||
|
||
@Resolver() | ||
export class MetricsResolver { | ||
constructor(private service: MetricsService) {} | ||
constructor(private readonly metricsService: MetricsService) {} | ||
|
||
@Query() | ||
@Allow(Permission.ReadOrder) | ||
async advancedMetricSummary( | ||
async advancedMetricSummaries( | ||
@Ctx() ctx: RequestContext, | ||
@Args('input') input: AdvancedMetricSummaryInput | ||
): Promise<AdvancedMetricSummary[]> { | ||
return this.service.getMetrics(ctx, input); | ||
return this.metricsService.getMetrics(ctx, input); | ||
} | ||
} |
Oops, something went wrong.