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

[REFACTOR] refactor save method and finish readme #35

Merged
merged 2 commits into from
May 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 0 additions & 72 deletions .github/workflows/scorecard.yml

This file was deleted.

59 changes: 54 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,61 @@
### Penanggung Jawab :
#### Muhammad Faishal Adly Nelwan (2206030754)

### ALL FEATURES REQUIRE ADMIN TOKEN
##### Link Get All Subscription BOX
http://34.124.168.155/subscription-box/list

##### Link Create Subscription BOX
##### Link Get All Subscription BOX
##### Link Get All Subscription BOX
##### Link Get All Subscription BOX
##### Link Get All Subscription BOX
http://34.124.168.155/subscription-box/create

##### Link Update Subscription BOX
http://34.124.168.155/subscription-box/update

##### Link Delete Subscription BOX
http://34.124.168.155/subscription-box/{id}

##### Link Find By ID Subscription BOX
http://34.124.168.155/subscription-box/{id}

##### Link Find By Name
http://34.124.168.155/subscription-box/name/{nameURL}

##### Link Find Distinct Names
http://34.124.168.155/subscription-box/name/{nameURL}

##### Link Find Price Lesser Than
http://34.124.168.155/subscription-box/price/lesser-than/{price}

##### Link Find Price Greater Than
http://34.124.168.155/subscription-box/price/greater-than/{price}

##### Link Find Price Equals
http://34.124.168.155/subscription-box/price/equals/{price}

##### Link Get Admin Subscription Box Logs
http://34.124.168.155/subscription-box/logs


# Monitoring And Profiling
### Grafana
![grafana 1.jpg](images%2Fgrafana%201.jpg)
![grafana 2.jpg](images%2Fgrafana%202.jpg)
![grafana 3.jpg](images%2Fgrafana%203.jpg)
![grafana 4.jpg](images%2Fgrafana%204.jpg)
![grafana 5.jpg](images%2Fgrafana%205.jpg)
![grafana 6.jpg](images%2Fgrafana%206.jpg)

### Profiling
![profiling 1.jpg](images%2Fprofiling%201.jpg)

Dapat dilihat dari profiling diatas bahwa method yang paling mahal adalah findAll() subscriptionbox dan update() subscriptionbox.
Akan tetapi, saya belum menemukan implementasi baru yang dapat mengalahkan waktu tersebut. Sehingga tidak dilakukan refactoring apa-apa terhadap kedua metode ini.

### Refactoring
![refactor 1.jpg](images%2Frefactor%201.jpg)
![refactor 1.2.jpg](images%2Frefactor%201.2.jpg)

Pada save method saya (untuk creation subscriptionBox) terdapat kode yang tidak menerapi SOLID principle.
Sehingga saya melakukan refactoring agar logic dari metode tersebut dipisah-pisah menjadi function tersendiri untuk menerapi single-responsibility principle.

# Monitoring And Profiling
![refactor 2.0.jpg](images%2Frefactor%202.0.jpg)
15 changes: 15 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,19 @@ tasks.jacocoTestReport {
csv.required.set(true)
html.outputLocation.set(layout.buildDirectory.dir("jacocoHtml"))
}

classDirectories.setFrom(
files(classDirectories.files.map {
fileTree(it) {
exclude(
"id/ac/ui/cs/advprog/snackscription_subscriptionbox/utils/**",
"id/ac/ui/cs/advprog/snackscription_subscriptionbox/config/**",
"id/ac/ui/cs/advprog/snackscription_subscriptionbox/model/LogAdmin**",
"id/ac/ui/cs/advprog/snackscription_subscriptionbox/repository/ItemRepository**",
"id/ac/ui/cs/advprog/snackscription_subscriptionbox/dto/ItemDTO**"
)
}
})
)

}
Binary file added images/grafana 1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/grafana 2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/grafana 3.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/grafana 4.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/grafana 5.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/grafana 6.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/profiling 1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/refactor 1.2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/refactor 1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/refactor 2.0.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,39 @@ public class SubscriptionBoxRepository {
@Autowired
private EntityManager entityManager;


@Transactional
public SubscriptionBox save(SubscriptionBox subscriptionBox) {
validateSubscriptionBox(subscriptionBox);

List<Item> attachedItems = attachItems(subscriptionBox.getItems());
subscriptionBox.setItems(attachedItems);

entityManager.persist(subscriptionBox);
return subscriptionBox;
}

private void validateSubscriptionBox(SubscriptionBox subscriptionBox) {
if (hasThreeSimilarNames(subscriptionBox.getName())) {
throw new IllegalArgumentException("Cannot save subscription box: more than 3 subscription boxes with similar names already exist.");
}
if (existsByNameAndType(subscriptionBox.getName(), subscriptionBox.getType())) {
throw new IllegalArgumentException("Cannot save subscription box: a subscription box with the same name and type already exists.");
}
}

private List<Item> attachItems(List<Item> items) {
List<Item> attachedItems = new ArrayList<>();
for (Item item : subscriptionBox.getItems()) {
for (Item item : items) {
Item existingItem = entityManager.find(Item.class, item.getId());
if (existingItem != null) {
attachedItems.add(existingItem);
} else {
// Persist new item
entityManager.persist(item);
attachedItems.add(item);
}
}
subscriptionBox.setItems(attachedItems);

entityManager.persist(subscriptionBox);
return subscriptionBox;
return attachedItems;
}


Expand Down