Skip to content

Test #456

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Test #456

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions .github/workflows/pr_review.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: PR Review Automation

on:
pull_request:
types: [opened, synchronize]

jobs:
review-pr:
runs-on: ubuntu-latest # Change to macos-latest if using Mac

steps:
- name: Checkout Code
uses: actions/checkout@v4

- name: Install Dependencies
run: |
sudo apt update
sudo apt install -y jq curl

- name: Install Ollama
run: |
curl -fsSL https://ollama.com/install.sh | bash
nohup ollama serve > /dev/null 2>&1 & # Run Ollama in background

- name: Fetch PR Changes
run: |
CHANGED_FILES=$(git diff --name-only origin/main...HEAD)
echo "Changed Files:"
echo "$CHANGED_FILES"

- name: Run PR Review with Ollama
run: |
REVIEW_TEXT=""
for FILE in $CHANGED_FILES; do
CODE=$(cat "$FILE")
REVIEW_TEXT+="Review for $FILE:\n"
REVIEW_TEXT+="$(echo "$CODE" | ollama run pr-reviewer)\n\n"
done
echo -e "$REVIEW_TEXT" > pr_review.txt

- name: Post PR Review Comments
uses: thollander/actions-comment-pull-request@v2
with:
message: "$(cat pr_review.txt)"
40 changes: 40 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
plugins {
java
id("org.springframework.boot") version "3.4.1"
id("io.spring.dependency-management") version "1.1.7"
}

group = "com.example"
version = "0.0.1-SNAPSHOT"

java {
toolchain {
languageVersion = JavaLanguageVersion.of(21)
}
}

configurations {
compileOnly {
extendsFrom(configurations.annotationProcessor.get())
}
}

repositories {
mavenCentral()
}

dependencies {
implementation("com.github.javaparser:javaparser-core:3.25.2")
implementation("org.apache.opennlp:opennlp-tools:2.3.2")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("org.postgresql:postgresql:42.6.0")
compileOnly("org.projectlombok:lombok")
annotationProcessor("org.projectlombok:lombok")
testImplementation("org.springframework.boot:spring-boot-starter-test")
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
}

tasks.withType<Test> {
useJUnitPlatform()
}
Binary file added gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
7 changes: 7 additions & 0 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.11.1-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rootProject.name = "rating"
20 changes: 20 additions & 0 deletions src/main/java/com/example/rating/Analyzer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.example.rating;

import com.github.javaparser.JavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.MethodDeclaration;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;

public class Analyzer {
public static void main(String[] args) throws Exception {
String code = new String(Files.readAllBytes(Paths.get("/Users/in45828930/Downloads/rating/src/main/java/com/example/rating/RatingService.java")));
CompilationUnit cu = new JavaParser().parse(code).getResult().orElseThrow();

cu.findAll(MethodDeclaration.class).forEach(method -> {
System.out.println("Method Name: " + method.getName());
});
}
}

31 changes: 31 additions & 0 deletions src/main/java/com/example/rating/OpenNlpExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.example.rating;

import opennlp.tools.sentdetect.SentenceDetectorME;
import opennlp.tools.sentdetect.SentenceModel;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

