Skip to content

Commit

Permalink
Clean codebase up and add toast messages to Vue client (ndg8743#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
iron768 authored May 10, 2024
1 parent 9e2af66 commit f077e19
Show file tree
Hide file tree
Showing 15 changed files with 66 additions and 107 deletions.
11 changes: 10 additions & 1 deletion fibvue/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion fibvue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
},
"dependencies": {
"axios": "^1.6.8",
"vue": "^3.4.21"
"vue": "^3.4.21",
"vue-toastification": "^2.0.0-rc.5"
},
"devDependencies": {
"@vitejs/plugin-vue": "^5.0.4",
Expand Down
6 changes: 6 additions & 0 deletions fibvue/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@

<script>
import axios from 'axios';
import { useToast } from 'vue-toastification';
const toast = useToast();
export default {
data() {
Expand Down Expand Up @@ -86,12 +89,15 @@ export default {
this.jobStatus = payload.data.status;
if (this.jobStatus === 'COMPLETED') {
this.fetchJobResults();
toast.success('Fibonacci job completed!');
} if (this.jobStatus === 'PENDING' || this.jobStatus === 'RUNNING') {
this.jobResult = payload.data;
setTimeout(this.pollJobStatus, 1000); // Poll every second
}
} catch (error) {
console.error('Error polling job status:', error);
toast.error("Error polling job status: " + error);
}
},
async fetchJobResults() {
Expand Down
9 changes: 8 additions & 1 deletion fibvue/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { createApp } from 'vue'
import './style.css'
import App from './App.vue'
import toast from 'vue-toastification'
import "vue-toastification/dist/index.css";

createApp(App).mount('#app')
const app = createApp(App)

app
.use(toast, {})

app.mount('#app')
12 changes: 9 additions & 3 deletions src/main/java/org/cliclient/CliClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.fibsters.interfaces.Result;
import org.fibsters.ComputeInputServiceGrpc;


import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
Expand All @@ -35,7 +34,6 @@
import java.util.List;
import java.util.Scanner;


public class CliClient {

private static final Gson gson_noBuff = new GsonBuilder()
Expand Down Expand Up @@ -65,7 +63,9 @@ public static void main(String[] args) {
System.out.println("Starting the compute job client...");

System.out.println("Set Network Request Type (POST/GRPC), POST by default");

String response = scanner.nextLine();

if ("GRPC".equalsIgnoreCase(response)) {
CliClient.networkRequestType = "GRPC";
}
Expand All @@ -88,8 +88,11 @@ public static void main(String[] args) {
if (jobId != null) {
doJob(jobId);
}

System.out.println("Do you want to start another job? (yes/no)");

response = scanner.nextLine();

if (!"yes".equalsIgnoreCase(response)) {
break;
}
Expand Down Expand Up @@ -141,6 +144,7 @@ public static void main(String[] args) {

public static void doJob(String jobId) {
boolean completed = false;

while (!completed) {
System.out.println("Checking job status for ID: " + jobId);

Expand All @@ -163,9 +167,11 @@ public static void doJob(String jobId) {
case COMPLETED:
completed = true;
System.out.println("Job completed. Fetching results...");
//String resultJson = "{ \"uniqueID\": \"" + jobId + "\", \"directive\": \"GET_JOB_BY_ID\" }";

Result<OutputPayloadImpl> result = getJobById(jobId);

handleCompletedResults(result);

System.out.println("Job result: " + result.getData().getStatus().name());
break;
default:
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/fibsters/BufferedImageTypeAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ public BufferedImage read(JsonReader jsonReader) throws IOException {
jsonReader.nextNull();
return null;
}

byte[] imageBytes = Base64.getDecoder().decode(jsonReader.nextString());

return ImageIO.read(new ByteArrayInputStream(imageBytes));
}

Expand Down
20 changes: 1 addition & 19 deletions src/main/java/org/fibsters/ComputeJobPoolImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,30 +148,12 @@ private void processJob(ComputeJob job) {
jobClone.setStartIndex(start);
jobClone.setEndIndex(end);
jobClone.setChunk(i);
if (jobClone instanceof FibSpiralComputeEngineImpl) {
System.out.println("Job Pool: Chunk" + i + " fib" + jobClone.getTotalSize(i));
}


futureFibTasks[threadCount++] = executor.submit(jobClone);
}

}
System.out.println("Thread count: " + threadCount);
/*int threadGroupSize = Math.round(job.getTotalSize() / numThreads); // 90 fib numbers / 4 threads = 22.5
for (int i = numThreads; i >= 0; i--) {
int start = i * threadGroupSize;
int end = (i + 1) * threadGroupSize;
if (i == numThreads - 1) { // threadgroup didnt divide evenly so pick up the remaining elements
end = job.getTotalSize();
}
// important to set the start and end indices for the job so that it knows what to calculate
job.setStartIndex(start);
job.setEndIndex(end);
futureFibTasks[i] = executor.submit(job);
}*/
//executor.execute(job);
//System.out.println("Thread count: " + threadCount);
}

private int[] handleThreadPooling(int[] jobs, int numThreads) {
Expand Down
6 changes: 0 additions & 6 deletions src/main/java/org/fibsters/CoordinatorComputeEngineImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,10 @@

import org.fibsters.interfaces.*;

import java.util.Queue;

public class CoordinatorComputeEngineImpl implements CoordinatorComputeEngine {

private final DataStorageImpl dataStorage;
private final ComputeJobPoolImpl jobPool;
// TODO: still needed?
private Queue<InputPayload> inputPayloadQueue; // allowing for async input payloads for when jobPool is busy.

private Queue<OutputPayload> outputPayloadQueue; // allowing for async output payloads for when jobPool is busy.

public CoordinatorComputeEngineImpl(DataStorageImpl dataStorage) {
this.dataStorage = dataStorage;
Expand Down
42 changes: 14 additions & 28 deletions src/main/java/org/fibsters/FibCalcComputeEngineImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class FibCalcComputeEngineImpl implements FibCalcComputeEngine {

Expand All @@ -16,37 +15,19 @@ public class FibCalcComputeEngineImpl implements FibCalcComputeEngine {
private InputPayload inputPayload; // used for reference potentially(has uuid)
private int chunk;
private OutputPayloadImpl outputPayload;
private static Map<Integer, Integer> memoizationMap; // cache

public FibCalcComputeEngineImpl(OutputPayloadImpl outputPayload) {
this.status = ComputeJobStatus.UNSTARTED;
this.startIndex = 0;
this.endIndex = 10;
this.outputPayload = outputPayload;

memoizationMap = new ConcurrentHashMap<>();
}

@Override
public FibCalcComputeEngineImpl clone() {
FibCalcComputeEngineImpl clone = new FibCalcComputeEngineImpl(this.outputPayload);

clone.setStatus(this.status);
clone.setStartIndex(this.startIndex);
clone.setEndIndex(this.endIndex);
clone.setInputPayload(this.inputPayload);
clone.setOutputPayload(this.outputPayload);
clone.setChunk(this.chunk);

return clone;
}

@Override
public void setChunk(int chunk) {
this.chunk = chunk;
}

// set/get chunk
@Override
public int getChunk() {
return chunk;
Expand Down Expand Up @@ -101,9 +82,6 @@ public InputPayload getInputPayload() {

@Override
public void run() {
// TODO: report failure if that's possible
// TODO: Do proper chunking and offsetting, need to hold off on implementation for now

status = ComputeJobStatus.PENDING;

int total = endIndex - startIndex;
Expand Down Expand Up @@ -135,12 +113,6 @@ public void run() {
for (int i = 0; i < total; i++) {
fibonacci[i] = memoizationMap.get(i);
}
/* fibonacci[1] = calculateNthFibonacci(startIndex + 1);
for (int i = startIndex + 2; i < endIndex; i++) {
fibonacci[i - startIndex] = fibonacci[i - 1 - startIndex] + fibonacci[i - 2 - startIndex];
//System.err.println(fibonacci[i-startIndex]);
}*/
}

outputPayload.setFibCalcResults(this.chunk, fibonacci, startIndex, endIndex);
Expand All @@ -159,4 +131,18 @@ public void setOutputPayload(OutputPayloadImpl outputPayload) {
this.outputPayload = outputPayload;
}

@Override
public FibCalcComputeEngineImpl clone() {
FibCalcComputeEngineImpl clone = new FibCalcComputeEngineImpl(this.outputPayload);

clone.setStatus(this.status);
clone.setStartIndex(this.startIndex);
clone.setEndIndex(this.endIndex);
clone.setInputPayload(this.inputPayload);
clone.setOutputPayload(this.outputPayload);
clone.setChunk(this.chunk);

return clone;
}

}
32 changes: 3 additions & 29 deletions src/main/java/org/fibsters/FibHttpHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@ public FibHttpHandler(CoordinatorComputeEngineImpl api) {
public void handle(HttpExchange httpExchange) throws IOException {
Headers headers = httpExchange.getResponseHeaders();

// CORS
headers.set("Access-Control-Allow-Origin", "*"); // Allow all domains
headers.set("Access-Control-Allow-Methods", "GET, POST, OPTIONS"); // Allowed methods
headers.set("Access-Control-Allow-Headers", "Content-Type,Authorization"); // Allowed headers

String requestMethod = httpExchange.getRequestMethod();

// Check if the request method is GET
switch (requestMethod) {
case "GET":
Expand All @@ -42,9 +45,6 @@ public void handle(HttpExchange httpExchange) throws IOException {

private void handleGet(HttpExchange httpExchange) throws IOException {
String response = "get: hello world from fib handler";
//String exampleJson = "{'calcFibNumbersUpTo': [1, 10, 25]}";

//computeAPI.parseInputPayload(exampleJson);

sendResponse(httpExchange, response);
}
Expand All @@ -71,34 +71,8 @@ private void handlePost(HttpExchange httpExchange) throws IOException {

String inputString = new String(inputStream.readAllBytes());

System.out.println(inputString);

String response = processInputStringForOutput(inputString);

System.out.println("response" + response);

/*
Result<InputPayload> result = computeAPI.parseInputPayload(inputString);
// TODO: handle the result
ComputeJob job = computeAPI.createComputeJobFromInputPayload(result.getData());
computeAPI.queueJob(job);
OutputPayload output = job.getOutputPayload();
Gson gson = new GsonBuilder()
.registerTypeAdapter(BufferedImage.class, new BufferedImageTypeAdapter())
.create();
String response = "";
try {
response = gson.toJson(output);
} catch (Exception e) {
e.printStackTrace();
}
*/

sendResponse(httpExchange, response);
}

Expand Down
1 change: 0 additions & 1 deletion src/main/java/org/fibsters/FibHttpHandlerAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public FibHttpHandlerAPI(Integer givenPort) {
port = givenPort;
}

// start
public void start() throws IOException {
this.api = new CoordinatorComputeEngineImpl(new DataStorageImpl());

Expand Down
Loading

0 comments on commit f077e19

Please sign in to comment.