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 { BaseDataService } from 'src/core/modules/data/service/base-data.service';
|
||||
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()
|
||||
export class QueueDataService extends BaseReadService<QueueModel> {
|
||||
constructor(
|
||||
@InjectRepository(QueueModel, CONNECTION_NAME.DEFAULT)
|
||||
private repo: Repository<QueueModel>,
|
||||
|
||||
@InjectRepository(ItemQueueModel, CONNECTION_NAME.DEFAULT)
|
||||
private itemQueueRepo: Repository<ItemQueueModel>,
|
||||
) {
|
||||
super(repo);
|
||||
}
|
||||
|
||||
async queueItems(item_queue_id: string[]): Promise<QueueModel[]> {
|
||||
return this.repo.find({
|
||||
relations: ['item', 'item.item', 'item.item.item_queue'],
|
||||
async queueTimes(item_queue_id: string): Promise<any> {
|
||||
const queueTimes = {};
|
||||
let now = moment().valueOf();
|
||||
const itemQueue = await this.itemQueueRepo.findOne({
|
||||
relations: ['items'],
|
||||
where: {
|
||||
item: { item: { item_queue: { id: In(item_queue_id) } } },
|
||||
},
|
||||
order: {
|
||||
time: 'DESC',
|
||||
id: item_queue_id,
|
||||
},
|
||||
});
|
||||
|
||||
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';
|
||||
import { Queue } from '../entities/queue.entity';
|
||||
import * as moment from 'moment';
|
||||
import { PaginationResponse } from 'src/core/response/domain/ok-response.interface';
|
||||
|
||||
@Injectable()
|
||||
export class IndexQueueManager extends BaseIndexManager<Queue> {
|
||||
private queueTimes = {};
|
||||
async prepareData(): Promise<void> {
|
||||
return;
|
||||
}
|
||||
|
@ -19,6 +21,9 @@ export class IndexQueueManager extends BaseIndexManager<Queue> {
|
|||
}
|
||||
|
||||
async afterProcess(): Promise<void> {
|
||||
this.queueTimes = await this.dataService.queueTimes(
|
||||
this.filterParam.queue_id,
|
||||
);
|
||||
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[] {
|
||||
return [
|
||||
`${this.tableName}.id`,
|
||||
|
|
|
@ -32,6 +32,7 @@ import { QueueBucketModel } from './data/models/queue-bucket.model';
|
|||
import { QueueBucketReadService } from './data/services/queue-bucket';
|
||||
import { SplitQueueManager } from './domain/usecases/split-queue.manager';
|
||||
import { QueueTransactionCancelHandler } from './infrastructure/handlers/cancel-transaction.handler';
|
||||
import { ItemQueueModel } from '../item-related/item-queue/data/models/item-queue.model';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
|
@ -44,6 +45,7 @@ import { QueueTransactionCancelHandler } from './infrastructure/handlers/cancel-
|
|||
QueueModel,
|
||||
QueueBucketModel,
|
||||
ItemModel,
|
||||
ItemQueueModel,
|
||||
TransactionModel,
|
||||
],
|
||||
CONNECTION_NAME.DEFAULT,
|
||||
|
|
Loading…
Reference in New Issue