-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
5372: Rework stock_movement_status calculation r=jmcameron a=jniles This is a full rewrite of the `stock_movement_status` table and associated stored procedures. The goal of this rewrite is to improve clarity, remove user-defined variables that may potentially collide across MySQL connections, and improve the performance of the `GetCMM()` calculation. Closes #5073. Closes #5144. Closes #5332. Closes #5360. ---- ![image](https://user-images.githubusercontent.com/896472/108206217-6741e600-7126-11eb-86c1-ada342009708.png) ![image](https://user-images.githubusercontent.com/896472/108207993-c1dc4180-7128-11eb-9bf7-c611e20ec500.png) ![heidisql_0CKbDzKdzl](https://user-images.githubusercontent.com/896472/108201428-17602080-7120-11eb-81c5-723e8cc122e9.png) This PR contains many far reaching changes. Here is a lot of the notable ones: 1. I've removed the `avg_consumption` column from the inventory table. It was never used, as far as I can tell, to do any reporting. It was used in the stock import feature, but we don't use the `avg_consumption` to report on, so I just removed that code. 2. I've removed the `stock_consumption` table and associated procedures. It is used in several reports and they are now broken. However, I would argue they were always broken since the `stock_consumption` did not record accurate information about stock consumption. 3. I've entirely changed the structure behind the `stock_movement_status` table for the following reasons: 1. The [previous structure](https://github.com/IMA-WorldHealth/bhima/blob/e455a8011db69ad48839afc35977d3dd041d3241/server/models/schema.sql#L2016) was very hard to audit. For example, it had `start_date`, `end_date`, and `quantity`. Is that the quantity at the start? The quantity at the end? If I wanted to find the value on date X, I needed to do a BETWEEN query. 2. The table did not distinguish between consumption information and exit information. This made the `GetCMM()` calculation have to look up additional information from the `stock_movement` table. A user would need to as well. 3. In theory, we'll be calling `GetCMM()` more than we will be moving stock. It makes sense to make the performance tradeoff to increase the speed of `GetCMM()` and decrease the speed of inserting stock. 5. I've renamed the `GetCMM()` call to `GetAMC()` for clarity. It also takes in a single date and uses the depot to find the correct value of the date range. This prevents us from having inconsistent results because a developer didn't properly compute the date range. If we need to support a custom date range, we'll just write another SP. 6. I've slightly changed the API of `GetAMC()`: 1. It returns `head_days` and `tail_days` instead of `days_before_stock_consumption`. That is because `days_before_stock_consumption` is the real number of days before the first consumption. Instead, it the days between the start of the window and the next record. Similarly, `tail_days` gives you the number of days between the last consumption record and the end of the window. 2. It returns the `quantity_in_stock` so that we can perform stock out date calculations. 3. `first_inventory_movement_date` -> `min_date` 4. `last_inventory_movement_date` -> `max_date` ------ It isn't all good. In fact, initializing this is terrible. On my machine (4GB of RAM): ```mysql mysql> use vanga; mysql> CALL zRecomputeStockMovementStatus(); Query OK, 0 rows affected (22 min 4.81 sec) mysql> use imck; mysql> CALL zRecomputeStockMovementStatus(); Query OK, 0 rows affected (4 min 20.31 sec) ``` This is _horrible_. I did get a lot faster speeds when I was just testing on MySQL 8, but the syntax didn't work on MySQL 5.7 (#5364). When I fixed it on MySQL5.7, I ran into issues on MySQL 8. Basically, we got the worst performance of both worlds by trying to get it to work correctly on both worlds. However, I still think it is worth merging because the performance of any individual transaction isn't bad. It is only when rebuilding the database that things go horribly long. ---- ### How to test Download this PR and run the tests! (`yarn test:integration` and `yarn test:integration:stock`). If that works, try with a production database. Note, as above, you'll need to `CALL zRecomputeStockMovementStatus()` to build the table. Co-authored-by: Jonathan Cameron <jmcameron@gmail.com> Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com> Co-authored-by: mbayopanda <mbayopanda@gmail.com> Co-authored-by: Jonathan Niles <jonathanwniles@gmail.com>
- Loading branch information
Showing
34 changed files
with
3,211 additions
and
6,090 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,6 +70,7 @@ blocks: | |
- env_var: MYSQL_VERSION | ||
values: | ||
- '5.7' | ||
- '8' | ||
- env_var: NODEJS_VERSION | ||
values: | ||
- '12' | ||
|
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
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
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
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
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
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
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
Oops, something went wrong.