@things-factory/dataset 6.0.124 → 6.0.125

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@things-factory/dataset",
3
- "version": "6.0.124",
3
+ "version": "6.0.125",
4
4
  "main": "dist-server/index.js",
5
5
  "browser": "dist-client/index.js",
6
6
  "things-factory": true,
@@ -36,19 +36,19 @@
36
36
  "@operato/shell": "^1.0.1",
37
37
  "@operato/styles": "^1.0.0",
38
38
  "@operato/utils": "^1.0.1",
39
- "@things-factory/auth-base": "^6.0.124",
40
- "@things-factory/aws-base": "^6.0.124",
41
- "@things-factory/board-service": "^6.0.124",
39
+ "@things-factory/auth-base": "^6.0.125",
40
+ "@things-factory/aws-base": "^6.0.125",
41
+ "@things-factory/board-service": "^6.0.125",
42
42
  "@things-factory/env": "^6.0.124",
43
- "@things-factory/organization": "^6.0.124",
44
- "@things-factory/scheduler-client": "^6.0.124",
45
- "@things-factory/shell": "^6.0.124",
46
- "@things-factory/work-shift": "^6.0.124",
47
- "@things-factory/worklist": "^6.0.124",
43
+ "@things-factory/organization": "^6.0.125",
44
+ "@things-factory/scheduler-client": "^6.0.125",
45
+ "@things-factory/shell": "^6.0.125",
46
+ "@things-factory/work-shift": "^6.0.125",
47
+ "@things-factory/worklist": "^6.0.125",
48
48
  "cron-parser": "^4.3.0",
49
49
  "moment-timezone": "^0.5.40",
50
50
  "simple-statistics": "^7.8.3",
51
51
  "statistics": "^3.3.0"
52
52
  },
53
- "gitHead": "6841e3180fd0fb4ef7ee81c2a153cda80300e904"
53
+ "gitHead": "2d741c0c6ad8cf572560ac5bcc6fb7de470ede72"
54
54
  }
@@ -1,7 +1,13 @@
1
1
  import moment from 'moment-timezone'
2
2
  import { In } from 'typeorm'
3
3
 
4
- import { getQueryBuilderFromListParams, getRepository, ListParam, Sorting } from '@things-factory/shell'
4
+ import {
5
+ getQueryBuilderFromListParams,
6
+ getRepository,
7
+ getTimesForPeriod,
8
+ ListParam,
9
+ Sorting
10
+ } from '@things-factory/shell'
5
11
 
6
12
  import { DataSet, DataSetSummaryPeriodType } from '../service/data-set/data-set'
7
13
  import { DataSummary } from '../service/data-summary/data-summary'
@@ -18,41 +24,6 @@ const STAT_FUNCTION_MAP = {
18
24
  mode: 'mode'
19
25
  }
20
26
 
