WIP usage info table

Might get replaced with gradio depending on how well it works
This commit is contained in:
DominikDoom
2023-12-04 15:00:19 +01:00
parent 1fe8f26670
commit 20b6635a2a
3 changed files with 56 additions and 10 deletions

View File

@@ -200,8 +200,17 @@ function calculateUsageBias(result, count, uses, lastUseDate) {
}
}
// Beautify return type for easier parsing
function mapUseCountArray(useCounts) {
function mapUseCountArray(useCounts, posAndNeg = false) {
return useCounts.map(useCount => {
if (posAndNeg) {
return {
"name": useCount[0],
"type": useCount[1],
"count": useCount[2],
"negCount": useCount[3],
"lastUseDate": useCount[4]
}
}
return {
"name": useCount[0],
"type": useCount[1],
@@ -224,14 +233,51 @@ async function getUseCounts(tagNames, types, negative = false) {
const rawArray = (await postAPI(`tacapi/v1/get-use-count-list`, body))["result"]
return mapUseCountArray(rawArray);
}
async function getAllUseCounts(negative = false) {
const rawArray = (await fetchAPI(`tacapi/v1/get-all-use-counts?neg=${negative}`))["result"];
return mapUseCountArray(rawArray);
async function getAllUseCounts() {
const rawArray = (await fetchAPI(`tacapi/v1/get-all-use-counts`))["result"];
return mapUseCountArray(rawArray, true);
}
async function resetUseCount(tagName, type, resetPosCount, resetNegCount) {
await putAPI(`tacapi/v1/reset-use-count?tagname=${tagName}&ttype=${type}&pos=${resetPosCount}&neg=${resetNegCount}`);
}
function createTagUsageTable(tagCounts) {
// Create table
let tagTable = document.createElement("table");
tagTable.innerHTML =
`<thead>
<tr>
<td>Name</td>
<td>Type</td>
<td>Count(+)</td>
<td>Count(-)</td>
<td>Last used</td>
</tr>
</thead>`;
tagTable.id = "tac_tagUsageTable"
tagCounts.forEach(t => {
let tr = document.createElement("tr");
// Fill values
let values = [t.name, t.type-1, t.count, t.negCount, t.lastUseDate]
values.forEach(v => {
let td = document.createElement("td");
td.innerText = v;
tr.append(td);
});
// Add delete/reset button
let delButton = document.createElement("button");
delButton.innerText = "🗑️";
delButton.title = "Reset count";
tr.append(delButton);
tagTable.append(tr)
});
return tagTable;
}
// Sliding window function to get possible combination groups of an array
function toNgrams(inputArray, size) {
return Array.from(

View File

@@ -634,7 +634,7 @@ def api_tac(_: gr.Blocks, app: FastAPI):
db_request(lambda: db.reset_tag_count(tagname, ttype, pos, neg))
@app.get("/tacapi/v1/get-all-use-counts")
async def get_all_tag_counts(neg: bool = False):
return db_request(lambda: db.get_all_tags(neg), get=True)
async def get_all_tag_counts():
return db_request(lambda: db.get_all_tags(), get=True)
script_callbacks.on_app_started(api_tac)

View File

@@ -86,14 +86,14 @@ class TagFrequencyDb:
return db_version[0] if db_version else 0
def get_all_tags(self, negative=False):
count_str = "count_neg" if negative else "count_pos"
def get_all_tags(self):
with transaction() as cursor:
cursor.execute(
f"""
SELECT name, type, {count_str}, last_used
SELECT name, type, count_pos, count_neg, last_used
FROM tag_frequency
ORDER BY {count_str} DESC
WHERE count_pos > 0 OR count_neg > 0
ORDER BY count_pos + count_neg DESC
"""
)
tags = cursor.fetchall()