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

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,72 @@
1
+ // tslint:disable:no-console
2
+ import * as mongoose from 'mongoose';
3
+
4
+ import { chevre } from '../../../../lib/index';
5
+
6
+ // const project = { id: <string>process.env.PROJECT_ID };
7
+ const EXCLUDED_PROJECT_ID = process.env.EXCLUDED_PROJECT_ID;
8
+ const AGGREGATE_DAYS = Number(process.env.AGGREGATE_DAYS);
9
+
10
+ async function main() {
11
+ await mongoose.connect(<string>process.env.MONGOLAB_URI, { autoIndex: false });
12
+
13
+ const aggregationRepo = new chevre.repository.Aggregation(mongoose.connection);
14
+ const transactionRepo = new chevre.repository.Transaction(mongoose.connection);
15
+ const reservationRepo = new chevre.repository.Reservation(mongoose.connection);
16
+ const orderRepo = new chevre.repository.Order(mongoose.connection);
17
+ const actionRepo = new chevre.repository.Action(mongoose.connection);
18
+
19
+ await chevre.service.aggregation.system.aggregatePlaceOrder({
20
+ aggregationDays: AGGREGATE_DAYS,
21
+ excludedProjectId: EXCLUDED_PROJECT_ID
22
+ })({
23
+ agregation: aggregationRepo,
24
+ transaction: transactionRepo
25
+ });
26
+
27
+ await chevre.service.aggregation.system.aggregateAuthorizeEventServiceOfferAction({
28
+ aggregationDays: AGGREGATE_DAYS,
29
+ excludedProjectId: EXCLUDED_PROJECT_ID
30
+ })({
31
+ agregation: aggregationRepo,
32
+ action: actionRepo
33
+ });
34
+
35
+ await chevre.service.aggregation.system.aggregateAuthorizePaymentAction({
36
+ aggregationDays: AGGREGATE_DAYS,
37
+ excludedProjectId: EXCLUDED_PROJECT_ID
38
+ })({
39
+ agregation: aggregationRepo,
40
+ action: actionRepo
41
+ });
42
+
43
+ await chevre.service.aggregation.system.aggregateUseAction({
44
+ aggregationDays: AGGREGATE_DAYS,
45
+ excludedProjectId: EXCLUDED_PROJECT_ID
46
+ })({
47
+ agregation: aggregationRepo,
48
+ action: actionRepo
49
+ });
50
+
51
+ await chevre.service.aggregation.system.aggregateOrder({
52
+ aggregationDays: AGGREGATE_DAYS,
53
+ excludedProjectId: EXCLUDED_PROJECT_ID
54
+ })({
55
+ agregation: aggregationRepo,
56
+ order: orderRepo
57
+ });
58
+
59
+ await chevre.service.aggregation.system.aggregateReservation({
60
+ aggregationDays: AGGREGATE_DAYS,
61
+ excludedProjectId: EXCLUDED_PROJECT_ID
62
+ })({
63
+ agregation: aggregationRepo,
64
+ reservation: reservationRepo
65
+ });
66
+ }
67
+
68
+ main()
69
+ .then(() => {
70
+ console.log('success!');
71
+ })
72
+ .catch(console.error);
@@ -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,35 @@ 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
+ aggregateUseAction(params: {
148
+ project?: {
149
+ id?: {
150
+ $ne?: string;
151
+ };
152
+ };
153
+ startFrom: Date;
154
+ startThrough: Date;
155
+ typeOf: factory.actionType;
156
+ }): Promise<IAggregateAction>;
157
+ private agggregateByStatus;
110
158
  }
