feat: calculate time queue
parent
0e9ae569ba
commit
72827aa83e
|
@ -11,26 +11,52 @@ import {
|
||||||
import { BaseReadService } from 'src/core/modules/data/service/base-read.service';
|
import { BaseReadService } from 'src/core/modules/data/service/base-read.service';
|
||||||
import { BaseDataService } from 'src/core/modules/data/service/base-data.service';
|
import { BaseDataService } from 'src/core/modules/data/service/base-data.service';
|
||||||
import * as moment from 'moment';
|
import * as moment from 'moment';
|
||||||
|
import { ItemQueueModel } from 'src/modules/item-related/item-queue/data/models/item-queue.model';
|
||||||
|
import * as math from 'mathjs';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class QueueDataService extends BaseReadService<QueueModel> {
|
export class QueueDataService extends BaseReadService<QueueModel> {
|
||||||
constructor(
|
constructor(
|
||||||
@InjectRepository(QueueModel, CONNECTION_NAME.DEFAULT)
|
@InjectRepository(QueueModel, CONNECTION_NAME.DEFAULT)
|
||||||
private repo: Repository<QueueModel>,
|
private repo: Repository<QueueModel>,
|
||||||
|
|
||||||
|
@InjectRepository(ItemQueueModel, CONNECTION_NAME.DEFAULT)
|
||||||
|
private itemQueueRepo: Repository<ItemQueueModel>,
|
||||||
) {
|
) {
|
||||||
super(repo);
|
super(repo);
|
||||||
}
|
}
|
||||||
|
|
||||||
async queueItems(item_queue_id: string[]): Promise<QueueModel[]> {
|
async queueTimes(item_queue_id: string): Promise<any> {
|
||||||
return this.repo.find({
|
const queueTimes = {};
|
||||||
relations: ['item', 'item.item', 'item.item.item_queue'],
|
let now = moment().valueOf();
|
||||||
|
const itemQueue = await this.itemQueueRepo.findOne({
|
||||||
|
relations: ['items'],
|
||||||
where: {
|
where: {
|
||||||
item: { item: { item_queue: { id: In(item_queue_id) } } },
|
id: item_queue_id,
|
||||||
},
|
|
||||||
order: {
|
|
||||||
time: 'DESC',
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const times = itemQueue.items.map((item) => item.play_estimation ?? 0);
|
||||||
|
const average = math.mean(times) * 60 * 1000; // change average minute to milliseconds
|
||||||
|
const queues = await this.repo.find({
|
||||||
|
where: {
|
||||||
|
item_queue_id,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
for (const queue of queues) {
|
||||||
|
// duration will be total qty multiple by average
|
||||||
|
const duration = queue.qty * average;
|
||||||
|
|
||||||
|
// time to call will be now + duration
|
||||||
|
const time = now + duration;
|
||||||
|
queueTimes[queue.id] = time;
|
||||||
|
|
||||||
|
// update now to last call time
|
||||||
|
now = time;
|
||||||
|
}
|
||||||
|
|
||||||
|
return queueTimes;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,9 +7,11 @@ import {
|
||||||
} from 'src/core/modules/domain/entities/base-filter.entity';
|
} from 'src/core/modules/domain/entities/base-filter.entity';
|
||||||
import { Queue } from '../entities/queue.entity';
|
import { Queue } from '../entities/queue.entity';
|
||||||
import * as moment from 'moment';
|
import * as moment from 'moment';
|
||||||
|
import { PaginationResponse } from 'src/core/response/domain/ok-response.interface';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class IndexQueueManager extends BaseIndexManager<Queue> {
|
export class IndexQueueManager extends BaseIndexManager<Queue> {
|
||||||
|
private queueTimes = {};
|
||||||
async prepareData(): Promise<void> {
|
async prepareData(): Promise<void> {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -19,6 +21,9 @@ export class IndexQueueManager extends BaseIndexManager<Queue> {
|
||||||
}
|
}
|
||||||
|
|
||||||
async afterProcess(): Promise<void> {
|
async afterProcess(): Promise<void> {
|
||||||
|
this.queueTimes = await this.dataService.queueTimes(
|
||||||
|
this.filterParam.queue_id,
|
||||||
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -30,6 +35,20 @@ export class IndexQueueManager extends BaseIndexManager<Queue> {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getResult(): PaginationResponse<Queue> {
|
||||||
|
const result = this.result;
|
||||||
|
|
||||||
|
return {
|
||||||
|
total: result.total,
|
||||||
|
data: result.data.map((queue) => {
|
||||||
|
return {
|
||||||
|
...queue,
|
||||||
|
time: this.queueTimes[queue.id],
|
||||||
|
};
|
||||||
|
}),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
get selects(): string[] {
|
get selects(): string[] {
|
||||||
return [
|
return [
|
||||||
`${this.tableName}.id`,
|
`${this.tableName}.id`,
|
||||||
|
|
|
@ -32,6 +32,7 @@ import { QueueBucketModel } from './data/models/queue-bucket.model';
|
||||||
import { QueueBucketReadService } from './data/services/queue-bucket';
|
import { QueueBucketReadService } from './data/services/queue-bucket';
|
||||||
import { SplitQueueManager } from './domain/usecases/split-queue.manager';
|
import { SplitQueueManager } from './domain/usecases/split-queue.manager';
|
||||||
import { QueueTransactionCancelHandler } from './infrastructure/handlers/cancel-transaction.handler';
|
import { QueueTransactionCancelHandler } from './infrastructure/handlers/cancel-transaction.handler';
|
||||||
|
import { ItemQueueModel } from '../item-related/item-queue/data/models/item-queue.model';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [
|
||||||
|
@ -44,6 +45,7 @@ import { QueueTransactionCancelHandler } from './infrastructure/handlers/cancel-
|
||||||
QueueModel,
|
QueueModel,
|
||||||
QueueBucketModel,
|
QueueBucketModel,
|
||||||
ItemModel,
|
ItemModel,
|
||||||
|
ItemQueueModel,
|
||||||
TransactionModel,
|
TransactionModel,
|
||||||
],
|
],
|
||||||
CONNECTION_NAME.DEFAULT,
|
CONNECTION_NAME.DEFAULT,
|
||||||
|
|
Loading…
Reference in New Issue