Skip to content

Commit 8420f07

Browse files
core: frontend: components: SettingsMenu: Use new remove_log_stream
Signed-off-by: Patrick José Pereira <patrickelectric@gmail.com>
1 parent 4a72154 commit 8420f07

File tree

1 file changed

+91
-19
lines changed

1 file changed

+91
-19
lines changed

core/frontend/src/components/app/SettingsMenu.vue

Lines changed: 91 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
<v-btn
7474
v-tooltip="'Frees up space on the SD card'"
7575
class="ma-2"
76-
:disabled="disable_remove"
76+
:disabled="disable_remove || deletion_in_progress"
7777
@click="remove_service_log_files"
7878
>
7979
<v-icon left>
@@ -83,6 +83,29 @@
8383
</v-btn>
8484
</v-card-actions>
8585

86+
<v-expand-transition>
87+
<div v-if="deletion_in_progress" class="pa-4">
88+
<v-progress-linear
89+
indeterminate
90+
color="primary"
91+
/>
92+
<div class="mt-2">
93+
<div class="text-subtitle-2">
94+
Deleting: {{ current_deletion_path }}
95+
</div>
96+
<div class="text-caption">
97+
Size: {{ formatSize(current_deletion_size / 1024) }}
98+
</div>
99+
<div class="text-caption">
100+
Total: {{ formatSize(current_deletion_total_size / 1024) }}
101+
</div>
102+
<div class="text-caption">
103+
Status: {{ current_deletion_status }}
104+
</div>
105+
</div>
106+
</div>
107+
</v-expand-transition>
108+
86109
<v-divider />
87110

88111
<v-card-title class="align-center">
@@ -150,6 +173,7 @@
150173
</template>
151174

152175
<script lang="ts">
176+
import axios, { CancelTokenSource } from 'axios'
153177
import Vue from 'vue'
154178
155179
import SpinningLogo from '@/components/common/SpinningLogo.vue'
@@ -159,6 +183,7 @@ import bag from '@/store/bag'
159183
import { commander_service } from '@/types/frontend_services'
160184
import back_axios from '@/utils/api'
161185
import { prettifySize } from '@/utils/helper_functions'
186+
import { parseStreamingResponse } from '@/utils/streaming'
162187
163188
const API_URL = '/commander/v1.0'
164189
@@ -180,13 +205,29 @@ export default Vue.extend({
180205
operation_in_progress: false,
181206
operation_description: '',
182207
operation_error: undefined as undefined | string,
208+
deletion_in_progress: false,
209+
deletion_log_abort_controller: null as null | CancelTokenSource,
210+
current_deletion_path: '',
211+
current_deletion_size: 0,
212+
current_deletion_total_size: 0,
213+
current_deletion_status: '',
183214
}
184215
},
185216
computed: {
186217
has_operation_error(): boolean {
187218
return this.operation_error !== undefined
188219
},
189220
},
221+
watch: {
222+
show_dialog: {
223+
handler(val) {
224+
if (!val) {
225+
this.deletion_log_abort_controller?.cancel()
226+
}
227+
},
228+
immediate: true,
229+
},
230+
},
190231
async mounted() {
191232
await this.get_log_folder_size()
192233
await this.get_mavlink_log_folder_size()
@@ -197,6 +238,9 @@ export default Vue.extend({
197238
this.operation_in_progress = true
198239
this.operation_description = description
199240
},
241+
formatSize(bytes: number): string {
242+
return prettifySize(bytes)
243+
},
200244
async download_service_log_files(): Promise<void> {
201245
const folder = await filebrowser.fetchFolder('system_logs')
202246
await filebrowser.downloadFolder(folder)
@@ -216,7 +260,7 @@ export default Vue.extend({
216260
const folder_data_bytes = response.data
217261
const one_hundred_MB = 100 * 2 ** 20
218262
this.disable_remove = folder_data_bytes < one_hundred_MB
219-
this.log_folder_size = prettifySize(folder_data_bytes / 1024)
263+
this.log_folder_size = this.formatSize(folder_data_bytes / 1024)
220264
})
221265
.catch((error) => {
222266
this.operation_error = String(error)
@@ -235,7 +279,7 @@ export default Vue.extend({
235279
const folder_data_bytes = response.data
236280
const one_hundred_MB = 100 * 2 ** 20
237281
this.disable_remove_mavlink = folder_data_bytes < one_hundred_MB
238-
this.mavlink_log_folder_size = prettifySize(folder_data_bytes / 1024)
282+
this.mavlink_log_folder_size = this.formatSize(folder_data_bytes / 1024)
239283
})
240284
.catch((error) => {
241285
this.operation_error = String(error)
@@ -264,24 +308,52 @@ export default Vue.extend({
264308
this.operation_in_progress = false
265309
},
266310
async remove_service_log_files(): Promise<void> {
267-
this.prepare_operation('Removing system log files...')
311+
this.deletion_log_abort_controller = axios.CancelToken.source()
312+
this.deletion_in_progress = true
313+
this.current_deletion_path = '...'
314+
this.current_deletion_size = 0
315+
this.current_deletion_total_size = 0
316+
this.current_deletion_status = 'Starting deletion...'
268317
269-
await back_axios({
270-
url: `${API_URL}/services/remove_log`,
271-
method: 'post',
272-
params: {
273-
i_know_what_i_am_doing: true,
274-
},
275-
timeout: 20000,
276-
})
277-
.then(() => {
278-
this.get_log_folder_size()
279-
})
280-
.catch((error) => {
281-
this.operation_error = String(error)
282-
notifier.pushBackError('REMOVE_SERVICES_LOG_FAIL', error)
318+
try {
319+
await back_axios({
320+
url: `${API_URL}/services/remove_log_stream`,
321+
method: 'post',
322+
params: {
323+
i_know_what_i_am_doing: true,
324+
},
325+
responseType: 'text',
326+
onDownloadProgress: (progressEvent) => {
327+
let result = parseStreamingResponse(progressEvent.currentTarget.response)
328+
result = result.filter((fragment) => fragment.fragment !== -1)
329+
const last_fragment = result.last()
330+
331+
if (last_fragment?.data) {
332+
try {
333+
const info = JSON.parse(last_fragment.data)
334+
this.current_deletion_path = info.path.length > 30 ? `...${info.path.slice(-20)}` : info.path
335+
this.current_deletion_size = info.size
336+
this.current_deletion_total_size += info.size
337+
this.current_deletion_status = info.success ? 'Successfully deleted' : 'Failed to delete'
338+
} catch (e) {
339+
console.error('Error parsing deletion info:', e)
340+
}
341+
}
342+
},
343+
cancelToken: this.deletion_log_abort_controller?.token,
283344
})
284-
this.operation_in_progress = false
345+
} catch (error) {
346+
this.operation_error = String(error)
347+
notifier.pushBackError('REMOVE_SERVICES_LOG_FAIL', error)
348+
} finally {
349+
this.operation_in_progress = false
350+
this.deletion_in_progress = false
351+
this.current_deletion_path = ''
352+
this.current_deletion_size = 0
353+
this.current_deletion_status = ''
354+
}
355+
356+
this.get_log_folder_size()
285357
},
286358
async remove_mavlink_log_files(): Promise<void> {
287359
this.prepare_operation('Removing MAVLink log files...')

0 commit comments

Comments
 (0)