Posts

Sample code to write custom query in reactive mongo

 This is a sample code snippet to demostrate how to write a custom aggregation with spring data reactive mongo. Here are the sample collection structures: Collection: UserData {    "userId":"123456",    "profilePhotoAssetId":"abcd1234" } Collection: Comment {    "authorId":"123456",    "body":"This is a sample comment",    "userPhotoId": null } userPhotoId is a transient field. It'll be set from the aggregation result. I want to get all comments and inside the comment, I want the profilePhotoAssetId from UserData in the userPhotoId field.  For the above example, the sample output should be: {    "authorId":"123456",    "body":"This is a sample comment",    "userPhotoId":"abcd1234" } What are the steps? 1. First step is to write a lookup operation that'll join the two collection based on comment.authorId and userData.userId. After

Use Postgresql from docker

To me - it's always painful to install and configure Postgresql on local machine. As a developer, it kills much time. Another problem is to manage a list of servers when I've to work with multiple postgres version at the same time. It's better to use Docker to setup database rather than manually installing the DB server. It's super easy to install any database version with just a few steps. In this post I'm going to note down the steps I've followed to install Postgresql 9 inside a docker container. Also I'll import from an existing database to this one.   Step one: Install docker There are plenty of tutorials on this so I'm skipping it. At the end of this step you should be able to run the docker command from terminal. If you are new to docker, I'll recommend to play with it a bit so that you are familiar with the keywords: container, image, docker hub etc.   Step two: Pull the docker image We need to pull the image from docker hub to our local mach

Django logging configuration

I wanted to configure logging in my django project. I had the following requirements: - All logs will be written to a file named app.log and the file will be rotated on each day - All error logs will be written to a file named app.error.log and the file will be rotated on each day - Logs will be written to console if DEBUG=True is set After experimenting with different configurations, I finally was able to achieve my goal using the following configuration. DJANGO_LOG_LEVEL = 'DEBUG' # need to change this value to enable/disable debug logs LOGGING = { 'version' : 1 , 'disable_existing_loggers' : False , 'formatters' : { 'simple' : { 'format' : '%(levelname)s %(asctime)s %(module)s %(funcName)s:%(lineno)d %(message)s' }, }, 'filters' : { 'require_debug_true' : { '()' : 'django.utils.log.RequireDebugTrue' , },

Unit testing of Spring Service with constructor dependencies

Constructor based dependency injection is the recommended way to use dependency injection in Spring. In this post I'm going to demonstrate how to write unit test of a service class which has dependencies on other beans e.g. repositories. Let's assume our service class is like the following: @Service class MyService { private final UserRepository userRepository;     private final AddressRepository addressRepository;     @Autowired     public MyService(UserRepository userRepository, AddressRepository addressRepository) {         this.userRepository = userRepository;         this.addressRepository = addressRepository;     }     public boolean isUserHasAddress(String username){     // omitting the method details, will return true always     return true;     } } Now we need to write unit test for the above class. Here is a way to do it: @RunWith(SpringRunner.class) public class MyServiceTest {     private MyService myService;     @MockBean     privat

Conditional field inclusion in Jackson and Spring Boot

When we write JSON API with spring boot, we often need to customize which fields should be included in our response JSON and which should not. For example, suppose we've a Model like the following: public class User {     private Long id;     private String name;     private String password;     private List<String> children;     // getters and setters here } Now we've the following controller: @RestController @RequestMapping("/api/users") public class UserController {     @GetMapping     public List<User> userList(){         User user = new User();         user.setId(10L);         user.setName("Rafiqunnabi Nayan");         user.setPassword("abcd");         user.setChildren(Arrays.asList("Child 1", "Child 2"));         return Arrays.asList(user);     }     @GetMapping("{id}")     public User userDetails(@PathVariable Long id){         User user = new User();         user.set

Deploy Python Application with Gunicorn and Supervisor on Ubuntu

Suppose we have to deploy 3 python applications on a Ubuntu server. The applications and their python versions are as follows: 1. Analytics application (analytics.example.com): python2 2. Android API (android.example.com): python3.5 3. Auth API (auth.example.com): python3.6 What will be the solution? How can we run 3 different python applications on 3 different python versions on same machine? Here is a simple solution to achieve this goal. I'm assuming you know about python virtual environments and wsgi. If not Google will help you on this. The following steps should work with most of Ubuntu versions. Here are our plans to achieve our goal: 1. We'll create 3 separate virtual environment to run these applications 2. We'll use gunicorn inside these virtual environments to run these applications on 3 different ports e.g. 8001, 8002, 8003 3. We'll use supervisor to start, stop and monitor these 3 applications running on gunicorn Lets assume we've the co

Get commits between two tags in Git

We often need to get commits between two git tags. For example, we want to prepare a release note. So we want to get the commit messages between the two release tags. Here is the command to do this: git log --pretty=oneline tagA...tagB # three dots Example: git log --pretty=oneline v3.9.0...v3.9.1 Source:  https://stackoverflow.com/questions/5863426/get-commit-list-between-tags-in-git