Skip to content

Commit c02f88d

Browse files
committed
code cleaning
1 parent 3a3fd46 commit c02f88d

File tree

8 files changed

+47
-64
lines changed

8 files changed

+47
-64
lines changed

algorithms/ACO.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from lca.util import get_config, get_cost_config, sort_vms, get_solution, export_results
99

1010

11-
# Numpy-optimized ACO scheduler
11+
# ACO scheduler
1212
class Ant:
1313
def __init__(self, lengths, eff_mips, pheromone, alpha=1.0, beta=1.0, Q=100):
1414
self.lengths = lengths # array of task lengths, shape (N,)

algorithms/PSO.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
sys.path.append(parent_dir)
88
from lca.util import get_config, get_cost_config, sort_vms, get_solution, export_results
99

10-
# Numpy-optimized PSO scheduler adapted to generate time matrix from cloudlets and vms
10+
# PSO scheduler
1111
class PSO:
1212
def __init__(self, num_particles=30, max_iter=500, w=0.9, c1=2.0, c2=2.0, alpha=0.3):
1313
self.num_particles = num_particles

backend/routes/api.js

Lines changed: 2 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const fs = require('fs');
33
const path = require('path');
44
const router = express.Router();
55

6-
const { getConfigs, getRunConfigs, saveConfig, getAllResults, resetRunConfigs, saveAlgorithms, getAllAlgorithms } = require('../utils/fileHandlers');
6+
const { getConfigs, getRunConfigs, saveConfig, getAllResults, resetRunConfigs, saveAlgorithms, getAllAlgorithms, getFitnessData } = require('../utils/fileHandlers');
77
const { runPythonScript } = require('../utils/pythonRunner');
88
const { getResults } = require('../utils/resultProcessor');
99
const { runExperiments } = require('../utils/runExperiments');
@@ -50,14 +50,6 @@ router.post('/config', asyncHandler(async (req, res) => {
5050
});
5151
}));
5252

53-
// Run Python script
54-
router.post('/run-python', asyncHandler(async (req, res) => {
55-
const result = await runPythonScript(req.body);
56-
res.json({
57-
success: true,
58-
data: result
59-
});
60-
}));
6153

