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

add a reporting endpoint for the trend of total sales amount #2

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

naumaan123
Copy link
Owner

No description provided.

.orElse(LocalDateTime.MIN);

// Add in data for all orders to the SimpleRegression
customerOrderDao.getAllOrders().forEach(order -> {
Copy link
Owner Author

Choose a reason for hiding this comment

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

You are fetching the orders all over again, but it is already done on line 28. It would be better fetch the orders once and store it into a variable and use that to perform your logic.

public String saleAmountTrend () {
SimpleRegression r = new SimpleRegression();

LocalDateTime tmp = customerOrderDao.getAllOrders().stream()
Copy link
Owner Author

@naumaan123 naumaan123 May 18, 2024

Choose a reason for hiding this comment

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

We should be checking if the query returns nothing, otherwise this would result in the wrong trend being returned. The orElse should never be reached as the only situation it can happen is if the result set is empty or we have missing data, both which are problematic.

Also, can we rename the variable to something a little more descriptive? It would clarify what this block of code is doing

* Returns whether the total sales data is trending up or down
*/
public String saleAmountTrend () {
SimpleRegression r = new SimpleRegression();
Copy link
Owner Author

@naumaan123 naumaan123 May 18, 2024

Choose a reason for hiding this comment

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

nit: this could be placed below the LocalDateTime block as it is not used until line 34. Can become an unnecessary creation if for whatever reason the code after this fails. Also lets go with a more descriptive variable name.

// down, positive is trending up
Double result = r.getSlope();
if (result < 0) {
return "down";
Copy link
Owner Author

Choose a reason for hiding this comment

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

Can we store these return values as static strings at the top of the class?

* Analyzes trends in sales data
*/
@Component
public class TrendAnalyzer {
Copy link
Owner Author

@naumaan123 naumaan123 May 18, 2024

Choose a reason for hiding this comment

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

Non PR Comment: This could be made a PR comment, but I am choosing to discuss it here as I don't know the patterns established. I don't believe this class should be calling the customerOrderDao directly. I believe the caller should provide the data into the saleAmountTrend method. Something like ->

List<CustomerOrder> customerOrders = customerOrderDao.getAllOrders();
trendAnalyzer.saleAmountTrend(customerOrders)

This decouples the 2 components and allows TrendAnalyzer to be much more testable as no mocks are needed. It also allows saleAmountTrend to possibly be a util function if we ever genericize the type of data being passed. This further allows TrendAnalyzer to be a generic object instead of a bean, and therefor not needing to be managed by spring.

* Returns the trend of the total sales data
*/
@RequestMapping(value = "/trend", method = RequestMethod.GET)
public String trend () {
Copy link
Owner Author

Choose a reason for hiding this comment

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

nit: can we add a verb to this like getTrend?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants