@chevre/domain 26.0.0-alpha.3 → 26.0.0-alpha.4

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,32 @@
1
+ import type { Connection } from 'mongoose';
2
+ import { factory } from '../factory';
3
+ interface IAggregationByStatus {
4
+ taskCount: number;
5
+ avgLatency: number;
6
+ maxLatency: number;
7
+ minLatency: number;
8
+ percentilesLatency: {
9
+ name: string;
10
+ value: number;
11
+ }[];
12
+ }
13
+ interface IStatus {
14
+ status: factory.taskStatus;
15
+ aggregation: IAggregationByStatus;
16
+ }
17
+ interface IAggregateTask {
18
+ statuses: IStatus[];
19
+ }
20
+ /**
21
+ * 予定タスク集計リポジトリ
22
+ */
23
+ export declare class AggregateScheduledTaskRepo {
24
+ private readonly scheduledTaskModel;
25
+ constructor(connection: Connection);
26
+ aggregateScheduledTask(params: {
27
+ runsFrom: Date;
28
+ runsThrough: Date;
29
+ }): Promise<IAggregateTask>;
30
+ private agggregateByStatus;
31
+ }
32
+ export {};
@@ -0,0 +1,136 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AggregateScheduledTaskRepo = void 0;
4
+ const factory_1 = require("../factory");
5
+ const scheduledTasks_1 = require("./mongoose/schemas/scheduledTasks");
6
+ /**
7
+ * 予定タスク集計リポジトリ
8
+ */
9
+ class AggregateScheduledTaskRepo {
10
+ scheduledTaskModel;
11
+ constructor(connection) {
12
+ this.scheduledTaskModel = connection.model(scheduledTasks_1.modelName, (0, scheduledTasks_1.createSchema)());
13
+ }
14
+ async aggregateScheduledTask(params) {
15
+ const statuses = await Promise.all([
16
+ factory_1.factory.taskStatus.Executed,
17
+ factory_1.factory.taskStatus.Aborted
18
+ ].map(async (taskStatus) => {
19
+ const matchConditions = {
20
+ status: { $eq: taskStatus },
21
+ runsAt: {
22
+ $gte: params.runsFrom,
23
+ $lte: params.runsThrough
24
+ },
25
+ // ...(typeof params.project?.id?.$ne === 'string')
26
+ // ? { 'project.id': { $ne: params.project.id.$ne } }
27
+ // : undefined
28
+ };
29
+ return this.agggregateByStatus({ matchConditions, status: taskStatus });
30
+ }));
31
+ return { statuses };
32
+ }
33
+ async agggregateByStatus(params) {
34
+ const matchConditions = params.matchConditions;
35
+ const taskStatus = params.status;
36
+ const aggregate1 = this.scheduledTaskModel.aggregate([
37
+ {
38
+ $match: matchConditions
39
+ },
40
+ {
41
+ $project: {
42
+ latency: { $subtract: ['$lastTriedAt', '$runsAt'] },
43
+ status: '$status',
44
+ runsAt: '$runsAt',
45
+ lastTriedAt: '$lastTriedAt'
46
+ }
47
+ },
48
+ {
49
+ $group: {
50
+ _id: '$status',
51
+ taskCount: { $sum: 1 },
52
+ maxLatency: { $max: '$latency' },
53
+ minLatency: { $min: '$latency' },
54
+ avgLatency: { $avg: '$latency' }
55
+ }
56
+ },
57
+ {
58
+ $project: {
59
+ _id: 0,
60
+ taskCount: '$taskCount',
61
+ avgLatency: '$avgLatency',
62
+ maxLatency: '$maxLatency',
63
+ minLatency: '$minLatency'
64
+ }
65
+ }
66
+ ]);
67
+ // const explainResult = await aggregate1.explain();
68
+ // console.dir(explainResult, { depth: null });
69
+ // return;
70
+ const aggregations = await aggregate1.exec();
71
+ const percents = [50, 95, 99];
72
+ if (aggregations.length === 0) {
73
+ return {
74
+ status: taskStatus,
75
+ aggregation: {
76
+ taskCount: 0,
77
+ avgLatency: 0,
78
+ maxLatency: 0,
79
+ minLatency: 0,
80
+ percentilesLatency: percents.map((percent) => {
81
+ return {
82
+ name: String(percent),
83
+ value: 0
84
+ };
85
+ })
86
+ }
87
+ };
88
+ }
89
+ const ranks4percentile = percents.map((percentile) => {
90
+ return {
91
+ percentile,
92
+ rank: Math.floor(aggregations[0].taskCount * percentile / 100)
93
+ };
94
+ });
95
+ const aggregate2 = this.scheduledTaskModel.aggregate([
96
+ {
97
+ $match: matchConditions
98
+ },
99
+ {
100
+ $project: {
101
+ latency: { $subtract: ['$lastTriedAt', '$runsAt'] },
102
+ status: '$status',
103
+ runsAt: '$runsAt',
104
+ lastTriedAt: '$lastTriedAt'
105
+ }
106
+ },
107
+ { $sort: { latency: 1 } },
108
+ {
109
+ $group: {
110
+ _id: '$status',
111
+ latencies: { $push: '$latency' }
112
+ }
113
+ },
114
+ {
115
+ $project: {
116
+ _id: 0,
117
+ percentilesLatency: ranks4percentile.map((rank) => {
118
+ return {
119
+ name: String(rank.percentile),
120
+ value: { $arrayElemAt: ['$latencies', rank.rank] }
121
+ };
122
+ })
123
+ }
124
+ }
125
+ ]);
126
+ const aggregations2 = await aggregate2.exec();
127
+ return {
128
+ status: taskStatus,
129
+ aggregation: {
130
+ ...aggregations[0],
131
+ ...aggregations2[0]
132
+ }
133
+ };
134
+ }
135
+ }
136
+ exports.AggregateScheduledTaskRepo = AggregateScheduledTaskRepo;
@@ -24,6 +24,7 @@ import type { AggregateActionRepo } from './repo/aggregateAction';
24
24
  import type { AggregateOfferRepo } from './repo/aggregateOffer';
25
25
  import type { AggregateOrderRepo } from './repo/aggregateOrder';
26
26
  import type { AggregateReservationRepo } from './repo/aggregateReservation';
27
+ import type { AggregateScheduledTaskRepo } from './repo/aggregateScheduledTask';
27
28
  import type { AggregateTaskRepo } from './repo/aggregateTask';
28
29
  import type { AggregationRepo } from './repo/aggregation';
29
30
  import type { AssetTransactionRepo } from './repo/assetTransaction';
@@ -180,6 +181,10 @@ export type AggregateAction = AggregateActionRepo;
180
181
  export declare namespace AggregateAction {
181
182
  function createInstance(...params: ConstructorParameters<typeof AggregateActionRepo>): Promise<AggregateActionRepo>;
182
183
  }
184
+ export type AggregateScheduledTask = AggregateScheduledTaskRepo;
185
+ export declare namespace AggregateScheduledTask {
186
+ function createInstance(...params: ConstructorParameters<typeof AggregateScheduledTaskRepo>): Promise<AggregateScheduledTaskRepo>;
187
+ }
183
188
  export type AggregateTask = AggregateTaskRepo;
184
189
  export declare namespace AggregateTask {
185
190
  function createInstance(...params: ConstructorParameters<typeof AggregateTaskRepo>): Promise<AggregateTaskRepo>;
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.PaymentServiceChannel = exports.PaymentService = exports.Passport = exports.OwnershipInfo = exports.OrderNumber = exports.OrderInTransaction = exports.Order = exports.Offer = exports.OfferItemCondition = exports.OfferCatalogItem = exports.OfferCatalog = exports.NoteAboutOrder = exports.Note = exports.MovieTicketType = exports.Message = exports.MerchantReturnPolicy = exports.MemberProgram = exports.Member = exports.Issuer = exports.IdentityProvider = exports.Identity = exports.EventSeries = exports.EventSellerMakesOffer = exports.EventOffer = exports.Event = exports.EmailMessage = exports.CustomerType = exports.Customer = exports.Credentials = exports.CreativeWork = exports.ConfirmationNumber = exports.Authorization = exports.CategoryCode = exports.assetTransaction = exports.AssetTransaction = exports.Aggregation = exports.AggregateReservation = exports.AggregateOrder = exports.AggregateOffer = exports.AggregateTask = exports.AggregateAction = exports.AdminTask = exports.AdminScheduledTask = exports.AdminAsyncAction = exports.AdditionalProperty = exports.action = exports.Action = exports.AccountTitle = exports.AccountingReport = exports.AcceptedOffer = void 0;
4
- exports.WebSite = exports.rateLimit = exports.TransactionProcess = exports.TransactionNumber = exports.transaction = exports.Transaction = exports.Ticket = exports.AsyncAction = exports.Task = exports.ScheduledTask = exports.StockHolder = exports.setting = exports.Setting = exports.ServiceAvailableHour = exports.SellerReturnPolicy = exports.SellerPaymentAccepted = exports.SellerMakesOffer = exports.Seller = exports.Schedule = exports.Role = exports.ReserveInterface = exports.Reservation = exports.ProjectMakesOffer = exports.Project = exports.ProductHasOfferCatalog = exports.Product = exports.PriceSpecification = exports.PotentialAction = exports.place = exports.Person = exports.PendingReservation = exports.PaymentServiceProvider = void 0;
3
+ exports.PaymentService = exports.Passport = exports.OwnershipInfo = exports.OrderNumber = exports.OrderInTransaction = exports.Order = exports.Offer = exports.OfferItemCondition = exports.OfferCatalogItem = exports.OfferCatalog = exports.NoteAboutOrder = exports.Note = exports.MovieTicketType = exports.Message = exports.MerchantReturnPolicy = exports.MemberProgram = exports.Member = exports.Issuer = exports.IdentityProvider = exports.Identity = exports.EventSeries = exports.EventSellerMakesOffer = exports.EventOffer = exports.Event = exports.EmailMessage = exports.CustomerType = exports.Customer = exports.Credentials = exports.CreativeWork = exports.ConfirmationNumber = exports.Authorization = exports.CategoryCode = exports.assetTransaction = exports.AssetTransaction = exports.Aggregation = exports.AggregateReservation = exports.AggregateOrder = exports.AggregateOffer = exports.AggregateTask = exports.AggregateScheduledTask = exports.AggregateAction = exports.AdminTask = exports.AdminScheduledTask = exports.AdminAsyncAction = exports.AdditionalProperty = exports.action = exports.Action = exports.AccountTitle = exports.AccountingReport = exports.AcceptedOffer = void 0;
4
+ exports.WebSite = exports.rateLimit = exports.TransactionProcess = exports.TransactionNumber = exports.transaction = exports.Transaction = exports.Ticket = exports.AsyncAction = exports.Task = exports.ScheduledTask = exports.StockHolder = exports.setting = exports.Setting = exports.ServiceAvailableHour = exports.SellerReturnPolicy = exports.SellerPaymentAccepted = exports.SellerMakesOffer = exports.Seller = exports.Schedule = exports.Role = exports.ReserveInterface = exports.Reservation = exports.ProjectMakesOffer = exports.Project = exports.ProductHasOfferCatalog = exports.Product = exports.PriceSpecification = exports.PotentialAction = exports.place = exports.Person = exports.PendingReservation = exports.PaymentServiceProvider = exports.PaymentServiceChannel = void 0;
5
5
  var AcceptedOffer;
6
6
  (function (AcceptedOffer) {
7
7
  let repo;
@@ -214,6 +214,17 @@ var AggregateAction;
214
214
  }
215
215
  AggregateAction.createInstance = createInstance;
216
216
  })(AggregateAction || (exports.AggregateAction = AggregateAction = {}));
