Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ SELECT
END AS price1,
basic,
preferred,
is_extended_promotional,
category,
subcategory,
CASE
Expand Down Expand Up @@ -55,3 +56,5 @@ CREATE INDEX IF NOT EXISTS idx_search_index_basic ON search_index(basic);
CREATE INDEX IF NOT EXISTS idx_search_index_basic_stock ON search_index(basic, stock DESC);
CREATE INDEX IF NOT EXISTS idx_search_index_preferred ON search_index(preferred);
CREATE INDEX IF NOT EXISTS idx_search_index_preferred_stock ON search_index(preferred, stock DESC);
CREATE INDEX IF NOT EXISTS idx_search_index_is_extended_promotional ON search_index(is_extended_promotional);
CREATE INDEX IF NOT EXISTS idx_search_index_is_extended_promotional_stock ON search_index(is_extended_promotional, stock DESC);
10 changes: 10 additions & 0 deletions cf-proxy/scripts/sync-db.sh
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ SELECT
package,
basic,
preferred,
is_extended_promotional,
description,
stock,
price,
Expand All @@ -306,6 +307,7 @@ CREATE INDEX IF NOT EXISTS idx_component_catalog_subcategory ON component_catalo
CREATE INDEX IF NOT EXISTS idx_component_catalog_package ON component_catalog(package);
CREATE INDEX IF NOT EXISTS idx_component_catalog_basic ON component_catalog(basic);
CREATE INDEX IF NOT EXISTS idx_component_catalog_preferred ON component_catalog(preferred);
CREATE INDEX IF NOT EXISTS idx_component_catalog_is_extended_promotional ON component_catalog(is_extended_promotional);
CREATE INDEX IF NOT EXISTS idx_component_catalog_stock ON component_catalog(stock DESC);
COMPONENT_CATALOG_SCHEMA

Expand All @@ -319,6 +321,7 @@ CREATE TABLE component_catalog (
package TEXT,
basic INTEGER,
preferred INTEGER,
is_extended_promotional INTEGER,
description TEXT,
stock INTEGER,
price TEXT,
Expand All @@ -328,6 +331,7 @@ CREATE INDEX IF NOT EXISTS idx_component_catalog_subcategory ON component_catalo
CREATE INDEX IF NOT EXISTS idx_component_catalog_package ON component_catalog(package);
CREATE INDEX IF NOT EXISTS idx_component_catalog_basic ON component_catalog(basic);
CREATE INDEX IF NOT EXISTS idx_component_catalog_preferred ON component_catalog(preferred);
CREATE INDEX IF NOT EXISTS idx_component_catalog_is_extended_promotional ON component_catalog(is_extended_promotional);
CREATE INDEX IF NOT EXISTS idx_component_catalog_stock ON component_catalog(stock DESC);
COMPONENT_CATALOG_SCHEMA_EXPORT
}
Expand All @@ -350,6 +354,7 @@ SELECT
END AS price1,
basic,
preferred,
is_extended_promotional,
category,
subcategory,
CASE
Expand Down Expand Up @@ -391,6 +396,8 @@ CREATE INDEX IF NOT EXISTS idx_search_index_basic ON search_index(basic);
CREATE INDEX IF NOT EXISTS idx_search_index_basic_stock ON search_index(basic, stock DESC);
CREATE INDEX IF NOT EXISTS idx_search_index_preferred ON search_index(preferred);
CREATE INDEX IF NOT EXISTS idx_search_index_preferred_stock ON search_index(preferred, stock DESC);
CREATE INDEX IF NOT EXISTS idx_search_index_is_extended_promotional ON search_index(is_extended_promotional);
CREATE INDEX IF NOT EXISTS idx_search_index_is_extended_promotional_stock ON search_index(is_extended_promotional, stock DESC);
SEARCH_INDEX_SCHEMA

cat > search_index_schema.sql <<'SEARCH_INDEX_SCHEMA_EXPORT'
Expand All @@ -405,6 +412,7 @@ CREATE TABLE search_index (
price1 REAL,
basic INTEGER,
preferred INTEGER,
is_extended_promotional INTEGER,
category TEXT,
subcategory TEXT,
manufacturer_name TEXT,
Expand All @@ -422,6 +430,8 @@ CREATE INDEX IF NOT EXISTS idx_search_index_basic ON search_index(basic);
CREATE INDEX IF NOT EXISTS idx_search_index_basic_stock ON search_index(basic, stock DESC);
CREATE INDEX IF NOT EXISTS idx_search_index_preferred ON search_index(preferred);
CREATE INDEX IF NOT EXISTS idx_search_index_preferred_stock ON search_index(preferred, stock DESC);
CREATE INDEX IF NOT EXISTS idx_search_index_is_extended_promotional ON search_index(is_extended_promotional);
CREATE INDEX IF NOT EXISTS idx_search_index_is_extended_promotional_stock ON search_index(is_extended_promotional, stock DESC);
SEARCH_INDEX_SCHEMA_EXPORT
}

