Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[API] Statistics #15598

Merged
merged 32 commits into from Jan 18, 2024
Merged

[API] Statistics #15598

merged 32 commits into from Jan 18, 2024

Conversation

Rafikooo
Copy link
Contributor

@Rafikooo Rafikooo commented Dec 4, 2023

Q A
Branch? 1.13
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
License MIT

This data response format is designed to facilitate chart generation. The following example is specifically for the annual sales summary with monthly intervals; other configurations will be added in future PRs.

This data format is intended for the Chart.js library, but it should also be easy to integrate with Google Charts.
It has built-in support for handling multiple datasets.

{
	"salesChart": {
		"labels": [
			"2023-01-01",
			"2023-02-01",
			"2023-03-01",
			"2023-04-01",
			"2023-05-01",
			"2023-06-01",
			"2023-07-01",
			"2023-08-01",
			"2023-09-01",
			"2023-10-01",
			"2023-11-01",
			"2023-12-01"
		],
		"datasets": {
			"sales": [
				97043,
				17740,
				125045,
				54611,
				85021,
				78858,
				104452,
				45415,
				50234,
				29463,
				0,
				0
			] // If necessary, additional datasets could be included
		}
	},
	"businessActivitySummary": {
		"totalSales": 687882,
		"newOrdersCount": 14,
		"newCustomersCount": 21,
		"averageOrderValue": 49134
	},
	"intervalType": "month"
}

@Rafikooo Rafikooo added Feature New feature proposals. DX Issues and PRs aimed at improving Developer eXperience. labels Dec 4, 2023
@Rafikooo Rafikooo requested a review from a team as a code owner December 4, 2023 16:20
@Rafikooo Rafikooo force-pushed the SYL-3246/api-sales-statistics branch from 6e0f70a to f5d7f65 Compare December 4, 2023 16:24
Copy link

github-actions bot commented Dec 4, 2023

Bunnyshell Preview Environment deleted

Available commands:

  • /bns:deploy to redeploy the environment

@Rafikooo Rafikooo force-pushed the SYL-3246/api-sales-statistics branch from f5d7f65 to bdf0ddd Compare December 4, 2023 16:33
@Rafikooo Rafikooo force-pushed the SYL-3246/api-sales-statistics branch from bdf0ddd to 5aca55d Compare December 6, 2023 12:54
@probot-autolabeler probot-autolabeler bot added Admin AdminBundle related issues and PRs. API APIs related issues and PRs. labels Dec 6, 2023
@Rafikooo Rafikooo force-pushed the SYL-3246/api-sales-statistics branch from 5aca55d to 19a8d1c Compare December 6, 2023 12:59
@Rafikooo Rafikooo changed the title [WIP][API] Sales Statistics [API] Sales Statistics Dec 6, 2023
Comment on lines 48 to 49
"period": "2023-12-01 00:00:00",
"total": 19500
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this date intentional or will we get failing tests when the month changes?
Also would be nice to have at least one order in any other month.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly, I'm not sure. My plan was to test these various scenarios in a separate PR and add the possibility for the client to set the period and interval.

src/Sylius/Behat/Service/SharedStorageChannelContext.php Outdated Show resolved Hide resolved
src/Sylius/Bundle/ApiBundle/Resources/config/routing.yml Outdated Show resolved Hide resolved
Comment on lines 42 to 57
case 'year':
$queryBuilder
->addSelect('YEAR(o.checkoutCompletedAt) as year')
->groupBy('year')
->andWhere('YEAR(o.checkoutCompletedAt) >= :startYear AND YEAR(o.checkoutCompletedAt) <= :endYear')
->setParameter('startYear', $salesPeriod->getStartDate()->format('Y'))
->setParameter('endYear', $salesPeriod->getEndDate()->format('Y'))
;
$dateFormatter = static function (\DateTimeInterface $date): string {
return $date->format('Y');
};
$resultFormatter = static function (array $data): string {
return (string) $data['year'];
};

break;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before the switch statement:

$interval = $salesPeriod->getInterval();
Suggested change
case 'year':
$queryBuilder
->addSelect('YEAR(o.checkoutCompletedAt) as year')
->groupBy('year')
->andWhere('YEAR(o.checkoutCompletedAt) >= :startYear AND YEAR(o.checkoutCompletedAt) <= :endYear')
->setParameter('startYear', $salesPeriod->getStartDate()->format('Y'))
->setParameter('endYear', $salesPeriod->getEndDate()->format('Y'))
;
$dateFormatter = static function (\DateTimeInterface $date): string {
return $date->format('Y');
};
$resultFormatter = static function (array $data): string {
return (string) $data['year'];
};
break;
case 'year':
$this->modifyQueryBuilderForHandlingYear($queryBuilder)
break;

And after the switch statement

$dateFormatter = $this->getDateFormatterFor($interval);
$resultFormatter = $this->getResultFormatterFor($interval);

It'd improve readability IMO.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

