From efe62eb3036bf9a9e0931931b19a3f42cd1d50b3 Mon Sep 17 00:00:00 2001 From: syuilo Date: Sun, 6 Feb 2022 08:39:23 +0900 Subject: [PATCH] fix --- packages/backend/src/services/chart/core.ts | 22 ++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/packages/backend/src/services/chart/core.ts b/packages/backend/src/services/chart/core.ts index 192b0d142..b6db598cf 100644 --- a/packages/backend/src/services/chart/core.ts +++ b/packages/backend/src/services/chart/core.ts @@ -365,22 +365,22 @@ export default abstract class Chart { if (v > 0) queryForDay[name] = () => `"${name}" + ${v}`; if (v < 0) queryForDay[name] = () => `"${name}" - ${Math.abs(v)}`; } else if (Array.isArray(v) && v.length > 0) { // ユニークインクリメント - const name = uniqueTempColumnPrefix + k.replaceAll('.', columnDot); - // TODO: item が文字列以外の場合も対応 + const tempColumnName = uniqueTempColumnPrefix + k.replaceAll('.', columnDot); // TODO: item をSQLエスケープ - // TODO: 値が重複しないようにしたい - const items = v.map(item => `"${item}"`).join(','); - queryForHour[name] = () => `array_cat("${name}", '{${items}}'::varchar[])`; - queryForDay[name] = () => `array_cat("${name}", '{${items}}'::varchar[])`; + const itemsForHour = v.filter(item => !logHour[tempColumnName].includes(item)).map(item => `"${item}"`); + const itemsForDay = v.filter(item => !logDay[tempColumnName].includes(item)).map(item => `"${item}"`); + if (itemsForHour.length > 0) queryForHour[tempColumnName] = () => `array_cat("${tempColumnName}", '{${itemsForHour.join(',')}}'::varchar[])`; + if (itemsForDay.length > 0) queryForDay[tempColumnName] = () => `array_cat("${tempColumnName}", '{${itemsForDay.join(',')}}'::varchar[])`; } } - for (const [k, v] of Object.entries(this.schema)) { - const name = columnPrefix + k.replaceAll('.', columnDot); - if (v.uniqueIncrement) { + // bake unique count + for (const [k, v] of Object.entries(finalDiffs)) { + if (this.schema[k].uniqueIncrement) { + const name = columnPrefix + k.replaceAll('.', columnDot); const tempColumnName = uniqueTempColumnPrefix + k.replaceAll('.', columnDot); - queryForHour[name] = new Set([...finalDiffs[k], ...logHour[tempColumnName]]).size; - queryForDay[name] = new Set([...finalDiffs[k], ...logDay[tempColumnName]]).size; + queryForHour[name] = new Set([...v, ...logHour[tempColumnName]]).size; + queryForDay[name] = new Set([...v, ...logDay[tempColumnName]]).size; } }