Skip to content

Commit

Permalink
Merge pull request #4 from lxdao-official/develop
Browse files Browse the repository at this point in the history
feat: about erc error
  • Loading branch information
wwei-github authored Nov 16, 2023
2 parents aba6f9d + 99012f8 commit eebb80e
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 110 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ lerna-debug.log*

#ETH-EIPs
ETH-EIPs/*
ETH-ERCS/*
.env
2 changes: 2 additions & 0 deletions prisma/migrations/20231116025842_/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterEnum
ALTER TYPE "EIPStatus" ADD VALUE 'Moved';
2 changes: 2 additions & 0 deletions prisma/migrations/20231116030112_/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "EIPs" ALTER COLUMN "type" DROP NOT NULL;
2 changes: 2 additions & 0 deletions prisma/migrations/20231116030412_/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "EIPs" ALTER COLUMN "created" DROP NOT NULL;
5 changes: 3 additions & 2 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ enum EIPStatus {
Stagnant
Withdrawn
Living
Moved
}

model EmailSubscribe {
Expand All @@ -48,9 +49,9 @@ model EIPs {
author String
discussions_to String?
status EIPStatus
type EIPType
type EIPType?
category EIPCategory?
created DateTime
created DateTime?
requires Int[] @default([])
last_call_deadline DateTime?
withdrawal_reason String?
Expand Down
7 changes: 7 additions & 0 deletions src/app.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ export class AppController {
// return { data: result };
// }

@Get('/ercs/download')
@ApiOperation({ description: 'download Ecs.' })
async downloadErcs() {
const result = await this.appService.downloadErcs();
return { data: result };
}

@Post('/email/subscribe')
@ApiOperation({ description: 'Subscribe email.' })
@ApiBody({
Expand Down
251 changes: 144 additions & 107 deletions src/app.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,35 +80,38 @@ export class AppService {
}

async search(content: string) {
const result = {};
// eip match
if (this.isNumeric(content)) {
const eipRecords = await this.connection.query(
`SELECT eip, title, type, category FROM "EIPs" WHERE eip='${content}'`,
);
if (eipRecords && eipRecords.length > 0) {
result['eip_list'] = eipRecords;
}
} else {
// title match
const titleRecords = await this.connection.query(
`SELECT eip, type, category, ts_headline('english',title, q), rank FROM (SELECT eip, type, category, title, q, ts_rank_cd(title_ts, q) AS rank FROM "EIPs", phraseto_tsquery('english','${content}') q WHERE title_ts @@ q ORDER BY rank DESC LIMIT 20) AS foo;`,
);
try {
const result = {};
// eip match
if (this.isNumeric(content)) {
const eipRecords = await this.connection.query(
`SELECT eip, title, type, category FROM "EIPs" WHERE eip='${content}'`,
);
if (eipRecords && eipRecords.length > 0) {
result['eip_list'] = eipRecords;
}
} else {
// title match
const titleRecords = await this.connection.query(
`SELECT eip, type, category, ts_headline('english',title, q), rank FROM (SELECT eip, type, category, title, q, ts_rank_cd(title_ts, q) AS rank FROM "EIPs", phraseto_tsquery('english','${content}') q WHERE title_ts @@ q ORDER BY rank DESC LIMIT 20) AS foo;`,
);

if (titleRecords && titleRecords.length > 0) {
result['title_list'] = titleRecords;
}
if (titleRecords && titleRecords.length > 0) {
result['title_list'] = titleRecords;
}

// content match
const contentRecords = await this.connection.query(
`SELECT eip, type, category, title, ts_headline('english',content, q), rank FROM (SELECT eip, type, category, title, content, q, ts_rank_cd(content_ts, q) AS rank FROM "EIPs", phraseto_tsquery('english','${content}') q WHERE content_ts @@ q ORDER BY rank DESC LIMIT 20) AS foo;`,
);
if (contentRecords && contentRecords.length > 0) {
result['content_list'] = contentRecords;
// content match
const contentRecords = await this.connection.query(
`SELECT eip, type, category, title, ts_headline('english',content, q), rank FROM (SELECT eip, type, category, title, content, q, ts_rank_cd(content_ts, q) AS rank FROM "EIPs", phraseto_tsquery('english','${content}') q WHERE content_ts @@ q ORDER BY rank DESC LIMIT 20) AS foo;`,
);
if (contentRecords && contentRecords.length > 0) {
result['content_list'] = contentRecords;
}
}
return result;
} catch (err) {
console.log(err);
}

return result;
}

isEmail(email): boolean {
Expand Down Expand Up @@ -171,6 +174,91 @@ export class AppService {
return add_update_response;
}

formateData(data: any) {
const metaInfo = data.split('---')[1];
const content = data.split('---')[2];
const lines = metaInfo.split('\n');
//默认值填充
const result = <EIPs>{
eip: null,
title: '',
description: '',
status: null,
discussions_to: null,
author: '',
content: content,
extension_sub_title: null,
extension_short_read: null,
createdAt: new Date(),
updatedAt: new Date(),
type: null,
category: null,
requires: [],
created: null,
last_call_deadline: null,
withdrawal_reason: null,
};
for (let q = 0; q < lines.length; q++) {
const parts = lines[q].split(': ');
const field: string = parts[0];
let value: string = parts[1];
//兼容value中包含:的情况
if (parts.length > 2) {
value = parts.slice(1).join(': ');
}
if (field) {
if (field === 'eip') {
result['eip'] = value;
} else if (field === 'requires') {
result[field] = value.split(', ').map((eip) => {
return Number(eip);
});
} else if (field === 'title') {
result[field] = value.replace(/^\"|\"$/g, '');
} else if (field === 'type') {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
result[field] = value;
if (value === 'Standards Track') {
result[field] = EIPType.Standards_Track;
}
} else if (field === 'status') {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
result[field] = value;
if (value === 'Last Call') {
result[field] = EIPStatus.Last_Call;
}
} else if (field === 'created') {
result[field] = new Date(value);
} else if (field === 'last-call-deadline') {
result['last_call_deadline'] = new Date(value);
} else if (field === 'discussions-to') {
result['discussions_to'] = value;
} else if (field === 'withdrawal-reason') {
result['withdrawal_reason'] = value;
} else {
if (field === 'updated') {
} else {
result[field] = value;
}
}
}
}
return result;
}

async downloadErcs() {
const paths = './ETH-ERCS/';
deleteFolder(paths);
download(
'direct:https://github.com/ethereum/ERCs.git',
'ETH-ERCS',
{ clone: true },
() => console.log('下载完成'),
);
}

async updateEips() {
// eslint-disable-next-line @typescript-eslint/no-this-alias
const that = this;
Expand All @@ -192,10 +280,12 @@ export class AppService {
console.error(err);
return;
}
(function getFileMeta(i) {
(async function getFileMeta(i) {
console.log(i, files.length);

if (i === files.length) {
console.log('解析EIPS文件完成');
that.saveData(writeData);
await that.saveData(writeData);
console.log('写入DB完成');
} else {
fs.readFile(
Expand All @@ -206,85 +296,28 @@ export class AppService {
console.log('读取文件失败', err);
return;
}
const metaInfo = data.split('---')[1];
const content = data.split('---')[2];
const lines = metaInfo.split('\n');
//默认值填充
const result = <EIPs>{
eip: null,
title: '',
description: '',
status: null,
discussions_to: null,
author: '',
content: content,
extension_sub_title: null,
extension_short_read: null,
createdAt: new Date(),
updatedAt: new Date(),
type: null,
category: null,
requires: [],
created: null,
last_call_deadline: null,
withdrawal_reason: null,
};
for (let q = 0; q < lines.length; q++) {
const parts = lines[q].split(': ');
const field: string = parts[0];
let value: string = parts[1];
//兼容value中包含:的情况
if (parts.length > 2) {
value = parts.slice(1).join(': ');
}
if (field) {
if (field === 'eip') {
result['eip'] = value;
} else if (field === 'requires') {
result[field] = value.split(', ').map((eip) => {
return Number(eip);
});
} else if (field === 'title') {
result[field] = value.replace(/^\"|\"$/g, '');
} else if (field === 'type') {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
result[field] = value;
if (value === 'Standards Track') {
result[field] = EIPType.Standards_Track;
}
} else if (field === 'status') {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
result[field] = value;
if (value === 'Last Call') {
result[field] = EIPStatus.Last_Call;
}
} else if (field === 'created') {
result[field] = new Date(value);
} else if (field === 'last-call-deadline') {
result['last_call_deadline'] = new Date(value);
} else if (field === 'discussions-to') {
result['discussions_to'] = value;
} else if (field === 'withdrawal-reason') {
result['withdrawal_reason'] = value;
} else {
if (field === 'updated') {
} else {
result[field] = value;
}
}
}
const result = that.formateData(data);
if (result.status !== 'Moved') {
writeData.push(result);
} else {
const ercDirectory = './ETH-ERCS/ERCS/';
const id = files[i].split('-')[1];
const fsInfo = fs.readFileSync(
path.join(ercDirectory, `erc-${id}`),
'utf8',
);
const ercData = that.formateData(fsInfo);
writeData.push(ercData);
}
writeData.push(result);

getFileMeta(i + 1);
},
);
}
})(0);
});
} else {
console.log(err);
// console.log(err);
deleteFolder(paths);
}
},
Expand All @@ -297,15 +330,19 @@ export class AppService {
}

async saveData(writeData: EIPs[]) {
// console.log(writeData.length);
await this.prisma.eIPs.deleteMany({});
// for (const item of writeData) {
// console.log('item:', item);
// await this.prisma.eIPs.create({ data: item });
// }
await this.prisma.eIPs.createMany({
data: writeData,
});
try {
// console.log(writeData.length);
await this.prisma.eIPs.deleteMany({});
// for (const item of writeData) {
// console.log('item:', item);
// await this.prisma.eIPs.create({ data: item });
// }
await this.prisma.eIPs.createMany({
data: writeData,
});
} catch (err) {
console.log(err);
}
}

@Cron(CronExpression.EVERY_WEEK)
Expand Down
9 changes: 8 additions & 1 deletion tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
{
"extends": "./tsconfig.json",
"exclude": ["node_modules", "test", "dist", "**/*spec.ts","ETH-EIPs"]
"exclude": [
"node_modules",
"test",
"dist",
"**/*spec.ts",
"ETH-EIPs",
"ETH-ERCS"
]
}

0 comments on commit eebb80e

Please sign in to comment.