@things-factory/spc 8.0.0-beta.8 → 8.0.0

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.
@@ -0,0 +1,21 @@
1
+ /* EXPORT ENTITY TYPES */
2
+
3
+ /* IMPORT ENTITIES AND RESOLVERS */
4
+ import { entities as SpcChartEntities, resolvers as SpcChartResolvers, subscribers as SpcChartSubscribers } from './spc-chart'
5
+
6
+ export const entities = [
7
+ /* ENTITIES */
8
+ ...SpcChartEntities
9
+ ]
10
+
11
+ export const subscribers = [
12
+ /* SUBSCRIBERS */
13
+ ...SpcChartSubscribers
14
+ ]
15
+
16
+ export const schema = {
17
+ resolverClasses: [
18
+ /* RESOLVER CLASSES */
19
+ ...SpcChartResolvers
20
+ ]
21
+ }
@@ -0,0 +1,5 @@
1
+ import { SpcChartQuery } from './spc-chart-query'
2
+
3
+ export const entities = []
4
+ export const resolvers = [SpcChartQuery]
5
+ export const subscribers = []
@@ -0,0 +1,102 @@
1
+ import { Resolver, Query, Arg, Ctx } from 'type-graphql'
2
+ import { Between, In } from 'typeorm'
3
+
4
+ import { getRepository } from '@things-factory/shell'
5
+ import { User } from '@things-factory/auth-base'
6
+ import { DataSet, DataSample } from '@things-factory/dataset'
7
+
8
+ import { SPCChartAnalysis, SPCChartAnalysisResult } from './spc-chart-type'
9
+ import { calculateXBarAnalysisResult } from '../../controllers/spc-chart/x-bar'
10
+ import { calculateRChartAnalysisResult } from '../../controllers/spc-chart/r'
11
+ import { calculateIChartAnalysisResult } from '../../controllers/spc-chart/i'
12
+ import { calculateMRChartAnalysisResult } from '../../controllers/spc-chart/mr'
13
+ import { calculateUChartAnalysisResult } from '../../controllers/spc-chart/u'
14
+ import { calculateCChartAnalysisResult } from '../../controllers/spc-chart/c'
15
+ import { calculatePChartAnalysisResult } from '../../controllers/spc-chart/p'
16
+ import { calculateNPChartAnalysisResult } from '../../controllers/spc-chart/np'
17
+
18
+ @Resolver(DataSet)
19
+ export class SpcChartQuery {
20
+ @Query(returns => SPCChartAnalysisResult!, { nullable: true, description: 'To fetch a SpcChart' })
21
+ async spcChart(
22
+ @Arg('dataSetId') dataSetId: string,
23
+ @Arg('variable') variable: string,
24
+ @Arg('chartType') chartType: string,
25
+ @Arg('fromDate') fromDate: string,
26
+ @Arg('toDate') toDate: string,
27
+ @Ctx() context: ResolverContext
28
+ ): Promise<SPCChartAnalysisResult> {
29
+ const { domain } = context.state
30
+
31
+ const dataSet = (await getRepository(DataSet).findOne({
32
+ where: { domain: { id: In([domain.id, domain.parentId].filter(Boolean)) }, id: dataSetId }
33
+ })) as DataSet
34
+
35
+ if (!dataSet) {
36
+ throw 'no given dataset'
37
+ }
38
+
39
+ const dataItem = dataSet.dataItems.find(dataItem => dataItem.name == variable)
40
+
41
+ if (!dataItem) {
42
+ throw 'no given variables in the dataset'
43
+ }
44
+
45
+ // TODO timezone
46
+ const fromTime = new Date(fromDate) /* default: 30days before */
47
+ const toTime = new Date(toDate) /* default: today */
48
+
49
+ const dataSamples = (await getRepository(DataSample).find({
50
+ where: {
51
+ dataSet: { id: dataSet.id },
52
+ createdAt: Between(fromTime, toTime)
53
+ }
54
+ })) as DataSample[]
55
+
56
+ const tag = dataItem.tag
57
+ const samples = dataSamples
58
+ .map(dataSample => {
59
+ const data = dataSample.data[tag]
60
+
61
+ return {
62
+ x: dataSample.createdAt,
63
+ values: Array.isArray(data) ? data : [data]
64
+ }
65
+ })
66
+ .filter(sample => {
67
+ const { x, values } = sample
68
+ return x && values && values.length > 0 && values.every(v => v ?? false)
69
+ })
70
+
71
+ const charts = [] as SPCChartAnalysis[]
72
+
73
+ switch (chartType) {
74
+ case 'Xbar-R':
75
+ charts.push(calculateXBarAnalysisResult(samples as any))
76
+ charts.push(calculateRChartAnalysisResult(samples as any))
77
+ break
78
+ case 'I-MR':
79
+ charts.push(calculateIChartAnalysisResult(samples as any))
80
+ charts.push(calculateMRChartAnalysisResult(samples as any))
81
+ break
82
+ case 'C':
83
+ charts.push(calculateUChartAnalysisResult(samples as any))
84
+ break
85
+ case 'U':
86
+ charts.push(calculateCChartAnalysisResult(samples as any))
87
+ break
88
+ case 'P':
89
+ charts.push(calculatePChartAnalysisResult(samples as any))
90
+ break
91
+ case 'NP':
92
+ charts.push(calculateNPChartAnalysisResult(samples as any))
93
+ break
94
+ }
95
+
96
+ return {
97
+ dataSet,
98
+ variable,
99
+ charts
100
+ }
101
+ }
102
+ }
@@ -0,0 +1,105 @@
1
+ import { ObjectType, Field, ID, Int, Float } from 'type-graphql'
2
+
3
+ import { DataSet } from '@things-factory/dataset'
4
+
5
+ @ObjectType()
6
+ export class SPCChartPlot {
7
+ @Field(type => ID)
8
+ x: string
9
+
10
+ @Field(type => [Float], { nullable: true })
11
+ values?: number[]
12
+
13
+ @Field(type => Float, { nullable: true })
14
+ xbar?: number
15
+
16
+ @Field(type => Float, { nullable: true })
17
+ r?: number
18
+
19
+ @Field(type => Float, { nullable: true })
20
+ i?: number
21
+
22
+ @Field(type => Float, { nullable: true })
23
+ mr?: number
24
+
25
+ @Field(type => Float, { nullable: true })
26
+ n?: number
27
+
28
+ @Field(type => Float, { nullable: true })
29
+ defects?: number
30
+ }
31
+
32
+ @ObjectType()
33
+ class SPCControlLimits {
34
+ @Field(type => Float, { nullable: true })
35
+ ucl?: number
36
+
37
+ @Field(type => Float, { nullable: true })
38
+ lcl?: number
39
+
40
+ @Field(type => Float, { nullable: true })
41
+ cl?: number
42
+ }
43
+
44
+ @ObjectType()
45
+ class SPCSpecLimits {
46
+ @Field(type => Float, { nullable: true })
47
+ target?: number
48
+
49
+ @Field(type => Float, { nullable: true })
50
+ lsl?: number
51
+
52
+ @Field(type => Float, { nullable: true })
53
+ usl?: number
54
+ }
55
+
56
+ @ObjectType()
57
+ export class SPCChartAnalysis {
58
+ @Field()
59
+ chartType: string
60
+
61
+ @Field(type => SPCControlLimits, { nullable: true })
62
+ controlLimits?: SPCControlLimits
63
+
64
+ @Field(type => SPCSpecLimits, { nullable: true })
65
+ specLimits?: SPCSpecLimits
66
+
67
+ @Field(type => [SPCChartPlot], { nullable: true })
68
+ plots?: SPCChartPlot[]
69
+
70
+ // @Field(type => [Bin], { nullable: true })
71
+ // bins?: Bin[]
72
+
73
+ // @Field(type => [Category], { nullable: true })
74
+ // categories?: Category[]
75
+ }
76
+
77
+ // @ObjectType()
78
+ // class Bin {
79
+ // @Field()
80
+ // binRange: string
81
+
82
+ // @Field()
83
+ // count: number
84
+ // }
85
+
86
+ // @ObjectType()
87
+ // class Category {
88
+ // @Field()
89
+ // category: string
90
+
91
+ // @Field()
92
+ // count: number
93
+ // }
94
+
95
+ @ObjectType()
96
+ export class SPCChartAnalysisResult {
97
+ @Field(type => DataSet)
98
+ dataSet: DataSet
99
+
100
+ @Field()
101
+ variable: string
102
+
103
+ @Field(type => [SPCChartAnalysis])
104
+ charts: SPCChartAnalysis[]
105
+ }
@@ -0,0 +1,10 @@
1
+ {
2
+ "extends": "../../tsconfig-base.json",
3
+ "compilerOptions": {
4
+ "strict": false,
5
+ "module": "commonjs",
6
+ "outDir": "../dist-server",
7
+ "baseUrl": "./"
8
+ },
9
+ "include": ["./**/*"]
10
+ }