vault backup: 2023-11-06 14:45:54

This commit is contained in:
Paul Trowbridge 2023-11-06 14:45:54 -05:00
parent 48e3e2691e
commit dbac08f9f3

59
api.ts
View File

@ -32,38 +32,67 @@ await client.connect();
const query = await Deno.readTextFile("sql/get.pg.sql"); const query = await Deno.readTextFile("sql/get.pg.sql");
function apply_guidance(doc: any) { function apply_guidance(doc: any) {
let mostRelevantPrice = null;
let mostRelevantKey = null;
let highestRelevanceLevel = -1; // Assume -1 is less than any relevance level
// Iterate over each key in the "hist" object // Function to update price and assign relevance indicator
for (let key of Object.keys(doc.hist)) { function updatePriceAndAssignRelevance(items, channelFirstChar, v1ds, v0ds, histKey) {
// Update each item in the current key with the most recent price
updateWithMostRecentPrice(doc.hist[key]);
console.log(doc.chan[0]);
assignRelevanceIndicator(doc.hist[key], doc.chan[0], doc.v1ds, doc.v0ds);
}
return doc;
}
function assignRelevanceIndicator(items, channelFirstChar, v1ds, v0ds) {
for (let item of items) { for (let item of items) {
// Update the last_price with the most recent price
const years = Object.keys(item.season);
if (years.length > 0) {
const recentYear = Math.max(...years.map(Number));
const lastPrice = item.season[recentYear].price_usd;
item.last_price = lastPrice;
} else {
item.last_price = null; // or some default value as appropriate
}
// Initialize relevance as numeric value // Initialize relevance as numeric value
let relevance = 0; // Assume 0 is 'not relevant' let relevance = 0; // Assume 0 is 'not relevant'
// Check if the first character of the item's channel matches the first character of the document's channel // Check if the first character of the item's channel matches the first character of the document's channel
if (item.chan === channelFirstChar) { if (item.chan.charAt(0) === channelFirstChar) {
relevance = 1; // 'relevant' relevance = 1; // 'relevant'
// Further refine relevance based on v1ds and v0ds // Further refine relevance based on v1ds and v0ds
if (v1ds === item.v1ds && relevance === 1) { if (item.v1ds === v1ds) {
relevance = 2; // 'most relevant' because v1ds matches relevance = 2; // 'most relevant' because v1ds matches
} else if (v0ds === item.v0ds && relevance === 1) { } else if (item.v0ds === v0ds) {
// Keep relevance as 1 because v0ds matches but is less relevant than v1ds relevance = relevance === 2 ? 2 : 1; // Keep relevance as is if v1ds was matched, otherwise it's just 'relevant'
} }
} }
// Assign the calculated relevance to the item // Assign the calculated relevance to the item
item.relevance = relevance; item.relevance = relevance;
// Update the most relevant price if this item's relevance is higher
if (relevance > highestRelevanceLevel) {
highestRelevanceLevel = relevance;
mostRelevantPrice = item.last_price;
mostRelevantKey = histKey;
} }
} }
}
// Iterate over each key in the "hist" object
for (let key of Object.keys(doc.hist)) {
// Update price and relevance for each item in the current key
updatePriceAndAssignRelevance(doc.hist[key], doc.chan[0], doc.v1ds, doc.v0ds, key);
}
// Assign the most relevant price and key to the top level of the document
if (mostRelevantPrice !== null) {
doc.mostRelevantPriceInfo = {
price: mostRelevantPrice,
histKey: mostRelevantKey
};
}
return doc;
}
// Function to update each item with the most recent price // Function to update each item with the most recent price
function updateWithMostRecentPrice(items) { function updateWithMostRecentPrice(items) {