Skip to content

Commit

Permalink
Health
Browse files Browse the repository at this point in the history
  • Loading branch information
nikunjmulani committed Oct 31, 2018
1 parent dc21cab commit 23520f3
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 8 deletions.
9 changes: 9 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ dependencies {
compile("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.9.1")
compile("org.springframework.boot:spring-boot-starter-jdbc")
compile("mysql:mysql-connector-java:6.0.6")
compile("org.springframework.boot:spring-boot-starter-actuator")
testCompile("org.springframework.boot:spring-boot-starter-test")
}

Expand All @@ -23,12 +24,16 @@ def developmentDbUrl = "jdbc:mysql://localhost:3306/tracker_dev?user=tracker&use
bootRun.environment([
"WELCOME_MESSAGE": "hello",
"SPRING_DATASOURCE_URL": developmentDbUrl,
"MANAGEMENT_SECURITY_ENABLED": false,

])

def testDbUrl = "jdbc:mysql://localhost:3306/tracker_test?user=tracker&useSSL=false&useTimezone=true&serverTimezone=UTC&useLegacyDatetimeCode=false"
test.environment([
"WELCOME_MESSAGE": "Hello from test",
"SPRING_DATASOURCE_URL": testDbUrl,
"MANAGEMENT_SECURITY_ENABLED": false,

])

flyway {
Expand All @@ -41,3 +46,7 @@ flyway {
task testMigrate(type: FlywayMigrateTask) {
url = testDbUrl
}

springBoot {
buildInfo()
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import java.util.*;
import java.util.stream.Collectors;


@Repository
public class InMemoryTimeEntryRepository implements TimeEntryRepository {
private Map<Long, TimeEntry> data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.springframework.jdbc.core.namedparam.NamedParameterUtils;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import javax.sql.DataSource;
import java.sql.Date;
Expand All @@ -15,6 +16,7 @@

import static java.sql.Statement.RETURN_GENERATED_KEYS;


public class JdbcTimeEntryRepository implements TimeEntryRepository {


Expand Down
32 changes: 28 additions & 4 deletions src/main/java/io/pivotal/pal/tracker/TimeEntryController.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,50 @@
package io.pivotal.pal.tracker;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.actuate.metrics.CounterService;
import org.springframework.boot.actuate.metrics.GaugeService;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.sql.DataSource;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping(value = "/time-entries")
public class TimeEntryController {


@Autowired
private GaugeService gauge;
private CounterService counter;
private TimeEntryRepository timeEntryRepository;

public TimeEntryController(TimeEntryRepository timeEntryRepository) {
public TimeEntryController(
TimeEntryRepository timeEntryRepository,
CounterService counter,
GaugeService gauge
) {
this.timeEntryRepository = timeEntryRepository;
this.counter = counter;
this.gauge = gauge;
}





@PostMapping
public ResponseEntity create(@RequestBody TimeEntry timeEntryToCreate) {
return ResponseEntity.status(HttpStatus.CREATED).body(this.timeEntryRepository.create(timeEntryToCreate));
timeEntryToCreate = this.timeEntryRepository.create(timeEntryToCreate);

counter.increment("TimeEntry.created");
gauge.submit("timeEntries.count", timeEntryRepository.list().size());

return ResponseEntity.status(HttpStatus.CREATED).body(timeEntryToCreate);
}


Expand All @@ -34,6 +53,7 @@ public ResponseEntity<TimeEntry> read(@PathVariable long timeEntryId) {
TimeEntry timeEntry = this.timeEntryRepository.find(timeEntryId);

if (Objects.nonNull(timeEntry)) {
counter.increment("TimeEntry.read");
return ResponseEntity.ok(timeEntry);

}
Expand All @@ -42,6 +62,7 @@ public ResponseEntity<TimeEntry> read(@PathVariable long timeEntryId) {

@GetMapping
public ResponseEntity<List<TimeEntry>> list() {
counter.increment("TimeEntry.listed");
return ResponseEntity.ok(this.timeEntryRepository.list());
}

Expand All @@ -51,6 +72,7 @@ public ResponseEntity update(@PathVariable long timeEntryId, @RequestBody TimeEn
timeEntry = this.timeEntryRepository.update(timeEntryId, timeEntry);

if (Objects.nonNull(timeEntry)) {
counter.increment("TimeEntry.updated");
return ResponseEntity.ok(timeEntry);

}
Expand All @@ -64,6 +86,8 @@ public ResponseEntity update(@PathVariable long timeEntryId, @RequestBody TimeEn
@DeleteMapping("/{timeEntryId}")
public ResponseEntity<TimeEntry> delete(@PathVariable long timeEntryId) {
this.timeEntryRepository.delete(timeEntryId);
counter.increment("TimeEntry.deleted");
gauge.submit("timeEntries.count", timeEntryRepository.list().size());
return ResponseEntity.noContent().build();

}
Expand Down
30 changes: 30 additions & 0 deletions src/main/java/io/pivotal/pal/tracker/TimeEntryHealthIndicator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.pivotal.pal.tracker;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;

@Component
public class TimeEntryHealthIndicator implements HealthIndicator {

private static final int MAX_TIME_ENTRIES = 5;

@Autowired
private TimeEntryRepository timeEntryRepository;


@Override
public Health health() {
Health.Builder builder = new Health.Builder();

if(timeEntryRepository.list().size() < MAX_TIME_ENTRIES) {
builder.up();
} else {
builder.down();
}

return builder.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import java.util.List;


@Repository
public interface TimeEntryRepository {
TimeEntry create(TimeEntry timeEntry);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import io.pivotal.pal.tracker.TimeEntryRepository;
import org.junit.Before;
import org.junit.Test;
import org.springframework.boot.actuate.metrics.CounterService;
import org.springframework.boot.actuate.metrics.GaugeService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

Expand All @@ -21,12 +23,15 @@
public class TimeEntryControllerTest {
private TimeEntryRepository timeEntryRepository;
private TimeEntryController controller;

private GaugeService gauge;
private CounterService counter;

@Before
public void setUp() throws Exception {
timeEntryRepository = mock(TimeEntryRepository.class);
controller = new TimeEntryController(timeEntryRepository);
gauge = mock(GaugeService.class);
counter = mock(CounterService.class);
controller = new TimeEntryController(timeEntryRepository, counter, gauge);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

import com.jayway.jsonpath.DocumentContext;
import com.mysql.cj.jdbc.MysqlDataSource;
import io.pivotal.pal.tracker.JdbcTimeEntryRepository;
import io.pivotal.pal.tracker.PalTrackerApplication;
import io.pivotal.pal.tracker.TimeEntry;
import io.pivotal.pal.tracker.TimeEntryRepository;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
Expand All @@ -18,6 +21,7 @@
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.junit4.SpringRunner;

import javax.sql.DataSource;
import java.time.LocalDate;
import java.util.Collection;
import java.util.TimeZone;
Expand Down Expand Up @@ -48,6 +52,9 @@ public void setUp() throws Exception {
TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
}




@Test
public void testCreate() throws Exception {

Expand Down

0 comments on commit 23520f3

Please sign in to comment.