@chevre/domain 20.2.0-alpha.17 → 20.2.0-alpha.18

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,87 @@
1
+ // tslint:disable:no-console
2
+ import * as moment from 'moment-timezone';
3
+ import * as mongoose from 'mongoose';
4
+
5
+ import { chevre } from '../../../../lib/index';
6
+
7
+ // const project = { id: <string>process.env.PROJECT_ID };
8
+ const EXCLUDED_PROJECT_ID = process.env.EXCLUDED_PROJECT_ID;
9
+ const AGGREGATE_DAYS = Number(process.env.AGGREGATE_DAYS);
10
+
11
+ interface IAggregation {
12
+ typeOf: 'AggregateAuthorizeEventServiceOfferAction';
13
+ project: { id: string; typeOf: chevre.factory.organizationType.Project };
14
+ aggregateDate: Date;
15
+ aggregateDuration: string;
16
+ aggregateStart: Date;
17
+ }
18
+
19
+ async function main() {
20
+ await mongoose.connect(<string>process.env.MONGOLAB_URI, { autoIndex: false });
21
+
22
+ const now: Date = new Date();
23
+
24
+ const aggregationRepo = new chevre.repository.Aggregation(mongoose.connection);
25
+ const actionRepo = new chevre.repository.Action(mongoose.connection);
26
+
27
+ let i = 0;
28
+ // tslint:disable-next-line:no-magic-numbers
29
+ while (i < AGGREGATE_DAYS) {
30
+ i += 1;
31
+
32
+ const startFrom: Date = moment()
33
+ // .tz('Asia/Tokyo')
34
+ .add(-i, 'days')
35
+ .startOf('day')
36
+ .toDate();
37
+ const startThrough: Date = moment()
38
+ // .tz('Asia/Tokyo')
39
+ .add(-i, 'days')
40
+ .endOf('day')
41
+ .toDate();
42
+ const aggregateResult = await actionRepo.aggregateAuthorizeEventServiceOfferAction({
43
+ project: { id: <any>{ $ne: EXCLUDED_PROJECT_ID } },
44
+ startFrom,
45
+ startThrough,
46
+ typeOf: chevre.factory.actionType.AuthorizeAction
47
+ });
48
+ console.log('aggregateResult', aggregateResult, aggregateResult.statuses[0]?.aggregation.percentilesDuration, startFrom);
49
+
50
+ const aggregation: IAggregation = {
51
+ typeOf: 'AggregateAuthorizeEventServiceOfferAction',
52
+ project: { id: '*', typeOf: chevre.factory.organizationType.Project },
53
+ aggregateDuration: moment.duration(1, 'days')
54
+ .toISOString(),
55
+ aggregateStart: startFrom,
56
+ aggregateDate: now,
57
+ ...aggregateResult
58
+ };
59
+ const { typeOf, project, aggregateDuration, aggregateStart, ...setFields } = aggregation;
60
+ const doc = await aggregationRepo.aggregationModel.findOneAndUpdate(
61
+ {
62
+ typeOf: { $eq: aggregation.typeOf },
63
+ 'project.id': { $eq: aggregation.project.id },
64
+ aggregateStart: { $eq: aggregation.aggregateStart },
65
+ aggregateDuration: { $eq: aggregation.aggregateDuration }
66
+ },
67
+ {
68
+ $setOnInsert: {
69
+ typeOf: aggregation.typeOf,
70
+ project: aggregation.project,
71
+ aggregateDuration: aggregation.aggregateDuration,
72
+ aggregateStart: aggregation.aggregateStart
73
+ },
74
+ $set: setFields
75
+ },
76
+ { upsert: true, new: true }
77
+ )
78
+ .exec();
79
+ console.log(doc);
80
+ }
81
+ }
82
+
83
+ main()
84
+ .then(() => {
85
+ console.log('success!');
86
+ })
87
+ .catch(console.error);
@@ -0,0 +1,87 @@
1
+ // tslint:disable:no-console
2
+ import * as moment from 'moment-timezone';
3
+ import * as mongoose from 'mongoose';
4
+
5
+ import { chevre } from '../../../../lib/index';
6
+
7
+ // const project = { id: <string>process.env.PROJECT_ID };
8
+ const EXCLUDED_PROJECT_ID = process.env.EXCLUDED_PROJECT_ID;
9
+ const AGGREGATE_DAYS = Number(process.env.AGGREGATE_DAYS);
10
+
11
+ interface IAggregation {
12
+ typeOf: 'AggregateAuthorizePaymentAction';
13
+ project: { id: string; typeOf: chevre.factory.organizationType.Project };
14
+ aggregateDate: Date;
15
+ aggregateDuration: string;
16
+ aggregateStart: Date;
17
+ }
18
+
19
+ async function main() {
20
+ await mongoose.connect(<string>process.env.MONGOLAB_URI, { autoIndex: false });
21
+
22
+ const now: Date = new Date();
23
+
24
+ const aggregationRepo = new chevre.repository.Aggregation(mongoose.connection);
25
+ const actionRepo = new chevre.repository.Action(mongoose.connection);
26
+
27
+ let i = 0;
28
+ // tslint:disable-next-line:no-magic-numbers
29
+ while (i < AGGREGATE_DAYS) {
30
+ i += 1;
31
+
32
+ const startFrom: Date = moment()
33
+ // .tz('Asia/Tokyo')
34
+ .add(-i, 'days')
35
+ .startOf('day')
36
+ .toDate();
37
+ const startThrough: Date = moment()
38
+ // .tz('Asia/Tokyo')
39
+ .add(-i, 'days')
40
+ .endOf('day')
41
+ .toDate();
42
+ const aggregateResult = await actionRepo.aggregateAuthorizePaymentAction({
43
+ project: { id: <any>{ $ne: EXCLUDED_PROJECT_ID } },
44
+ startFrom,
45
+ startThrough,
46
+ typeOf: chevre.factory.actionType.AuthorizeAction
47
+ });
48
+ console.log('aggregateResult', aggregateResult, aggregateResult.statuses[0]?.aggregation.percentilesDuration, startFrom);
49
+
50
+ const aggregation: IAggregation = {
51
+ typeOf: 'AggregateAuthorizePaymentAction',
52
+ project: { id: '*', typeOf: chevre.factory.organizationType.Project },
53
+ aggregateDuration: moment.duration(1, 'days')
54
+ .toISOString(),
55
+ aggregateStart: startFrom,
56
+ aggregateDate: now,
57
+ ...aggregateResult
58
+ };
59
+ const { typeOf, project, aggregateDuration, aggregateStart, ...setFields } = aggregation;
60
+ const doc = await aggregationRepo.aggregationModel.findOneAndUpdate(
61
+ {
62
+ typeOf: { $eq: aggregation.typeOf },
63
+ 'project.id': { $eq: aggregation.project.id },
64
+ aggregateStart: { $eq: aggregation.aggregateStart },
65
+ aggregateDuration: { $eq: aggregation.aggregateDuration }
66
+ },
67
+ {
68
+ $setOnInsert: {
69
+ typeOf: aggregation.typeOf,
70
+ project: aggregation.project,
71
+ aggregateDuration: aggregation.aggregateDuration,
72
+ aggregateStart: aggregation.aggregateStart
73
+ },
74
+ $set: setFields
75
+ },
76
+ { upsert: true, new: true }
77
+ )
78
+ .exec();
79
+ console.log(doc);
80
+ }
81
+ }
82
+
83
+ main()
84
+ .then(() => {
85
+ console.log('success!');
86
+ })
87
+ .catch(console.error);
@@ -6,6 +6,7 @@ import { chevre } from '../../../../lib/index';
6
6
 
