Skip to content

Commit

Permalink
refactor: 抽出 ProfessionTable
Browse files Browse the repository at this point in the history
  • Loading branch information
huayemao committed Jun 23, 2024
1 parent 35021d4 commit 33243cf
Show file tree
Hide file tree
Showing 6 changed files with 293 additions and 215 deletions.
77 changes: 62 additions & 15 deletions app/(content)/data-process/DBContext.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,33 @@
"use client";
import * as Comlink from "comlink";
import { createContext, useEffect, useState } from "react";
import {
createContext,
useCallback,
useContext,
useEffect,
useState,
} from "react";

export const DBContext = createContext<{
state: { dbWorker: Comlink.Remote<WorkerProxy> | null };
state: {
dbWorker: Comlink.Remote<WorkerProxy> | null;
activeDb: string | null;
dbs: string[];
baseTables: any[];
};
actions: {
refetch: () => void;
setActiveDb: (db: string) => void;
};
}>({
state: { dbWorker: null },
state: { dbWorker: null, dbs: [], baseTables: [], activeDb: null },
actions: {
refetch() {
{
}
},
setActiveDb() {},
},
});

interface WorkerProxy {
Expand All @@ -23,9 +45,15 @@ interface WorkerProxy {
}

export function DBContextProvider({ children }) {
const getTableSql = `select * from sqlite_schema where type = 'table';`;

const [dbWorker, setDbWorker] = useState<Comlink.Remote<WorkerProxy> | null>(
null
);
const [dbs, setDbs] = useState<string[]>([]);
const [activeDb, setActiveDb] = useState<string | null>(null);
const [baseTables, setBaseTables] = useState<any[]>([]);

// const [query, setQuery] = useState<any>("")
let needsSetup = true;

Expand All @@ -34,31 +62,50 @@ export function DBContextProvider({ children }) {
needsSetup = false;
const _dbWorker = new Worker(new URL("dbworker.js", import.meta.url));
const Myclass = Comlink.wrap<WorkerProxy>(_dbWorker);
new Myclass().then((_i) => {
setDbWorker(() => _i);
});
new Myclass()
.then((instance) => {
setDbWorker(() => instance);
return instance;
})
.then((instance) => {
instance.getDbs().then((dbs) => setDbs(dbs.map(decodeURIComponent)));
});
}
}, []);

let needFileWritten = true;
useEffect(() => {
if (needFileWritten) {
needFileWritten = false;
const _fileWorker = new Worker(new URL("fileworker.js", import.meta.url));
_fileWorker.postMessage(`this is a long text string.
Actually not that long.
But long enough to do a quick test.
`);
if (activeDb) {
const name = encodeURIComponent(activeDb);
dbWorker?.readWriteDB?.(name);
dbWorker?.execSql(getTableSql).then(setBaseTables);
}
}, [activeDb]);

const refetch = useCallback(async () => {
const dbs = await dbWorker?.getDbs();
if (!dbs) {
return setDbs([]);
}
setDbs(dbs.map(decodeURIComponent));
}, []);

// create the value for the context provider
const context = {
state: {
dbWorker,
dbs: dbs,
baseTables,
activeDb,
},
actions: {
refetch,
setActiveDb,
},
actions: {},
};

return <DBContext.Provider value={context}>{children}</DBContext.Provider>;
}

export const useDBContext = () => {
return useContext(DBContext);
};
96 changes: 96 additions & 0 deletions app/(content)/data-process/ProfessionTable.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
"use client";
import { useCallback, useEffect } from "react";
import { useDBContext } from "./DBContext";
import { Field, Profession } from "./types";
import { parseProfessionJobCount } from "./util";

export function ProfessionTable() {
const {
state: { dbWorker, activeDb, baseTables },
} = useDBContext();

const parseAlltables = useCallback(async () => {
const jobTables: string[] = [];
for (const { name: tableName } of baseTables) {
console.log(dbWorker);
const cols = (await dbWorker!.execSql(
`PRAGMA table_info(${tableName});`
)) as Field[];
console.log(cols);

if (
["职位代码", "专业要求"].every((s) =>
cols.map((e) => e.name).includes(s)
)
) {
jobTables.push(tableName);
}
}
console.log(jobTables);

return await Promise.all(jobTables.map(parseTable));

async function parseTable(tableName: string) {
let jobGroupsByProfession: {
专业要求: string;
cnt: number;
}[] = [];
let professions: Profession[] = [];
const professionsSql = `select count(*) as cnt, 专业要求 from ${tableName} group by 专业要求 having 学历要求 like '%本科%' and (招录单位全称 like '%昆明%' or 招录单位全称 like '%云南省%') order by cnt desc;`;

return dbWorker
?.execSql(professionsSql)
.then((res) => (jobGroupsByProfession = res))
.then(() => dbWorker?.execSql(`select * from 本科专业目录;`))
.then((res) => {
professions = res.filter(
(e) => !["▲", "★"].some((s) => JSON.stringify(e).includes(s))
);
})
.then(() => {
console.log(jobGroupsByProfession);
jobGroupsByProfession.forEach(parseProfessionJobCount(professions));
return professions;
});
}
}, [dbWorker]);

// todo: 表名是未知的,除非加一个 meta 表。。。才能知道哪些表是 Job 表

useEffect(() => {
if (!dbWorker) {
return;
}

parseAlltables().then((res) => {
console.log(res);
// const baseCnt = jobGroupsByProfession.find(
// (e) => e.专业要求 == "不限"
// )?.cnt;
// console.log(baseCnt);
// console.log(
// `红牌专业 ${professions.filter((e) => e.岗位数 == baseCnt).length}`
// );
// console.table(
// professions
// .sort((a, b) => a.岗位数 - b.岗位数)
// .slice(0, 100)
// .map((e) => ({
// ...e,
// 除不限外岗位数: e.岗位数 - (baseCnt || 0),
// }))
// );
// console.log("top 100");
// console.log(
// professions
// .sort((a, b) => b.岗位数 - a.岗位数)
// .map((e) => ({
// ...e,
// 除不限外岗位数: e.岗位数 - (baseCnt || 0),
// }))
// );
});
}, [dbWorker]);

return <></>;
}
27 changes: 0 additions & 27 deletions app/(content)/data-process/fileworker.js

This file was deleted.

Loading

0 comments on commit 33243cf

Please sign in to comment.