@@ -677,5 +677,170 @@ 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.CompletedActionStatus,
684
+ factory.actionStatusType.CanceledActionStatus,
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.CompletedActionStatus,
706
+ factory.actionStatusType.CanceledActionStatus,
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
+ aggregateUseAction(params) {
725
+ return __awaiter(this, void 0, void 0, function* () {
726
+ const statuses = yield Promise.all([
727
+ factory.actionStatusType.CompletedActionStatus,
728
+ factory.actionStatusType.CanceledActionStatus,
729
+ factory.actionStatusType.FailedActionStatus
730
+ ].map((actionStatus) => __awaiter(this, void 0, void 0, function* () {
731
+ var _a, _b;
732
+ const matchConditions = Object.assign({ startDate: {
733
+ $gte: params.startFrom,
734
+ $lte: params.startThrough
735
+ }, typeOf: { $eq: params.typeOf }, actionStatus: { $eq: actionStatus } }, (typeof ((_b = (_a = params.project) === null || _a === void 0 ? void 0 : _a.id) === null || _b === void 0 ? void 0 : _b.$ne) === 'string')
736
+ ? { 'project.id': { $ne: params.project.id.$ne } }
737
+ : undefined);
738
+ return this.agggregateByStatus({ matchConditions, actionStatus });
739
+ })));
740
+ return { statuses };
741
+ });
742
+ }
743
+ // tslint:disable-next-line:max-func-body-length
744
+ agggregateByStatus(params) {
745
+ return __awaiter(this, void 0, void 0, function* () {
746
+ const aggregations = yield this.actionModel.aggregate([
747
+ { $match: params.matchConditions },
748
+ {
749
+ $project: {
750
+ duration: { $subtract: ['$endDate', '$startDate'] },
751
+ actionStatus: '$actionStatus',
752
+ startDate: '$startDate',
753
+ endDate: '$endDate',
754
+ typeOf: '$typeOf'
755
+ }
756
+ },
757
+ {
758
+ $group: {
759
+ _id: '$typeOf',
760
+ actionCount: { $sum: 1 },
761
+ maxDuration: { $max: '$duration' },
762
+ minDuration: { $min: '$duration' },
763
+ avgDuration: { $avg: '$duration' }
764
+ }
765
+ },
766
+ {
767
+ $project: {
768
+ _id: 0,
769
+ actionCount: '$actionCount',
770
+ avgDuration: '$avgDuration',
771
+ maxDuration: '$maxDuration',
772
+ minDuration: '$minDuration'
773
+ }
774
+ }
775
+ ])
776
+ .exec();
777
+ // tslint:disable-next-line:no-magic-numbers
778
+ const percents = [50, 95, 99];
779
+ if (aggregations.length === 0) {
780
+ return {
781
+ status: params.actionStatus,
782
+ aggregation: {
783
+ actionCount: 0,
784
+ avgDuration: 0,
785
+ maxDuration: 0,
786
+ minDuration: 0,
787
+ percentilesDuration: percents.map((percent) => {
788
+ return {
789
+ name: String(percent),
790
+ value: 0
791
+ };
792
+ })
793
+ }
794
+ };
795
+ }
796
+ const ranks4percentile = percents.map((percentile) => {
797
+ return {
798
+ percentile,
799
+ // tslint:disable-next-line:no-magic-numbers
800
+ rank: Math.floor(aggregations[0].actionCount * percentile / 100)
801
+ };
802
+ });
803
+ const aggregations2 = yield this.actionModel.aggregate([
804
+ {
805
+ $match: params.matchConditions
806
+ },
807
+ {
808
+ $project: {
809
+ duration: { $subtract: ['$endDate', '$startDate'] },
810
+ actionStatus: '$actionStatus',
811
+ startDate: '$startDate',
812
+ endDate: '$endDate',
813
+ typeOf: '$typeOf'
814
+ }
815
+ },
816
+ { $sort: { duration: 1 } },
817
+ {
818
+ $group: {
819
+ _id: '$typeOf',
820
+ durations: { $push: '$duration' }
821
+ }
822
+ },
823
+ {
824
+ $project: {
825
+ _id: 0,
826
+ avgSmallDuration: '$avgSmallDuration',
827
+ avgMediumDuration: '$avgMediumDuration',
828
+ avgLargeDuration: '$avgLargeDuration',
829
+ percentilesDuration: ranks4percentile.map((rank) => {
830
+ return {
831
+ name: String(rank.percentile),
832
+ value: { $arrayElemAt: ['$durations', rank.rank] }
833
+ };
834
+ })
835
+ }
836
+ }
837
+ ])
838
+ .exec();
839
+ return {
840
+ status: params.actionStatus,
841
+ aggregation: Object.assign(Object.assign({}, aggregations[0]), aggregations2[0])
842
+ };
843
+ });
844
+ }
680
845
  }
681
846
  exports.MongoRepository = MongoRepository;
@@ -1,8 +1,28 @@
1
1
  import { Connection, Model } from 'mongoose';
2
+ import * as factory from '../factory';
3
+ export declare enum AggregationType {
4
+ AggregatePlaceOrder = "AggregatePlaceOrder",
5
+ AggregateAuthorizeEventServiceOfferAction = "AggregateAuthorizeEventServiceOfferAction",
6
+ AggregateAuthorizePaymentAction = "AggregateAuthorizePaymentAction",
7
+ AggregateOrder = "AggregateOrder",
8
+ AggregateReservation = "AggregateReservation",
9
+ AggregateUseAction = "AggregateUseAction"
10
+ }
11
+ export interface IAggregation {
12
+ typeOf: AggregationType;
13
+ project: {
14
+ id: string;
15
+ typeOf: factory.organizationType.Project;
16
+ };
17
+ aggregateDate: Date;
18
+ aggregateDuration: string;
19
+ aggregateStart: Date;
20
+ }
2
21
  /**
3
22
  * 集計リポジトリ
4
23
  */
5
24
  export declare class MongoRepository {
6
25
  readonly aggregationModel: typeof Model;
7
26
  constructor(connection: Connection);
27
+ saveAggregation(params: IAggregation): Promise<any>;
8
28
  }
@@ -1,7 +1,36 @@
1
1
  "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __rest = (this && this.__rest) || function (s, e) {
12
+ var t = {};
13
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
14
+ t[p] = s[p];
15
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
16
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
17
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
18
+ t[p[i]] = s[p[i]];
19
+ }
20
+ return t;
21
+ };
2
22
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.MongoRepository = void 0;
23
+ exports.MongoRepository = exports.AggregationType = void 0;
4
24
  const aggregation_1 = require("./mongoose/model/aggregation");
25
+ var AggregationType;
26
+ (function (AggregationType) {
27
+ AggregationType["AggregatePlaceOrder"] = "AggregatePlaceOrder";
28
+ AggregationType["AggregateAuthorizeEventServiceOfferAction"] = "AggregateAuthorizeEventServiceOfferAction";
29
+ AggregationType["AggregateAuthorizePaymentAction"] = "AggregateAuthorizePaymentAction";
30
+ AggregationType["AggregateOrder"] = "AggregateOrder";
31
+ AggregationType["AggregateReservation"] = "AggregateReservation";
32
+ AggregationType["AggregateUseAction"] = "AggregateUseAction";
33
+ })(AggregationType = exports.AggregationType || (exports.AggregationType = {}));
5
34
  /**
6
35
  * 集計リポジトリ
7
36
  */
@@ -9,5 +38,26 @@ class MongoRepository {
9
38
  constructor(connection) {
10
39
  this.aggregationModel = connection.model(aggregation_1.modelName);
11
40
  }
41
+ saveAggregation(params) {
42
+ return __awaiter(this, void 0, void 0, function* () {
43
+ const { typeOf, project, aggregateDuration, aggregateStart } = params, setFields = __rest(params, ["typeOf", "project", "aggregateDuration", "aggregateStart"]);
44
+ const doc = yield this.aggregationModel.findOneAndUpdate({
45
+ typeOf: { $eq: params.typeOf },
46
+ 'project.id': { $eq: params.project.id },
47
+ aggregateStart: { $eq: params.aggregateStart },
48
+ aggregateDuration: { $eq: params.aggregateDuration }
49
+ }, {
50
+ $setOnInsert: {
51
+ typeOf: params.typeOf,
52
+ project: params.project,
53
+ aggregateDuration: params.aggregateDuration,
54
+ aggregateStart: params.aggregateStart
55
+ },
56
+ $set: setFields
57
+ }, { upsert: true, new: true })
58
+ .exec();
59
+ return doc.toObject();
60
+ });
61
+ }
12
62
  }
13
63
  exports.MongoRepository = MongoRepository;
@@ -84,5 +84,5 @@ export declare class MongoRepository {
84
84
  maxPrice: number;
85
85
  minPrice: number;
86
86
  avgPrice: number;
87
- }[]>;
87
+ }>;
88
88
  }
@@ -923,7 +923,7 @@ class MongoRepository {
923
923
  aggregateOrder(params) {
924
924
  var _a, _b;
925
925
  return __awaiter(this, void 0, void 0, function* () {
926
- return this.orderModel.aggregate([
926
+ const aggregations = yield this.orderModel.aggregate([
927
927
  {
928
928
  $match: {
929
929
  orderDate: {
@@ -951,9 +951,22 @@ class MongoRepository {
951
951
  minPrice: { $min: '$price' },
952
952
  avgPrice: { $avg: '$price' }
953
953
  }
954
+ },
955
+ {
956
+ $project: {
957
+ _id: 0,
958
+ acceptedOfferCount: '$acceptedOfferCount',
959
+ avgAcceptedOfferCount: '$avgAcceptedOfferCount',
960
+ orderCount: '$orderCount',
961
+ totalPrice: '$totalPrice',
962
+ maxPrice: '$maxPrice',
963
+ minPrice: '$minPrice',
964
+ avgPrice: '$avgPrice'
965
+ }
954
966
  }
955
967
  ])
956
968
  .exec();
969
+ return aggregations[0];
957
970
  });
958
971
  }
959
972
  }
@@ -146,5 +146,15 @@ export declare class MongoRepository {
146
146
  findByIdAndDelete(params: {
147
147
  id: string;
148
148
  }): Promise<void>;
149
- aggregatePlaceOrder(params: factory.transaction.placeOrder.ISearchConditions): Promise<IAggregatePlaceOrder>;
149
+ aggregatePlaceOrder(params: {
150
+ project?: {
151
+ id?: {
152
+ $ne?: string;
153
+ };
154
+ };
155
+ startFrom: Date;
156
+ startThrough: Date;
157
+ typeOf: factory.transactionType;
158
+ }): Promise<IAggregatePlaceOrder>;
159
+ private agggregateByStatus;
150
160
  }