modifyQueryBuilderForHandlingYear can be replaced by any other name. Just an example, nothing better came to my mind.

@Rafikooo Rafikooo force-pushed the SYL-3246/api-sales-statistics branch 2 times, most recently from 80365bc to f273e8c Compare December 6, 2023 17:39
Copy link
Member

@diimpp diimpp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My biggest concerns are:

  1. Feature named SalesStatistics instead of more general purpose Statistics:
    • while already contains "customer created within period", which could have just registered.
    • which limits/confuses re-usability for end-users.
  2. Ages-olds issue with orders count, total sales, average order value only include paid orders and doesn't include refunded, partial refunded.
  3. Placement of the new sales statistics in the core, which immediatly makes it part of BC promise, while I've a feeling it should be polished more. For example it can be placed to ApiBundle and moved to Core at 2.0. (Plus current DashboardStatistics needs to be ported to this new usage as well)
  4. Extensibility needs to be checked, e.g. how many classes I will need to extend and override to add refunds total, coupon usage for period, new products created for the period or products sold for the period.

"Request changes" is due comments.

features/admin/dashboard.feature Outdated Show resolved Hide resolved
And I choose "United States" channel
Then I should see 2 new orders
And I should see 8 new customers
And there should be total sales of "$459.00"
And the average order value should be "$229.50"

@ui
@api @ui
Scenario: Changing channel in administration dashboard
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. This and previous scenario have the same name. Could they be renamed?

Scenario: Changing channel in administration dashboard
Scenario: Changing channel in administration dashboard

  1. Cases look identical, only different is base channel and selected channel. Are both of them needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even though these scenarios may seem similar, I found them valuable because one consists only of fulfilled orders and the other is a mix of fulfilled and placed orders, which affects the final summary. I modified the scenario names to accurately reflect that fact

Comment on lines 47 to 48
And there should be total sales of "$5,241.00"
And the average order value should be "$1,310.25"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Total sales and average order value depend on selected period, should it be indicated somehow in the cases?

image
image

Comment on lines 45 to 46
Then I should see 4 new orders
And I should see 8 new customers
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume it checks order summary
image
and not last customers/orders widget
image

then phrasing can be adjusted to

Suggested change
Then I should see 4 new orders
And I should see 8 new customers
Then I should see 4 paid orders
And I should see 8 customers

P.S. should it indicate period as well? (Year/Month/2 weeks)

src/Sylius/Behat/Client/ResponseChecker.php Outdated Show resolved Hide resolved
@diimpp
Copy link
Member

diimpp commented Dec 7, 2023

With no connection to others source codes and only focusing on ValueObjects design,

image

@Rafikooo
Copy link
Contributor Author

Rafikooo commented Dec 8, 2023

@diimpp, thank you for the valuable review. I'll strive to implement most of your suggestions. However, I want to clarify that this task isn't among our top priorities at the moment. We have agreed to focus on enabling retrieval within a yearly period, and closer to the release of 'Sylius 2.0', we will incorporate the requested period and interval management. Afterwards, we will deprecate the current code utilized by the UI and change the JS logic to use the new one

@Rafikooo Rafikooo changed the title [API] Sales Statistics [API] Statistics Dec 8, 2023
@Rafikooo Rafikooo force-pushed the SYL-3246/api-sales-statistics branch 5 times, most recently from 837e6f0 to b749e28 Compare December 11, 2023 13:31
@Rafikooo Rafikooo changed the title [API] Statistics [WIP][API] Statistics Dec 12, 2023
@Rafikooo Rafikooo force-pushed the SYL-3246/api-sales-statistics branch from b749e28 to 73df243 Compare December 13, 2023 22:56
@Rafikooo Rafikooo force-pushed the SYL-3246/api-sales-statistics branch from 8840f9f to a119ffe Compare January 17, 2024 13:22
@Rafikooo Rafikooo force-pushed the SYL-3246/api-sales-statistics branch from a119ffe to 1e196e2 Compare January 17, 2024 14:52
@jakubtobiasz
Copy link
Member

/bns:start

$result = ['message' => $exception->getMessage()];

if ($exception instanceof ChannelNotFoundException) {
$status = Response::HTTP_NOT_FOUND;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Setting this status is not needed as ChannelNotFoundException extends NotFoundHttpException

$dataKeys = array_keys($data);

foreach ($requiredFields as $field) {
Assert::inArray($field, $dataKeys, "The data must contain the $field key.");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Assert::inArray($field, $dataKeys, "The data must contain the $field key.");
Assert::inArray($field, $dataKeys, sprintf('The data must contain the %s key.', $field));

@GSadee GSadee merged commit 4997681 into Sylius:1.13 Jan 18, 2024
25 checks passed
@GSadee
Copy link
Member

GSadee commented Jan 18, 2024

Thanks, Rafał! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Admin AdminBundle related issues and PRs. API APIs related issues and PRs. DX Issues and PRs aimed at improving Developer eXperience. Feature New feature proposals.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants