Commit 5bad1819 authored by pooja.mulaguri's avatar pooja.mulaguri

changes for

parent ec053b0d
import pytz
from scripts.errors import DataNotFound
if __name__ == '__main__': if __name__ == '__main__':
from dotenv import load_dotenv from dotenv import load_dotenv
load_dotenv() load_dotenv()
import argparse
import os import os
import time import time
from datetime import datetime from datetime import datetime
import pytz
from scripts.constants import CommonConstants, TagCategoryConstants from scripts.constants import CommonConstants, TagCategoryConstants
from scripts.constants.db_connections import mongo_client from scripts.constants.db_connections import mongo_client
from scripts.core.engine.oee_calculator import OEEEngine from scripts.core.engine.oee_calculator import OEEEngine
...@@ -17,6 +17,7 @@ from scripts.core.handlers.batch_oee_calc_handler import CalculateBatchOEEHandle ...@@ -17,6 +17,7 @@ from scripts.core.handlers.batch_oee_calc_handler import CalculateBatchOEEHandle
from scripts.core.handlers.common_handler import CommonHandler from scripts.core.handlers.common_handler import CommonHandler
from scripts.db.mongo.ilens_configuration.aggregations.customer_projects import ProjectAggregate from scripts.db.mongo.ilens_configuration.aggregations.customer_projects import ProjectAggregate
from scripts.db.mongo.ilens_configuration.collections.customer_projects import CustomerProjects from scripts.db.mongo.ilens_configuration.collections.customer_projects import CustomerProjects
from scripts.errors import DataNotFound
from scripts.logging import logger from scripts.logging import logger
from scripts.schemas.batch_oee import MachineOEERequest, BatchOEEData, OEEDataInsertRequest, OEEDataSaveRequest from scripts.schemas.batch_oee import MachineOEERequest, BatchOEEData, OEEDataInsertRequest, OEEDataSaveRequest
from scripts.utils.common_utils import CommonUtils from scripts.utils.common_utils import CommonUtils
...@@ -36,8 +37,7 @@ class MachineOEECalculator: ...@@ -36,8 +37,7 @@ class MachineOEECalculator:
def calculate_machine_oee(self, request_data: MachineOEERequest): def calculate_machine_oee(self, request_data: MachineOEERequest):
try: try:
hierarchy_dict = self.common_handler.get_valid_oee_monitoring_hierarchy(project_id=request_data.project_id) hierarchy_dict = self.common_handler.get_valid_oee_monitoring_hierarchy(request_data)
# now = datetime.today() - timedelta(days=1)
now = datetime.today() now = datetime.today()
start_timestamp = int(datetime.strptime(request_data.monitor_time, '%H:%M').replace(year=now.year, start_timestamp = int(datetime.strptime(request_data.monitor_time, '%H:%M').replace(year=now.year,
month=now.month, month=now.month,
...@@ -113,15 +113,39 @@ class MachineOEECalculator: ...@@ -113,15 +113,39 @@ class MachineOEECalculator:
if __name__ == '__main__': if __name__ == '__main__':
ap = argparse.ArgumentParser()
ap.add_argument(
"--skip",
"-sk",
required=False,
default=0,
help="Skip Records.",
)
ap.add_argument(
"--limit",
"-li",
required=False,
default=10,
help="Limit Records",
)
ap.add_argument(
"--project_id",
"-p",
required=False,
default=None,
help="project_id",
)
arguments = vars(ap.parse_args())
while True: while True:
project_dict = customer_conn.find_project_by_aggregate(customer_agg.get_tz_mapping_query_with_project_id()) project_dict = customer_conn.find_project_by_aggregate(
customer_agg.get_tz_mapping_query_with_project_id(arguments['project_id']))
project_dict = project_dict[0] if project_dict else {} project_dict = project_dict[0] if project_dict else {}
projects_list = os.environ.get("OEE_PROJECTS", default="project_170")
monitor_start_time = os.environ.get("OEE_START_TIME", default="00:00") monitor_start_time = os.environ.get("OEE_START_TIME", default="00:00")
for project in projects_list.split(","): # for project in projects_list.split(","):
for k, v in project_dict.items():
MachineOEECalculator().calculate_machine_oee( MachineOEECalculator().calculate_machine_oee(
request_data=MachineOEERequest(project_id=project, monitor_time="00:00", request_data=MachineOEERequest(monitor_time="00:00", skip=arguments['skip'], limit=arguments['limit'],
tz=project_dict.get(project, "Asia/Kolkata"))) project_id=k, tz=v))
sleep_time_in_mins = int(os.environ.get("AUTOMATION_SLEEP_TIME_IN_MINS", default=1)) sleep_time_in_mins = int(os.environ.get("AUTOMATION_SLEEP_TIME_IN_MINS", default=1))
sleep_time_in_seconds = sleep_time_in_mins * 60 sleep_time_in_seconds = sleep_time_in_mins * 60
time.sleep(sleep_time_in_seconds) time.sleep(sleep_time_in_seconds)
...@@ -78,6 +78,7 @@ class DBConstants: ...@@ -78,6 +78,7 @@ class DBConstants:
collection_lookup_table = "lookup_table" collection_lookup_table = "lookup_table"
collection_tags = "tags" collection_tags = "tags"
collection_customer_projects = "customer_projects" collection_customer_projects = "customer_projects"
collection_asset="assets"
class EndpointConstants: class EndpointConstants:
...@@ -183,3 +184,10 @@ class TagsCollectionKeys: ...@@ -183,3 +184,10 @@ class TagsCollectionKeys:
KEY_VALUE_LIST = "value_list" KEY_VALUE_LIST = "value_list"
KEY_PRODUCT_ENCRYPTED = "product_encrypted" KEY_PRODUCT_ENCRYPTED = "product_encrypted"
KEY_TAG_CATEGORY_ID = "tag_category_id" KEY_TAG_CATEGORY_ID = "tag_category_id"
class AssetCollectionKeys:
KEY_EQUIPMENT_ID = "equipment_id"
KEY_STATUS = "status"
KEY_TAG_TYPE = "tag_type"
KEY_HIERARCHY = "hierarchy"
...@@ -3,10 +3,12 @@ import os ...@@ -3,10 +3,12 @@ import os
from scripts.constants import TagCategoryConstants from scripts.constants import TagCategoryConstants
from scripts.constants.db_connections import mongo_client from scripts.constants.db_connections import mongo_client
from scripts.core.handlers.tag_handler import TagHierarchyHandler from scripts.core.handlers.tag_handler import TagHierarchyHandler
from scripts.db.mongo.ilens_configuration.collections.asset_overview import AssetOverview
from scripts.db.mongo.ilens_configuration.collections.lookup_table import LookupTable from scripts.db.mongo.ilens_configuration.collections.lookup_table import LookupTable
from scripts.db.mongo.ilens_configuration.collections.tags import Tags from scripts.db.mongo.ilens_configuration.collections.tags import Tags
from scripts.db.mongo.schema.tag_hierarchy import OutputTagsList from scripts.db.mongo.schema.tag_hierarchy import OutputTagsList
from scripts.logging import logger from scripts.logging import logger
from scripts.schemas.batch_oee import MachineOEERequest
class CommonHandler: class CommonHandler:
...@@ -14,6 +16,7 @@ class CommonHandler: ...@@ -14,6 +16,7 @@ class CommonHandler:
self.lookup_table = LookupTable(project_id=project_id, mongo_client=mongo_client) self.lookup_table = LookupTable(project_id=project_id, mongo_client=mongo_client)
self.tag_hierarchy_handler = TagHierarchyHandler(project_id=project_id) self.tag_hierarchy_handler = TagHierarchyHandler(project_id=project_id)
self.tag_conn = Tags(mongo_client=mongo_client, project_id=project_id) self.tag_conn = Tags(mongo_client=mongo_client, project_id=project_id)
self.asset_conn = AssetOverview(mongo_client=mongo_client, project_id=project_id)
def fetch_oee_hierarchy_from_look_up(self, project_id): def fetch_oee_hierarchy_from_look_up(self, project_id):
oee_lookup_name = os.environ.get("OEE_LOOKUP_NAME", default="oee_monitoring") oee_lookup_name = os.environ.get("OEE_LOOKUP_NAME", default="oee_monitoring")
...@@ -27,16 +30,20 @@ class CommonHandler: ...@@ -27,16 +30,20 @@ class CommonHandler:
logger.exception(f"Failed to fetch hierarchy details from lookup {e.args}") logger.exception(f"Failed to fetch hierarchy details from lookup {e.args}")
return {} return {}
def get_valid_oee_monitoring_hierarchy(self, project_id): def get_valid_oee_monitoring_hierarchy(self, request_data: MachineOEERequest):
valid_hierarchies_dict = {} valid_hierarchies_dict = {}
try: try:
oee_lookup_dict = self.fetch_oee_hierarchy_from_look_up(project_id=project_id) hierarchy_list = []
hierarchy_list = list(oee_lookup_dict.values()) asset_data = self.asset_conn.find_by_query(query=dict(project_id=request_data.project_id, monitor_oee=True),
skip=request_data.skip,
limit=request_data.limit)
for each in asset_data:
hierarchy_list.append(each.get('hierarchy'))
if not hierarchy_list: if not hierarchy_list:
logger.debug(f'Hierarchy details not found for the project {project_id} for OEE Monitoring!!!!') logger.debug(f'Hierarchy details for {request_data.project_id} not found for OEE Monitoring!!!!')
return {} return {}
tags_dict_by_hierarchy = self.tag_hierarchy_handler.get_output_tags_for_oee( tags_dict_by_hierarchy = self.tag_hierarchy_handler.get_output_tags_for_oee(
OutputTagsList(project_id=project_id, hierarchy_list=hierarchy_list)) OutputTagsList(project_id=request_data.project_id, hierarchy_list=hierarchy_list))
for each_hierarchy in tags_dict_by_hierarchy: for each_hierarchy in tags_dict_by_hierarchy:
if not tags_dict_by_hierarchy.get(each_hierarchy): if not tags_dict_by_hierarchy.get(each_hierarchy):
logger.debug(f'Tag details not found for the hierarchy {each_hierarchy} for OEE Monitoring!!!!') logger.debug(f'Tag details not found for the hierarchy {each_hierarchy} for OEE Monitoring!!!!')
......
class ProjectAggregate: class ProjectAggregate:
@staticmethod @staticmethod
def get_tz_mapping_query_with_project_id(): def get_tz_mapping_query_with_project_id(project_id=None):
query_json = [ query_json = [
{'$group': { {'$group': {
'_id': None, '_id': None,
...@@ -12,4 +12,6 @@ class ProjectAggregate: ...@@ -12,4 +12,6 @@ class ProjectAggregate:
} }
}, },
}}, {'$replaceRoot': {'newRoot': {'$arrayToObject': '$data'}}}] }}, {'$replaceRoot': {'newRoot': {'$arrayToObject': '$data'}}}]
if project_id:
query_json[0].update({'$match': {'customer_project_id': project_id}})
return query_json return query_json
from typing import Optional
from scripts.constants import DBConstants, AssetCollectionKeys
from scripts.db.mongo.schema import MongoBaseSchema
from scripts.utils.mongo_util import MongoCollectionBaseClass
class AssetOverviewSchema(MongoBaseSchema):
"""
This is the Schema for the Mongo DB Collection.
All datastore and general responses will be following the schema.
"""
equipment_id: Optional[str]
status: Optional[str]
hierarchy: Optional[str]
class AssetOverview(MongoCollectionBaseClass):
def __init__(self, mongo_client, project_id=None):
super().__init__(mongo_client, database=DBConstants.db_metadata,
collection=DBConstants.collection_asset)
self.project_id = project_id
@property
def key_equipment_id(self):
return AssetCollectionKeys.KEY_EQUIPMENT_ID
@property
def key_status(self):
return AssetCollectionKeys.KEY_STATUS
@property
def key_hierarchy(self):
return AssetCollectionKeys.KEY_HIERARCHY
def update_asset_detail(self, hierarchy, data):
"""
The following function will update the record based on hierarchy
"""
query = {self.key_hierarchy: hierarchy}
return self.update_one(query=query, data=data, upsert=True)
def insert_one_asset_status(self, data):
"""
The following function will insert one tag in the
tags collections
:param data:
:return:
"""
return self.insert_one(data)
def find_one_asset_data(self, hierarchy):
"""
The following function will fetch one asset overview data
"""
query = {self.key_hierarchy: hierarchy}
return self.find_one(query=query)
def delete_many_tags(self, **query):
"""
The following function will delete many tag in
tags collection based on the given query
:param query:
:return:
"""
return self.delete_many(query=query)
def delete_one_tag(self, **query):
"""
The following function will delete one tag in
tags collection based on the given query
:param query:
:return:
"""
if query:
return self.delete_one(query=query)
else:
return False
def distinct_tag(self, query_key):
"""
Get a list of distinct values for `key` among all documents
in the result set of this query.
:param query_key:
:return:
"""
return self.distinct(query_key=query_key)
def find_by_query(self, query, skip=None, limit=None, filter_dict=None):
return list(self.find(query=query, filter_dict=filter_dict, skip=skip, limit=limit))
def get_asset_data_by_aggregate(self, query_json: list):
return list(self.aggregate(query_json))
...@@ -233,6 +233,8 @@ class GetOeeServices(BaseModel): ...@@ -233,6 +233,8 @@ class GetOeeServices(BaseModel):
class MachineOEERequest(BaseModel): class MachineOEERequest(BaseModel):
project_id: str skip: Optional[int]=0
limit: Optional[int]=10
project_id: Optional[str]
monitor_time: Optional[str] = "00:00" monitor_time: Optional[str] = "00:00"
tz: Optional[str] = "Asia/Kolkata" tz: Optional[str] = "Asia/Kolkata"
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment