Update: 2026-07-09 01:01:15
This commit is contained in:
@@ -129,6 +129,46 @@ export async function saveFormulas(
|
||||
await pool.execute(sql, flatParams);
|
||||
}
|
||||
|
||||
/**
|
||||
* لقطة insert-only من معاملات كل معادلة — لا تُستبدل أبداً، بعكس
|
||||
* competitor_secret_formulas (UPSERT). هاي المصدر اللي يقارن عليه
|
||||
* محرك الثبات (cron_pricing_stability_engine.php) "المعامل اليوم
|
||||
* مقابل المعامل قبل أسبوع" لتمييز التغيير الحقيقي عن البرومو المؤقت.
|
||||
*/
|
||||
export async function saveFormulaHistory(
|
||||
pool: mysql.Pool,
|
||||
formulas: Array<{
|
||||
competitorName: string;
|
||||
countryCode: string;
|
||||
tier: string;
|
||||
baseFare: number;
|
||||
kmRate: number;
|
||||
minRate: number;
|
||||
minFare: number;
|
||||
rSquared: number;
|
||||
sampleCount: number;
|
||||
}>
|
||||
): Promise<void> {
|
||||
if (formulas.length === 0) return;
|
||||
|
||||
const values = formulas.map(() => `(?, ?, ?, ?, ?, ?, ?, ?, ?, NOW())`).join(',');
|
||||
const flatParams: (string | number)[] = [];
|
||||
|
||||
for (const f of formulas) {
|
||||
flatParams.push(
|
||||
f.competitorName, f.countryCode, f.tier,
|
||||
f.baseFare, f.kmRate, f.minRate, f.minFare,
|
||||
f.rSquared, f.sampleCount
|
||||
);
|
||||
}
|
||||
|
||||
const sql = `INSERT INTO competitor_formula_history
|
||||
(competitor_name, country_code, tier, base_fare, price_per_km, price_per_min, min_fare, r_squared, sample_size, snapshotted_at)
|
||||
VALUES ${values}`;
|
||||
|
||||
await pool.execute(sql, flatParams);
|
||||
}
|
||||
|
||||
export async function saveSurgeInsights(
|
||||
pool: mysql.Pool,
|
||||
insights: Array<{
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
* Cron integration: see crontab examples in package.json scripts
|
||||
*/
|
||||
|
||||
import { getMySQL, fetchSamples, saveFormulas, saveSurgeInsights, saveSurgeZones, closeConnections } from './db/connection';
|
||||
import { getMySQL, fetchSamples, saveFormulas, saveFormulaHistory, saveSurgeInsights, saveSurgeZones, closeConnections } from './db/connection';
|
||||
import { runAnalysis } from './analysis/engine';
|
||||
import { Pool, RowDataPacket } from 'mysql2/promise';
|
||||
|
||||
@@ -236,6 +236,19 @@ async function processCompetitor(
|
||||
if (peakHours.length > 0) {
|
||||
console.log(` Peak hours stored: [${peakHours.join(', ')}]`);
|
||||
}
|
||||
|
||||
// لقطة insert-only لمحرك الثبات (drift detection) — لا تُستبدل أبداً
|
||||
await saveFormulaHistory(pool, formulas.map(f => ({
|
||||
competitorName: f.competitorName,
|
||||
countryCode: f.countryCode,
|
||||
tier: f.tier,
|
||||
baseFare: f.baseFare,
|
||||
kmRate: f.kmRate,
|
||||
minRate: f.minRate,
|
||||
minFare: f.minFare,
|
||||
rSquared: f.rSquared,
|
||||
sampleCount: f.sampleCount,
|
||||
})));
|
||||
}
|
||||
|
||||
// --- Save surge insights ---
|
||||
|
||||
Reference in New Issue
Block a user