Skip to content

Commit 01110c1

Browse files
vishwab1DurgaPrasad-54vanitha1822
authored
Health api (#376)
* Cherry-pick health and version API enhancements to release-3.6.1 (#371) * feat(health,version): update version and health endpoints and add advance check for database * fix(health): normalize severity and fix slow query false positives * fix(health): avoid false CRITICAL on single long-running MySQL transaction * fix(health): enforce 3s DB connection timeout via HikariCP * Release 3.6.1 (#374) * feat(health,version): update version and health endpoints and add advance check for database * fix(health): normalize severity and fix slow query false positives * fix(health): avoid false CRITICAL on single long-running MySQL transaction * fix(health): enforce 3s DB connection timeout via HikariCP * feat(health): add healthcontroller and fix versioncontroller issues * fix: build error (#375) --------- Co-authored-by: KOPPIREDDY DURGA PRASAD <144464542+DurgaPrasad-54@users.noreply.github.com> Co-authored-by: Vanitha S <116701245+vanitha1822@users.noreply.github.com>
1 parent 1cc3888 commit 01110c1

5 files changed

Lines changed: 581 additions & 53 deletions

File tree

pom.xml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,32 @@
520520
<build>
521521
<finalName>${artifactId}-${version}</finalName>
522522
<plugins>
523+
<plugin>
524+
<groupId>io.github.git-commit-id</groupId>
525+
<artifactId>git-commit-id-maven-plugin</artifactId>
526+
<version>9.0.2</version>
527+
<executions>
528+
<execution>
529+
<id>get-the-git-infos</id>
530+
<goals>
531+
<goal>revision</goal>
532+
</goals>
533+
<phase>initialize</phase>
534+
</execution>
535+
</executions>
536+
<configuration>
537+
<generateGitPropertiesFile>true</generateGitPropertiesFile>
538+
<generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties</generateGitPropertiesFilename>
539+
<includeOnlyProperties>
540+
<property>^git.branch$</property>
541+
<property>^git.commit.id.abbrev$</property>
542+
<property>^git.build.version$</property>
543+
<property>^git.build.time$</property>
544+
</includeOnlyProperties>
545+
<failOnNoGitDirectory>false</failOnNoGitDirectory>
546+
<failOnUnableToExtractRepoInfo>false</failOnUnableToExtractRepoInfo>
547+
</configuration>
548+
</plugin>
523549
<plugin>
524550
<groupId>org.apache.maven.plugins</groupId>
525551
<artifactId>maven-jar-plugin</artifactId>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* AMRIT – Accessible Medical Records via Integrated Technology
3+
* Integrated EHR (Electronic Health Records) Solution
4+
*
5+
* Copyright (C) "Piramal Swasthya Management and Research Institute"
6+
*
7+
* This file is part of AMRIT.
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU General Public License as published by
11+
* the Free Software Foundation, either version 3 of the License, or
12+
* (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17+
* See the GNU General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU General Public License
20+
* along with this program. If not, see https://www.gnu.org/licenses/.
21+
*/
22+
23+
package com.iemr.common.controller.health;
24+
25+
import com.iemr.common.service.health.HealthService;
26+
import java.util.Map;
27+
import org.slf4j.Logger;
28+
import org.slf4j.LoggerFactory;
29+
import org.springframework.http.HttpStatus;
30+
import org.springframework.http.ResponseEntity;
31+
import org.springframework.web.bind.annotation.GetMapping;
32+
import org.springframework.web.bind.annotation.RestController;
33+
34+
/**
35+
* Health check controller for Common-API.
36+
* Verifies application liveness and dependency health (DB, Redis).
37+
*
38+
* @author vaishnavbhosale
39+
*/
40+
@RestController
41+
public class HealthController {
42+
43+
private static final Logger logger = LoggerFactory.getLogger(HealthController.class);
44+
45+
private final HealthService healthService;
46+
47+
public HealthController(HealthService healthService) {
48+
this.healthService = healthService;
49+
}
50+
51+
@GetMapping("/health")
52+
public ResponseEntity<Map<String, Object>> health() {
53+
logger.info("Health check endpoint called");
54+
55+
56+
Map<String, Object> healthStatus = healthService.checkHealth();
57+
58+
// Standard HTTP Status logic
59+
String status = (String) healthStatus.get("status");
60+
HttpStatus httpStatus = "UP".equals(status) ? HttpStatus.OK : HttpStatus.SERVICE_UNAVAILABLE;
61+
62+
logger.info("Health check completed with status: {}", status);
63+
64+
return ResponseEntity.status(httpStatus).body(healthStatus);
65+
}
66+
}
Lines changed: 63 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,88 @@
11
/*
2-
* AMRIT – Accessible Medical Records via Integrated Technology
3-
* Integrated EHR (Electronic Health Records) Solution
4-
*
5-
* Copyright (C) "Piramal Swasthya Management and Research Institute"
6-
*
7-
* This file is part of AMRIT.
8-
*
9-
* This program is free software: you can redistribute it and/or modify
10-
* it under the terms of the GNU General Public License as published by
11-
* the Free Software Foundation, either version 3 of the License, or
12-
* (at your option) any later version.
13-
*
14-
* This program is distributed in the hope that it will be useful,
15-
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17-
* GNU General Public License for more details.
18-
*
19-
* You should have received a copy of the GNU General Public License
20-
* along with this program. If not, see https://www.gnu.org/licenses/.
21-
*/
2+
* AMRIT – Accessible Medical Records via Integrated Technology
3+
* Integrated EHR (Electronic Health Records) Solution
4+
*
5+
* Copyright (C) "Piramal Swasthya Management and Research Institute"
6+
*
7+
* This file is part of AMRIT.
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU General Public License as published by
11+
* the Free Software Foundation, either version 3 of the License, or
12+
* (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17+
* See the GNU General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU General Public License
20+
* along with this program. If not, see https://www.gnu.org/licenses/.
21+
*/
22+
/**
23+
* REST controller exposing application version and build metadata.
24+
* <p>
25+
* Provides the <code>/version</code> endpoint which returns Git metadata
26+
* in a standardized JSON format consistent across all AMRIT APIs.
27+
* </p>
28+
*
29+
* @author Vaishnav Bhosale
30+
*/
2231
package com.iemr.common.controller.version;
2332

24-
import java.io.BufferedReader;
25-
import java.io.IOException;
2633
import java.io.InputStream;
27-
import java.io.InputStreamReader;
34+
import java.io.IOException;
35+
import java.util.LinkedHashMap;
36+
import java.util.Map;
37+
import java.util.Properties;
2838

2939
import org.slf4j.Logger;
3040
import org.slf4j.LoggerFactory;
31-
import org.springframework.web.bind.annotation.RequestMapping;
32-
import org.springframework.web.bind.annotation.RequestMethod;
41+
import org.springframework.http.MediaType;
42+
import org.springframework.http.ResponseEntity;
43+
import org.springframework.web.bind.annotation.GetMapping;
3344
import org.springframework.web.bind.annotation.RestController;
3445

35-
import com.iemr.common.utils.response.OutputResponse;
36-
3746
import io.swagger.v3.oas.annotations.Operation;
3847

39-
4048
@RestController
4149
public class VersionController {
4250

43-
private Logger logger = LoggerFactory.getLogger(this.getClass().getSimpleName());
51+
private final Logger logger =
52+
LoggerFactory.getLogger(this.getClass().getSimpleName());
53+
54+
private static final String UNKNOWN_VALUE = "unknown";
4455

45-
@Operation(summary = "Get version")
46-
@RequestMapping(value = "/version", method = { RequestMethod.GET })
47-
public String versionInformation() {
48-
OutputResponse output = new OutputResponse();
56+
@Operation(summary = "Get version information")
57+
@GetMapping(value = "/version", produces = MediaType.APPLICATION_JSON_VALUE)
58+
public ResponseEntity<Map<String, String>> versionInformation() {
59+
Map<String, String> response = new LinkedHashMap<>();
4960
try {
5061
logger.info("version Controller Start");
51-
output.setResponse(readGitProperties());
62+
Properties gitProperties = loadGitProperties();
63+
response.put("buildTimestamp", gitProperties.getProperty("git.build.time", UNKNOWN_VALUE));
64+
response.put("version", gitProperties.getProperty("git.build.version", UNKNOWN_VALUE));
65+
response.put("branch", gitProperties.getProperty("git.branch", UNKNOWN_VALUE));
66+
response.put("commitHash", gitProperties.getProperty("git.commit.id.abbrev", UNKNOWN_VALUE));
5267
} catch (Exception e) {
53-
output.setError(e);
68+
logger.error("Failed to load version information", e);
69+
response.put("buildTimestamp", UNKNOWN_VALUE);
70+
response.put("version", UNKNOWN_VALUE);
71+
response.put("branch", UNKNOWN_VALUE);
72+
response.put("commitHash", UNKNOWN_VALUE);
5473
}
55-
5674
logger.info("version Controller End");
57-
return output.toString();
58-
}
59-
60-
private String readGitProperties() throws Exception {
61-
ClassLoader classLoader = getClass().getClassLoader();
62-
InputStream inputStream = classLoader.getResourceAsStream("git.properties");
63-
64-
return readFromInputStream(inputStream);
75+
return ResponseEntity.ok(response);
6576
}
6677

67-
private String readFromInputStream(InputStream inputStream) throws IOException {
68-
StringBuilder resultStringBuilder = new StringBuilder();
69-
try (BufferedReader br = new BufferedReader(new InputStreamReader(inputStream))) {
70-
String line;
71-
while ((line = br.readLine()) != null) {
72-
resultStringBuilder.append(line).append("\n");
78+
private Properties loadGitProperties() throws IOException {
79+
Properties properties = new Properties();
80+
try (InputStream input = getClass().getClassLoader()
81+
.getResourceAsStream("git.properties")) {
82+
if (input != null) {
83+
properties.load(input);
7384
}
7485
}
75-
return resultStringBuilder.toString();
86+
return properties;
7687
}
77-
}
88+
}

0 commit comments

Comments
 (0)