Skip to content

Commit

Permalink
feat: pass data to frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
invm committed Oct 14, 2023
1 parent a36bcda commit 6c4ca8e
Show file tree
Hide file tree
Showing 10 changed files with 361 additions and 278 deletions.
12 changes: 11 additions & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,18 @@ module.exports = {
},
plugins: ['@typescript-eslint'],
rules: {
indent: ['error', 2],
indent: ['error', 2, { SwitchCase: 1 }],
'linebreak-style': ['error', 'unix'],
semi: ['error', 'always'],
'no-unused-vars': 'off',
'@typescript-eslint/ban-ts-comment': 'off',
'@typescript-eslint/no-unused-vars': [
'warn', // or "error"
{
argsIgnorePattern: '^_',
varsIgnorePattern: '^_',
caughtErrorsIgnorePattern: '^_',
},
],
},
};
2 changes: 1 addition & 1 deletion src-tauri/src/handlers/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub async fn enqueue_query(
return Ok(QueryTaskEnqueueResult {
conn_id,
tab_idx,
status: QueryTaskStatus::Queued,
status: QueryTaskStatus::Progress,
results_sets: statements.iter().map(|s| s.1.clone()).collect(),
});
}
Expand Down
65 changes: 31 additions & 34 deletions src-tauri/src/queues/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,14 @@ impl Events {

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum QueryTaskStatus {
Queued,
Progress,
Completed,
Error,
}

impl Default for QueryTaskStatus {
fn default() -> Self {
QueryTaskStatus::Queued
QueryTaskStatus::Progress
}
}

Expand Down Expand Up @@ -57,7 +56,7 @@ impl QueryTask {
tab_idx,
query_idx,
query: query.to_string(),
status: QueryTaskStatus::Queued,
status: QueryTaskStatus::Progress,
}
}
}
Expand Down Expand Up @@ -89,38 +88,36 @@ pub async fn async_process_model(
while let Some(input) = input_rx.recv().await {
let task = input;
match task.conn.execute_query(&task.query).await {
Ok(result_set) => {
match write_query(&task.id, result_set) {
Ok(path) => {
output_tx
.send(QueryTaskResult {
conn_id: task.conn.config.id.to_string(),
status: QueryTaskStatus::Completed,
query: task.query,
id: task.id,
query_idx: task.query_idx,
tab_idx: task.tab_idx,
path: Some(path),
error: None,
})
.await?
}
Err(e) => {
output_tx
.send(QueryTaskResult {
conn_id: task.conn.config.id.to_string(),
status: QueryTaskStatus::Error,
query: task.query,
id: task.id,
query_idx: task.query_idx,
tab_idx: task.tab_idx,
path: None,
error: Some(e.to_string()),
})
.await?
}
Ok(result_set) => match write_query(&task.id, result_set) {
Ok(path) => {
output_tx
.send(QueryTaskResult {
conn_id: task.conn.config.id.to_string(),
status: QueryTaskStatus::Completed,
query: task.query,
id: task.id,
query_idx: task.query_idx,
tab_idx: task.tab_idx,
path: Some(path),
error: None,
})
.await?
}
}
Err(e) => {
output_tx
.send(QueryTaskResult {
conn_id: task.conn.config.id.to_string(),
status: QueryTaskStatus::Error,
query: task.query,
id: task.id,
query_idx: task.query_idx,
tab_idx: task.tab_idx,
path: None,
error: Some(e.to_string()),
})
.await?
}
},
Err(e) => {
output_tx
.send(QueryTaskResult {
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/src/utils/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub fn write_file(path: &PathBuf, content: &str) -> Result<()> {
pub fn write_query(id: &str, result_set: ResultSet) -> Result<String> {
let rows = json!(result_set.rows).to_string();
let metadata = json!({
"rows": result_set.rows.len(),
"count": result_set.rows.len(),
"affected_rows": result_set.affected_rows,
"warnings": result_set.warnings,
"info": result_set.info,
Expand Down
52 changes: 36 additions & 16 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
import "utils/i18n";
import "tabulator-tables/dist/css/tabulator_midnight.min.css";
import "solid-command-palette/pkg-dist/style.css";
import { Main } from "components/Screens/Main";
import { CommandPaletteContext } from "components/CommandPalette/CommandPaletteContext";
import { Loader } from "components/UI";
import { createEffect, createSignal, onMount } from "solid-js";
import { useAppSelector } from "services/Context";
import { listen } from "@tauri-apps/api/event";
import { Events, QueryTaskResult } from "interfaces";
import { log } from "utils/utils";
import 'utils/i18n';
import 'tabulator-tables/dist/css/tabulator_midnight.min.css';
import 'solid-command-palette/pkg-dist/style.css';
import { Main } from 'components/Screens/Main';
import { CommandPaletteContext } from 'components/CommandPalette/CommandPaletteContext';
import { Loader } from 'components/UI';
import { createEffect, createSignal, onMount } from 'solid-js';
import { useAppSelector } from 'services/Context';
import { listen } from '@tauri-apps/api/event';
import { Events, QueryMetadataResult, QueryTaskResult, Row } from 'interfaces';
import { log } from 'utils/utils';
import { invoke } from '@tauri-apps/api';

function App() {
const [loading, setLoading] = createSignal(true);
const {
connections: {
restoreConnectionStore,
getConnection,
updateResultSet,
contentStore: { idx },
queryIdx,
},
Expand All @@ -28,27 +30,45 @@ function App() {
}, 500);
});

const getQueryResults = () => { };
const getQueryMetadata = (path: string) =>
invoke<string>('get_query_metadata', { path });

const compareAndAssign = (event: QueryTaskResult) => {
const getInitialQueryResults = (path: string) => {
// TODO: move page size to global store
return invoke<string>('query_results', {
params: { path, page: 0, page_size: 20 },
});
};

const compareAndAssign = async (event: QueryTaskResult) => {
if (
getConnection().id === event.conn_id &&
idx === event.tab_idx &&
queryIdx() === event.query_idx
) {
// TODO: should get 0 page of results
if (event.status === 'Completed') {
const res = await getInitialQueryResults(event.path);
const md_res = await getQueryMetadata(event.path);
const rows = JSON.parse(res) as Row[];
const metadata = JSON.parse(md_res) as QueryMetadataResult;
updateResultSet(event.tab_idx, event.query_idx, { rows, ...metadata });
} else if (event.status === 'Error') {
updateResultSet(event.tab_idx, event.query_idx, { info: event.error });
}
}
// setContentStore('tabs', idx ?? contentStore.idx, key, data);
};

onMount(async () => {
await listen<QueryTaskResult>(Events.QueryFinished, (event) => {
await listen<QueryTaskResult>(Events.QueryFinished, async (event) => {
log(event);
compareAndAssign(event.payload);
await compareAndAssign(event.payload);
});
});

onMount(async () => {
const theme = localStorage.getItem("theme") || "dark";
const theme = localStorage.getItem('theme') || 'dark';
document.documentElement.dataset.theme = theme;
await restoreAppStore();
await restoreConnectionStore();
Expand Down
4 changes: 2 additions & 2 deletions src/components/Screens/Console/Content/QueryTab/QueryTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ export const QueryTab = () => {
});

return (
<div class="flex flex-col h-full overflow-hidden">
<div class="h-full max-h-full overflow-hidden">
<div id="query" class="flex flex-col">
<QueryTextArea />
</div>
<div id="results" class="flex">
<div id="results" class="">
<ResultsTable />
</div>
</div>
Expand Down
Loading

0 comments on commit 6c4ca8e

Please sign in to comment.