Skip to content
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

feature/CONNECTOR-151/synchro-run #148

Merged
merged 2 commits into from
Jan 6, 2025
Merged
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
1 change: 1 addition & 0 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
['name' => 'synchronizations#contracts', 'url' => '/api/synchronizations-contracts/{id}', 'verb' => 'GET'],
['name' => 'synchronizations#logs', 'url' => '/api/synchronizations-logs/{id}', 'verb' => 'GET'],
['name' => 'synchronizations#test', 'url' => '/api/synchronizations-test/{id}', 'verb' => 'POST'],
['name' => 'synchronizations#run', 'url' => '/api/synchronizations-run/{id}', 'verb' => 'POST'],
// Mapping endpoints
['name' => 'mappings#test', 'url' => '/api/mappings/test', 'verb' => 'POST'],
['name' => 'mappings#saveObject', 'url' => '/api/mappings/objects', 'verb' => 'POST'],
Expand Down
47 changes: 47 additions & 0 deletions lib/Controller/SynchronizationsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,4 +278,51 @@ public function test(int $id): JSONResponse
);
}
}

/**
* Run a synchronization
*
* @NoAdminRequired
* @NoCSRFRequired
*
* Endpoint: /api/synchronizations-run/{id}
*
* @param int $id The ID of the synchronization to test
* @return JSONResponse A JSON response containing the run results
* @throws GuzzleException
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function run(int $id): JSONResponse
{
try {
$synchronization = $this->synchronizationMapper->find(id: $id);
} catch (DoesNotExistException $exception) {
return new JSONResponse(data: ['error' => 'Not Found'], statusCode: 404);
}

// Try to synchronize
try {
$logAndContractArray = $this->synchronizationService->synchronize(
synchronization: $synchronization,
isTest: false
);

// Return the result as a JSON response
return new JSONResponse(data: $logAndContractArray, statusCode: 200);
} catch (Exception $e) {
// Check if getHeaders method exists and use it if available
$headers = method_exists($e, 'getHeaders') ? $e->getHeaders() : [];

// If synchronization fails, return an error response
return new JSONResponse(
data: [
'error' => 'Synchronization error',
'message' => $e->getMessage()
],
statusCode: $e->getCode() ?? 400,
headers: $headers
);
}
}
}
3 changes: 3 additions & 0 deletions src/modals/Modals.vue
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { navigationStore } from '../store/store.js'
<DeleteSynchronization />
<EditSynchronization v-if="navigationStore.modal === 'editSynchronization'" />
<TestSynchronization v-if="navigationStore.modal === 'testSynchronization'" />
<RunSynchronization v-if="navigationStore.modal === 'runSynchronization'" />
<EditJobArgument />
<DeleteJobArgument />
<EditSourceConfiguration />
Expand Down Expand Up @@ -64,6 +65,7 @@ import TestMapping from './MappingTest/TestMapping.vue'
import EditSynchronization from './Synchronization/EditSynchronization.vue'
import DeleteSynchronization from './Synchronization/DeleteSynchronization.vue'
import TestSynchronization from './Synchronization/TestSynchronization.vue'
import RunSynchronization from './Synchronization/RunSynchronization.vue'
import EditJobArgument from './JobArgument/EditJobArgument.vue'
import DeleteJobArgument from './JobArgument/DeleteJobArgument.vue'
import EditSourceConfiguration from './SourceConfiguration/EditSourceConfiguration.vue'
Expand Down Expand Up @@ -106,6 +108,7 @@ export default {
DeleteSynchronization,
EditSynchronization,
TestSynchronization,
RunSynchronization,
EditJobArgument,
DeleteJobArgument,
EditSourceConfiguration,
Expand Down
245 changes: 245 additions & 0 deletions src/modals/Synchronization/RunSynchronization.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,245 @@
<script setup>
import { synchronizationStore, navigationStore } from '../../store/store.js'
import { getTheme } from '../../services/getTheme.js'
</script>

<template>
<NcModal ref="modalRef"
label-id="runSynchronization"
@close="closeModal">
<div class="modalContent runSynchronization">
<h2>Run synchronization</h2>

<div v-if="response === null" class="runButtonContainer">
<NcButton type="primary" @click="runSynchronization">
<template #icon>
<Play :size="20" />
</template>
Run
</NcButton>
</div>

<div v-if="loading">
<NcLoadingIcon :size="64" name="Running synchronization" />
</div>

<NcNoteCard v-if="success === false" type="error">
<p>An error occurred while running the synchronization.</p>
</NcNoteCard>

<div v-if="success !== null">
<div v-if="success" class="SuccessMarker">
Success Marker to expand the modal
</div>

<NcNoteCard v-if="response?.ok" type="success">
<p>The synchronization was run successfully.</p>
</NcNoteCard>
<NcNoteCard v-if="!response?.ok || error" type="error">
<p>
An error occurred while running the synchronization: {{
synchronizationStore.synchronizationRun
? synchronizationStore.synchronizationRun.message
? synchronizationStore.synchronizationRun.message
: synchronizationStore.synchronizationRun.error
: response?.statusMessage
? response?.statusMessage
: `${response?.status} - ${response?.statusText}`
}}
</p>
</NcNoteCard>

<div v-if="response" class="detailTable">
<table>
<tr>
<td><b>Status:</b></td>
<td>{{ response?.statusText }} ({{ response?.status }})</td>
</tr>
<tr>
<td><b>Response time:</b></td>
<td>{{ response?.responseTime ?? 'Onbekend' }} (Milliseconds)</td>
</tr>
<tr>
<td><b>Size:</b></td>
<td>{{ response?.size ?? 'Onbekend' }} (Bytes)</td>
</tr>
<tr>
<td><b>Remote IP:</b></td>
<td>{{ response?.remoteIp ?? 'Onbekend' }}</td>
</tr>
<tr>
<td><b>Headers:</b></td>
<td>
<table>
<tr v-for="(header, index) in response?.headers" :key="index">
<td><b>{{ header[0] }}:</b></td>
<td>{{ header[1] }}</td>
</tr>
</table>
</td>
</tr>
<tr>
<td><b>Body:</b></td>
<td :class="`codeMirrorContainer ${getTheme()}`">
<CodeMirror v-model="responseBodyString"
:basic="true"
:dark="getTheme() === 'dark'"
:linter="jsonParseLinter()"
:lang="json()"
:readonly="true" />
</td>
</tr>
</table>
</div>
</div>
</div>
</NcModal>
</template>

<script>
import {
NcModal,
NcLoadingIcon,
NcNoteCard,
NcButton,
} from '@nextcloud/vue'
import CodeMirror from 'vue-codemirror6'
import { json, jsonParseLinter } from '@codemirror/lang-json'

import Play from 'vue-material-design-icons/Play.vue'

export default {
name: 'RunSynchronization',
components: {
NcModal,
NcLoadingIcon,
NcNoteCard,
CodeMirror,
NcButton,
},
data() {
return {
response: null,
responseBody: '',
responseBodyString: '',
success: null,
loading: false,
error: false,
}
},
methods: {
closeModal() {
navigationStore.setModal(false)
synchronizationStore.synchronizationRun = null
},
async runSynchronization() {
this.success = null
this.loading = true
this.error = false

synchronizationStore.runSynchronization(synchronizationStore.synchronizationItem.id)
.then(({ response, data }) => {
this.response = response
this.responseBody = data
this.responseBodyString = JSON.stringify(data, null, 2)
this.success = response.ok
}).catch((error) => {
this.success = false
this.error = error.message || 'An error occurred while running the synchronization'
console.error(error)
}).finally(() => {
this.loading = false
})
},
},
}
</script>
<style>
div[class='modal-container']:has(.runSynchronization .SuccessMarker) {
width: 900px !important;
}

.detailGrid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 5px;
}

.detailTable {
overflow-x: auto;
}

.detailTable > table {
width: 100%;
border: 1px solid grey;
border-collapse: collapse;
}

.detailTable > table > tr > td,
.detailTable > table > tr > th {
border: 1px solid grey;
padding: 5px;
}
</style>

<style scoped>
.runButtonContainer {
display: flex;
justify-content: center;
margin-block-start: 10px;
}

.SuccessMarker {
display: none;
}

/* ================ */
/* CodeMirror */
/* ================ */
.codeMirrorContainer {
margin-block-start: 6px;
}

