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(S...

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);     ...

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

Installing Python 2.7.13 on CentOS 6.5

This post describes how to install python 2.7 from source without affecting the existing python installation. It'll install the python2.7 in a separate location other than the standard location in CentOS. Install required packages:  yum -y update yum groupinstall -y 'development tools' yum install -y gcc zlib-devel bzip2-devel openssl-devel xz-libs wget Download and extract python source: wget http://www.python.org/ftp/python/2.7.13/Python-2.7.13.tar.xz  xz -d Python-2.7.13.tar.xz  tar -xvf Python-2.7.13.tar Create a directory where to install python e.g. ~/localpython: mkdir /home/nayan/localpython Go to extracted python source directory: cd Python-2.7.13 Run the configure: ./configure --prefix=/home/nayan/localpython --enable-shared Compile and install it:  make make altinstall Check if python installed by running python2.7 Make a link to Python library path: On 32 bit OS ln -s /home/nayan/localpython/lib/libpython2.7.so.1...

Generate model class in peewee

peewee is a very lighweight but rich orm for python. Often we need to generate model classes from an existing database table. It's possible to do this in peewee using an addon pwiz model generator . After installing pwiz, use the following command to generate model classes from an existing database. python -m pwiz -e mysql -u root -H localhost -P pesp_db > models.py Here is a brief description of the options: -e : name of the database engine e.g. mysql, postgresql -u : name of db user -H: name of db host The last parameter pesp_db in this case is the name of the database schema. If you run this command, you'll be prompted to provide your password. After providing the password, the model classes will be generated and written in the models.py

Generate a Excel file from Spring

We often need to generate a spreadsheet file from server and allow users to download the file. Here is how to do that with Spring using the Apache POI library. Dependency: We need to add the following dependencies in our spring application. Here is the Gradle dependencies. compile group: 'org.apache.poi', name: 'poi', version: '3.15' compile group: 'org.apache.poi', name: 'poi-ooxml', version: '3.15' Once we add the dependencies we'll be able to access the POI library in our application. Generate WorkBook We need to generate a Workbook object in POI. Here is a sample method that returns a Workbook object public Workbook downloadPersonList() throws IOException {     // sample list. this can be the output of a DB query     List<Person> personList = new ArrayList<>();       personList.add(new Person("nayan", 29));     personList.add(new Person("rafiq", 35)) ;     personList.add(ne...

Print a progress in Python

Here is how to do this: import sys sys.stdout.write("\r%d" % i) sys.stdout.flush() Writing '\r' will move the cursor back to the beginning of the line. If we want to print a %, we need to print an additional % like the folllowing: sys.stdout.write("\r%d%%" % i)

Automatic MongoDB backup in windows

It's a very good idea to take backups of our MongoDB periodically. We can use the following script to create a folder with current date time and dump the database in it. The script also runs 7zip and compresses the backup directory. When compression is done, this script will delete the backup directory. This will save a lot of disk space. @echo OFF :: This will create a timestamp like yyyy-mm-dd-hh-mm-ss. set BACKUPNAME=E:\mongo-db-backup set BACKUPNAME=%BACKUPNAME%\%DATE:~10,4%-%DATE:~4,2%-%DATE:~7,2%-%TIME:~0,2%-%TIME:~3,2%-%TIME:~6,2% @echo BACKUPNAME=%BACKUPNAME% :: Create a new directory md "%BACKUPNAME%" echo Running backup "%BACKUPNAME%" mongodump -h localhost -d stipend_icr -u stipend_icr_user -p pr0g0t1 -o "%BACKUPNAME%" REM ZIP the backup directory echo Running 7zip on backup "%BACKUPNAME%" "C:\Program Files\7-Zip\7z.exe" a -tzip "%BACKUPNAME%.zip" "%BACKUPNAME%" REM Delete the back...

Map GET parameters to Object in Spring

It's possbile to map GET parameters to an object in Spring. It's really useful when we need to pass lots of parameters as query string e.g. submitting a search form with many parameters. The following controller demonstrates this.   @RestController @RequestMapping("hello") public class MyController {     @RequestMapping(value = "data", method = RequestMethod.GET)     public MyRequest getData(MyRequest request){             return request;     }     public static class  MyRequest {         private String name;          private int age;         public String getName(){return this.name;}         public int getAge(){return this.name;}         public void setName(String name){this.name=name;}          public void setAge(int age){this.age=ag...

Pass state parameter from url in angular-ui-router

URL parameters are automatically sent as state parameters in angular-ui-router . All we need to do is declare the parameter names in state configuration. Here is an example of state configuration: .state('home.profile', {     url: '/profile?id',     templateUrl: 'templates/profile.html',     controller: 'ProfileCtrl' }) Here we've declared a parameter id in url attribute.  Now if we hit the url with parameter id e.g /profile?id=1234343 , w e'll be able to receive id=1234343 in state params.  We'll also be able to pass state parameter through ui-sre f lik e the following: <a ui-sref="home.profile({id: '1234343' })">Go to profile</a> Here is an example controller that demonstrates how to receive the parameter : .controller('ProfileCtrl', ['$scope', '$stateParams',     function ($scope, $stateParams) {         $scope.id = $stateParams.id; // id will be 1234...