7
7
  // const project = { id: <string>process.env.PROJECT_ID };
8
8
  const EXCLUDED_PROJECT_ID = process.env.EXCLUDED_PROJECT_ID;
9
+ const AGGREGATE_DAYS = Number(process.env.AGGREGATE_DAYS);
9
10
 
10
11
  interface IAggregation {
11
12
  typeOf: 'AggregatePlaceOrder';
@@ -29,7 +30,7 @@ async function main() {
29
30
 
30
31
  let i = 0;
31
32
  // tslint:disable-next-line:no-magic-numbers
32
- while (i < 365) {
33
+ while (i < AGGREGATE_DAYS) {
33
34
  i += 1;
34
35
 
35
36
  const startFrom: Date = moment()
@@ -42,7 +43,6 @@ async function main() {
42
43
  .add(-i, 'days')
43
44
  .endOf('day')
44
45
  .toDate();
45
- // console.log(bookingFrom, bookingThrough);
46
46
 
47
47
  const aggregateResult = await transactionRepo.aggregatePlaceOrder({
48
48
  project: { id: <any>{ $ne: EXCLUDED_PROJECT_ID } },
@@ -7,6 +7,23 @@ export interface IUseActionCountByOffer {
7
7
  _id: string[];
8
8
  useActionCount?: number;
9
9
  }
10
+ interface IAggregationByStatus {
11
+ actionCount: number;
12
+ avgDuration: number;
13
+ maxDuration: number;
14
+ minDuration: number;
15
+ percentilesDuration: {
16
+ name: string;
17
+ value: number;
18
+ }[];
19
+ }
20
+ interface IStatus {
21
+ status: factory.actionStatusType;
22
+ aggregation: IAggregationByStatus;
23
+ }
24
+ interface IAggregateAction {
25
+ statuses: IStatus[];
26
+ }
10
27
  export { modelName };
11
28
  /**
12
29
  * アクションリポジトリ
@@ -107,4 +124,25 @@ export declare class MongoRepository {
107
124
  deleteEndDatePassedCertainPeriod(params: {
108
125
  $lt: Date;
109
126
  }): Promise<void>;
127
+ aggregateAuthorizeEventServiceOfferAction(params: {
128
+ project?: {
129
+ id?: {
130
+ $ne?: string;
131
+ };
132
+ };
133
+ startFrom: Date;
134
+ startThrough: Date;
135
+ typeOf: factory.actionType;
136
+ }): Promise<IAggregateAction>;
137
+ aggregateAuthorizePaymentAction(params: {
138
+ project?: {
139
+ id?: {
140
+ $ne?: string;
141
+ };
142
+ };
143
+ startFrom: Date;
144
+ startThrough: Date;
145
+ typeOf: factory.actionType;
146
+ }): Promise<IAggregateAction>;
147
+ private agggregateByStatus;
110
148
  }
@@ -677,5 +677,151 @@ class MongoRepository {
677
677
  .exec();
678
678
  });
679
679
  }
680
+ aggregateAuthorizeEventServiceOfferAction(params) {
681
+ return __awaiter(this, void 0, void 0, function* () {
682
+ const statuses = yield Promise.all([
683
+ factory.actionStatusType.CanceledActionStatus,
684
+ factory.actionStatusType.CompletedActionStatus,
685
+ factory.actionStatusType.FailedActionStatus
686
+ ].map((actionStatus) => __awaiter(this, void 0, void 0, function* () {
687
+ var _a, _b;
688
+ const matchConditions = Object.assign({ startDate: {
689
+ $gte: params.startFrom,
690
+ $lte: params.startThrough
691
+ }, typeOf: { $eq: params.typeOf }, actionStatus: { $eq: actionStatus }, 'object.typeOf': {
692
+ $exists: true,
693
+ $eq: factory.action.authorize.offer.seatReservation.ObjectType.SeatReservation
694
+ } }, (typeof ((_b = (_a = params.project) === null || _a === void 0 ? void 0 : _a.id) === null || _b === void 0 ? void 0 : _b.$ne) === 'string')
695
+ ? { 'project.id': { $ne: params.project.id.$ne } }
696
+ : undefined);
697
+ return this.agggregateByStatus({ matchConditions, actionStatus });
698
+ })));
699
+ return { statuses };
700
+ });
701
+ }
702
+ aggregateAuthorizePaymentAction(params) {
703
+ return __awaiter(this, void 0, void 0, function* () {
704
+ const statuses = yield Promise.all([
705
+ factory.actionStatusType.CanceledActionStatus,
706
+ factory.actionStatusType.CompletedActionStatus,
707
+ factory.actionStatusType.FailedActionStatus
708
+ ].map((actionStatus) => __awaiter(this, void 0, void 0, function* () {
709
+ var _a, _b;
710
+ const matchConditions = Object.assign({ startDate: {
711
+ $gte: params.startFrom,
712
+ $lte: params.startThrough
713
+ }, typeOf: { $eq: params.typeOf }, actionStatus: { $eq: actionStatus }, 'object.typeOf': {
714
+ $exists: true,
715
+ $eq: factory.action.authorize.paymentMethod.any.ResultType.Payment
716
+ } }, (typeof ((_b = (_a = params.project) === null || _a === void 0 ? void 0 : _a.id) === null || _b === void 0 ? void 0 : _b.$ne) === 'string')
717
+ ? { 'project.id': { $ne: params.project.id.$ne } }
718
+ : undefined);
719
+ return this.agggregateByStatus({ matchConditions, actionStatus });
720
+ })));
721
+ return { statuses };
722
+ });
723
+ }
724
+ // tslint:disable-next-line:max-func-body-length
725
+ agggregateByStatus(params) {
726
+ return __awaiter(this, void 0, void 0, function* () {
727
+ const aggregations = yield this.actionModel.aggregate([
728
+ { $match: params.matchConditions },
729
+ {
730
+ $project: {
731
+ duration: { $subtract: ['$endDate', '$startDate'] },
732
+ actionStatus: '$actionStatus',
733
+ startDate: '$startDate',
734
+ endDate: '$endDate',
735
+ typeOf: '$typeOf'
736
+ }
737
+ },
738
+ {
739
+ $group: {
740
+ _id: '$typeOf',
741
+ actionCount: { $sum: 1 },
742
+ maxDuration: { $max: '$duration' },
743
+ minDuration: { $min: '$duration' },
744
+ avgDuration: { $avg: '$duration' }
745
+ }
746
+ },
747
+ {
748
+ $project: {
749
+ _id: 0,
750
+ actionCount: '$actionCount',
751
+ avgDuration: '$avgDuration',
752
+ maxDuration: '$maxDuration',
753
+ minDuration: '$minDuration'
754
+ }
755
+ }
756
+ ])
757
+ .exec();
758
+ // tslint:disable-next-line:no-magic-numbers
759
+ const percents = [50, 95, 99];
760
+ if (aggregations.length === 0) {
761
+ return {
762
+ status: params.actionStatus,
763
+ aggregation: {
764
+ actionCount: 0,
765
+ avgDuration: 0,
766
+ maxDuration: 0,
767
+ minDuration: 0,
768
+ percentilesDuration: percents.map((percent) => {
769
+ return {
770
+ name: String(percent),
771
+ value: 0
772
+ };
773
+ })
774
+ }
775
+ };
776
+ }
777
+ const ranks4percentile = percents.map((percentile) => {
778
+ return {
779
+ percentile,
780
+ // tslint:disable-next-line:no-magic-numbers
781
+ rank: Math.floor(aggregations[0].actionCount * percentile / 100)
782
+ };
783
+ });
784
+ const aggregations2 = yield this.actionModel.aggregate([
785
+ {
786
+ $match: params.matchConditions
787
+ },
788
+ {
789
+ $project: {
790
+ duration: { $subtract: ['$endDate', '$startDate'] },
791
+ actionStatus: '$actionStatus',
792
+ startDate: '$startDate',
793
+ endDate: '$endDate',
794
+ typeOf: '$typeOf'
795
+ }
796
+ },
797
+ { $sort: { duration: 1 } },
798
+ {
799
+ $group: {
800
+ _id: '$typeOf',
801
+ durations: { $push: '$duration' }
802
+ }
803
+ },
804
+ {
805
+ $project: {
806
+ _id: 0,
807
+ avgSmallDuration: '$avgSmallDuration',
808
+ avgMediumDuration: '$avgMediumDuration',
809
+ avgLargeDuration: '$avgLargeDuration',
810
+ percentilesDuration: ranks4percentile.map((rank) => {
811
+ return {
812
+ name: String(rank.percentile),
813
+ value: { $arrayElemAt: ['$durations', rank.rank] }
814
+ };
815
+ })
816
+ }
817
+ }
818
+ ])
819
+ .exec();
820
+ return {
821
+ status: params.actionStatus,
822
+ aggregation: Object.assign(Object.assign({}, aggregations[0]), aggregations2[0])
823
+ };
824
+ });
825
+ }
680
826
  }
681
827
  exports.MongoRepository = MongoRepository;
@@ -650,8 +650,7 @@ class MongoRepository {
650
650
  },
651
651
  avgDuration: {
652
652
  $avg: '$duration'
653
- },
654
- stdDevDuration: { $stdDevPop: '$duration' }
653
+ }
655
654
  }
656
655
  },
657
656
  {
@@ -660,8 +659,7 @@ class MongoRepository {
660
659
  transactionCount: '$transactionCount',
661
660
  avgDuration: '$avgDuration',
662
661
  maxDuration: '$maxDuration',
663
- minDuration: '$minDuration',
664
- stdDevDuration: '$stdDevDuration'
662
+ minDuration: '$minDuration'
665
663
  }
666
664
  }
667
665
  ])
@@ -43,7 +43,10 @@ exports.TRANSACTION_CONFIRMED_STORAGE_PERIOD_IN_DAYS = 365;
43
43
  /**
44
44
  * 取引保管期間(Canceled)
45
45
  */
46
- exports.TRANSACTION_CANCELED_STORAGE_PERIOD_IN_DAYS = 7;
46
+ exports.TRANSACTION_CANCELED_STORAGE_PERIOD_IN_DAYS = (typeof process.env.TRANSACTION_CANCELED_STORAGE_PERIOD_IN_DAYS === 'string')
47
+ ? Number(process.env.TRANSACTION_CANCELED_STORAGE_PERIOD_IN_DAYS)
48
+ // tslint:disable-next-line:no-magic-numbers
49
+ : 7;
47
50
  exports.DEFAULT_SENDER_EMAIL = process.env.DEFAULT_SENDER_EMAIL;
48
51
  exports.DEFAULT_PAYMENT_METHOD_TYPE_FOR_CREDIT_CARD = String(process.env.DEFAULT_PAYMENT_METHOD_TYPE_FOR_CREDIT_CARD);
49
52
  exports.USE_ASSET_TRANSACTION_SYNC_PROCESSING = process.env.USE_ASSET_TRANSACTION_SYNC_PROCESSING === '1';
package/package.json CHANGED
@@ -120,5 +120,5 @@
120
120
  "postversion": "git push origin --tags",
121
121
  "prepublishOnly": "npm run clean && npm run build && npm test && npm run doc"
122
122
  },
123
- "version": "20.2.0-alpha.17"
123
+ "version": "20.2.0-alpha.18"
124
124
  }