-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.js
120 lines (97 loc) · 2.65 KB
/
api.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import express from "express";
import cors from "cors";
import fs from "node:fs/promises";
import path from "node:path";
import Knex from "knex";
export async function createApp(datasetPath, dbPath) {
const knex = Knex({
client: "better-sqlite3",
connection: {
filename: dbPath,
},
useNullAsDefault: true,
});
const Change = () => knex("changes");
if (!(await knex.schema.hasTable("changes"))) {
await knex.schema.createTable("changes", (t) => {
t.increments("id").primary();
t.string("filename", 1024).unique().notNullable();
t.string("text", 2048).notNullable();
t.string("status", 16).defaultTo("normal").notNullable();
t.timestamps(true, true);
});
}
async function readMetadata(datasetDir) {
const metadataFile = path.join(datasetDir, "metadata.tsv");
const metadata = await fs.readFile(metadataFile, "utf8");
const values = metadata
.split(/[\n\r]+/)
.map((row) => row.split("\t").slice(0, 2));
return values;
}
// http server
const app = express();
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
app.use(cors());
app.use("/api/static", express.static(datasetPath));
app.post("/api/update", async (req, res) => {
const { text, filename, status } = req.body;
const currentItem = await Change().where("filename", filename).first();
if (currentItem == null) {
const ids = await Change().insert({
text,
filename,
status,
});
res.json(await Change().where("id", ids[0]).first());
return;
}
await Change()
.update({
text,
filename,
status,
updated_at: knex.fn.now(),
})
.where("id", currentItem.id);
res.json(await Change().where("id", currentItem.id).first());
});
app.get("/api/values", async (req, res) => {
const changes = await Change();
const map = new Map(changes.map((change) => [change.filename, change]));
const values = [];
const data = await readMetadata(datasetPath);
for (const item of data) {
const file = path.join("api", "static", "wavs", item[0]);
if (map.has(item[0])) {
values.push({
...map.get(item[0]),
file,
text_src: item[1],
});
continue;
}
values.push({
filename: item[0],
file,
text: item[1],
});
}
res.json(values);
});
app.get("/api/summary", async (req, res) => {
const data = await readMetadata(datasetPath);
const changes = await Change()
.groupBy("status")
.select("status")
.count("* as total");
const remaining = changes.reduce((prev, cur) => prev + cur.total, 0);
res.json({
total: data.length,
...Object.fromEntries(changes.map((item) => [item.status, item.total])),
remaining,
});
});
return app;
}