217
+ var AggregateScheduledTask;
218
+ (function (AggregateScheduledTask) {
219
+ let repo;
220
+ async function createInstance(...params) {
221
+ if (repo === undefined) {
222
+ repo = (await import('./repo/aggregateScheduledTask.js')).AggregateScheduledTaskRepo;
223
+ }
224
+ return new repo(...params);
225
+ }
226
+ AggregateScheduledTask.createInstance = createInstance;
227
+ })(AggregateScheduledTask || (exports.AggregateScheduledTask = AggregateScheduledTask = {}));
217
228
  var AggregateTask;
218
229
  (function (AggregateTask) {
219
230
  let repo;
@@ -1,4 +1,5 @@
1
1
  import type { AggregateActionRepo } from '../../repo/aggregateAction';
2
+ import type { AggregateScheduledTaskRepo } from '../../repo/aggregateScheduledTask';
2
3
  import type { AggregateTaskRepo } from '../../repo/aggregateTask';
3
4
  import { AggregationRepo } from '../../repo/aggregation';
4
5
  import type { AssetTransactionRepo } from '../../repo/assetTransaction';
@@ -10,7 +11,7 @@ interface IAggregateParams {
10
11
  aggregateDate: Date;
11
12
  aggregateDurationUnit: AggregateDurationUnit;
12
13
  aggregationCount: number;
13
- excludedProjectId?: string;
14
+ excludedProjectId?: never;
14
15
  }
15
16
  declare function aggregateEvent(params: IAggregateParams): (repos: {
16
17
  agregation: AggregationRepo;
@@ -146,4 +147,11 @@ declare function aggregateTask(params: IAggregateParams): (repos: {
146
147
  aggregationCount: number;
147
148
  aggregateDuration: string;
148
149
  }>;
149
- export { aggregateAuthorizeEventServiceOfferAction, aggregateAuthorizeOrderAction, aggregateAuthorizePaymentAction, aggregateCancelReservationAction, aggregateCheckMovieTicketAction, aggregateEvent, aggregateOrder, aggregateOrderAction, aggregatePayMovieTicketAction, aggregatePayTransaction, aggregatePlaceOrder, aggregateReserveAction, aggregateReserveTransaction, aggregateTask, aggregateUseAction };
150
+ declare function aggregateScheduledTask(params: IAggregateParams): (repos: {
151
+ agregation: AggregationRepo;
152
+ aggregateScheduledTask: AggregateScheduledTaskRepo;
153
+ }) => Promise<{
154
+ aggregationCount: number;
155
+ aggregateDuration: string;
156
+ }>;
157
+ export { aggregateAuthorizeEventServiceOfferAction, aggregateAuthorizeOrderAction, aggregateAuthorizePaymentAction, aggregateCancelReservationAction, aggregateCheckMovieTicketAction, aggregateEvent, aggregateOrder, aggregateOrderAction, aggregatePayMovieTicketAction, aggregatePayTransaction, aggregatePlaceOrder, aggregateReserveAction, aggregateReserveTransaction, aggregateTask, aggregateScheduledTask, aggregateUseAction };
@@ -17,6 +17,7 @@ exports.aggregatePlaceOrder = aggregatePlaceOrder;
17
17
  exports.aggregateReserveAction = aggregateReserveAction;
18
18
  exports.aggregateReserveTransaction = aggregateReserveTransaction;
19
19
  exports.aggregateTask = aggregateTask;
20
+ exports.aggregateScheduledTask = aggregateScheduledTask;
20
21
  exports.aggregateUseAction = aggregateUseAction;
21
22
  const debug_1 = __importDefault(require("debug"));
22
23
  const moment_timezone_1 = __importDefault(require("moment-timezone"));
@@ -692,3 +693,44 @@ function aggregateTask(params) {
692
693
  return { aggregationCount: i, aggregateDuration };
693
694
  };
694
695
  }
696
+ function aggregateScheduledTask(params) {
697
+ return async (repos) => {
698
+ const { aggregateDate } = params;
699
+ if (!(aggregateDate instanceof Date)) {
700
+ throw new factory_1.factory.errors.Argument('aggregateDate', 'must be Date');
701
+ }
702
+ const aggregateDuration = moment_timezone_1.default.duration(1, params.aggregateDurationUnit)
703
+ .toISOString();
704
+ let i = -1;
705
+ while (i < params.aggregationCount) {
706
+ i += 1;
707
+ const runsFrom = (0, moment_timezone_1.default)(aggregateDate)
708
+ .utc()
709
+ // .tz('Asia/Tokyo')
710
+ .add(-i, params.aggregateDurationUnit)
711
+ .startOf(params.aggregateDurationUnit)
712
+ .toDate();
713
+ const runsThrough = (0, moment_timezone_1.default)(aggregateDate)
714
+ .utc()
715
+ // .tz('Asia/Tokyo')
716
+ .add(-i, params.aggregateDurationUnit)
717
+ .endOf(params.aggregateDurationUnit)
718
+ .toDate();
719
+ const aggregateResult = await repos.aggregateScheduledTask.aggregateScheduledTask({
720
+ // project: { id: { $ne: params.excludedProjectId } },
721
+ runsFrom,
722
+ runsThrough
723
+ });
724
+ await repos.agregation.saveAggregation({
725
+ typeOf: factory_1.factory.aggregation.AggregationType.AggregateScheduledTask,
726
+ project: { id: '*', typeOf: factory_1.factory.organizationType.Project },
727
+ aggregateDuration,
728
+ aggregateStart: runsFrom,
729
+ aggregateDate,
730
+ ...aggregateResult
731
+ });
732
+ }
733
+ debug(i, 'aggregations saved');
734
+ return { aggregationCount: i, aggregateDuration };
735
+ };
736
+ }
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "dependencies": {
12
12
  "@aws-sdk/client-cognito-identity-provider": "3.1090.0",
13
13
  "@aws-sdk/credential-providers": "3.1090.0",
14
- "@chevre/factory": "10.1.0-alpha.4",
14
+ "@chevre/factory": "10.1.0-alpha.5",
15
15
  "@motionpicture/coa-service": "10.0.0",
16
16
  "@motionpicture/gmo-service": "6.1.0-alpha.0",
17
17
  "@sendgrid/client": "8.1.4",
@@ -88,5 +88,5 @@
88
88
  "postversion": "git push origin --tags",
89
89
  "prepublishOnly": "npm run clean && npm run build"
90
90
  },
91
- "version": "26.0.0-alpha.3"
91
+ "version": "26.0.0-alpha.4"
92
92
  }