.codeMirrorContainer :deep(.cm-content) {
border-radius: 0 !important;
border: none !important;
}
.codeMirrorContainer :deep(.cm-editor) {
outline: none !important;
}
.codeMirrorContainer.light > .vue-codemirror {
border: 1px dotted silver;
}
.codeMirrorContainer.dark > .vue-codemirror {
border: 1px dotted grey;
}

/* value text color */
.codeMirrorContainer.light :deep(.ͼe) {
color: #448c27;
}
.codeMirrorContainer.dark :deep(.ͼe) {
color: #88c379;
}

/* text cursor */
.codeMirrorContainer :deep(.cm-content) * {
cursor: text !important;
}

/* value number color */
.codeMirrorContainer.light :deep(.ͼd) {
color: #c68447;
}
.codeMirrorContainer.dark :deep(.ͼd) {
color: #d19a66;
}

/* value boolean color */
.codeMirrorContainer.light :deep(.ͼc) {
color: #221199;
}
.codeMirrorContainer.dark :deep(.ͼc) {
color: #260dd4;
}
</style>
26 changes: 26 additions & 0 deletions src/store/modules/synchronization.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const useSynchronizationStore = defineStore('synchronization', {
synchronizationList: [],
synchronizationContracts: [],
synchronizationTest: null,
synchronizationRun: null,
synchronizationLogs: [],
synchronizationSourceConfigKey: null,
synchronizationTargetConfigKey: null,
Expand Down Expand Up @@ -209,6 +210,31 @@ export const useSynchronizationStore = defineStore('synchronization', {
console.info('Synchronization tested')
this.refreshSynchronizationLogs()

return { response, data }
},
// Test a synchronization
async runSynchronization(id) {
if (!id) {
throw new Error('No synchronization item to run')
}

console.info('Testing synchronization...')

const endpoint = `/index.php/apps/openconnector/api/synchronizations-run/${id}`

const response = await fetch(endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
})

const data = await response.json()
this.synchronizationRun = data

console.info('Synchronization run')
this.refreshSynchronizationLogs()

return { response, data }
},
},
Expand Down
7 changes: 7 additions & 0 deletions src/views/Synchronization/SynchronizationDetails.vue
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ import { synchronizationStore, navigationStore, logStore } from '../../store/sto
</template>
Test
</NcActionButton>
<NcActionButton @click="navigationStore.setModal('runSynchronization')">
<template #icon>
<Play :size="20" />
</template>
Run
</NcActionButton>
<NcActionButton @click="navigationStore.setDialog('deleteSynchronization')">
<template #icon>
<TrashCanOutline :size="20" />
Expand Down Expand Up @@ -291,6 +297,7 @@ import Sync from 'vue-material-design-icons/Sync.vue'
import EyeOutline from 'vue-material-design-icons/EyeOutline.vue'
import DatabaseSettingsOutline from 'vue-material-design-icons/DatabaseSettingsOutline.vue'
import CardBulletedSettingsOutline from 'vue-material-design-icons/CardBulletedSettingsOutline.vue'
import Play from 'vue-material-design-icons/Play.vue'

export default {
name: 'SynchronizationDetails',
Expand Down
Loading