Skip to content
This repository was archived by the owner on Apr 5, 2024. It is now read-only.

Commit adbe33f

Browse files
authored
FF-96 Implement SystemHealth DataIntegrity Key (#24)
1 parent 80887a6 commit adbe33f

File tree

12 files changed

+133
-9
lines changed

12 files changed

+133
-9
lines changed

pom.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@
3131
<artifactId>spring-boot-starter-data-mongodb</artifactId>
3232
</dependency>
3333

34+
<dependency>
35+
<groupId>org.springframework.boot</groupId>
36+
<artifactId>spring-boot-configuration-processor</artifactId>
37+
<optional>true</optional>
38+
</dependency>
39+
3440
<dependency>
3541
<groupId>io.springfox</groupId>
3642
<artifactId>springfox-boot-starter</artifactId>
@@ -97,6 +103,14 @@
97103
<plugin>
98104
<groupId>org.springframework.boot</groupId>
99105
<artifactId>spring-boot-maven-plugin</artifactId>
106+
<configuration>
107+
<excludes>
108+
<exclude>
109+
<groupId>org.springframework.boot</groupId>
110+
<artifactId>spring-boot-configuration-processor</artifactId>
111+
</exclude>
112+
</excludes>
113+
</configuration>
100114
</plugin>
101115
<plugin>
102116
<groupId>org.jacoco</groupId>

src/main/java/de/filefighter/rest/RestApplication.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package de.filefighter.rest;
22

3+
import de.filefighter.rest.configuration.FileFighterProperties;
34
import org.springframework.boot.SpringApplication;
45
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
57
import org.springframework.context.annotation.Bean;
68
import org.springframework.web.client.RestTemplate;
79

810
@SpringBootApplication
11+
@EnableConfigurationProperties(FileFighterProperties.class)
912
public class RestApplication {
1013
public static void main(String[] args) {
1114
SpringApplication.run(RestApplication.class, args);
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package de.filefighter.rest.configuration;
2+
3+
import lombok.Data;
4+
import org.springframework.boot.context.properties.ConfigurationProperties;
5+
import org.springframework.context.annotation.Configuration;
6+
7+
@Configuration
8+
@ConfigurationProperties(prefix = "filefighter")
9+
public class FileFighterProperties {
10+
11+
/**
12+
* Version String.
13+
*/
14+
private String version = "undefined";
15+
private String date = "undefined";
16+
17+
public String getVersion() {
18+
return version;
19+
}
20+
21+
public void setVersion(String version) {
22+
this.version = version;
23+
}
24+
25+
public String getDate() {
26+
return date;
27+
}
28+
29+
public void setDate(String date) {
30+
this.date = date;
31+
}
32+
}

src/main/java/de/filefighter/rest/configuration/PrepareDataBase.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ public class PrepareDataBase {
2424
@Value("${server.port}")
2525
int serverPort;
2626

27+
@Value("${filefighter.version}")
28+
String version;
29+
30+
@Value("${filefighter.date}")
31+
String date;
32+
2733
private static final Logger LOG = LoggerFactory.getLogger(PrepareDataBase.class);
2834

2935
@Bean
@@ -39,7 +45,7 @@ CommandLineRunner veryImportantFileFighterStartScript() {
3945
System.out.println(" | _| | | | | | __/ | _| | | | (_| | | | | | | |_ | __/ | | ");
4046
System.out.println(" |_| |_| |_| \\___| |_| |_| \\__, | |_| |_| \\__| \\___| |_| ");
4147
System.out.println(" |___/ ");
42-
System.out.println(" Version 0.2 Last updated at 03.11.20 ");
48+
System.out.println(" Version v"+version+" Last updated at "+date+" ");
4349
System.out.println(" Developed by Gimleux, Valentin, Open-Schnick. ");
4450
System.out.println(" Development Blog: https://filefighter.github.io ");
4551
System.out.println(" The code can be found at: https://www.github.com/filefighter ");

src/main/java/de/filefighter/rest/configuration/SwaggerConfiguration.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package de.filefighter.rest.configuration;
22

3+
import org.springframework.beans.factory.annotation.Value;
34
import org.springframework.context.annotation.Bean;
45
import org.springframework.context.annotation.Configuration;
56
import springfox.documentation.builders.RequestHandlerSelectors;
@@ -15,6 +16,9 @@
1516
@EnableSwagger2
1617
public class SwaggerConfiguration {
1718

19+
@Value("${filefighter.version}")
20+
String version;
21+
1822
@Bean
1923
public Docket swaggerConfig() {
2024
return new Docket(DocumentationType.SWAGGER_2)
@@ -28,7 +32,7 @@ private ApiInfo apiInfo() {
2832
return new ApiInfo(
2933
"FileFighter REST",
3034
"REST-API of the FileFighter application.",
31-
"0.2",
35+
version,
3236
null,
3337
new Contact("FileFighter Dev-Team", "https://github.com/filefighter/", "[email protected]"),
3438
"MIT License",

src/main/java/de/filefighter/rest/domain/health/business/SystemHealthBusinessService.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package de.filefighter.rest.domain.health.business;
22

33
import de.filefighter.rest.domain.health.data.SystemHealth;
4+
import de.filefighter.rest.domain.health.data.SystemHealth.DataIntegrity;
5+
import de.filefighter.rest.domain.token.business.AccessTokenBusinessService;
46
import de.filefighter.rest.domain.user.business.UserBusinessService;
7+
import org.springframework.beans.factory.annotation.Value;
58
import org.springframework.stereotype.Service;
69

710
import java.time.Instant;
@@ -10,10 +13,15 @@
1013
public class SystemHealthBusinessService {
1114

1215
private final UserBusinessService userBusinessService;
16+
private final AccessTokenBusinessService accessTokenBusinessService;
1317
private final long serverStartedAt;
1418

15-
public SystemHealthBusinessService(UserBusinessService userBusinessService) {
19+
@Value("${filefighter.version}")
20+
String version;
21+
22+
public SystemHealthBusinessService(UserBusinessService userBusinessService, AccessTokenBusinessService accessTokenBusinessService) {
1623
this.userBusinessService = userBusinessService;
24+
this.accessTokenBusinessService = accessTokenBusinessService;
1725
this.serverStartedAt = this.getCurrentEpochSeconds();
1826
}
1927

@@ -22,9 +30,23 @@ public SystemHealth getCurrentSystemHealthInfo(){
2230
return SystemHealth.builder()
2331
.uptimeInSeconds(currentEpoch - serverStartedAt)
2432
.userCount(userBusinessService.getUserCount())
33+
.dataIntegrity(calculateDataIntegrity())
34+
.version("v"+this.version)
2535
.build();
2636
}
2737

38+
private DataIntegrity calculateDataIntegrity() {
39+
long userCount = userBusinessService.getUserCount();
40+
long accessTokenCount = accessTokenBusinessService.getAccessTokenCount();
41+
42+
// Risk / Unstable Cases.
43+
if(userCount < accessTokenCount){
44+
return DataIntegrity.POSSIBLE_RISK;
45+
}
46+
47+
return DataIntegrity.STABLE;
48+
}
49+
2850
public long getCurrentEpochSeconds(){
2951
return Instant.now().getEpochSecond();
3052
}

src/main/java/de/filefighter/rest/domain/health/data/SystemHealth.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,10 @@
1212
public class SystemHealth {
1313
private final long uptimeInSeconds;
1414
private final long userCount;
15+
private final DataIntegrity dataIntegrity;
16+
private final String version;
17+
18+
public enum DataIntegrity {
19+
STABLE, POSSIBLE_RISK, UNSTABLE
20+
}
1521
}

src/main/java/de/filefighter/rest/domain/token/business/AccessTokenBusinessService.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,18 @@ public AccessToken findAccessTokenByValue(String accessTokenValue) {
8080
return accessTokenDtoService.createDto(accessTokenEntity);
8181
}
8282

83-
public String generateRandomTokenValue() {
84-
return UUID.randomUUID().toString();
85-
}
8683

8784
public String checkBearerHeader(String accessTokenValue) {
8885
if (!accessTokenValue.matches("^" + AUTHORIZATION_BEARER_PREFIX + "[^\\s](.*)$"))
8986
throw new RequestDidntMeetFormalRequirementsException("Header does not contain '" + AUTHORIZATION_BEARER_PREFIX + "', or format is invalid.");
9087
return accessTokenValue.split(AUTHORIZATION_BEARER_PREFIX)[1];
9188
}
89+
90+
public String generateRandomTokenValue() {
91+
return UUID.randomUUID().toString();
92+
}
93+
94+
public long getAccessTokenCount() {
95+
return accessTokenRepository.count();
96+
}
9297
}
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
server.servlet.context-path=/
22
server.port=8080
33
server.error.whitelabel.enabled=false
4-
54
#-------------------MONGO---------------
65
spring.data.mongodb.authentication-database=admin
76
spring.data.mongodb.database=filefighter
87
spring.data.mongodb.host=localhost
9-
spring.data.mongodb.port=27017
8+
spring.data.mongodb.port=27017
9+
#-------------------Custom------------------
10+
filefighter.version=0.0.3
11+
filefighter.date=11.11.2020

src/main/resources/filefighter.png

-38.6 KB
Binary file not shown.

0 commit comments

Comments
 (0)