|
| 1 | +from itertools import chain |
| 2 | +from operator import attrgetter |
| 3 | +from random import Random |
| 4 | +from sampo.schemas.time import Time |
| 5 | + |
| 6 | +from typing import Type |
| 7 | + |
| 8 | +from sampo.utilities.collections_util import build_index |
| 9 | +from sampo.schemas import WorkTimeEstimator, WorkUnit, Worker, WorkerReq, WorkEstimationMode, WorkerProductivityMode |
| 10 | +from idbadapter import MschmAdapter |
| 11 | +from stairsres.res_time_model import ResTimeModel |
| 12 | + |
| 13 | + |
| 14 | +class FieldDevWorkEstimator(WorkTimeEstimator): |
| 15 | + def __init__(self, |
| 16 | + url: str, |
| 17 | + rand: Random = Random()): |
| 18 | + self._url = url |
| 19 | + self._model = ResTimeModel(MschmAdapter(url)) |
| 20 | + self._use_idle = True |
| 21 | + self._estimation_mode = WorkEstimationMode.Realistic |
| 22 | + self.rand = rand |
| 23 | + self._productivity_mode = WorkerProductivityMode.Static |
| 24 | + |
| 25 | + def estimate_time(self, work_unit: WorkUnit, worker_list: list[Worker]): |
| 26 | + w_u = {'name': work_unit.name.split('_stage_')[0], |
| 27 | + 'volume': work_unit.volume, |
| 28 | + 'measurement': work_unit.volume_type} |
| 29 | + w_l = [{'name': w.name, '_count': w.count} for w in worker_list] |
| 30 | + name2worker = build_index(worker_list, attrgetter('name')) |
| 31 | + if self._estimation_mode == WorkEstimationMode.Realistic: |
| 32 | + mode_str = '0.5' |
| 33 | + elif self._estimation_mode == WorkEstimationMode.Optimistic: |
| 34 | + mode_str = '0.1' |
| 35 | + else: |
| 36 | + mode_str = '0.9' |
| 37 | + |
| 38 | + for res_req in work_unit.worker_reqs: |
| 39 | + if name2worker.get(res_req.kind, None) is None: |
| 40 | + w_l.append({'name': res_req.kind, '_count': 0}) |
| 41 | + if w_u['name'] in ['Начало работ по марке', 'Окончание работ по марке', 'NaN', 'start of project', |
| 42 | + 'finish of project']: |
| 43 | + return Time(0) |
| 44 | + try: |
| 45 | + return Time(int(self._model.estimate_time(work_unit=w_u, worker_list=w_l, mode=mode_str))) |
| 46 | + except: |
| 47 | + print(w_u['name']) |
| 48 | + |
| 49 | + def find_work_resources(self, work_name: str, work_volume: float, |
| 50 | + resource_name: list[str] | None = None, |
| 51 | + measurement: str = None) \ |
| 52 | + -> list[WorkerReq]: |
| 53 | + if work_name in ['Начало работ по марке', 'Окончание работ по марке', 'NaN', 'start of project', |
| 54 | + 'finish of project']: |
| 55 | + return [] |
| 56 | + worker_req_dict = self._model.get_resources_volumes(work_name=work_name, work_volume=work_volume, |
| 57 | + measurement=measurement) |
| 58 | + |
| 59 | + worker_reqs = [[WorkerReq(kind=req['kind'], |
| 60 | + volume=Time(req['volume']), |
| 61 | + min_count=req['min_count'], |
| 62 | + max_count=req['max_count']) for req in worker_req] for |
| 63 | + worker_req in |
| 64 | + worker_req_dict.values()] |
| 65 | + return list(chain.from_iterable(worker_reqs)) |
| 66 | + |
| 67 | + def set_estimation_mode(self, use_idle: bool = True, mode: WorkEstimationMode = WorkEstimationMode.Realistic): |
| 68 | + self._use_idle = use_idle |
| 69 | + self._estimation_mode = mode |
| 70 | + |
| 71 | + def set_productivity_mode(self, mode: WorkerProductivityMode = WorkerProductivityMode.Static): |
| 72 | + self._productivity_mode = mode |
| 73 | + |
| 74 | + def get_recreate_info(self) -> tuple[Type, tuple]: |
| 75 | + return FieldDevWorkEstimator, tuple(self._url) |
0 commit comments