- Updated sidebar component to support dynamic width based on collapsed state. - Improved sidebar icon rendering and added user account management features. - Introduced new trend chart settings component for better user interaction. - Added utility functions for engineering units and trend chart settings. - Implemented color assignment logic for trend series to enhance visual representation. - Enhanced CSS styles for better responsiveness and usability across different screen sizes. - Introduced new configuration for Vite server to allow shared directory access. - Added .dockerignore file to optimize Docker build context.
93 lines
2.6 KiB
JavaScript
93 lines
2.6 KiB
JavaScript
import mysql from "mysql2/promise";
|
|
import { config } from "./config.js";
|
|
import { setUtcSession } from "../../shared/trend-time.mjs";
|
|
|
|
function createPool(password) {
|
|
const pool = mysql.createPool({
|
|
host: config.dbHost,
|
|
port: config.dbPort,
|
|
user: config.dbUser,
|
|
password,
|
|
database: config.dbName,
|
|
waitForConnections: true,
|
|
connectionLimit: 10,
|
|
namedPlaceholders: true,
|
|
multipleStatements: true,
|
|
charset: "utf8mb4",
|
|
timezone: "Z",
|
|
});
|
|
pool.on("connection", setUtcSession);
|
|
return pool;
|
|
}
|
|
|
|
const candidatePasswords = [...new Set([config.dbPassword, config.dbPasswordFallback].filter(Boolean))];
|
|
let activePassword = candidatePasswords[0];
|
|
let activePool = createPool(activePassword);
|
|
|
|
function isAccessDenied(error) {
|
|
return error?.code === "ER_ACCESS_DENIED_ERROR";
|
|
}
|
|
|
|
function isTransientConnectionError(error) {
|
|
return ["ECONNREFUSED", "ECONNRESET", "ETIMEDOUT", "PROTOCOL_CONNECTION_LOST"].includes(error?.code);
|
|
}
|
|
|
|
function delay(milliseconds) {
|
|
return new Promise((resolve) => setTimeout(resolve, milliseconds));
|
|
}
|
|
|
|
async function execute(pool, sql, values, isolated) {
|
|
if (!isolated) return pool.query(sql, values);
|
|
const connection = await pool.getConnection();
|
|
try {
|
|
return await connection.query(sql);
|
|
} finally {
|
|
connection.destroy();
|
|
}
|
|
}
|
|
|
|
async function queryWithCredentials(sql, values, isolated) {
|
|
try {
|
|
return await execute(activePool, sql, values, isolated);
|
|
} catch (error) {
|
|
if (!isAccessDenied(error) || candidatePasswords.length < 2) {
|
|
throw error;
|
|
}
|
|
|
|
for (const password of candidatePasswords) {
|
|
if (password === activePassword) continue;
|
|
const fallbackPool = createPool(password);
|
|
try {
|
|
const result = await execute(fallbackPool, sql, values, isolated);
|
|
const previousPool = activePool;
|
|
activePool = fallbackPool;
|
|
activePassword = password;
|
|
previousPool.end().catch(() => undefined);
|
|
return result;
|
|
} catch (fallbackError) {
|
|
await fallbackPool.end().catch(() => undefined);
|
|
if (!isAccessDenied(fallbackError)) throw fallbackError;
|
|
}
|
|
}
|
|
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async function query(sql, values) {
|
|
let lastError;
|
|
for (let attempt = 0; attempt < 5; attempt += 1) {
|
|
try {
|
|
return await queryWithCredentials(sql, values);
|
|
} catch (error) {
|
|
lastError = error;
|
|
if (!isTransientConnectionError(error) || attempt === 4) throw error;
|
|
await delay(250 * 2 ** attempt);
|
|
}
|
|
}
|
|
throw lastError;
|
|
}
|
|
|
|
export const pool = { query };
|
|
export const importSql = (sql) => queryWithCredentials(sql, undefined, true);
|