-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
27 changed files
with
5,492 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/main/java/com/nimshub/biobeacon/activities/ActivityController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.nimshub.biobeacon.activities; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.util.UUID; | ||
|
||
/** | ||
* @Author Nirmala : 11:October:2023 | ||
* This controller implements all end points required to handle the requests of Activity | ||
*/ | ||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/api/v1/activities") | ||
public class ActivityController { | ||
private final ActivityService activityService; | ||
@GetMapping("/activity/{id}") | ||
public ResponseEntity<ActivityTimeResponse> getActivityTimes(@PathVariable UUID id){ | ||
return new ResponseEntity<>(activityService.getActivityTime(id), HttpStatus.OK); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/main/java/com/nimshub/biobeacon/activities/ActivityService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.nimshub.biobeacon.activities; | ||
|
||
import com.nimshub.biobeacon.exceptions.SessionNotFoundException; | ||
import com.nimshub.biobeacon.session.Session; | ||
import com.nimshub.biobeacon.session.SessionRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.UUID; | ||
|
||
/** | ||
* @Author Nirmala : 11:October:2023 | ||
* This service implements all methods required to handle the business logic of Activity | ||
*/ | ||
@Service | ||
@RequiredArgsConstructor | ||
public class ActivityService { | ||
private final ActivityTimeRepository activityTimeRepository; | ||
private final SessionRepository sessionRepository; | ||
|
||
public ActivityTimeResponse getActivityTime(UUID id) { | ||
Session session = sessionRepository.findBySessionId(id) | ||
.orElseThrow(() -> new SessionNotFoundException("Session Not Found")); | ||
|
||
ActivityTime activityTime = activityTimeRepository.findBySession(session) | ||
.orElseThrow(() -> new SessionNotFoundException("Session Not Found")); | ||
|
||
|
||
return ActivityTimeResponse.builder() | ||
.cycling(activityTime.getCycling()) | ||
.pushUp(activityTime.getPushUp()) | ||
.squat(activityTime.getSquat()) | ||
.tableTennis(activityTime.getTableTennis()) | ||
.running(activityTime.getRunning()) | ||
.walking(activityTime.getWalking()) | ||
.build(); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/com/nimshub/biobeacon/activities/ActivityTime.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.nimshub.biobeacon.activities; | ||
|
||
import com.nimshub.biobeacon.session.Session; | ||
import jakarta.persistence.*; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
/** | ||
* @Author Nirmala : 11:October:2023 | ||
* This entity class defines the all the properties of ActivityTime | ||
*/ | ||
@Data | ||
@Builder | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Entity | ||
public class ActivityTime { | ||
@Id | ||
@SequenceGenerator(name = "ActivityTime_SEQ", sequenceName = "ActivityTime_SEQ", allocationSize = 1) | ||
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ActivityTime_SEQ") | ||
private Integer id; | ||
@OneToOne(cascade = CascadeType.DETACH,fetch = FetchType.EAGER) | ||
@JoinColumn(name = "session", referencedColumnName = "id", foreignKey = | ||
@ForeignKey(name = "fk_session_id") | ||
) | ||
private Session session; | ||
private Integer cycling; | ||
private Integer pushUp; | ||
private Integer running; | ||
private Integer squat; | ||
private Integer tableTennis; | ||
private Integer walking; | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/com/nimshub/biobeacon/activities/ActivityTimeRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.nimshub.biobeacon.activities; | ||
|
||
import com.nimshub.biobeacon.activities.ActivityTime; | ||
import com.nimshub.biobeacon.session.Session; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.Optional; | ||
|
||
/** | ||
* @Author Nirmala : 11:October:2023 | ||
* This interface implements all methods required database transactions for ActivityTime using Jpa | ||
*/ | ||
|
||
@Repository | ||
public interface ActivityTimeRepository extends JpaRepository<ActivityTime,Integer> { | ||
Optional<ActivityTime> findBySession(Session session); | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/nimshub/biobeacon/activities/ActivityTimeResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.nimshub.biobeacon.activities; | ||
|
||
import lombok.Builder; | ||
import lombok.Data; | ||
|
||
@Data | ||
@Builder | ||
public class ActivityTimeResponse { | ||
private Integer cycling; | ||
private Integer pushUp; | ||
private Integer running; | ||
private Integer squat; | ||
private Integer tableTennis; | ||
private Integer walking; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,14 @@ | ||
package com.nimshub.biobeacon.constants; | ||
|
||
public class Constants { | ||
public class Constants { | ||
// creating a private constructor to hide the public implicit one | ||
private Constants() { | ||
throw new IllegalStateException("Constants class"); | ||
} | ||
public static final String NO_DATA = "*"; | ||
public static final String BASH = "bash"; | ||
public static final String SCRIPT = "source env/bin/activate && python3 "; | ||
public static final String COMMAND = "-c"; | ||
public static final String COMMA = ","; | ||
public static final Integer CHUNK_SIZE = 4; | ||
} |
114 changes: 114 additions & 0 deletions
114
src/main/java/com/nimshub/biobeacon/ml/ModelService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package com.nimshub.biobeacon.ml; | ||
|
||
import com.nimshub.biobeacon.activities.ActivityTime; | ||
import com.nimshub.biobeacon.activities.ActivityTimeRepository; | ||
import com.nimshub.biobeacon.exceptions.SessionNotFoundException; | ||
import com.nimshub.biobeacon.session.Session; | ||
import com.nimshub.biobeacon.session.SessionRepository; | ||
import com.nimshub.biobeacon.utils.Converters; | ||
import lombok.RequiredArgsConstructor; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import static com.nimshub.biobeacon.constants.Constants.*; | ||
|
||
|
||
/** | ||
* @Author Nirmala : 30:August:2023 | ||
* This service implements all methods required to handle the business logic of ModelService | ||
*/ | ||
@Service | ||
@RequiredArgsConstructor | ||
public class ModelService { | ||
private final ActivityTimeRepository activityTimeRepository; | ||
private final SessionRepository sessionRepository; | ||
Logger logger = LoggerFactory.getLogger(ModelService.class); | ||
|
||
@Value("${ml.process-directory}") | ||
String processDirectory; | ||
|
||
public String doAnalyze() { | ||
|
||
String outputCsv = ""; | ||
try { | ||
String scriptName = "scripts/model5.py"; | ||
|
||
String[] command = {BASH, COMMAND, SCRIPT + scriptName}; | ||
|
||
ProcessBuilder processBuilder = new ProcessBuilder(command); | ||
|
||
processBuilder.directory(new File(processDirectory)); | ||
|
||
Process process = processBuilder.start(); | ||
|
||
logger.info("Executing command : {}", Arrays.toString(command)); | ||
|
||
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); | ||
String line; | ||
List<String> outputLines = new ArrayList<>(); | ||
while ((line = reader.readLine()) != null) { | ||
outputLines.add(line); | ||
} | ||
String output = outputLines.subList(3, outputLines.size()).toString() | ||
.replace(", ", "") | ||
.replace("[", "") | ||
.replace("]", ""); | ||
|
||
outputCsv = output.replace(" ", ","); | ||
int exitCode = process.waitFor(); | ||
|
||
if (exitCode == 0) { | ||
logger.info("Script executed successfully. Predicted output : {}", output); | ||
} else { | ||
logger.error("Script execution failed with exit code : {}", output); | ||
} | ||
|
||
} catch (IOException | InterruptedException e) { | ||
e.printStackTrace(); | ||
|
||
} | ||
return outputCsv; | ||
} | ||
|
||
public void predictActivities(Integer id) { | ||
Converters converter = new Converters(); | ||
String output = doAnalyze(); | ||
|
||
List<String> predictionList = converter.getStringList(output); | ||
|
||
int cyclingTime = Collections.frequency(predictionList, "0")*10; | ||
int pushUpTime = Collections.frequency(predictionList, "1")*10; | ||
int runTime = Collections.frequency(predictionList, "2")*10; | ||
int squatTime = Collections.frequency(predictionList, "3")*10; | ||
int tableTennisTime = Collections.frequency(predictionList, "4")*10; | ||
int walkTime = Collections.frequency(predictionList, "5")*10; | ||
|
||
Session session = sessionRepository.findById(id) | ||
.orElseThrow(()-> new SessionNotFoundException("Session not found")); | ||
|
||
ActivityTime activityTime = ActivityTime.builder() | ||
.session(session) | ||
.cycling(cyclingTime) | ||
.pushUp(pushUpTime) | ||
.running(runTime) | ||
.squat(squatTime) | ||
.tableTennis(tableTennisTime) | ||
.walking(walkTime) | ||
.build(); | ||
|
||
activityTimeRepository.save(activityTime); | ||
} | ||
|
||
|
||
} |
Oops, something went wrong.