6254
// Get results
6355
router.get('/results', asyncHandler(async (req, res) => {
@@ -106,40 +98,7 @@ router.post('/run-algorithms', asyncHandler(async (req, res) => {
10698

10799
// Get fitness data for all algorithms from txt files in lca or algorithms folders
108100
router.get('/fitness/all', asyncHandler(async (req, res) => {
109-
const folders = ['lca', 'algorithms'];
110-
const data = {};
111-
112-
for (const folder of folders) {
113-
const folderPath = path.join(process.env.MAIN_DIR, folder);
114-
115-
// Check if folder exists
116-
if (!fs.existsSync(folderPath)) {
117-
continue;
118-
}
119-
120-
// Get all txt files in the folder
121-
const files = fs.readdirSync(folderPath).filter(file => file.endsWith('.txt'));
122-
123-
for (const file of files) {
124-
const algoName = path.basename(file, '.txt');
125-
const filePath = path.join(folderPath, file);
126-
127-
try {
128-
const fileContent = fs.readFileSync(filePath, 'utf8');
129-
data[algoName] = fileContent.trim().split('\n').map(line => {
130-
const match = line.match(/t\s*=(\d+),\s*f_best=(\d+\.?\d*)/);
131-
if (match) {
132-
const [, t, f] = match;
133-
return { t: Number(t), fitness: Number(f) };
134-
}
135-
return null;
136-
}).filter(Boolean);
137-
} catch (error) {
138-
console.error(`Error reading file ${filePath}:`, error);
139-
data[algoName] = [];
140-
}
141-
}
142-
}
101+
data = getFitnessData();
143102

144103
res.json({
145104
data

backend/utils/fileHandlers.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,44 @@ const saveAlgorithms = (algorithmsData) => {
109109

110110
}
111111

112+
const getFitnessData = () => {
113+
const folders = ['lca', 'algorithms'];
114+
const data = {};
115+
116+
for (const folder of folders) {
117+
const folderPath = path.join(process.env.MAIN_DIR, folder);
118+
119+
// Check if folder exists
120+
if (!fs.existsSync(folderPath)) {
121+
continue;
122+
}
123+
124+
// Get all txt files in the folder
125+
const files = fs.readdirSync(folderPath).filter(file => file.endsWith('.txt'));
126+
127+
for (const file of files) {
128+
const algoName = path.basename(file, '.txt');
129+
const filePath = path.join(folderPath, file);
130+
131+
try {
132+
const fileContent = fs.readFileSync(filePath, 'utf8');
133+
data[algoName] = fileContent.trim().split('\n').map(line => {
134+
const match = line.match(/t\s*=(\d+),\s*f_best=(\d+\.?\d*)/);
135+
if (match) {
136+
const [, t, f] = match;
137+
return { t: Number(t), fitness: Number(f) };
138+
}
139+
return null;
140+
}).filter(Boolean);
141+
} catch (error) {
142+
console.error(`Error reading file ${filePath}:`, error);
143+
data[algoName] = [];
144+
}
145+
}
146+
}
147+
return data;
148+
}
149+
112150
module.exports = {
113151
getConfigs,
114152
getRunConfigs,
@@ -120,4 +158,5 @@ module.exports = {
120158
resetRunConfigs,
121159
saveAlgorithms,
122160
getAllAlgorithms,
161+
getFitnessData,
123162
};

frontend/src/pages/EditConfigPage.jsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ export default function EditConfigPage() {
1111
setResetStatus(null);
1212

1313
try {
14-
await resetConfiguration(); // Call your API function
14+
await resetConfiguration();
1515
setResetStatus('success');
16-
// Optional: Add any post-reset logic here
1716
} catch (error) {
1817
console.error('Reset failed:', error);
1918
setResetStatus('error');

frontend/src/pages/FitnessPage.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
Legend,
1111
CategoryScale,
1212
} from "chart.js";
13-
import { getAllFitness } from "../utils/api"; // 👈 import your API helper
13+
import { getAllFitness } from "../utils/api";
1414
import Loading from "../components/loading"
1515
import LoadingError from "../components/loadingError"
1616
import { chartOptions, getAlgorithmColor, sortAlgorithm } from "../utils/fitnessPageUtil";
@@ -26,7 +26,7 @@ export default function FitnessPage() {
2626
useEffect(() => {
2727
const fetchFitnessData = async () => {
2828
try {
29-
let { data } = await getAllFitness(); // 👈 call API helper
29+
let { data } = await getAllFitness();
3030
data = sortAlgorithm(data);
3131
console.log("data loaded: ", data);
3232
const colors = {};

frontend/src/pages/RunPage.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { useEffect, useState } from "react";
2-
import { getRunConfigs, runExperiments, getAllAlgorithms, saveAlgorithms } from "../utils/api"; // adjust path if needed
2+
import { getRunConfigs, runExperiments, getAllAlgorithms, saveAlgorithms } from "../utils/api";
33
import SelectableAlgorithmCard from "../components/SelectableAlgorithmCard"
44

55
const RunPage = () => {

frontend/src/utils/api.js

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,6 @@ export async function getAllResults() {
6363
return await res.json();
6464
}
6565

66-
// Run Python script
67-
export async function runPython(args) {
68-
const res = await fetch(`${API_BASE_URL}/api/run-python`, {
69-
method: "POST",
70-
headers: { "Content-Type": "application/json" },
71-
body: JSON.stringify(args),
72-
});
73-
if (!res.ok) {
74-
const error = await res.json();
75-
throw new Error(error.output || "Failed to run Python script");
76-
}
77-
return await res.json();
78-
}
79-
8066
// Get all fitness data
8167
export async function getAllFitness() {
8268
const res = await fetch(`${API_BASE_URL}/api/fitness/all`);

0 commit comments

Comments
 (0)