def stockIntegrity(params, data): Debug = DebugManager.DebugManager() Debug.start() global msia_tz, date_retrieve_limit result = [] today = DateTime.now(tzinfo=msia_tz) start_date = DateTime.getDaysAgo(date_retrieve_limit, datefrom=today) durations = DateTime.getBetween([start_date, today], element='date', offset=24)['order'] # offset 24 hour to include today state_data = fn.getNestedElement(data, 'state') facility_data_by_state = fn.getNestedElement(data, 'state_facility') check_data = combinedFacilityList(data=facility_data_by_state) result = getIntegrity(params={ 'durations': durations, }, data={ 'facility': facility_data_by_state, 'state': state_data, 'to_update': result, 'check_data': check_data, }) updateStateData(result) result = list(sorted(result, key=lambda k: k['name'], reverse=False)) Debug.end() Debug.show('Model.Structure.stockIntegrity') return result
def generateTemplate(params): result = {}; report_keys = fn.getNestedElement(params, 'keys.report', ['procurement', 'budget']); first_date = fn.getNestedElement(params, 'first_date'); last_date = fn.getNestedElement(params, 'last_date'); state_by = fn.getNestedElement(params, 'state_by'); states = fn.getNestedElement(params, 'states'); today = DateTime.now(tzinfo=msia_tz); # date only for rk in report_keys: if rk not in result: result[rk] = {}; for date in DateTime.getBetween([first_date, last_date], element='date')['order']: end_date_of_month = DateTime.getDaysAgo(days_to_crawl=1, datefrom=DateTime.getNextMonth(DateTime.convertDateTimeFromString(date))); year_month = date[:7]; day_diff = DateTime.getDifferenceBetweenDuration([today, end_date_of_month]); if day_diff >= 0: date_str = DateTime.toString(today); else: date_str = DateTime.toString(end_date_of_month); if date_str not in result[rk]: result[rk][date_str] = {}; result[rk][date_str].update({ 'date': date_str, }) for idx in range(0, len(states)): state = states[idx][state_by]; result[rk][date_str].update({ state: 0, }); return result;
def check(params): global msia_tz, date_retrieve_limit, date_count, collection_name dbManager = SharedMemoryManager.getInstance() db = dbManager.query() today = DateTime.now(tzinfo=msia_tz) start_date = DateTime.getDaysAgo(date_retrieve_limit, datefrom=today) durations = DateTime.getBetween([start_date, today], element='date', offset=24)['order'] # offset 24 to include today Logger.v('durations', durations) data = db[collection_name].aggregate([{ '$match': { 'state_updated_at': { '$in': durations }, 'facility_updated_at': { '$in': durations } } }, { '$project': { '_id': 0, 'inserted_at': 0, 'updated_at': 0 } }]) data = list(data) Logger.v('Total stock issue integrity in', date_retrieve_limit, 'days:', len(data)) state_data = {} facility_data_by_state = {} for idx in range(0, len(data)): row = data[idx] state_code = fn.getNestedElement(row, 'state_code') if state_code not in facility_data_by_state: facility_data_by_state[state_code] = {} state_data = addIntegrityData(data={ 'row': row, 'to_update': state_data }, category='state') facility_data_by_state[state_code] = addIntegrityData( data={ 'row': row, 'to_update': facility_data_by_state[state_code] }, category='facility') if date_count > date_retrieve_limit: # limit loop data/ show data in N days break date_count = 0 # reset to 0th day return { 'state': state_data, 'state_facility': facility_data_by_state, }
def getQuery(params): duration = fn.getNestedElement(params, 'duration', ['2020-03-30', '2020-03-30']) item_codes = fn.getNestedElement(params, 'item_codes', []) item_desc = fn.getNestedElement(params, 'item_desc') group_by_list = fn.getNestedElement(params, 'group_by', []) facility_group = fn.getNestedElement(params, 'facility_group', []) dates = DateTime.getBetween(duration, element='date')['order'] query = {} if item_desc: # TEST wildcard search query.update({'item_desc': { '$regex': item_desc.lower() }}) if item_codes: query.update({ 'item_code': { '$in': [c.lower() for c in item_codes] }, }) if facility_group: facility_code_list = ModelFacility.getFacilityCodeList( facility_group=facility_group) query.update({ 'facility_code': { '$in': facility_code_list }, }) for gbl in group_by_list: gbl_id = gbl['id'] gbl_value = gbl['value'] val = gbl_value if type(val) == str: val = val.lower() if gbl_id == 'state': val = val.replace('_', ' ') query.update({ query_key[gbl_id]: val, }) Logger.v('query', query) return query