SPB Git

Repository Public

source.ts 3.9 KB 101 lines typescript

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : UQO Éval — Le Grand Défi Immobilier8 * File             : apps/api/src/source.ts9 * Module           : api10 * Repository       : https://git.spboucher.ai/uqo-eval-jeux11 * Project Website  : https://jeux.uqo-eval.app12 * Created          : 2026-11-2513 * Last Modified    : 2026-11-2514 *15 * Author           : Simon-Pierre Boucher16 * Position         : Professor of Finance17 * Institution      : Université du Québec en Outaouais (UQO)18 * Organization     : SP Boucher19 * Website          : https://spboucher.ai20 * Git              : https://git.spboucher.ai21 *22 * Preferred Contact: contact@spboucher.ai23 * Development      : dev@spboucher.ai24 * Support          : support@spboucher.ai25 * Security/Admin   : admin@spboucher.ai26 *27 * Copyright (c) 202628 * Simon-Pierre Boucher29 * All rights reserved where applicable. This file is governed by the30 * applicable project license, copyright notices and relevant law.31 * Existing third-party rights and notices remain unaffected.32 *33 * License          : None specified (no license file in this repository)34 *35 * Do not embed credentials, private keys, access tokens,36 * certificates or confidential personal information.37 *38 * =============================================================================39 * END OF SP BOUCHER SOURCE HEADER40 * =============================================================================41 */4243/**44 * ListingDataProvider — couche d'accès aux inscriptions immobilières.45 *46 * Accès strictement en lecture seule : la base source n'est jamais modifiée.47 * Toute autre source (API, autre base) peut être branchée en implémentant48 * la même interface.49 */50import fs from "node:fs";51import { DatabaseSync } from "node:sqlite";52import type { RawListing, RawPricePoint } from "@jeux/engine";5354export interface ListingBatch {55  listing: RawListing;56  priceLog: RawPricePoint[];57}5859export interface ListingDataProvider {60  /** Nombre d'inscriptions candidates. */61  count(): number;62  /** Parcourt les inscriptions actives, avec leur historique de prix. */63  iterate(): IterableIterator<ListingBatch>;64  close(): void;65}6667const COLUMNS = [68  "uid", "source", "title", "address", "sector", "city", "region", "property_type", "price", "bedrooms", "bathrooms",69  "powder_rooms", "area_sqft", "lot_sqft", "year_built", "mls", "description", "details", "images", "lat", "lng",70  "first_seen", "last_seen", "updated_at", "vraiprix as assessment_json", "quality_score", "dup_hidden", "dup_of", "active", "published",71].join(", ");7273export class SqliteListingProvider implements ListingDataProvider {74  private db: DatabaseSync;7576  constructor(file: string) {77    if (!fs.existsSync(file)) throw new Error(`Base d'inscriptions introuvable : ${file}`);78    this.db = new DatabaseSync(file, { readOnly: true });79    this.db.exec("pragma query_only = on; pragma busy_timeout = 15000");80  }8182  count(): number {83    const row = this.db.prepare("select count(*) as n from listings where active = 1 and price > 0").get() as { n: number };84    return row.n;85  }8687  *iterate(): IterableIterator<ListingBatch> {88    const hasLog = this.db.prepare("select 1 from sqlite_master where type = 'table' and name = 'price_log'").get();89    const logs = hasLog ? this.db.prepare("select ts, price from price_log where uid = ? order by ts") : null;90    const rows = this.db.prepare(`select ${COLUMNS} from listings where active = 1 and price > 0`).iterate();91    for (const row of rows) {92      const listing = row as unknown as RawListing;93      yield { listing, priceLog: logs ? (logs.all(listing.uid) as unknown as RawPricePoint[]) : [] };94    }95  }9697  close(): void {98    this.db.close();99  }100}101
Files main
Menu

Theme