mirror of
https://codeberg.org/PLG-Development/PLG-MuDiCS
synced 2026-07-06 00:47:09 +00:00
chore: better logging
log some things only in dev mode
This commit is contained in:
@@ -7,6 +7,7 @@ import {
|
||||
type ShellCommandResponse,
|
||||
type TreeElement
|
||||
} from './types';
|
||||
import { dev } from '$app/environment';
|
||||
|
||||
export async function get_screenshot(ip: string): Promise<Blob | null> {
|
||||
const options = { method: 'PATCH' };
|
||||
@@ -62,7 +63,7 @@ export async function get_file_data(
|
||||
echo
|
||||
done
|
||||
`;
|
||||
const raw_response = await run_shell_command(ip, command, true);
|
||||
const raw_response = await run_shell_command(ip, command);
|
||||
if (!raw_response.ok || !raw_response.json) return null;
|
||||
const json_response = raw_response.json as ShellCommandResponse;
|
||||
if (json_response.exitCode === 0 && json_response.stdout.trim() === '') return [];
|
||||
@@ -93,7 +94,7 @@ export async function get_file_data(
|
||||
|
||||
export async function get_file_tree_data(ip: string, path: string): Promise<TreeElement[] | null> {
|
||||
const command = `cd ".${path}" && tree -Js`;
|
||||
const raw_response = await run_shell_command(ip, command, true);
|
||||
const raw_response = await run_shell_command(ip, command);
|
||||
|
||||
if (!raw_response.ok || !raw_response.json) return null;
|
||||
const json_response = raw_response.json as ShellCommandResponse;
|
||||
@@ -166,7 +167,6 @@ export async function get_thumbnail_blob(ip: string, path_to_file: string): Prom
|
||||
ip,
|
||||
`/file/preview${path_to_file}`,
|
||||
{ method: 'GET' },
|
||||
true,
|
||||
[415]
|
||||
);
|
||||
if (!raw_response.ok || !raw_response.blob) return null;
|
||||
@@ -188,11 +188,10 @@ async function request_display(
|
||||
ip: string,
|
||||
api_route: string,
|
||||
options: { method: string; headers?: Record<string, string>; body?: string },
|
||||
log_in_debug: boolean = false,
|
||||
supress_error_handling_http_codes: number[] = []
|
||||
): Promise<RequestResponse> {
|
||||
const url = `http://${ip}:1323/api${api_route}`;
|
||||
return await request(url, options, log_in_debug, supress_error_handling_http_codes);
|
||||
return await request(url, options, supress_error_handling_http_codes);
|
||||
}
|
||||
|
||||
async function request_control(
|
||||
@@ -206,15 +205,12 @@ async function request_control(
|
||||
async function request(
|
||||
url: string,
|
||||
options: { method: string; headers?: Record<string, string>; body?: string },
|
||||
log_in_debug: boolean = false,
|
||||
supress_error_handling_http_codes: number[] = []
|
||||
): Promise<RequestResponse> {
|
||||
try {
|
||||
const cache_buster = `${url.includes('?') ? '&' : '?'}=${Date.now()}`;
|
||||
if (log_in_debug) {
|
||||
console.debug(url + cache_buster, options.body);
|
||||
} else {
|
||||
console.log(url + cache_buster, options.body);
|
||||
if (dev) {
|
||||
console.debug('Sending request: ', url + cache_buster, 'with', options.body ?? 'none');
|
||||
}
|
||||
const response = await fetch(url + cache_buster, options);
|
||||
if (response.ok || supress_error_handling_http_codes.includes(response.status)) {
|
||||
@@ -227,10 +223,8 @@ async function request(
|
||||
const json: Record<string, unknown> = await response.json();
|
||||
request_response = { ok: response.ok, http_code: response.status, json: json };
|
||||
}
|
||||
if (log_in_debug) {
|
||||
if (dev) {
|
||||
console.debug(request_response);
|
||||
} else {
|
||||
console.log(request_response);
|
||||
}
|
||||
return request_response;
|
||||
}
|
||||
@@ -250,7 +244,9 @@ async function request(
|
||||
);
|
||||
} catch (error: unknown) {
|
||||
if (error instanceof TypeError && /fetch|NetworkError/i.test(error.message)) {
|
||||
console.log('Request failed - Is the targeted device online?');
|
||||
if (dev) {
|
||||
console.warn('Request failed - Is the targeted device online?');
|
||||
}
|
||||
} else {
|
||||
console.error(url, error);
|
||||
notifications.push('error', `Fataler Fehler bei API-Anfrage`, `${url}\nFehler: ${error}`);
|
||||
@@ -271,7 +267,9 @@ function handle_shell_error(
|
||||
shell_response.stderr &&
|
||||
/bash: line \d+: cd: .+: No such file or directory/.test(shell_response.stderr)
|
||||
) {
|
||||
console.log('current file_path does not exist on display:', ip);
|
||||
if (dev) {
|
||||
console.debug('current file_path does not exist on display:', ip);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
console.error(shell_response);
|
||||
@@ -286,11 +284,7 @@ function handle_shell_error(
|
||||
return false;
|
||||
}
|
||||
|
||||
async function run_shell_command(
|
||||
ip: string,
|
||||
command: string,
|
||||
log_in_debug: boolean = false
|
||||
): Promise<RequestResponse> {
|
||||
async function run_shell_command(ip: string, command: string): Promise<RequestResponse> {
|
||||
const options = {
|
||||
method: 'PATCH',
|
||||
headers: { 'content-type': 'application/json' },
|
||||
@@ -298,5 +292,5 @@ async function run_shell_command(
|
||||
command: command
|
||||
})
|
||||
};
|
||||
return await request_display(ip, '/shellCommand', options, log_in_debug);
|
||||
return await request_display(ip, '/shellCommand', options);
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { dev } from '$app/environment';
|
||||
import { db } from './files_display.db';
|
||||
import { get_display_by_id } from './stores/displays';
|
||||
import { remove_all_files_without_display, remove_file_from_display } from './stores/files';
|
||||
@@ -86,7 +87,9 @@ async function start_task_loop() {
|
||||
break;
|
||||
}
|
||||
|
||||
console.log('AKTUELL IN TASKS', tasks.length);
|
||||
if (dev) {
|
||||
console.debug('AKTUELL IN TASKS', tasks.length);
|
||||
}
|
||||
tasks.shift(); // Remove current_task from tasks
|
||||
}
|
||||
}
|
||||
@@ -164,6 +167,6 @@ async function show_error(
|
||||
`Datei: "${task.file_name}", Display-IP: ${task.display_ip}\nFehler: ${error}`
|
||||
);
|
||||
if (type === 'download') return;
|
||||
await remove_file_from_display(task.display_id, task.file_primary_key);
|
||||
await remove_file_from_display(task.display_id, task.file_primary_key);
|
||||
await remove_all_files_without_display();
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import { get_uuid, image_content_hash } from '../utils';
|
||||
import { get_screenshot } from '../api_handler';
|
||||
import { delete_and_deselect_unique_files_from_display } from './files';
|
||||
import { db } from '../files_display.db';
|
||||
import { dev } from '$app/environment';
|
||||
|
||||
export async function is_display_name_taken(name: string): Promise<boolean> {
|
||||
const exists = await db.displays.where('name').equals(name).first();
|
||||
@@ -23,11 +24,15 @@ export async function add_display(
|
||||
let group_id: string;
|
||||
if (group) {
|
||||
group_id = group.id;
|
||||
console.log('DISPLAYGROUP WURDE NICHT ERSTELLT');
|
||||
if (dev) {
|
||||
console.debug('DISPLAYGROUP WURDE NICHT ERSTELLT');
|
||||
}
|
||||
} else {
|
||||
group_id = get_uuid();
|
||||
await db.display_groups.put({ id: group_id, position: 0 });
|
||||
console.log('DISPLAYGROUP WURDE ERSTELLT');
|
||||
if (dev) {
|
||||
console.info('DISPLAYGROUP WURDE ERSTELLT');
|
||||
}
|
||||
}
|
||||
const element_count_in_group = (await db.displays.where('group_id').equals(group_id).toArray())
|
||||
.length;
|
||||
|
||||
@@ -29,7 +29,7 @@ export async function change_file_path(new_path: string) {
|
||||
for (const display of displays) {
|
||||
const changed_paths = await get_changed_directory_paths(display, new_path);
|
||||
if (!changed_paths) continue;
|
||||
console.log('Update file system from', display.name, ':', changed_paths);
|
||||
console.debug('Update file system from', display.name, ':', changed_paths);
|
||||
for (const path of changed_paths) {
|
||||
await update_folder_elements_recursively(display, path);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user