Skip to content

Commit

Permalink
feat: 记录接口调用状态
Browse files Browse the repository at this point in the history
  • Loading branch information
aooiuu committed May 31, 2024
1 parent f48d3ba commit 5fc06d7
Show file tree
Hide file tree
Showing 11 changed files with 165 additions and 129 deletions.
6 changes: 0 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@
QQ群: 50057637

## 快速体验

```sh
npx any-reader
```

## 多端支持

### VSCode 插件
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"electron:build": "npm -C packages/web run electron:build",
"vscode:server": "npm -C packages/web run vscode:dev",
"vscode:dev": "npm-run-all build:core build:shared --parallel vscode:server server",
"vscode:build": "npm -C packages/vscode run build",
"server:build": "npm -C packages/server run build",
"cli:build-tpl": "npm -C packages/web run cli:build",
"cli:build-js": "npm -C packages/cli run build",
Expand Down
8 changes: 3 additions & 5 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@any-reader/cli",
"version": "1.0.0",
"version": "1.0.1",
"description": "",
"type": "commonjs",
"files": [
Expand All @@ -18,11 +18,9 @@
"keywords": [],
"author": "",
"license": "GPL3",
"dependencies": {
"@any-reader/server": "workspace:^",
"open": "^10.1.0"
},
"devDependencies": {
"@any-reader/server": "workspace:^",
"open": "^10.1.0",
"@rollup/plugin-alias": "^5.0.0",
"@rollup/plugin-commonjs": "^25.0.2",
"@rollup/plugin-json": "^6.0.0",
Expand Down
89 changes: 57 additions & 32 deletions packages/shared/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ export async function discoverMap(ruleId: string) {
return ruleManager.discoverMap()
}

/**
* 发现页列表
* @returns
*/
export async function discover({ ruleId, data }: any) {
const rule = await ruleFileManager.findById(ruleId)
const ruleManager = new RuleManager(rule)
return ruleManager.discover(data.value)
}

/**
* 收藏列表
* @returns
Expand All @@ -49,16 +59,6 @@ export function getLocalBooks(dir: string) {
return localBookManager.getBookList(dir)
}

/**
* 发现页列表
* @returns
*/
export async function discover({ ruleId, data }: any) {
const rule = await ruleFileManager.findById(ruleId)
const ruleManager = new RuleManager(rule)
return ruleManager.discover(data.value)
}

/**
* 收藏
* @param param0
Expand Down Expand Up @@ -231,26 +231,51 @@ function success(data: any, msg = '') {

// vscode 、electron、服务端通用注册接口
export function useApi(register: any, { CONFIG_PATH, bookDir }: any) {
register('get@discoverMap', async ({ ruleId = '' } = {}) => success(await discoverMap(ruleId)))
register('get@getFavorites', async () => success(await getFavorites()))
register('get@getHistory', async () => success(await getHistory()))
register('get@getLocalBooks', async () => success(await getLocalBooks(bookDir)))
register('post@discover', async (data: any) => success(await discover(data)))
register('post@star', async (data: any) => success(await star(data)))
register('post@unstar', async (data: any) => success(await unstar(data)))
register('get@rules', async () => success(await rules()))
register('get@getRuleById', async ({ id = '' } = {}) => success(await getRuleById(id)))
register('post@createRule', async (data: any) => success(await createRule(data)))
register('post@updateRule', async (data: any) => success(await updateRule(data)))
register('post@searchByRuleId', async (data: any) => success(await searchByRuleId(data)))
register('post@content', async (data: any) => success(await content(data)))
register('post@getChapter', async (data: any) => success(await getChapter(data)))
register('get@readConfig', async () => success(await readConfig(CONFIG_PATH)))
register('post@updateConfig', async (data: any) => success(await updateConfig(CONFIG_PATH, data)))
register('get@getRuleExtras', async () => success(await getRuleExtras()))
register('post@ping', async (data: any) => success(await ping(data)))
register('post@batchUpdateRules', async (data: any) => success(await batchUpdateRules(data)))
register('post@delRules', async (data: any) => success(await delRules(data)))
register('post@updateRuleSort', async (data: any) => success(await ruleFileManager.updateRuleSort(data && data.id)))
register('post@importRules', async (data: any) => success(await ruleFileManager.importRules(data && data.url)))
const registerApi = async (apiPath: string, handle: Function, log?: {
ruleId: (...arg: any) => string
check: (arg: any) => boolean
}) => {
register(apiPath, async (...arg: any) => {
// 原始返回值
const result = await handle(...arg).catch(() => {})

// 记录接口调用情况
if (typeof log === 'object' && log.ruleId && log.check) {
const isOk = log.check(result)
ruleExtraManager.updateApiStatus(log.ruleId(...arg), isOk ? `${apiPath}.ok` : `${apiPath}.fail`)
}

// 返回数据
return success(result)
})
}

const discoverLog = {
ruleId: (data: any) => data.ruleId,
check: (v: any[]) => Array.isArray(v) && v.length > 0,
}

// 注册接口
registerApi('get@discoverMap', async ({ ruleId = '' } = {}) => await discoverMap(ruleId), discoverLog)
registerApi('post@discover', async (data: any) => await discover(data), discoverLog)
registerApi('get@getFavorites', async () => await getFavorites())
registerApi('get@getHistory', async () => await getHistory())
registerApi('get@getLocalBooks', async () => await getLocalBooks(bookDir))
registerApi('post@star', async (data: any) => await star(data))
registerApi('post@unstar', async (data: any) => await unstar(data))
registerApi('get@rules', async () => await rules())
registerApi('get@getRuleById', async ({ id = '' } = {}) => await getRuleById(id))
registerApi('post@createRule', async (data: any) => await createRule(data))
registerApi('post@updateRule', async (data: any) => await updateRule(data))
registerApi('post@searchByRuleId', async (data: any) => await searchByRuleId(data), discoverLog)
registerApi('post@content', async (data: any) => await content(data))
registerApi('post@getChapter', async (data: any) => await getChapter(data))
registerApi('get@readConfig', async () => await readConfig(CONFIG_PATH))
registerApi('post@updateConfig', async (data: any) => await updateConfig(CONFIG_PATH, data))
registerApi('get@getRuleExtras', async () => await getRuleExtras())
registerApi('post@ping', async (data: any) => await ping(data))
registerApi('post@batchUpdateRules', async (data: any) => await batchUpdateRules(data))
registerApi('post@delRules', async (data: any) => await delRules(data))
registerApi('post@updateRuleSort', async (data: any) => await ruleFileManager.updateRuleSort(data && data.id))
registerApi('post@importRules', async (data: any) => await ruleFileManager.importRules(data && data.url))
}
10 changes: 9 additions & 1 deletion packages/shared/src/ruleExtraManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { RULE_EXTRA_PATH } from './constants'
import _ping from './ping'

export interface SourceExtraRow {
ping?: number
[_: string]: number
}

export interface SourceExtra {
Expand Down Expand Up @@ -47,3 +47,11 @@ export async function getRuleExtras(): Promise<SourceExtra> {
const db = await getDb()
return db.data
}

export async function updateApiStatus(id: string, field: string) {
const db = await getDb()
if (!db.data[id])
db.data[id] = {}
_.set(db.data[id], field, _.get(db.data[id], field, 0) + 1)
writeDB()
}
2 changes: 1 addition & 1 deletion packages/shared/src/ruleFileManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export function isRule(rule: any): boolean {
if (typeof rule !== 'object')
return false

return rule.id && rule.host && rule.contentType
return rule.id && rule.host && typeof rule.contentType !== 'undefined'
}

export async function importRules(url: string) {
Expand Down
13 changes: 11 additions & 2 deletions packages/vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"displayName": "any-reader",
"description": "摸鱼,自定义规则多站点聚合搜索阅读小说、漫画。包含JS规则解析库和VSCode插件。支持本地小说 TXT、EPUB",
"icon": "resources/icon.png",
"version": "1.3.2",
"version": "1.3.3",
"publisher": "aooiu",
"qna": "https://github.com/aooiuu/any-reader/issues",
"bugs": {
Expand All @@ -30,7 +30,16 @@
],
"activationEvents": [
"onView:any-reader",
"onWebviewPanel:any-reader"
"onWebviewPanel:any-reader",
"onCommand:any-reader.getBookSource",
"onCommand:any-reader.searchBook",
"onCommand:any-reader.searchBookByRule",
"onCommand:any-reader.discover",
"onCommand:any-reader.home",
"onCommand:any-reader.gamePlay",
"onCommand:any-reader.star",
"onCommand:any-reader.unstar",
"onCommand:any-reader.openLocalBookDir"
],
"main": "./dist/extension.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/web/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "any-reader-web",
"version": "1.0.0-dev.6",
"version": "1.0.0-dev.8",
"private": true,
"scripts": {
"dev": "cross-env VITE_APP_PLATFORM=browser vite",
Expand Down
62 changes: 61 additions & 1 deletion packages/web/src/pages/pc/rules/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,65 @@ const tableColumns = ref([
/>
)
},
{
title: '接口调用',
width: 120,
align: 'center',
filterable: {
filters: [
{
text: '搜索失败>3且成功=0',
value: 1
},
{
text: '发现分类失败>3且成功=0',
value: 2
},
{
text: '发现列表失败>3且成功=0',
value: 3
}
],
filter: (value, record) => {
if (value.includes(1)) {
const ok = _.get(record, 'extra.post@searchByRuleId.ok', 0);
const fail = _.get(record, 'extra.post@searchByRuleId.fail', 0);
return ok === 0 && fail > 3;
}
if (value.includes(2)) {
const ok = _.get(record, 'extra.get@discoverMap.ok', 0);
const fail = _.get(record, 'extra.get@discoverMap.fail', 0);
return ok === 0 && fail > 3;
}
if (value.includes(2)) {
const ok = _.get(record, 'extra.post@discover.ok', 0);
const fail = _.get(record, 'extra.post@discover.fail', 0);
return ok === 0 && fail > 3;
}
return true;
},
multiple: false
},
render: ({ record }) => (
<div>
<div class="flex items-center">
<span class="text-10 mr-4">搜索</span>
<span class="color-green">{_.get(record, 'extra.post@searchByRuleId.ok', 0)}</span>/
<span class="color-red">{_.get(record, 'extra.post@searchByRuleId.fail', 0)}</span>
</div>
<div class="flex items-center">
<span class="text-10 mr-4">发现分类</span>
<span class="color-green">{_.get(record, 'extra.get@discoverMap.ok', 0)}</span>/
<span class="color-red">{_.get(record, 'extra.get@discoverMap.fail', 0)}</span>
</div>
<div class="flex items-center">
<span class="text-10 mr-4">发现列表</span>
<span class="color-green">{_.get(record, 'extra.post@discover.ok', 0)}</span>/
<span class="color-red">{_.get(record, 'extra.post@discover.fail', 0)}</span>
</div>
</div>
)
},
{
title: '操作',
width: 120,
Expand Down Expand Up @@ -365,7 +424,8 @@ async function handleChange(data, extra, currentData) {
}
async function drop(event) {
event.preventdefault();
// vscode
if (typeof event.preventdefault === 'function') event.preventdefault();
const files = event.dataTransfer.files;
for (const file of files) {
await dropFile(file);
Expand Down
2 changes: 1 addition & 1 deletion packages/web/src/utils/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ export const isRule = (rule: any) => {

if (typeof rule !== 'object') return false;

return rule.id && rule.host && rule.contentType;
return rule.id && rule.host && typeof rule.contentType !== 'undefined';
};
Loading

0 comments on commit 5fc06d7

Please sign in to comment.