public class OpenNlpExample {
public static void main(String[] args) {
try (InputStream modelIn = new FileInputStream(new File("/Users/in45828930/Downloads/rating/src/main/java/com/example/rating/da-sent.bin"))) {
SentenceModel model = new SentenceModel(modelIn);
SentenceDetectorME sentenceDetector = new SentenceDetectorME(model);

String javaComment = " CompletableFuture<ResponseEntity<String>> responseFuture = CompletableFuture.supplyAsync(() -> {\n" +
" try {\n" +
" return restTemplate.getForEntity(FEED_URL, String.class);\n" +
" } catch (Exception e) {\n" +
" throw new RuntimeException(\"Failed to fetch data\", e);\n" +
" }\n" +
" });";
String[] sentences = sentenceDetector.sentDetect(javaComment);

for (String sentence : sentences) {
System.out.println(sentence);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
29 changes: 29 additions & 0 deletions src/main/java/com/example/rating/Rating.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.example.rating;

import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDate;
import java.util.UUID;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Entity
public class Rating {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private UUID recipeId;

private String urlFriendlyTitle;


private String averageRating;

private LocalDate currentDay;
}
13 changes: 13 additions & 0 deletions src/main/java/com/example/rating/RatingApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.example.rating;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class RatingApplication {

public static void main(String[] args) {
SpringApplication.run(RatingApplication.class, args);
}

}
23 changes: 23 additions & 0 deletions src/main/java/com/example/rating/RatingController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.example.rating;

import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.ExecutionException;

@RestController
@RequiredArgsConstructor
public class RatingController {

private final RatingService ratingService;


@PostMapping("/fetch")
public ResponseEntity<String> fetchAndSaveRatings() throws ExecutionException, InterruptedException, JsonProcessingException {
ratingService.fetchAndSaveRatings();
return ResponseEntity.ok("Ratings fetched and saved successfully!");
}
}
8 changes: 8 additions & 0 deletions src/main/java/com/example/rating/RatingRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.example.rating;

import org.springframework.data.jpa.repository.JpaRepository;

import java.util.UUID;

public interface RatingRepository extends JpaRepository<Rating, UUID> {
}
108 changes: 108 additions & 0 deletions src/main/java/com/example/rating/RatingService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package com.example.rating;

import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
import org.springframework.http.HttpStatus;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

import static java.lang.System.exit;

@Service
@RequiredArgsConstructor
public class RatingService {
private static final String FEED_URL = "https://realfood.tesco.com/api/feed/dataextract?authkey=fd2693ee-494e-40bd-8600-576d59293d46";

private final RatingRepository ratingRepository;

public void fetchAndSaveRatings() throws InterruptedException, ExecutionException, JsonProcessingException {
RestTemplate restTemplate = new RestTemplate();
CompletableFuture<ResponseEntity<String>> responseFuture = CompletableFuture.supplyAsync(() -> {
try {
return restTemplate.getForEntity(FEED_URL, String.class);
} catch (Exception e) {
throw new RuntimeException("Failed to fetch data", e);
}
});

// Wait for the response asynchronously
ResponseEntity<String> response = responseFuture.get();

if (response.getStatusCode() == HttpStatus.OK) {
final String responseBody = response.getBody();

// Parse JSON response using Jackson's streaming API for efficiency
final ObjectMapper objectMapper = new ObjectMapper();
final List<Rating> ratings = new ArrayList<>();
JsonNode rootNode = objectMapper.readTree(responseBody);
// Iterate over each field in the root node
rootNode.fields().forEachRemaining(entry -> {
String key = entry.getKey(); // Key of the current field (if needed)
JsonNode recipeNode = entry.getValue(); // Value (the actual object)


// If the entry itself is an array, iterate through its elements
if (recipeNode.isArray()) {
for (JsonNode recipe : recipeNode) {

// Check for the outputRecipe and its contents
if (recipe.has("outputRecipe")) {
JsonNode outputRecipe = recipe.get("outputRecipe");

// If outputRecipe is an array, process each element

// Extract urlFriendlyTitle and rating
String urlFriendlyTitle = outputRecipe.has("urlFriendlyTitle") ? outputRecipe.get("urlFriendlyTitle").asText() : null;
JsonNode ratingNode = outputRecipe.get("rating");

if (ratingNode != null && ratingNode.has("average")) {
Double averageRating = ratingNode.get("average").asDouble();

// Validate urlFriendlyTitle and averageRating before processing
if (urlFriendlyTitle != null && !urlFriendlyTitle.trim().isEmpty() && averageRating != null) {
Rating rating = new Rating().builder()
.urlFriendlyTitle(urlFriendlyTitle)
.currentDay(LocalDate.now())
.averageRating(String.valueOf(averageRating))
.build();

// Save the Rating entity to the repository
ratings.add(rating);
} else {
System.out.println("Skipping outputRecipe with invalid data: " + outputRecipe);
}
}
}

}
} else {
System.out.println("Entry is not an array: " + recipeNode);
}
});


// Save ratings in batches to avoid individual DB calls for each entry
batchSaveRatings(ratings);
} else {
throw new RuntimeException("Failed to fetch data: " + response.getStatusCode());
}
}

// Optimized batch save method
public void batchSaveRatings(List<Rating> ratings) {
if (!ratings.isEmpty()) {
// Assuming RatingRepository is set up for batch inserts
ratingRepository.saveAll(ratings); // Save all ratings at once (assuming it supports batch save)
}
}
}
23 changes: 23 additions & 0 deletions src/main/java/com/example/rating/Recipe.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.example.rating;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import lombok.Builder;
import lombok.Data;
import lombok.Setter;

import java.util.UUID;

@Data
@JsonIgnoreProperties
@Setter
@Builder
public class Recipe {

private String urlFriendlyTitle;

private Rating rating;
}
17 changes: 17 additions & 0 deletions src/main/java/com/example/rating/Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.example.rating;

public class Test {

public static void main(String[] args) {
pending(9);
}



private static int pending(int n)
{
return n <= 1 ? n : 2 * ( 1+n/2-pending(n/2));


}
}
Binary file added src/main/java/com/example/rating/da-sent.bin
Binary file not shown.
8 changes: 8 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.datasource.driver-class-name=org.postgresql.Driver

# Hibernate properties for JPA
spring.jpa.hibernate.ddl-auto=validate
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
Loading