21
- async function getTimesForPeriod(
22
- period: 'today' | 'this month' | '30 days' | 'this year' | '12 months',
23
- context: ResolverContext
24
- ): Promise<{ from: string; to: string }> {
25
- const { domain } = context.state
26
- const theDate = moment.tz(new Date(), domain.timezone)
27
-
28
- if (period == 'today') {
29
- const from = theDate.clone().format('YYYY-MM-DD')
30
- const to = theDate.clone().add(1, 'day').startOf('day').format('YYYY-MM-DD')
31
-
32
- return { from, to }
33
- } else if (period == 'this month') {
34
- const from = theDate.clone().startOf('month').format('YYYY-MM-DD')
35
- const to = theDate.clone().add(1, 'month').startOf('month').format('YYYY-MM-DD')
36
-
37
- return { from, to }
38
- } else if (period == '30 days') {
39
- const from = theDate.clone().subtract(30, 'day').format('YYYY-MM-DD')
40
- const to = theDate.clone().add(1, 'day').startOf('day').format('YYYY-MM-DD')
41
-
42
- return { from, to }
43
- } else if (period == 'this year') {
44
- const from = theDate.clone().startOf('year').format('YYYY-MM-DD')
45
- const to = theDate.clone().add(1, 'year').startOf('year').format('YYYY-MM-DD')
46
-
47
- return { from, to }
48
- } else if (period == '12 months') {
49
- const from = theDate.clone().subtract(12, 'month').startOf('month').format('YYYY-MM-DD')
50
- const to = theDate.clone().add(1, 'month').startOf('month').format('YYYY-MM-DD')
51
-
52
- return { from, to }
53
- }
54
- }
55
-
56
27
  export async function queryDataSummaryByPeriod(
57
28
  period: 'today' | 'this month' | '30 days' | 'this year' | '12 months',
58
29
  dataSetName: string,
@@ -2,7 +2,13 @@ import { In } from 'typeorm'
2
2
  import { Arg, Args, Ctx, Directive, FieldResolver, Query, Resolver, Root } from 'type-graphql'
3
3
 
4
4
  import { User } from '@things-factory/auth-base'
5
- import { Domain, getQueryBuilderFromListParams, getRepository, ListParam } from '@things-factory/shell'
5
+ import {
6
+ Domain,
7
+ getQueryBuilderFromListParams,
8
+ getRepository,
9
+ getISOStringsForPeriod,
10
+ ListParam
11
+ } from '@things-factory/shell'
6
12
 
7
13
  import { DataKeySet } from '../data-key-set/data-key-set'
8
14
  import { DataSetHistory } from '../data-set-history/data-set-history'
@@ -104,6 +110,55 @@ export class DataSampleQuery {
104
110
  return { items, total }
105
111
  }
106
112
 
113
+ @Query(returns => DataSampleList, { description: 'To fetch multiple DataSamples by period' })
114
+ async dataSamplesByPeriod(
115
+ @Arg('period') period: 'today' | 'this month' | '30 days' | 'this year' | '12 months',
116
+ @Arg('dataSetName') dataSetName: string,
117
+ @Args() params: ListParam,
118
+ @Ctx() context: ResolverContext
119
+ ): Promise<DataSampleList> {
120
+ const { domain } = context.state
121
+ const { from, to } = await getISOStringsForPeriod(period, context)
122
+
123
+ const dataSet = await getRepository(DataSet).findOne({
124
+ where: { domain: { id: In([domain.id, domain.parentId].filter(Boolean)) }, name: dataSetName },
125
+ relations: ['dataKeySet']
126
+ })
127
+
128
+ if (!dataSet) {
129
+ throw new Error(`dataSet not found by the given dataSetName(${dataSetName})`)
130
+ }
131
+
132
+ const { filters = [] } = params || {}
133
+ const alteredFilters = filters.concat([
134
+ {
135
+ name: 'collectedAt',
136
+ operator: 'between',
137
+ value: [from, to]
138
+ }
139
+ ])
140
+
141
+ const dataKeyItems = dataSet.dataKeySet?.dataKeyItems || []
142
+ const searchables = dataKeyItems.map((item, index) => `key0${index + 1}`)
143
+
144
+ const queryBuilder = getQueryBuilderFromListParams({
145
+ repository: getRepository(DataSample),
146
+ params: {
147
+ ...params,
148
+ filters: alteredFilters
149
+ },
150
+ domain,
151
+ alias: 'sample',
152
+ searchables: ['name', 'description'].concat(searchables)
153
+ }).innerJoin('sample.dataSet', 'ds', 'ds.id = :dataSetId', {
154
+ dataSetId: dataSet.id
155
+ })
156
+
157
+ const [items, total] = await queryBuilder.getManyAndCount()
158
+
159
+ return { items, total }
160
+ }
161
+
107
162
  @FieldResolver(type => [DataItem])
108
163
  async dataItems(@Root() dataSample: DataSample): Promise<DataItem[]> {
109
164
  const dataSetHistory: DataSetHistory =