aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAdrián Oliva <adrian.oliva@cimat.mx>2023-05-29 22:52:44 -0600
committerAdrián Oliva <adrian.oliva@cimat.mx>2023-05-29 22:52:44 -0600
commit4072c7c3890a6629ab6a96830481716898972028 (patch)
tree8df0f83e4fc7d5c46ada43d78ef37d9e71d3c27a /src
parent82a11efa31f0a0d259e089e702f58dd9c141ed02 (diff)
downloadToDo-App-BE-4072c7c3890a6629ab6a96830481716898972028.tar.gz
ToDo-App-BE-4072c7c3890a6629ab6a96830481716898972028.zip
Getting average of time to complete tasks.
Not as good as expected. But I don't have time anymore. :'( It appears as if the milliseconds are not converted into seconds and minutes properly. Will have to see this in depth.
Diffstat (limited to 'src')
-rw-r--r--src/main/java/com/encora/Main.java25
-rw-r--r--src/main/java/com/encora/ToDosRepository.java33
2 files changed, 58 insertions, 0 deletions
diff --git a/src/main/java/com/encora/Main.java b/src/main/java/com/encora/Main.java
index ea3a2a4..368fa29 100644
--- a/src/main/java/com/encora/Main.java
+++ b/src/main/java/com/encora/Main.java
@@ -6,6 +6,8 @@ import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
import java.util.*;
@SpringBootApplication
@@ -233,4 +235,27 @@ public class Main {
final int pageSize = 10;
return (int) Math.ceil((double) toDosRepository.filteredToDos.size() / pageSize);
}
+
+
+ // Return the average time on completing to dos.
+ @CrossOrigin(origins=allowed_origin)
+ @GetMapping("/todos/average/{priority}")
+ @ResponseStatus(value=HttpStatus.OK)
+ public String getAverageTime(@PathVariable("priority") String priority) {
+ String result = "";
+ Double milliseconds = toDosRepository.getAverageCompletingTime(priority);
+
+ if (milliseconds > 86400000) {
+ // More than a day.
+ Integer days = (int) (milliseconds / 86400000);
+ result += String.valueOf(days) + " days, ";
+ }
+
+ // Convert the milliseconds into an hour, minute, second and
+ // millisecond format.
+ DateFormat simple = new SimpleDateFormat("HH:mm:ss:SSS");
+ simple.setTimeZone(TimeZone.getTimeZone("GMT"));
+
+ return result + simple.format(milliseconds);
+ }
}
diff --git a/src/main/java/com/encora/ToDosRepository.java b/src/main/java/com/encora/ToDosRepository.java
index c4bdcba..9333cd9 100644
--- a/src/main/java/com/encora/ToDosRepository.java
+++ b/src/main/java/com/encora/ToDosRepository.java
@@ -201,6 +201,39 @@ public class ToDosRepository implements JpaRepository<ToDos, Integer> {
return this.filteredToDos;
}
+ public Double getAverageCompletingTime(String priority) {
+ List<Double> recordedTimes = new ArrayList<>();
+
+ if (priority.equalsIgnoreCase("all")) {
+ for (ToDos todo : this.todos) {
+ if (!todo.isDone()) {
+ continue;
+ }
+ recordedTimes.add(
+ (double) (todo.getDoneDate().getTime() - todo.getCreationDate().getTime())
+ );
+ }
+ } else {
+ for (ToDos todo : this.todos) {
+ if (!todo.isDone() || !Objects.equals(String.valueOf(todo.getPriority()), priority)) {
+ continue;
+ }
+ recordedTimes.add(
+ (double) (todo.getDoneDate().getTime() - todo.getCreationDate().getTime())
+ );
+ }
+ }
+
+ if (recordedTimes.size() == 0) {
+ return (double) 0;
+ }
+ Double valuesSum = (double) 0;
+ for (Double d : recordedTimes) {
+ valuesSum += d;
+ }
+ return valuesSum / (double) recordedTimes.size();
+ }
+
/*
* N O T Y E T D E F I N E D .
*/