Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/pir_pipeline/dashboard/review.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,11 @@ def link():
if action == "build":
data = payload["data"]
link_dict = session.get("link_dict")
dict_id = f"{data["base_question_id"]}{data.get("match_question_id", "") if data.get("match_question_id") else ""}"
dict_id = (
f"{data['base_question_id']}"
f"{data.get('match_question_id') or ''}"
f"{data.get('base_year') or ''}{data.get('match_year') or ''}"
)
dict_id = sha1(dict_id.encode("utf-8")).hexdigest()
if link_dict:
if (dict_id in link_dict) and (
Expand Down
1 change: 0 additions & 1 deletion src/pir_pipeline/dashboard/static/metrics/metrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ document.addEventListener("DOMContentLoaded", toggleActive())
function toggleActive() {
const links = document.querySelectorAll('.floating-nav-link');
for (let i = 0; i < links.length; i++) {
console.log(links[i]);
if (document.URL.includes(links[i].getAttribute("href"))) {
links.forEach(link => {
link.classList.remove("active");
Expand Down
22 changes: 19 additions & 3 deletions src/pir_pipeline/dashboard/static/search/search.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { updateTable, rowToJSON, updateFlashcardTables, storeLink, buildTable, expandContractRow, freezeQuestionTable } from "../utilities.js";
import { updateTable, rowToJSON, updateFlashcardTables, storeLink, buildTable, expandContractRow, freezeQuestionTable, getYearRange } from "../utilities.js";

// Constant forms and modals
const searchForm = document.getElementById("search-form");
Expand Down Expand Up @@ -104,9 +104,24 @@ async function commitChanges(e) {
})

// Commit changes to the database
const rows = [...questionTable.getElementsByTagName("tr")];
const [columns, ...records] = rows.map(rowToJSON);

// Recompute the summary row's year range from the individual-year rows
records[0]["year"] = getYearRange(records.slice(1).map(r => r["year"]));

delete columns["expand"];
delete columns["action"];

let dbHtml = questionTable.cloneNode(questionTable);
let tableDict = {
"columns": Object.keys(columns),
[records[0].question_id]: records
}
buildTable(tableDict, dbHtml);
payload = {
"action": "store",
"html": questionTable.outerHTML
"html": dbHtml.outerHTML
}

fetch("/review/link", {
Expand Down Expand Up @@ -143,4 +158,5 @@ document.getFlashcardData = getFlashcardData;
document.storeLink = storeLink;
document.commitChanges = commitChanges;
document.expandContractRow = expandContractRow;
document.freezeQuestionTable = freezeQuestionTable;
document.freezeQuestionTable = freezeQuestionTable;
document.getYearRange = getYearRange;
33 changes: 31 additions & 2 deletions src/pir_pipeline/dashboard/static/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function updateTable(event) {
* @returns JSON object containing data from a table row
*/
function rowToJSON(row) {
const cells = row.getElementsByTagName("td");
const cells = row.getElementsByTagName("td").length ? row.getElementsByTagName("td") : row.getElementsByTagName("th");
let record = {}
for (let i = 0; i < cells.length; i++) {
const cell = cells[i];
Expand Down Expand Up @@ -341,6 +341,16 @@ function storeLink(event) {
"match_question_id": matchRecord.question_id
}

// Only send a year for single-year (expanded) rows. A range/list (e.g.
// "2019-2021" or "2019, 2020") represents a whole question_id across years,
// which keeps the existing whole-question unlink behavior.
if (baseRecord.year && !baseRecord.year.match(/[,\-]/)) {
linkDetails.base_year = baseRecord.year;
}
if (matchRecord.year && !matchRecord.year.match(/[,\-]/)) {
linkDetails.match_year = matchRecord.year;
}

// Store the link
const payload = {
"action": "build",
Expand Down Expand Up @@ -450,12 +460,31 @@ function freezeQuestionTable(e) {

}

function getYearRange(years) {
const sorted = [...years].map(Number).sort((a, b) => a - b);

const groups = [];
for (const year of sorted) {
const run = groups.at(-1);
if (run && year === run.at(-1) + 1) {
run.push(year);
} else {
groups.push([year]);
}
}

return groups
.map(run => (run.length > 1 ? `${run[0]}-${run.at(-1)}` : `${run[0]}`))
.join(", ");
}

export {
buildTable,
updateTable,
rowToJSON,
updateFlashcardTables,
storeLink,
expandContractRow,
freezeQuestionTable
freezeQuestionTable,
getYearRange
}
6 changes: 2 additions & 4 deletions src/pir_pipeline/linking/PIRLinker.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Class for linking PIR data"""

import hashlib
from typing import Self

import numpy as np
Expand All @@ -10,7 +9,7 @@

from pir_pipeline.config import DB_CONFIG
from pir_pipeline.utils.SQLAlchemyUtils import SQLAlchemyUtils
from pir_pipeline.utils.utils import get_logger
from pir_pipeline.utils.utils import get_logger, new_uqid


class PIRLinker:
Expand Down Expand Up @@ -484,8 +483,7 @@ def gen_uqid(self, row: pd.Series) -> str | float:
if row["linked_id"] in uqid_dict:
return uqid_dict[row["linked_id"]]
else:
qids_concat = row["question_id"] + row["linked_id"]
uqid = hashlib.sha1(qids_concat.encode()).hexdigest()
uqid = new_uqid()
uqid_dict[row["question_id"]] = uqid
uqid_dict[row["linked_id"]] = uqid
return uqid
Expand Down
Loading