Weatherapianalysis
Case Study - Weather Api Analysis (Java 21, Spring Boot, Postgres, JUnit,Docker, AOP, Kubernetes, Prometheus, Grafana, Github Actions (CI/CD), Jenkins, Sonarqube, Test Container)
Install / Use
/learn @Rapter1990/WeatherapianalysisREADME
Case Study - Weather Api Analysis Api
<p align="center"> <img src="screenshots/main_screenshot.png" alt="Main Information" width="800" height="500"> </p>📖 Information
<ul style="list-style-type:disc"> <li> This project provides a <b>Spring Boot API</b> for <b>Air Quality Management</b>, allowing users to fetch <b>real-time and historical air quality data</b> based on request parameters. The API supports <b>logging</b> for request tracking and database integration. </li> <li> <b>Air Quality Management:</b> <ul> <li> Retrieve <b>real-time air quality data</b> based on latitude, longitude, and date. </li> <li> Retrieve <b>historical air pollution data</b> based on <b>city name</b> and <b>date range</b>. </li> <li> <b>City Name is mandatory</b> in queries, while the <b>date range</b> is optional. If not provided, the system defaults to the <b>last 7 days</b>. </li> <li> Only <b>London, Barcelona, Ankara, Tokyo, and Mumbai</b> are supported for historical data queries. </li> <li> The API response includes <b>pollutant categories</b> (CO, SO2, O3, etc.), sorted <b>chronologically</b> in <b>JSON format</b>. </li> <li> <b>Example Request:</b> <code>/api/v1/airquality/history?city=ankara&startDate=16-01-2025&endDate=17-01-2025</code> </li> <li> <b>Example Response:</b> </li> <pre> { "City": "Ankara", "Results": [ { "Date": "16-05-2025", "Categories": [ { "CO": "Satisfactory" }, { "SO2": "Moderate" }, { "O3": "Satisfactory" } ] }, { "Date": "17-05-2025", "Categories": [ { "CO": "Good" }, { "SO2": "Severe" }, { "O3": "Satisfactory" } ] } ] } </pre> </ul> </li> <li> <b>Database Integration:</b> <ul> <li> Each query first checks the <b>database</b>. Only missing data will be fetched from the <b>external API</b>, and then stored in the database. </li> <li> A log entry is created indicating whether data was retrieved from the <b>database</b> or from the <b>API</b>. </li> <li> <b>Example Workflow:</b> <ul> <li><b>Query:</b> `Paris, 05-01-2025 to 10-01-2025` → Data is <b>not in the database</b>, so it is fetched from the <b>API</b> and stored.</li> <li><b>Query:</b> `Ankara, 05-01-2025 to 10-01-2025` → Data is <b>not in the database</b>, so it is fetched from the <b>API</b> and stored.</li> <li> <b>Query:</b> `Paris, 01-01-2025 to 15-01-2025` → <ul> <li>01-01-2025 to 04-01-2025 → <b>Fetched from API</b> and stored.</li> <li>05-01-2025 to 10-01-2025 → <b>Fetched from the database</b>.</li> <li>11-01-2025 to 15-01-2025 → <b>Fetched from API</b> and stored.</li> </ul> </li> </ul> </li> </ul> </li> <li> <b>Logging:</b> <ul> <li> <b>Custom Logging Aspect:</b> Logs details of REST controller method calls for database Integration and exceptions. </li> </ul> </li> </ul>Explore Rest APIs
Endpoints Summary
<table style="width:100%;"> <tr> <th>Method</th> <th>Url</th> <th>Description</th> <th>Request Body</th> <th>Path Variable</th> <th>Response</th> </tr> <tr> <td>POST</td> <td>/api/v1/airquality</td> <td>Retrieve air quality data</td> <td>AirQualityRequest</td> <td>None</td> <td>CustomResponse<CustomAirQualityResponse></td> </tr> </table>Technologies
- Java 21
- Spring Boot 3.0
- Restful API
- Mapstruct
- Open Api (Swagger)
- Maven
- Junit5
- Mockito
- Integration Tests
- Docker
- Docker Compose
- CI/CD (Github Actions - Jenkins)
- Postman
- TestContainer
- Postgres
- Prometheus
- Grafana
- Kubernetes
- JaCoCo (Test Report)
- Sonarqube
Postman
Import postman collection under postman_collection folder
Prerequisites
Define Variable in .env file
WEATHER_DB_IP=localhost
WEATHER_DB_PORT=5432
POSTGRES_USER={POSTGRES_USER}
POSTGRES_PASSWORD={POSTGRES_PASSWORD}
OPEN_WEATHER_API_KEY={OPEN_WEATHER_API_KEY}
Open Api (Swagger)
http://localhost:1100/swagger-ui/index.html
JaCoCo (Test Report)
After the command named mvn clean install completes, the JaCoCo report will be available at:
target/site/jacoco/index.html
Navigate to the target/site/jacoco/ directory.
Open the index.html file in your browser to view the detailed coverage report.
Maven, Docker and Kubernetes Running Process
Maven Run
To build and run the application with Maven, please follow the directions shown below;
$ cd weatherapianalyis
$ mvn clean install
$ mvn spring-boot:run
Docker Run
The application can be built and run by the Docker engine. The Dockerfile has multistage build, so you do not need to build and run separately.
Please follow directions shown below in order to build and run the application with Docker Compose file;
$ cd weatherapianalyis
$ docker-compose up -d
If you change anything in the project and run it on Docker, you can also use this command shown below
$ cd weatherapianalyis
$ docker-compose up --build
To monitor the application, you can use the following tools:
-
Prometheus:
Open in your browser at http://localhost:9090
Prometheus collects and stores application metrics. -
Grafana:
Open in your browser at http://localhost:3000
Grafana provides a dashboard for visualizing the metrics.
Default credentials:- Username:
admin - Password:
admin
- Username:
-
Define prometheus data source url, use this link shown below
http://prometheus:9090
Kubernetes Run
To build and run the application with Maven, please follow the directions shown below;
- Start Minikube
$ minikube start
- Open Minikube Dashboard
$ minikube dashboard
- To deploy the application on Kubernetes, apply the Kubernetes configuration file underneath k8s folder
$ kubectl apply -f k8s
- To open Prometheus, click tunnel url link provided by the command shown below to reach out Prometheus
minikube service prometheus-service
- To open Grafana, click tunnel url link provided by the command shown below to reach out Prometheus
minikube service grafana-service
- Define prometheus data source url, use this link shown below
http://prometheus-service.default.svc.cluster.local:9090
- To open Sonarqube, click tunnel url link provided by the command shown below to reach out Sonarqube
minikube service sonarqube
Docker Image Location
https://hub.docker.com/repository/docker/noyandocker/jenkins-jenkins/general
https://hub.docker.com/repository/docker/noyandocker/weatherapianalysis/general
Sonarqube
- Go to
localhost:9000for Docker and Go there throughminikube service sonarqubefor Kubernetes - Enter username and password as
admin - Change password
- Click
Create Local Project - Choose the baseline for this code for the project as
Use the global setting - Click
Locallyin Analyze Method - Define Token
- Click
Continue - Copy
sonar.host.urlandsonar.token(sonar.login) in thepropertiespart inpom.xml - Run
mvn sonar:sonarto show code analysis
Jenkins
- Go to
jenkinsfolder - Run
docker-compose up -d - Open Jenkins in the browser via
localhost:8080 - Go to pipeline named
weatherapianalysis - Run Pipeline
- Show
Pipeline Stepto verify if it succeeded or failed
