@things-factory/kpi 10.0.0-beta.95 → 10.0.0-beta.99
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,17 +1,23 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.KpiMetricValueProvider = void 0;
|
|
4
|
+
const typeorm_1 = require("typeorm");
|
|
4
5
|
const shell_1 = require("@things-factory/shell");
|
|
5
6
|
const kpi_metric_1 = require("../service/kpi-metric/kpi-metric");
|
|
6
7
|
const kpi_metric_value_1 = require("../service/kpi-metric-value/kpi-metric-value");
|
|
7
8
|
const value_date_util_1 = require("../service/utils/value-date-util");
|
|
9
|
+
const domain_inheritance_1 = require("../service/utils/domain-inheritance");
|
|
8
10
|
class KpiMetricValueProvider {
|
|
9
11
|
constructor(options) {
|
|
10
12
|
this.options = options;
|
|
11
13
|
}
|
|
12
14
|
async get(name) {
|
|
13
15
|
const metricRepo = (0, shell_1.getRepository)(kpi_metric_1.KpiMetric, this.options.tx);
|
|
14
|
-
|
|
16
|
+
// 현재 도메인 + 모든 조상 도메인 ID. KpiMetric 정의는 보통 부모(admin)에,
|
|
17
|
+
// KpiMetricValue 는 자식(tenant)에 저장되는 패턴 대응.
|
|
18
|
+
const domain = await (0, shell_1.getRepository)(shell_1.Domain, this.options.tx).findOne({ where: { id: this.options.domainId } });
|
|
19
|
+
const domainIds = domain ? await (0, domain_inheritance_1.getDomainIdsWithAncestors)(domain) : [this.options.domainId];
|
|
20
|
+
const metric = await metricRepo.findOne({ where: { name, domain: { id: (0, typeorm_1.In)(domainIds) } } });
|
|
15
21
|
if (!metric)
|
|
16
22
|
throw new Error(`Metric not found: ${name}`);
|
|
17
23
|
// ALLTIME 타입인 경우 valueDate 무시하고 최신 값 조회
|
|
@@ -21,7 +27,7 @@ class KpiMetricValueProvider {
|
|
|
21
27
|
where: {
|
|
22
28
|
metric: { id: metric.id },
|
|
23
29
|
org: this.options.org ?? '',
|
|
24
|
-
domain: { id:
|
|
30
|
+
domain: { id: (0, typeorm_1.In)(domainIds) }
|
|
25
31
|
},
|
|
26
32
|
order: { createdAt: 'DESC' }
|
|
27
33
|
});
|
|
@@ -50,7 +56,7 @@ class KpiMetricValueProvider {
|
|
|
50
56
|
metric: { id: metric.id },
|
|
51
57
|
valueDate,
|
|
52
58
|
org: this.options.org ?? '',
|
|
53
|
-
domain: { id:
|
|
59
|
+
domain: { id: (0, typeorm_1.In)(domainIds) }
|
|
54
60
|
}
|
|
55
61
|
});
|
|
56
62
|
if (!value) {
|
|
@@ -59,7 +65,7 @@ class KpiMetricValueProvider {
|
|
|
59
65
|
where: {
|
|
60
66
|
metric: { id: metric.id },
|
|
61
67
|
org: this.options.org ?? '',
|
|
62
|
-
domain: { id:
|
|
68
|
+
domain: { id: (0, typeorm_1.In)(domainIds) }
|
|
63
69
|
},
|
|
64
70
|
order: { valueDate: 'DESC' }
|
|
65
71
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"kpi-metric-value-provider.js","sourceRoot":"","sources":["../../server/controllers/kpi-metric-value-provider.ts"],"names":[],"mappings":";;;AAAA,
|
|
1
|
+
{"version":3,"file":"kpi-metric-value-provider.js","sourceRoot":"","sources":["../../server/controllers/kpi-metric-value-provider.ts"],"names":[],"mappings":";;;AAAA,qCAA4B;AAC5B,iDAA6D;AAC7D,iEAA4D;AAC5D,mFAA6E;AAE7E,sEAAsE;AACtE,4EAA+E;AAE/E,MAAa,sBAAsB;IACjC,YACU,OAKP;QALO,YAAO,GAAP,OAAO,CAKd;IACA,CAAC;IAEJ,KAAK,CAAC,GAAG,CAAC,IAAY;QACpB,MAAM,UAAU,GAAG,IAAA,qBAAa,EAAC,sBAAS,EAAE,IAAI,CAAC,OAAO,CAAC,EAAE,CAAC,CAAA;QAE5D,sDAAsD;QACtD,2CAA2C;QAC3C,MAAM,MAAM,GAAG,MAAM,IAAA,qBAAa,E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oBACzB,GAAG,EAAE,IAAI,CAAC,OAAO,CAAC,GAAG,IAAI,EAAE;oBAC3B,MAAM,EAAE,EAAE,EAAE,EAAE,IAAA,YAAE,EAAC,SAAS,CAAC,EAAE;iBAC9B;gBACD,KAAK,EAAE,EAAE,SAAS,EAAE,MAAM,EAAE;aAC7B,CAAC,CAAA;YAEF,IAAI,CAAC,KAAK,EAAE,CAAC;gBACX,MAAM,IAAI,KAAK,CACb,mCAAmC,IAAI,WAAW,IAAI,CAAC,OAAO,CAAC,GAAG,IAAI,EAAE,iBAAiB,SAAS,kBAAkB,MAAM,CAAC,UAAU,GAAG,CACzI,CAAA;YACH,CAAC;QACH,CAAC;QAED,OAAO,KAAK,CAAC,KAAK,CAAA;IACpB,CAAC;CACF;AA9ED,wDA8EC","sourcesContent":["import { In } from 'typeorm'\nimport { Domain, getRepository } from '@things-factory/shell'\nimport { KpiMetric } from '../service/kpi-metric/kpi-metric'\nimport { KpiMetricValue } from '../service/kpi-metric-value/kpi-metric-value'\nimport { ValueProvider } from '../calculator/provider'\nimport { getDefaultValueDate } from '../service/utils/value-date-util'\nimport { getDomainIdsWithAncestors } from '../service/utils/domain-inheritance'\n\nexport class KpiMetricValueProvider implements ValueProvider {\n constructor(\n private options: {\n valueDate: string\n org?: string\n domainId: string\n tx?: any\n }\n ) {}\n\n async get(name: string) {\n const metricRepo = getRepository(KpiMetric, this.options.tx)\n\n // 현재 도메인 + 모든 조상 도메인 ID. KpiMetric 정의는 보통 부모(admin)에,\n // KpiMetricValue 는 자식(tenant)에 저장되는 패턴 대응.\n const domain = await getRepository(Domain, this.options.tx).findOne({ where: { id: this.options.domainId } })\n const domainIds = domain ? await getDomainIdsWithAncestors(domain) : [this.options.domainId]\n\n const metric = await metricRepo.findOne({ where: { name, domain: { id: In(domainIds) } } })\n if (!metric) throw new Error(`Metric not found: ${name}`)\n\n // ALLTIME 타입인 경우 valueDate 무시하고 최신 값 조회\n if (metric.periodType === 'ALLTIME') {\n const valueRepo = getRepository(KpiMetricValue, this.options.tx)\n const value = await valueRepo.findOne({\n where: {\n metric: { id: metric.id },\n org: this.options.org ?? '',\n domain: { id: In(domainIds) }\n },\n order: { createdAt: 'DESC' }\n })\n if (!value) {\n throw new Error(`Metric value not found: metric='${name}', org='${this.options.org ?? ''}' (ALLTIME type)`)\n }\n return value.value\n }\n\n // metric의 periodType에 맞게 valueDate 보정\n let valueDate = this.options.valueDate\n if (!valueDate) {\n valueDate = getDefaultValueDate(metric.periodType, 'current')\n } else {\n // valueDate가 metric.periodType에 맞는 포맷인지 검사, 아니면 보정\n // (간단히: 길이로 구분, 실제는 정규식 등으로 더 정교하게 가능)\n if (metric.periodType === 'MONTH' && valueDate.length > 7) valueDate = valueDate.slice(0, 7)\n if (metric.periodType === 'DAY' && valueDate.length > 10) valueDate = valueDate.slice(0, 10)\n // 기타 periodType별 보정 추가 가능\n }\n const valueRepo = getRepository(KpiMetricValue, this.options.tx)\n var value = await valueRepo.findOne({\n where: {\n metric: { id: metric.id },\n valueDate,\n org: this.options.org ?? '',\n domain: { id: In(domainIds) }\n }\n })\n if (!value) {\n // 임시로 최신의 데이타 하나를 찾아오는 것으로 하자.\n value = await valueRepo.findOne({\n where: {\n metric: { id: metric.id },\n org: this.options.org ?? '',\n domain: { id: In(domainIds) }\n },\n order: { valueDate: 'DESC' }\n })\n\n if (!value) {\n throw new Error(\n `Metric value not found: metric='${name}', org='${this.options.org ?? ''}', valueDate='${valueDate}', periodType='${metric.periodType}'`\n )\n }\n }\n\n return value.value\n }\n}\n"]}
|