Expand Down
3 changes: 3 additions & 0 deletions cf-proxy/src/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface ComponentCatalogQueryParams {
search?: string
is_basic?: string
is_preferred?: string
is_extended_promotional?: string
}

export async function queryComponentCatalog(
Expand All @@ -22,6 +23,7 @@ export async function queryComponentCatalog(
package: string | null
basic: number | null
preferred: number | null
is_extended_promotional: number | null
description: string | null
stock: number | null
price: string | null
Expand All @@ -34,6 +36,7 @@ export async function queryComponentCatalog(
subcategory_name: params.subcategory_name,
is_basic: params.is_basic,
is_preferred: params.is_preferred,
is_extended_promotional: params.is_extended_promotional,
limit: "100",
})

Expand Down
2 changes: 2 additions & 0 deletions cf-proxy/src/db/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ export interface ComponentCatalog {
category: string | null
description: string | null
extra: string | null
is_extended_promotional: number | null
lcsc: Generated<number | null>
mfr: string | null
package: string | null
Expand Down Expand Up @@ -660,6 +661,7 @@ export interface SearchIndex {
basic: number | null
category: string | null
description: string | null
is_extended_promotional: number | null
lcsc: Generated<number | null>
mfr: string | null
manufacturer_name: string | null
Expand Down
2 changes: 2 additions & 0 deletions cf-proxy/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@ async function handleD1Search(
package: row.package ?? "",
is_basic: Boolean(row.basic),
is_preferred: Boolean(row.preferred),
is_extended_promotional: Boolean(row.is_extended_promotional),
description: row.description ?? "",
stock: row.stock ?? 0,
price: row.price1 ?? extractSmallQuantityPrice(row.price),
Expand Down Expand Up @@ -491,6 +492,7 @@ async function handleD1ComponentsList(
subcategory: row.subcategory ?? "",
is_basic: Boolean(row.basic),
is_preferred: Boolean(row.preferred),
is_extended_promotional: Boolean(row.is_extended_promotional),
})),
}

Expand Down
4 changes: 4 additions & 0 deletions cf-proxy/src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ const COLUMN_LABELS: Record<string, string> = {
in_stock: "In Stock",
is_basic: "Basic",
is_preferred: "Preferred",
is_extended_promotional: "Promotional Extended",
capacitance_farads: "Capacitance",
tolerance_fraction: "Tolerance",
voltage_rating: "Voltage",
Expand Down Expand Up @@ -546,6 +547,9 @@ const renderComponentsFilters = (
<div>
<label>Preferred Part:<input type="checkbox" name="is_preferred" value="true"${params.is_preferred === "true" ? " checked" : ""} /></label>
</div>
<div>
<label>Promotional Extended Part:<input type="checkbox" name="is_extended_promotional" value="true"${params.is_extended_promotional === "true" ? " checked" : ""} /></label>
</div>
<button type="submit">Filter</button>
</form>`

Expand Down
10 changes: 10 additions & 0 deletions cf-proxy/src/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface SearchQueryParams {
limit?: string
is_basic?: string
is_preferred?: string
is_extended_promotional?: string
}

interface SearchRow {
Expand All @@ -21,6 +22,7 @@ interface SearchRow {
price1: number | null
basic: number | null
preferred: number | null
is_extended_promotional: number | null
category: string | null
subcategory: string | null
}
Expand Down Expand Up @@ -110,6 +112,13 @@ export async function searchIndex(
conditions.push(sql`search_index.preferred = 1`)
}

if (
params.is_extended_promotional === "true" ||
params.is_extended_promotional === "1"
) {
conditions.push(sql`search_index.is_extended_promotional = 1`)
}

const raw = params.q?.trim()

if (raw) {
Expand Down Expand Up @@ -151,6 +160,7 @@ export async function searchIndex(
search_index.price1,
search_index.basic,
search_index.preferred,
search_index.is_extended_promotional,
search_index.category,
search_index.subcategory
FROM search_index
Expand Down
2 changes: 2 additions & 0 deletions lib/db/generated/kysely.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ export interface Component {
description: string;
extra: string | null;
flag: Generated<number>;
is_extended_promotional: Generated<number>;
joints: number;
last_on_stock: Generated<number>;
last_update: number;
Expand Down Expand Up @@ -802,6 +803,7 @@ export interface VComponent {
datasheet: string | null;
description: string | null;
extra: string | null;
is_extended_promotional: number | null;
joints: number | null;
last_on_stock: number | null;
lcsc: number | null;
Expand Down
54 changes: 54 additions & 0 deletions lib/db/optimizations/component-extended-promotional-column.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { sql } from "kysely"
import type { KyselyDatabaseInstance } from "../kysely-types"
import type { DbOptimizationSpec } from "./types"

const isExtendedPromotionalSql = sql`
CASE
WHEN COALESCE(basic, 0) = 0
AND COALESCE(preferred, 0) = 0
AND extra IS NOT NULL
AND (
LOWER(extra) LIKE '%promotional extended%'
OR LOWER(extra) LIKE '%extended promotional%'
OR LOWER(extra) LIKE '%promotional_extended%'
OR LOWER(extra) LIKE '%extended_promotional%'
OR LOWER(extra) LIKE '%promotionalextended%'
OR LOWER(extra) LIKE '%extendedpromotional%'
)
THEN 1
ELSE 0
END
`

export const componentExtendedPromotionalColumn: DbOptimizationSpec = {
name: "add_components_is_extended_promotional_column",
description:
"Adds is_extended_promotional boolean column to components from source metadata",

async checkIfAdded(db: KyselyDatabaseInstance) {
const result = await sql`
SELECT name FROM pragma_table_info('components')
WHERE name = 'is_extended_promotional'
`.execute(db)

return result.rows.length > 0
},

async execute(db: KyselyDatabaseInstance) {
await sql`
ALTER TABLE components
ADD COLUMN is_extended_promotional INTEGER NOT NULL DEFAULT 0
`.execute(db)

await sql`
UPDATE components
SET is_extended_promotional = ${isExtendedPromotionalSql}
`.execute(db)

await db.schema
.createIndex("idx_components_is_extended_promotional")
.on("components")
.column("is_extended_promotional")
.execute()
},
}
74 changes: 74 additions & 0 deletions lib/util/is-extended-promotional.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
const PROMOTIONAL_EXTENDED_PATTERNS = [
"promotional extended",
"extended promotional",
"promotional_extended",
"extended_promotional",
]

const PROMOTIONAL_EXTENDED_BOOLEAN_KEYS = new Set([
"is_extended_promotional",
"isExtendedPromotional",
"extendedPromotional",
"promotionalExtended",
"promotional_extended",
"extended_promotional",
"promotionalExtendedFlag",
"promotionExtendedFlag",
])

const valueLooksPromotionalExtended = (value: unknown): boolean => {
if (typeof value === "boolean") return value
if (typeof value === "number") return value === 1
if (typeof value !== "string") return false

const normalized = value
.toLowerCase()
.replace(/[-_/]+/g, " ")
.trim()
return PROMOTIONAL_EXTENDED_PATTERNS.some((pattern) =>
normalized.includes(pattern.replace(/[-_/]+/g, " ")),
)
}

const hasPromotionalExtendedMarker = (value: unknown): boolean => {
if (!value || typeof value !== "object") {
return typeof value === "string" && valueLooksPromotionalExtended(value)
}
if (Array.isArray(value)) return value.some(hasPromotionalExtendedMarker)

for (const [key, entryValue] of Object.entries(value)) {
if (
PROMOTIONAL_EXTENDED_BOOLEAN_KEYS.has(key) &&
valueLooksPromotionalExtended(entryValue)
) {
return true
}

if (
valueLooksPromotionalExtended(key) &&
valueLooksPromotionalExtended(entryValue)
) {
return true
}

if (hasPromotionalExtendedMarker(entryValue)) {
return true
}
}

return false
}

export const isExtendedPromotionalComponent = (
extra: string | null | undefined,
basic?: number | boolean | null,
preferred?: number | boolean | null,
): boolean => {
if (Boolean(basic) || Boolean(preferred) || !extra) return false

try {
return hasPromotionalExtendedMarker(JSON.parse(extra))
} catch {
return valueLooksPromotionalExtended(extra)
}
}
5 changes: 5 additions & 0 deletions routes/api/search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export default withWinterSpec({
limit: z.string().optional(),
is_basic: z.boolean().optional(),
is_preferred: z.boolean().optional(),
is_extended_promotional: z.boolean().optional(),
}),
jsonResponse: z.any(),
} as const)(async (req, ctx) => {
Expand All @@ -72,6 +73,9 @@ export default withWinterSpec({
if (req.query.is_preferred) {
query = query.where("preferred", "=", 1)
}
if (req.query.is_extended_promotional) {
query = query.where("is_extended_promotional", "=", 1)
}

const baseQuery = query
let fallbackLikeTokens: string[] = []
Expand Down Expand Up @@ -193,6 +197,7 @@ export default withWinterSpec({
package: c.package,
is_basic: Boolean(c.basic),
is_preferred: Boolean(c.preferred),
is_extended_promotional: Boolean(c.is_extended_promotional),
description: c.description,
stock: c.stock,
price: extractSmallQuantityPrice(c.price),
Expand Down
Loading
Loading