Σάββατο 17 Οκτωβρίου 2020

Populate database on startup with Spring DataSourceInitializer









Populate database with hardcoded data, when Spring application starts up
















1. Create data.sql script with your data

  • src/main/resources/script/data.sql :



  • Example data.sql script data:

insert into activity_type values (1, 'GYM desc', 'GYM'), (2, 'CAR desc', 'CAR'), (3, 'BASKET desc', 'BASKET'), (4, 'HOUSE desc', 'HOUSE'); insert into activity values (1, 'STRENGTH', 'UpperChest_S', 1), (2, 'HYPERTROPHY', 'PullOverBack_H', 1), (3, 'GAS', 'GAS_MONTHLY', 4), (4, 'GAS', 'GAS_MAINTENANCE', 4), (5, 'CAR_PARTS', 'NewTires', 2), (6, 'CAR_CASUAL', 'Petrol', 2); insert into proj2.day_activity values ('1', NULL, '2020-03-04', '4', '60', '5', '4', '1', '1'), ('2', NULL, '2020-03-04', '9', '20', '8', '3', '2', '1'), ('3', NULL, '2020-03-05', '5', '65', '4', '4', '1', '1'), ('4', NULL, '2020-03-05', '10', '20', '7', '3', '2', '1'), ('5', '34', '2020-06-05', NULL, NULL, NULL, NULL, '3', '4'), ('6', '556', '2020-06-05', NULL, NULL, NULL, NULL, '4', '4'), ('7', '231', '2020-06-05', NULL, NULL, NULL, NULL, '5', '2'), ('8', '14', '2020-06-05', NULL, NULL, NULL, NULL, '6', '2'), ('9', NULL, '2020-03-06', '4', '70', '4', '4', '1', '1');


2. Application.yml


spring: datasource: url: jdbc:mysql://localhost:3306/yourDb?useSSL=false username: root password: yourpass driver-class-name: com.mysql.jdbc.Driver initialization-mode: never



3. Database @Configuration file


@Configuration @EnableJpaRepositories("com.yourproj.repository") @EnableTransactionManagement public class DatabaseConfiguration { @Autowired DataSource dataSource; public DatabaseConfiguration(Environment env) { this.env = env; } @Bean public DataSourceInitializer dataSourceInitializer() { ResourceDatabasePopulator resourceDatabasePopulator = new ResourceDatabasePopulator(); resourceDatabasePopulator.addScript(new ClassPathResource("/script/data.sql")); DataSourceInitializer dataSourceInitializer = new DataSourceInitializer(); dataSourceInitializer.setDataSource(dataSource); dataSourceInitializer.setDatabasePopulator(resourceDatabasePopulator); return dataSourceInitializer; } }

Κυριακή 27 Σεπτεμβρίου 2020

Lighthouse Angular performance improvement hints






Improve the performance of our Angular frontend app, using Lighthouse plugin of Chrome's DevTools:

















SERVE STATIC ASSETS WITH EFFICIENT CACHE POLICY



HTTP Caching
- Edit HTTP response's cache-control / max-age to a bigger number
- Verify in Inspect/Network tab, that images are cached (from memory cache)
See https://web.dev/uses-long-cache-ttl/


Angular-level caching
- Create an ImageService that:
- Stores images' blobs in array of url/blob objects
- Retrieves images from this array, if already stored there


Method-level caching with ngx-cacheable
- Use cacheable decorator to:
- Retrieve objects from cache (@Cacheable)
- Update cache when saving/updating function runs (@CacheBuster)







MINIMIZE MAIN THREAD WORK (SCRIPT EVALUATION)


Optimize third-party JavaScript



     Check all "Show 3rd-party resources" points, and see if they are time-consuming.
     Consider refactoring these 3rd parties' packages



Debounce your input handlers

 - Using input handlers / animations can slow your app's responses
 - In order to diagnose this, 
    - Run Performance test in Performance tab (or click View Trace in Lighthouse tab)
    - Search for "Recalculate Style" or "Forced Reflow" signs, like below:







Use web workers


 - Relief main thread's work, with web workers
 - Using these, some chosen actions/methods will execute on another thread.
 - First, we pass data from DOM/main thread to Worker thread (worker.postMessage(data))
 - Then, worker does the heavy job
 - Finally, worker passes data from worker's thread to DOM/main thread(postMessage(data))













MINIMIZE MAIN BUNDLE SIZE


Remove imports of large CSS files (e.g. styles.scss) in SCSS/CSS files of all components.
Refactor former ones to smaller files, and then import in each component.











IMPROVE IMAGES/MEDIA FETCHING


Avoid multiple calls for media assets for every component or request.
Have a centralized service for this task.










Κυριακή 13 Σεπτεμβρίου 2020

Spring - Validate REST layer (Java Bean Validation API 2.0 - JSR-380)






Validate a list of DTOs when reaching our endpoint

















1. Dependencies - pom.xml


<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>

or

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId> <version>2.2.6.RELEASE</version> </dependency>


(By default, Spring uses hibernate-validator, which implements the JSR-380)


2. Controller class - REST level


// Enables validation in method level @Validated @RestController @RequestMapping("/api/project") public class DayActivityResource { // Our list object cannot be null // List elements are validated each, using @Valid @PostMapping("/day-activities") public ResponseEntity<List<DayActivityDTO>> createDayActivities(@NotNull @RequestBody List<@Valid DayActivityDTO> dayActivityDtos) { // ... } }



3. DTO classes with constraint annotations



import javax.validation.constraints.NotNull; import javax.validation.constraints.Pattern; import javax.validation.constraints.Size; import javax.validation.constraints.Valid; public class DayActivityDTO implements Serializable { private String id; private String activityId; private String activityTypeId; // We want this String to be numeric @Pattern(message = "amountFactor must be a number", regexp="^[0-9]*$") @Size(min=1, max=7) private String generalFactor; // A log date must always exist @NotNull private LocalDate logDate; // Validate nested object @Valid private DayActivityRecurringInfo recurringInfo; @Email(message = "Please enter a valid email") private String sendUpdateToEmail; } // Nested class public class DayActivityRecurringInfo { @Min(1) private Integer recurEveryDays; @Max(4) private Integer recurForWeeks; }