Skip to content

Commit abd2074

Browse files
author
nicocapalbo
committed
feat: services refactor; useService + services repositories
1 parent 209896a commit abd2074

12 files changed

Lines changed: 153 additions & 189 deletions

File tree

components/ServiceCard.vue

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<script setup>
2-
import useService from "../composables/useService";
2+
import useService from "~/services/useService.js";
33
import {PROCESS_STATUS} from "~/constants/enums.js";
4+
const { processService } = useService()
45
56
const props = defineProps({
67
process: {type: Object}
@@ -12,26 +13,23 @@ const loading = ref(false) // Loading state
1213
1314
const updateStatus = async () => {
1415
try {
15-
const { fetchProcessStatus } = useService();
16-
status.value = await fetchProcessStatus(props.process.process_name);
16+
status.value = await processService.fetchProcessStatus(props.process.process_name);
1717
} catch (e) {
1818
console.error("Failed to get process status:", e);
1919
}
2020
}
2121
const executeAction = async () => {
2222
if (!selectedAction.value) return;
23-
24-
const { startProcess, stopProcess } = useService();
2523
loading.value = true; // Start loading spinner
2624
2725
try {
2826
if (selectedAction.value === "start") {
29-
await startProcess(props.process.process_name);
27+
await processService.startProcess(props.process.process_name);
3028
} else if (selectedAction.value === "stop") {
31-
await stopProcess(props.process.process_name);
29+
await processService.stopProcess(props.process.process_name);
3230
} else if (selectedAction.value === "restart") {
33-
await stopProcess(props.process.process_name);
34-
await startProcess(props.process.process_name);
31+
await processService.stopProcess(props.process.process_name);
32+
await processService.startProcess(props.process.process_name);
3533
}
3634
await updateStatus();
3735
} catch (err) {

components/ServicePoller.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<script setup>
2-
import useService from "../composables/useService.js";
2+
import useService from "~/services/useService.js";
33
import {computed, onMounted, ref} from "vue";
44
5-
const {statusService} = useService()
5+
const {processService} = useService()
66
const data = ref(null)
77
const loading = ref(false)
88
@@ -29,7 +29,7 @@ const statusStyle = computed(() => {
2929
const refreshStatus = async () => {
3030
loading.value = true
3131
try {
32-
const response = await statusService.getHealthCheck()
32+
const response = await processService.getHealthCheck()
3333
data.value = await response.json()
3434
loading.value = false
3535
} catch (e) {

composables/serviceActions.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
import useService from "@/composables/useService";
1+
import useService from "~/services/useService.js"
22

33
export async function performServiceAction(processName, action, successCallback) {
4-
const { startProcess, stopProcess, restartProcess, fetchProcessStatus } = useService();
4+
const { processService } = useService()
55

66
try {
7-
if (action === "start") await startProcess(processName);
8-
if (action === "stop") await stopProcess(processName);
9-
if (action === "restart") await restartProcess(processName);
7+
if (action === "start") await processService.startProcess(processName)
8+
if (action === "stop") await processService.stopProcess(processName)
9+
if (action === "restart") await processService.restartProcess(processName)
1010

11-
const updatedStatus = await fetchProcessStatus(processName);
12-
successCallback(updatedStatus);
11+
const updatedStatus = await processService.fetchProcessStatus(processName)
12+
successCallback(updatedStatus)
1313
} catch (error) {
14-
console.error(`Failed to execute ${action} for process '${processName}':`, error);
15-
throw new Error("Failed to execute action.");
14+
console.error(`Failed to execute ${action} for process '${processName}':`, error)
15+
throw new Error("Failed to execute action.")
1616
}
1717
}

composables/useService.js

Lines changed: 0 additions & 107 deletions
This file was deleted.

pages/rtl/index.vue

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<script setup>
22
import {useEventBus} from "@vueuse/core";
33
import SelectComponent from "~/pages/rtl/SelectComponent.vue";
4+
import useService from "~/services/useService.js";
45
5-
const { fetchProcesses } = useService();
6+
const { processService } = useService()
67
78
const logs = ref([]);
89
const filterText = ref("");
@@ -52,7 +53,7 @@ const downloadLogs = () => {
5253
5354
const fetchServices = async () => {
5455
try {
55-
SERVICES.value = await fetchProcesses();
56+
SERVICES.value = await processService.fetchProcesses();
5657
} catch (error) {
5758
console.error("Failed to fetch services:", error);
5859
}

pages/services/[serviceId]/index.vue

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
class="px-4 py-2 rounded hover:bg-gray-600"
2525
>
2626
Edit Service Config
27-
</button>
27+
</button>
2828
<button
2929
v-if="hasServiceLogs"
3030
@click="() => { fetchLogs(); showLogsView = true; }"
@@ -42,7 +42,7 @@
4242
<!-- Logs View with Filters -->
4343
<div v-if="showLogsView" class="log-container">
4444
<div class="log-header flex justify-between items-center w-full">
45-
<h3>Logs for {{ currentService.process_name }}</h3>
45+
<h3>Logs for {{ currentService.process_name }}</h3>
4646
<!-- Filters and Actions -->
4747
<div class="flex gap-2">
4848
<input
@@ -162,7 +162,7 @@
162162

163163

164164
<script>
165-
import useService from "@/composables/useService";
165+
import useService from "~/services/useService.js";
166166
import { performServiceAction } from "@/composables/serviceActions";
167167
168168
export default {
@@ -185,8 +185,8 @@ export default {
185185
selectedLog: "",
186186
showLogsView: false,
187187
logFilterText: "",
188-
logLevelFilter: "",
189-
logLimit: 1000,
188+
logLevelFilter: "",
189+
logLimit: 1000,
190190
};
191191
},
192192
async mounted() {
@@ -228,11 +228,11 @@ export default {
228228
this.loading = true;
229229
this.hasServiceConfig = false;
230230
this.hasServiceLogs = false;
231-
this.selectedLog = "";
231+
this.selectedLog = "";
232232
try {
233-
const { fetchProcesses, fetchProcessStatus } = useService();
233+
const { processService } = useService()
234234
const serviceId = this.$route.params.serviceId;
235-
const services = await fetchProcesses();
235+
const services = await processService.fetchProcesses();
236236
this.currentService =
237237
services.find((service) => service.process_name === serviceId) || {};
238238
const serviceWithConfig = this.currentService.config && this.currentService.config.config_file;
@@ -242,8 +242,8 @@ export default {
242242
this.editableConfig = this.currentService.config_raw || JSON.stringify(this.currentService.config, null, 2);
243243
this.configFormat = "json";
244244
const logsExist = await this.checkLogsAvailability(this.currentService.process_name);
245-
this.hasServiceLogs = logsExist;
246-
this.serviceStatus = await fetchProcessStatus(this.currentService.process_name);
245+
this.hasServiceLogs = logsExist;
246+
this.serviceStatus = await processService.fetchProcessStatus(this.currentService.process_name);
247247
this.updateLineCount();
248248
} catch (error) {
249249
console.error("Failed to load DMB config:", error);
@@ -259,9 +259,9 @@ export default {
259259
this.errorMessage = "";
260260
this.loading = true;
261261
try {
262-
const { fetchServiceConfig } = useService();
262+
const { configService } = useService()
263263
const serviceId = this.$route.params.serviceId;
264-
const serviceConfig = await fetchServiceConfig(serviceId);
264+
const serviceConfig = await configService.fetchServiceConfig(serviceId);
265265
const configMapping = {
266266
yaml: () => serviceConfig.raw,
267267
json: () => JSON.stringify(serviceConfig.config, null, 2),
@@ -287,8 +287,8 @@ export default {
287287
},
288288
async checkLogsAvailability(serviceName) {
289289
try {
290-
const { fetchServiceLogs } = useService();
291-
const logs = await fetchServiceLogs(serviceName);
290+
const { logsService } = useService()
291+
const logs = await logsService.fetchServiceLogs(serviceName);
292292
if (!logs || logs.trim() === "" || logs.includes("No logs")){
293293
return false;
294294
}
@@ -302,8 +302,8 @@ export default {
302302
if (!this.currentService.process_name) return;
303303
304304
try {
305-
const { fetchServiceLogs } = useService();
306-
this.selectedLog = await fetchServiceLogs(this.currentService.process_name);
305+
const { logsService } = useService()
306+
this.selectedLog = await logsService.fetchServiceLogs(this.currentService.process_name);
307307
this.showLogsView = true;
308308
309309
this.$nextTick(() => {
@@ -323,20 +323,20 @@ export default {
323323
link.href = URL.createObjectURL(blob);
324324
link.download = `logs_${this.currentService.process_name}.log`;
325325
link.click();
326-
},
326+
},
327327
async updateConfig(persist) {
328328
this.isProcessing = true;
329329
this.persisting = persist;
330330
this.successMessage = "";
331331
this.errorMessage = "";
332332
try {
333-
const { updateDMBConfig, updateServiceConfig } = useService();
333+
const { configService } = useService()
334334
if (this.isDMBConfig) {
335335
const configToSend =
336336
this.configFormat === "json" ? JSON.parse(this.editableConfig) : this.editableConfig;
337-
await updateDMBConfig(this.currentService.process_name, configToSend, persist);
337+
await configService.updateDMBConfig(this.currentService.process_name, configToSend, persist);
338338
} else {
339-
await updateServiceConfig(
339+
await configService.updateServiceConfig(
340340
this.currentService.process_name,
341341
this.editableConfig,
342342
this.configFormat

services/config.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import axios from "axios";
2+
import yaml from "js-yaml";
3+
4+
export const configRepository =() => ({
5+
async fetchServiceConfig(serviceName){
6+
try {
7+
const response = await axios.post(`/api/config/service-config`, {
8+
service_name: serviceName,
9+
});
10+
return response.data;
11+
} catch (error) {
12+
throw error.response || error;
13+
}
14+
},
15+
async updateServiceConfig(serviceName, updates, configFormat){
16+
try {
17+
const requestData = {
18+
service_name: serviceName,
19+
};
20+
if (configFormat === "rclone" || configFormat === "python" || configFormat === "postgresql") {
21+
requestData.updates = updates;
22+
} else if (configFormat === "yaml") {
23+
requestData.updates = yaml.load(updates);
24+
} else if (configFormat === "json") {
25+
requestData.updates = JSON.parse(updates);
26+
} else {
27+
requestData.updates = updates;
28+
}
29+
const response = await axios.post(`/api/config/service-config`, requestData);
30+
return response.data;
31+
} catch (error) {
32+
throw error.response || error;
33+
}
34+
},
35+
async updateDMBConfig(processName, updates, persist = false){
36+
try {
37+
const response = await axios.post(`/api/config/update-dmb-config`, {
38+
process_name: processName,
39+
updates,
40+
persist,
41+
});
42+
return response.data;
43+
} catch (error) {
44+
throw error.response || error;
45+
}
46+
},
47+
})

0 commit comments

Comments
 (0)