chore: add prettier with config and format all files
This commit is contained in:
@@ -6,15 +6,15 @@ import { matchKeys } from '@/lib/localization';
|
||||
|
||||
export const GET = withAuth(async (req: NextRequest) => {
|
||||
const p = req.nextUrl.searchParams;
|
||||
const combinators = p.getAll('combinator');
|
||||
const combinators = p.getAll('combinator');
|
||||
const itemsWhitelist = p.getAll('item');
|
||||
const itemsBlacklist = p.getAll('exclude');
|
||||
const signalType = p.get('signal') ?? 'both';
|
||||
const from = p.get('from');
|
||||
const to = p.get('to');
|
||||
const useRegex = p.get('regex') === 'true';
|
||||
const orderBy = p.get('order_by') ?? 'value_asc';
|
||||
const limit = p.get('limit') ? parseInt(p.get('limit')!, 10) : null;
|
||||
const signalType = p.get('signal') ?? 'both';
|
||||
const from = p.get('from');
|
||||
const to = p.get('to');
|
||||
const useRegex = p.get('regex') === 'true';
|
||||
const orderBy = p.get('order_by') ?? 'value_asc';
|
||||
const limit = p.get('limit') ? parseInt(p.get('limit')!, 10) : null;
|
||||
|
||||
const conditions: string[] = [];
|
||||
const values: unknown[] = [];
|
||||
@@ -28,8 +28,8 @@ export const GET = withAuth(async (req: NextRequest) => {
|
||||
if (itemsWhitelist.length > 0) {
|
||||
if (useRegex) {
|
||||
const localeMap = getServerLocaleMap();
|
||||
const localeKeys = [...new Set(itemsWhitelist.flatMap(p => matchKeys(p, localeMap)))];
|
||||
const sqlPattern = itemsWhitelist.map(p => `(${p})`).join('|');
|
||||
const localeKeys = [...new Set(itemsWhitelist.flatMap((p) => matchKeys(p, localeMap)))];
|
||||
const sqlPattern = itemsWhitelist.map((p) => `(${p})`).join('|');
|
||||
const orConds = [`item_key ~* $${i++}`];
|
||||
values.push(sqlPattern);
|
||||
if (localeKeys.length > 0) {
|
||||
@@ -46,8 +46,8 @@ export const GET = withAuth(async (req: NextRequest) => {
|
||||
if (itemsBlacklist.length > 0) {
|
||||
if (useRegex) {
|
||||
const localeMap = getServerLocaleMap();
|
||||
const localeKeys = [...new Set(itemsBlacklist.flatMap(p => matchKeys(p, localeMap)))];
|
||||
const sqlPattern = itemsBlacklist.map(p => `(${p})`).join('|');
|
||||
const localeKeys = [...new Set(itemsBlacklist.flatMap((p) => matchKeys(p, localeMap)))];
|
||||
const sqlPattern = itemsBlacklist.map((p) => `(${p})`).join('|');
|
||||
const andConds = [`item_key !~* $${i++}`];
|
||||
values.push(sqlPattern);
|
||||
if (localeKeys.length > 0) {
|
||||
@@ -61,16 +61,24 @@ export const GET = withAuth(async (req: NextRequest) => {
|
||||
}
|
||||
}
|
||||
|
||||
if (from) { conditions.push(`real_time >= $${i++}`); values.push(new Date(from)); }
|
||||
if (to) { conditions.push(`real_time <= $${i++}`); values.push(new Date(to)); }
|
||||
if (from) {
|
||||
conditions.push(`real_time >= $${i++}`);
|
||||
values.push(new Date(from));
|
||||
}
|
||||
if (to) {
|
||||
conditions.push(`real_time <= $${i++}`);
|
||||
values.push(new Date(to));
|
||||
}
|
||||
|
||||
const where = conditions.length > 0 ? `WHERE ${conditions.join(' AND ')}` : '';
|
||||
|
||||
const valueCol = signalType === 'red' ? 'red' : 'green';
|
||||
const valueCol = signalType === 'red' ? 'red' : 'green';
|
||||
const selectCols =
|
||||
signalType === 'green' ? 'real_time, game_tick, combinator, item_key, green'
|
||||
: signalType === 'red' ? 'real_time, game_tick, combinator, item_key, red'
|
||||
: 'real_time, game_tick, combinator, item_key, green, red';
|
||||
signalType === 'green'
|
||||
? 'real_time, game_tick, combinator, item_key, green'
|
||||
: signalType === 'red'
|
||||
? 'real_time, game_tick, combinator, item_key, red'
|
||||
: 'real_time, game_tick, combinator, item_key, green, red';
|
||||
|
||||
if ((orderBy === 'delta_asc' || orderBy === 'delta_desc') && limit !== null) {
|
||||
const baseConditions: string[] = [];
|
||||
@@ -84,8 +92,8 @@ export const GET = withAuth(async (req: NextRequest) => {
|
||||
if (itemsWhitelist.length > 0) {
|
||||
if (useRegex) {
|
||||
const localeMap = getServerLocaleMap();
|
||||
const localeKeys = [...new Set(itemsWhitelist.flatMap(p => matchKeys(p, localeMap)))];
|
||||
const sqlPattern = itemsWhitelist.map(p => `(${p})`).join('|');
|
||||
const localeKeys = [...new Set(itemsWhitelist.flatMap((p) => matchKeys(p, localeMap)))];
|
||||
const sqlPattern = itemsWhitelist.map((p) => `(${p})`).join('|');
|
||||
const orConds = [`item_key ~* $${j++}`];
|
||||
baseValues.push(sqlPattern);
|
||||
if (localeKeys.length > 0) {
|
||||
@@ -101,8 +109,8 @@ export const GET = withAuth(async (req: NextRequest) => {
|
||||
if (itemsBlacklist.length > 0) {
|
||||
if (useRegex) {
|
||||
const localeMap = getServerLocaleMap();
|
||||
const localeKeys = [...new Set(itemsBlacklist.flatMap(p => matchKeys(p, localeMap)))];
|
||||
const sqlPattern = itemsBlacklist.map(p => `(${p})`).join('|');
|
||||
const localeKeys = [...new Set(itemsBlacklist.flatMap((p) => matchKeys(p, localeMap)))];
|
||||
const sqlPattern = itemsBlacklist.map((p) => `(${p})`).join('|');
|
||||
const andConds = [`item_key !~* $${j++}`];
|
||||
baseValues.push(sqlPattern);
|
||||
if (localeKeys.length > 0) {
|
||||
@@ -116,7 +124,7 @@ export const GET = withAuth(async (req: NextRequest) => {
|
||||
}
|
||||
}
|
||||
|
||||
const baseWhere = baseConditions.length > 0 ? `WHERE ${baseConditions.join(' AND ')}` : '';
|
||||
const baseWhere = baseConditions.length > 0 ? `WHERE ${baseConditions.join(' AND ')}` : '';
|
||||
const baseWhereAnd = baseConditions.length > 0 ? `AND ${baseConditions.join(' AND ')}` : '';
|
||||
|
||||
const deltaQuery = `
|
||||
@@ -158,21 +166,27 @@ export const GET = withAuth(async (req: NextRequest) => {
|
||||
const top = deltaResult.rows;
|
||||
if (top.length === 0) return NextResponse.json([]);
|
||||
|
||||
const seriesConditions = top.map((_, idx) =>
|
||||
`(combinator = $${i + idx * 2} AND item_key = $${i + idx * 2 + 1})`,
|
||||
const seriesConditions = top.map(
|
||||
(_, idx) => `(combinator = $${i + idx * 2} AND item_key = $${i + idx * 2 + 1})`,
|
||||
);
|
||||
const fullWhere = `${where ? where + ' AND' : 'WHERE'} (${seriesConditions.join(' OR ')})`;
|
||||
const orderCase = top.map((_, idx) =>
|
||||
`WHEN combinator = $${i + idx * 2} AND item_key = $${i + idx * 2 + 1} THEN ${idx}`,
|
||||
).join(' ');
|
||||
const orderCase = top
|
||||
.map(
|
||||
(_, idx) =>
|
||||
`WHEN combinator = $${i + idx * 2} AND item_key = $${i + idx * 2 + 1} THEN ${idx}`,
|
||||
)
|
||||
.join(' ');
|
||||
const result = await pool.query(
|
||||
`SELECT ${selectCols} FROM signals ${fullWhere} ORDER BY CASE ${orderCase} ELSE ${top.length} END, real_time ASC`,
|
||||
[...values, ...top.flatMap(r => [r.combinator, r.item_key])],
|
||||
[...values, ...top.flatMap((r) => [r.combinator, r.item_key])],
|
||||
);
|
||||
return NextResponse.json(result.rows);
|
||||
}
|
||||
|
||||
if ((orderBy === 'value_asc' || orderBy === 'value_desc' || orderBy === 'abs_desc') && limit !== null) {
|
||||
if (
|
||||
(orderBy === 'value_asc' || orderBy === 'value_desc' || orderBy === 'abs_desc') &&
|
||||
limit !== null
|
||||
) {
|
||||
const latestVals = await pool.query<{ combinator: string; item_key: string; val: number }>(
|
||||
`SELECT DISTINCT ON (combinator, item_key)
|
||||
combinator, item_key, ${valueCol} AS val
|
||||
@@ -182,23 +196,27 @@ export const GET = withAuth(async (req: NextRequest) => {
|
||||
);
|
||||
|
||||
let sorted = latestVals.rows;
|
||||
if (orderBy === 'value_asc') sorted = [...sorted].sort((a, b) => a.val - b.val);
|
||||
if (orderBy === 'value_asc') sorted = [...sorted].sort((a, b) => a.val - b.val);
|
||||
if (orderBy === 'value_desc') sorted = [...sorted].sort((a, b) => b.val - a.val);
|
||||
if (orderBy === 'abs_desc') sorted = [...sorted].sort((a, b) => Math.abs(b.val) - Math.abs(a.val));
|
||||
if (orderBy === 'abs_desc')
|
||||
sorted = [...sorted].sort((a, b) => Math.abs(b.val) - Math.abs(a.val));
|
||||
|
||||
const top = sorted.slice(0, limit);
|
||||
if (top.length === 0) return NextResponse.json([]);
|
||||
|
||||
const seriesConditions = top.map((_, idx) =>
|
||||
`(combinator = $${i + idx * 2} AND item_key = $${i + idx * 2 + 1})`,
|
||||
const seriesConditions = top.map(
|
||||
(_, idx) => `(combinator = $${i + idx * 2} AND item_key = $${i + idx * 2 + 1})`,
|
||||
);
|
||||
const fullWhere = `${where ? where + ' AND' : 'WHERE'} (${seriesConditions.join(' OR ')})`;
|
||||
const orderCase = top.map((_, idx) =>
|
||||
`WHEN combinator = $${i + idx * 2} AND item_key = $${i + idx * 2 + 1} THEN ${idx}`,
|
||||
).join(' ');
|
||||
const orderCase = top
|
||||
.map(
|
||||
(_, idx) =>
|
||||
`WHEN combinator = $${i + idx * 2} AND item_key = $${i + idx * 2 + 1} THEN ${idx}`,
|
||||
)
|
||||
.join(' ');
|
||||
const result = await pool.query(
|
||||
`SELECT ${selectCols} FROM signals ${fullWhere} ORDER BY CASE ${orderCase} ELSE ${top.length} END, real_time ASC`,
|
||||
[...values, ...top.flatMap(r => [r.combinator, r.item_key])],
|
||||
[...values, ...top.flatMap((r) => [r.combinator, r.item_key])],
|
||||
);
|
||||
return NextResponse.json(result.rows);
|
||||
}
|
||||
@@ -209,4 +227,4 @@ export const GET = withAuth(async (req: NextRequest) => {
|
||||
values,
|
||||
);
|
||||
return NextResponse.json(result.rows);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user