修正数据写入

This commit is contained in:
散仙
2024-11-26 02:29:11 +08:00
parent 9f51113734
commit 9b976ad402
3 changed files with 20 additions and 8 deletions

View File

@@ -7,8 +7,17 @@
导出接口 导出接口
```bash ```bash
# 安装依赖
npm install --registry=https://registry.npmmirror.com npm install --registry=https://registry.npmmirror.com
node main.js node main.js
# 连接数据库
ssh -NCPf ai -L 3306:localhost:3306
``` ```
```sql
-- 创建字段
ALTER TABLE web_images ADD COLUMN day_rank INT UNSIGNED;
ALTER TABLE web_images ADD COLUMN week_rank INT UNSIGNED;
ALTER TABLE web_images ADD COLUMN month_rank INT UNSIGNED;
```

View File

@@ -50,7 +50,7 @@ export default function () {
// 定时存档每10分钟 // 定时存档每10分钟
setInterval(() => { setInterval(() => {
archive('每10分钟自动存档...') archive('每10分钟自动存档...')
update('day_rank', Object.fromEntries(screenshots)) // 更新数据库中 day_rank 字段 update('day_rank', Object.entries(Object.fromEntries(screenshots))) // 更新数据库中 day_rank 字段
}, 600000) }, 600000)
// 实时读取日志文件 // 实时读取日志文件

View File

@@ -1,19 +1,22 @@
import mysql from 'mysql2/promise'
import fs from 'fs' import fs from 'fs'
import mysql from 'mysql2/promise'
export async function update(list, field) { export async function update(field, list) {
const { mysql: config } = JSON.parse(fs.readFileSync('config.json', 'utf8')) const { mysql: config } = JSON.parse(fs.readFileSync('config.json', 'utf8'))
const conn = await mysql.createConnection(config) const conn = await mysql.createConnection(config)
await conn.query(`CREATE TEMPORARY TABLE temp_updates (id INT PRIMARY KEY, ${field} INT)`) console.log('创建临时表')
await conn.query(`CREATE TEMPORARY TABLE temp_updates (id INT PRIMARY KEY, ${mysql.escapeId(field)} INT UNSIGNED)`)
console.log(`数据总量 ${list.length} 条, 每次 10000 条写入 ${Math.ceil(list.length/10000)}`)
for (let i = 0; i < list.length; i += 10000) { for (let i = 0; i < list.length; i += 10000) {
const values = list.slice(i, i + 10000).map(({ id, ...rest }) => [id, rest[field]]) console.log(`${i/10000+1} 次写入`)
const values = list.slice(i, i + 10000)
await conn.query(`INSERT INTO temp_updates (id, ${field}) VALUES ?`, [values]) await conn.query(`INSERT INTO temp_updates (id, ${field}) VALUES ?`, [values])
} }
console.log('更新数据从临时表转入生产库')
await conn.query(`UPDATE web_images t JOIN temp_updates tu ON t.id = tu.id SET t.${field} = tu.${field}`) await conn.query(`UPDATE web_images t JOIN temp_updates tu ON t.id = tu.id SET t.${field} = tu.${field}`)
console.log('更新完成')
await conn.end() await conn.end()
console.log('更新完成')
} }