def on_update(self): """ Writes the .txt for this page and if write_content is checked, it will write out a .html file """ if not frappe.flags.in_import and getattr(frappe.get_conf(),'developer_mode', 0) and self.is_standard: from frappe.modules.export_file import export_to_files from frappe.modules import get_module_path, scrub import os # json export_to_files(record_list=[['Web Form', self.name]]) # write files path = os.path.join(get_module_path(self.module), 'web_form', scrub(self.name), scrub(self.name)) # js if not os.path.exists(path + '.js'): with open(path + '.js', 'w') as f: f.write("""frappe.ready(function() { // bind events here })""") # py if not os.path.exists(path + '.py'): with open(path + '.py', 'w') as f: f.write("""from __future__ import unicode_literals import frappe def get_context(context): # do your magic here pass """)
def export_customizations(module, doctype, sync_on_migrate=0, with_permissions=0): """Export Custom Field and Property Setter for the current document to the app folder. This will be synced with bench migrate""" if not frappe.get_conf().developer_mode: raise Exception('Not developer mode') custom = {'custom_fields': [], 'property_setters': [], 'custom_perms': [], 'doctype': doctype, 'sync_on_migrate': 1} def add(_doctype): custom['custom_fields'] += frappe.get_all('Custom Field', fields='*', filters={'dt': _doctype}) custom['property_setters'] += frappe.get_all('Property Setter', fields='*', filters={'doc_type': _doctype}) add(doctype) if with_permissions: custom['custom_perms'] = frappe.get_all('Custom DocPerm', fields='*', filters={'parent': doctype}) # also update the custom fields and property setters for all child tables for d in frappe.get_meta(doctype).get_table_fields(): export_customizations(module, d.options, sync_on_migrate, with_permissions) if custom["custom_fields"] or custom["property_setters"] or custom["custom_perms"]: folder_path = os.path.join(get_module_path(module), 'custom') if not os.path.exists(folder_path): os.makedirs(folder_path) path = os.path.join(folder_path, scrub(doctype)+ '.json') with open(path, 'w') as f: f.write(frappe.as_json(custom)) frappe.msgprint(_('Customizations for <b>{0}</b> exported to:<br>{1}').format(doctype,path))
def export_customizations(module, doctype, sync_on_migrate=0): """Export Custom Field and Property Setter for the current document to the app folder. This will be synced with bench migrate""" if not frappe.get_conf().developer_mode: raise 'Not developer mode' custom = {'custom_fields': [], 'property_setters': [], 'doctype': doctype, 'sync_on_migrate': 1} def add(_doctype): custom['custom_fields'] += frappe.get_all('Custom Field', fields='*', filters={'dt': _doctype}) custom['property_setters'] += frappe.get_all('Property Setter', fields='*', filters={'doc_type': _doctype}) add(doctype) # add custom fields and property setters for all child tables for d in frappe.get_meta(doctype).get_table_fields(): add(d.options) folder_path = os.path.join(get_module_path(module), 'custom') if not os.path.exists(folder_path): os.makedirs(folder_path) path = os.path.join(folder_path, scrub(doctype)+ '.json') with open(path, 'w') as f: f.write(frappe.as_json(custom)) frappe.msgprint('Customizations exported to {0}'.format(path))
def start_scheduler(): '''Run enqueue_events_for_all_sites every 2 minutes (default). Specify scheduler_interval in seconds in common_site_config.json''' interval = frappe.get_conf().scheduler_interval or 240 schedule.every(interval).seconds.do(enqueue_events_for_all_sites) while True: schedule.run_pending() time.sleep(1)
def export_module_json(doc, is_standard, module): """Make a folder for the given doc and add its json file (make it a standard object that will be synced)""" if (not frappe.flags.in_import and getattr(frappe.get_conf(),'developer_mode', 0) and is_standard): from frappe.modules.export_file import export_to_files # json export_to_files(record_list=[[doc.doctype, doc.name]], record_module=module) path = os.path.join(frappe.get_module_path(module), scrub(doc.doctype), scrub(doc.name), scrub(doc.name)) return path
def get_queues_timeout(): common_site_config = frappe.get_conf() custom_workers_config = common_site_config.get("workers", {}) default_timeout = 300 return { "default": default_timeout, "short": default_timeout, "long": 1500, **{ worker: config.get("timeout", default_timeout) for worker, config in custom_workers_config.items() }, }
def export_customizations(module, doctype, sync_on_migrate=0, with_permissions=0): """Export Custom Field and Property Setter for the current document to the app folder. This will be synced with bench migrate""" if not frappe.get_conf().developer_mode: raise Exception('Not developer mode') custom = { 'custom_fields': [], 'property_setters': [], 'custom_perms': [], 'doctype': doctype, 'sync_on_migrate': 1 } def add(_doctype): custom['custom_fields'] += frappe.get_all('Custom Field', fields='*', filters={'dt': _doctype}) custom['property_setters'] += frappe.get_all( 'Property Setter', fields='*', filters={'doc_type': _doctype}) add(doctype) if with_permissions: custom['custom_perms'] = frappe.get_all('Custom DocPerm', fields='*', filters={'parent': doctype}) # also update the custom fields and property setters for all child tables for d in frappe.get_meta(doctype).get_table_fields(): export_customizations(module, d.options, sync_on_migrate, with_permissions) if custom["custom_fields"] or custom["property_setters"] or custom[ "custom_perms"]: folder_path = os.path.join(get_module_path(module), 'custom') if not os.path.exists(folder_path): os.makedirs(folder_path) path = os.path.join(folder_path, scrub(doctype) + '.json') with open(path, 'w') as f: f.write(frappe.as_json(custom)) frappe.msgprint( _('Customizations for <b>{0}</b> exported to:<br>{1}').format( doctype, path))
def run_ui_tests(context, app, headless=False, parallel=True, with_coverage=False, ci_build_id=None): "Run UI tests" site = get_site(context) app_base_path = os.path.abspath( os.path.join(frappe.get_app_path(app), '..')) site_url = frappe.utils.get_site_url(site) admin_password = frappe.get_conf(site).admin_password # override baseUrl using env variable site_env = f'CYPRESS_baseUrl={site_url}' password_env = f'CYPRESS_adminPassword={admin_password}' if admin_password else '' coverage_env = f'CYPRESS_coverage={str(with_coverage).lower()}' os.chdir(app_base_path) node_bin = subprocess.getoutput("npm bin") cypress_path = f"{node_bin}/cypress" plugin_path = f"{node_bin}/../cypress-file-upload" testing_library_path = f"{node_bin}/../@testing-library" coverage_plugin_path = f"{node_bin}/../@cypress/code-coverage" # check if cypress in path...if not, install it. if not (os.path.exists(cypress_path) and os.path.exists(plugin_path) and os.path.exists(testing_library_path) and os.path.exists(coverage_plugin_path) and cint(subprocess.getoutput("npm view cypress version")[:1]) >= 6): # install cypress click.secho("Installing Cypress...", fg="yellow") frappe.commands.popen( "yarn add cypress@^6 cypress-file-upload@^5 @testing-library/cypress@^8 @cypress/code-coverage@^3 --no-lockfile" ) # run for headless mode run_or_open = 'run --browser firefox --record' if headless else 'open' formatted_command = f'{site_env} {password_env} {coverage_env} {cypress_path} {run_or_open}' if parallel: formatted_command += ' --parallel' if ci_build_id: formatted_command += f' --ci-build-id {ci_build_id}' click.secho("Running Cypress...", fg="yellow") frappe.commands.popen(formatted_command, cwd=app_base_path, raise_err=True)
def export_module_json(doc, is_standard, module): """Make a folder for the given doc and add its json file (make it a standard object that will be synced)""" if (not frappe.flags.in_import and getattr(frappe.get_conf(),'developer_mode', 0) and is_standard): from frappe.modules.export_file import export_to_files # json export_to_files(record_list=[[doc.doctype, doc.name]], record_module=module, create_init=is_standard) path = os.path.join(frappe.get_module_path(module), scrub(doc.doctype), scrub(doc.name), scrub(doc.name)) return path
def run_ui_tests(context, app, headless=False, parallel=True, ci_build_id=None): "Run UI tests" site = get_site(context) app_base_path = os.path.abspath(os.path.join(frappe.get_app_path(app), "..")) site_url = frappe.utils.get_site_url(site) admin_password = frappe.get_conf(site).admin_password # override baseUrl using env variable site_env = f"CYPRESS_baseUrl={site_url}" password_env = f"CYPRESS_adminPassword={admin_password}" if admin_password else "" os.chdir(app_base_path) node_bin = subprocess.getoutput("npm bin") cypress_path = f"{node_bin}/cypress" plugin_path = f"{node_bin}/../cypress-file-upload" testing_library_path = f"{node_bin}/../@testing-library" # check if cypress in path...if not, install it. if not ( os.path.exists(cypress_path) and os.path.exists(plugin_path) and os.path.exists(testing_library_path) and cint(subprocess.getoutput("npm view cypress version")[:1]) >= 6 ): # install cypress click.secho("Installing Cypress...", fg="yellow") frappe.commands.popen( "yarn add cypress@^6 cypress-file-upload@^5 @4tw/cypress-drag-drop@^2 @testing-library/cypress@^8 --no-lockfile" ) # run for headless mode run_or_open = "run --browser chrome --record" if headless else "open" command = "{site_env} {password_env} {cypress} {run_or_open}" formatted_command = command.format( site_env=site_env, password_env=password_env, cypress=cypress_path, run_or_open=run_or_open ) if parallel: formatted_command += " --parallel" if ci_build_id: formatted_command += f" --ci-build-id {ci_build_id}" click.secho("Running Cypress...", fg="yellow") frappe.commands.popen(formatted_command, cwd=app_base_path, raise_err=True)
def execute(): if hasattr(frappe, 'get_site_config'): # for legacy frappe versions config = frappe.get_site_config() else: config = frappe.get_conf() if frappe.db.sql( ''' SELECT * FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=%s AND TABLE_NAME="tabPrint Node Action" AND COLUMN_NAME = "print_on_submit"''', (config.db_name, )): frappe.db.sql( 'UPDATE `tabPrint Node Action` SET `print_on` = "Submit" WHERE `print_on_submit`=1;' ) frappe.db.commit()
def run_ui_tests(context, app, headless=False): "Run UI tests" site = get_site(context) app_base_path = os.path.abspath(os.path.join(frappe.get_app_path(app), '..')) site_url = frappe.utils.get_site_url(site) admin_password = frappe.get_conf(site).admin_password # override baseUrl using env variable site_env = 'CYPRESS_baseUrl={}'.format(site_url) password_env = 'CYPRESS_adminPassword={}'.format(admin_password) if admin_password else '' # run for headless mode run_or_open = 'run' if headless else 'open' command = '{site_env} {password_env} yarn run cypress:{run_or_open}' formatted_command = command.format(site_env=site_env, password_env=password_env, run_or_open=run_or_open) frappe.commands.popen(formatted_command, cwd=app_base_path, raise_err=True)
def browse(context, site): '''Opens the site on web browser''' import webbrowser site = context.sites[0] if context.sites else site if not site: click.echo('''Please provide site name\n\nUsage:\n\tbench browse [site-name]\nor\n\tbench --site [site-name] browse''') return site = site.lower() if site in frappe.utils.get_sites(): webbrowser.open('http://{site}:{port}'.format( site=site, port=frappe.get_conf(site).webserver_port ), new=2) else: click.echo("\nSite named \033[1m{}\033[0m doesn't exist\n".format(site))
def send_welcome_mail_to_user(self): from frappe.utils import get_url link = self.reset_password() subject = None method = frappe.get_hooks("welcome_email") if method: subject = frappe.get_attr(method[-1])() if not subject: site_name = frappe.db.get_default('site_name') or frappe.get_conf().get("site_name") if site_name: subject = _("Welcome to {0}".format(site_name)) else: subject = _("Complete Registration") self.send_login_mail(subject, "new_user", dict( link=link, site_url=get_url(), ))
def run_tests(context, app=None, module=None, doctype=None, test=(), driver=None, profile=False, coverage=False, junit_xml_output=False, ui_tests = False, doctype_list_path=None, skip_test_records=False, skip_before_tests=False, failfast=False): "Run tests" import frappe.test_runner tests = test site = get_site(context) allow_tests = frappe.get_conf(site).allow_tests if not (allow_tests or os.environ.get('CI')): click.secho('Testing is disabled for the site!', bold=True) click.secho('You can enable tests by entering following command:') click.secho('bench --site {0} set-config allow_tests true'.format(site), fg='green') return frappe.init(site=site) frappe.flags.skip_before_tests = skip_before_tests frappe.flags.skip_test_records = skip_test_records if coverage: # Generate coverage report only for app that is being tested source_path = os.path.join(get_bench_path(), 'apps', app or 'frappe') cov = Coverage(source=[source_path], omit=['*.html', '*.js', '*.xml', '*.css', '*/doctype/*/*_dashboard.py', '*/patches/*']) cov.start() ret = frappe.test_runner.main(app, module, doctype, context.verbose, tests=tests, force=context.force, profile=profile, junit_xml_output=junit_xml_output, ui_tests = ui_tests, doctype_list_path = doctype_list_path, failfast=failfast) if coverage: cov.stop() cov.save() if len(ret.failures) == 0 and len(ret.errors) == 0: ret = 0 if os.environ.get('CI'): sys.exit(ret)
def run_ui_tests(context, app, headless=False): "Run UI tests" site = get_site(context) app_base_path = os.path.abspath(os.path.join(frappe.get_app_path(app), '..')) site_url = frappe.utils.get_site_url(site) admin_password = frappe.get_conf(site).admin_password # override baseUrl using env variable site_env = 'CYPRESS_baseUrl={}'.format(site_url) password_env = 'CYPRESS_adminPassword={}'.format(admin_password) if admin_password else '' # run for headless mode run_or_open = 'run' if headless else 'open' # Brian - This may not work, simply substituting "npm" for "yarn" # Here's some info on the cypress CLI https://docs.cypress.io/guides/guides/command-line.html command = '{site_env} {password_env} npm run cypress:{run_or_open}' formatted_command = command.format(site_env=site_env, password_env=password_env, run_or_open=run_or_open) frappe.commands.popen(formatted_command, cwd=app_base_path, raise_err=True)
def iotPublish(topic=None, payload=None): site_name = cstr(frappe.local.site) path = frappe.get_site_path('../crt/') caPath = path + "rootCA.pem" topic = site_name + "/" + topic certPath = path + "certificate.pem.crt" keyPath = path + "private.pem.key" endpoint = frappe.get_conf().get("iot_server") url = "https://" + endpoint + ':8443/topics/' + topic parameters = (('qos', '0'), ) print("payload : ", payload) res = requests.post(url, params=parameters, json=payload, cert=(certPath, keyPath, caPath)) return res
def get_next_execution(self): CRON_MAP = { "Yearly": "0 0 1 1 *", "Annual": "0 0 1 1 *", "Monthly": "0 0 1 * *", "Monthly Long": "0 0 1 * *", "Weekly": "0 0 * * 0", "Weekly Long": "0 0 * * 0", "Daily": "0 0 * * *", "Daily Long": "0 0 * * *", "Hourly": "0 * * * *", "Hourly Long": "0 * * * *", "All": "0/" + str((frappe.get_conf().scheduler_interval or 240) // 60) + " * * * *", } if not self.cron_format: self.cron_format = CRON_MAP[self.frequency] return croniter( self.cron_format, get_datetime(self.last_execution or datetime(2000, 1, 1)) ).get_next(datetime)
def get_data(from_date, to_date, node, signal): site_name = cstr(frappe.local.site) es = Elasticsearch([frappe.get_conf().get("elastic_norita_server")],scheme="https", port=443) doc = {"size":0,"aggs":{"machine_performance":{"date_histogram":{"field":"id","interval":"8h","format":"yy-MM-dd HH:mm","time_zone":"+07:00","offset":"+0h"},"aggs":{"pm_output":{"max":{"field":"192_168_1_128.PM1_Line_Speed"}}}}}} res = es.search(index=site_name, body=doc) res = res['aggregations']['machine_performance']['buckets'] wss = frappe.get_doc("Work Shift Settings") label, data = [], [] for r in res: t = time.localtime(r['key']) l = "{}-{}-{}".format(t.tm_mday, t.tm_mon, t.tm_year) if r['max_output1']['value'] == None: continue label.append(r['key_as_string']) data.append([r['key_as_string'], int(r['max_output1']['value']), int(r['max_output2']['value'])]) # if t.hour == t_start_shift1.hour and abs(t.minute - t_start_shift1.minute) < 2: # data.append([r['key_as_string'], r['max_output1']['value'], r['max_output2']['value'],]) # elif t.hour == t_start_shift2.hour and abs(t.minute - t_start_shift2.minute) < 2: # elif t.hour == t_start_shift3.hour and abs(t.minute - t_start_shift3.minute) < 2: print(data) return label, data
def run_ui_tests(context, app, headless=False): "Run UI tests" site = get_site(context) app_base_path = os.path.abspath( os.path.join(frappe.get_app_path(app), '..')) site_url = frappe.utils.get_site_url(site) admin_password = frappe.get_conf(site).admin_password # override baseUrl using env variable site_env = 'CYPRESS_baseUrl={}'.format(site_url) password_env = 'CYPRESS_adminPassword={}'.format( admin_password) if admin_password else '' os.chdir(app_base_path) node_bin = subprocess.getoutput("npm bin") cypress_path = "{0}/cypress".format(node_bin) plugin_path = "{0}/cypress-file-upload".format(node_bin) # check if cypress in path...if not, install it. if not (os.path.exists(cypress_path) or os.path.exists(plugin_path)) \ or not subprocess.getoutput("npm view cypress version").startswith("6."): # install cypress click.secho("Installing Cypress...", fg="yellow") frappe.commands.popen( "yarn add cypress@^6 cypress-file-upload@^5 --no-lockfile") # run for headless mode run_or_open = 'run --browser firefox --record --key 4a48f41c-11b3-425b-aa88-c58048fa69eb' if headless else 'open' command = '{site_env} {password_env} {cypress} {run_or_open}' formatted_command = command.format(site_env=site_env, password_env=password_env, cypress=cypress_path, run_or_open=run_or_open) click.secho("Running Cypress...", fg="yellow") frappe.commands.popen(formatted_command, cwd=app_base_path, raise_err=True)
def check_all_task_status(): for d in frappe.get_all("IOT Batch Task", "name", filters={ "status": "New", "docstatus": 1 }): doc = frappe.get_doc("IOT Batch Task", d.name) time_delta = now_datetime() - get_datetime(doc.modified) min_timeout = (frappe.get_conf().scheduler_interval or 240) if time_delta.total_seconds() <= min_timeout: doc.run_task() else: frappe.db.set_value("IOT Batch Task", doc.name, "status", "Error") for d in frappe.get_all("IOT Batch Task", "name", filters={ "status": "Running", "docstatus": 1 }): # filters = {"status": ["in", ["Running", "Partial"]], "docstatus": 1}): doc = frappe.get_doc("IOT Batch Task", d.name) doc.update_status()
def on_update(self): """ Writes the .txt for this page and if write_content is checked, it will write out a .html file """ if not frappe.flags.in_import and getattr( frappe.get_conf(), 'developer_mode', 0) and self.is_standard: from frappe.modules.export_file import export_to_files from frappe.modules import get_module_path, scrub import os # json export_to_files(record_list=[['Web Form', self.name]]) # write files path = os.path.join(get_module_path(self.module), 'web_form', scrub(self.name), scrub(self.name)) # js if not os.path.exists(path + '.js'): with open(path + '.js', 'w') as f: f.write("""frappe.ready(function() { // bind events here })""") # py if not os.path.exists(path + '.py'): with open(path + '.py', 'w') as f: f.write("""from __future__ import unicode_literals import frappe def get_context(context): # do your magic here pass """)
def get_site_url(site): return 'http://{site}:{port}'.format( site=site, port=frappe.get_conf(site).webserver_port )
import socket from six.moves.urllib.parse import urlparse from frappe import get_conf config = get_conf() REDIS_KEYS = ('redis_cache', 'redis_queue', 'redis_socketio') def is_open(ip, port, timeout=10): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.settimeout(timeout) try: s.connect((ip, int(port))) s.shutdown(socket.SHUT_RDWR) return True except socket.error: return False finally: s.close() def check_database(): db_type = config.get("db_type", "mariadb") db_host = config.get("db_host", "localhost") db_port = config.get("db_port", 3306 if db_type == "mariadb" else 5342) return {db_type: is_open(db_host, db_port)} def check_redis(redis_services=None): services = redis_services or REDIS_KEYS
def get_data(from_date, to_date, interval, node, signal): site_name = cstr(frappe.local.site) # print("#####################################################################") # print(site_name) signal = signal.replace(",", "").strip() sg_signal = frappe.get_all("Signal", filters={ "parent": node, "label": signal }, fields=['ip', 'min', 'max'])[0] sg_signal_str = sg_signal.ip.replace('.', '_') + '.' + signal.replace( " ", "_") es = Elasticsearch([frappe.get_conf().get("elastic_server")], scheme="https", port=443) # doc = {"size":0,"query":{"constant_score":{"filter":{"range":{"id":{"gte":from_date,"lte":to_date,"format":"yyyy-MM-dd","time_zone":"+07:00"}}}}},"aggs":{"signal":{"date_histogram":{"field":"id","interval":"8h","format":"yy-MM-dd HH:mm","time_zone":"+07:00","offset":"+0h"},"aggs":{"max_output1":{"max":{"field":"192_168_1_128.PM1_Line_signal"}}}}}} query = { "size": 0, "query": { "constant_score": { "filter": { "range": { "id": { "gte": from_date, "lte": to_date, "format": "yyyy-MM-dd", "time_zone": "+07:00" } } } } }, "aggs": { "signal": { "date_histogram": { "field": "id", "interval": interval, "format": "yy-MM-dd HH:mm", "time_zone": "+07:00", "offset": "+0h" }, "aggs": { "signal_avg": { "avg": { "field": sg_signal_str } }, "signal_max": { "max": { "field": sg_signal_str } }, "signal_min": { "min": { "field": sg_signal_str } } } } } } res = es.search(index=site_name, body=query) res = res['aggregations']['signal']['buckets'] label, data, d, avail, perf = [], [], {}, {}, {} timespan = 8 * 60 # in minutes since the signal is in minutes for r in res: if r['signal_avg']['value'] == None: continue utc_dt = datetime.utcfromtimestamp(r['key'] / 1000) t = convert_utc_to_user_timezone(utc_dt) if interval == "24h": date_str = t.strftime('%y-%m-%d') else: date_str = t.strftime('%y-%m-%d %H:%M') d[date_str] = [ round(float(r['signal_avg']['value']), 2), round(float(r['signal_min']['value']), 2), round(float(r['signal_max']['value']), 2), ] od = collections.OrderedDict(sorted(d.items())) data = [[k] + v for k, v in od.items()] return label, data
def check_database(): config = get_conf() db_type = config.get("db_type", "mariadb") db_host = config.get("db_host", "localhost") db_port = config.get("db_port", 3306 if db_type == "mariadb" else 5432) return {db_type: is_open(db_host, db_port)}
def get_bench_id(): return frappe.get_conf().get('bench_id', get_bench_path().strip('/').replace('/', '-'))
def get_data(from_date, to_date, node, speed, mOnOff): site_name = cstr(frappe.local.site) speed = speed.replace(",", "").strip() mOnOff = mOnOff.replace(",", "").strip() sg_speed = frappe.get_all("Signal", filters={ "parent": node, "label": speed }, fields=['ip', 'min', 'max'])[0] sg_mOnOff = frappe.get_all("Signal", filters={ "parent": node, "label": mOnOff }, fields=['ip'])[0] sg_speed_str = sg_speed.ip.replace('.', '_') + '.' + speed.replace( " ", "_") sg_mOnOff_str = sg_mOnOff.ip.replace('.', '_') + '.' + mOnOff.replace( " ", "_") es = Elasticsearch([frappe.get_conf().get("elastic_server")], scheme="https", port=443) # doc = {"size":0,"query":{"constant_score":{"filter":{"range":{"id":{"gte":from_date,"lte":to_date,"format":"yyyy-MM-dd","time_zone":"+07:00"}}}}},"aggs":{"machine_performance":{"date_histogram":{"field":"id","interval":"8h","format":"yy-MM-dd HH:mm","time_zone":"+07:00","offset":"+0h"},"aggs":{"max_output1":{"max":{"field":"192_168_1_128.PM1_Line_Speed"}}}}}} query = { "size": 0, "query": { "constant_score": { "filter": { "range": { "id": { "gte": from_date, "lte": to_date, "format": "yyyy-MM-dd", "time_zone": "+07:00" } } } } }, "aggs": { "machine_performance": { "date_histogram": { "field": "id", "interval": "1d", "format": "yy-MM-dd HH:mm", "time_zone": "+07:00", "offset": "+0h" }, "aggs": { "avg_output1": { "avg": { "field": sg_speed_str } }, "avg_pm_on1": { "avg": { "field": sg_mOnOff_str } }, "pm_output": { "bucket_script": { "buckets_path": { "tavg_output1": "avg_output1", "tavg_pm_on1": "avg_pm_on1" }, "script": "params.tavg_output1 * params.tavg_pm_on1" } } } } } } res = es.search(index=site_name, body=query) res = res['aggregations']['machine_performance']['buckets'] wss = frappe.get_doc("Work Shift Settings") t_start_shift1 = datetime.strptime(wss.shift_1_start, "%H:%M:%S").time() t_start_shift2 = datetime.strptime(wss.shift_2_start, "%H:%M:%S").time() t_start_shift3 = datetime.strptime(wss.shift_3_start, "%H:%M:%S").time() label, data, d, avail, perf = [], [], {}, {}, {} timespan = 8 * 60 # in minutes since the speed is in minutes for r in res: if r['avg_pm_on1']['value'] == None: continue utc_dt = datetime.utcfromtimestamp(r['key'] / 1000 - 1) t = convert_utc_to_user_timezone(utc_dt) date_str = t.strftime('%y-%m-%d') if date_str not in d: d[date_str] = [0, 0, 0, 0.0, 0.0, 0.0] avail[date_str] = 0 # helper to averaging availability perf[date_str] = 0 # Averaging Availibility, avail[date_str] += float(r['avg_pm_on1']['value']) perf[date_str] += float(r['avg_output1']['value']) d[date_str][3] = round(avail[date_str] * 100 / 3, 1) # 3 is the shift sum is 3 d[date_str][4] = round(perf[date_str] * 100 / sg_speed.max / 3, 1) d[date_str][5] = round( d[date_str][3] * d[date_str][4] / 100, 2 ) # OEE without defects calculation, divide by 100 to compensate one of percentage if get_time_delta( t.time(), t_start_shift1 ) < 2: #and (t_old.tm_mon != t.tm_mon or t_old.tm_mday != t.tm_mday or t_old.tm_year != t.tm_year) d[date_str][0] = int(r['pm_output']['value']) * timespan # print('shift 1: ' + str(r['pm_output']['value'])) elif get_time_delta(t.time(), t_start_shift2) < 2: d[date_str][1] = int(r['pm_output']['value']) * timespan # print('shift 2: ' + str(r['pm_output']['value'])) elif get_time_delta( t.time(), t_start_shift3) > 86280 or get_time_delta( t.time(), t_start_shift3) < 2: # 86280 = 23 hours*3600 + 58 min* 60 if date_str not in d: d[date_str] = [0, 0, 0] d[date_str][2] = int(r['pm_output']['value']) * timespan # print('shift 3: ' + str(r['pm_output']['value'])) od = collections.OrderedDict(sorted(d.items())) data = [[k] + v for k, v in od.items()] return label, data
def can_migrate(): if not cint(frappe.get_conf().get("migrate_over_http", 0)): frappe.throw("Please set migrate_over_http: 1 in site conf")
def get_agGrid_licenseKey(): return frappe.get_conf().agGrid_licenseKey or ""
DATETIME_FORMAT = '%Y-%m-%d %H:%M:%S' cron_map = { "yearly": "0 0 1 1 *", "annual": "0 0 1 1 *", "monthly": "0 0 1 * *", "monthly_long": "0 0 1 * *", "weekly": "0 0 * * 0", "weekly_long": "0 0 * * 0", "daily": "0 0 * * *", "daily_long": "0 0 * * *", "midnight": "0 0 * * *", "hourly": "0 * * * *", "hourly_long": "0 * * * *", "all": "0/" + str((frappe.get_conf().scheduler_interval or 240) // 60) + " * * * *", } def start_scheduler(): '''Run enqueue_events_for_all_sites every 2 minutes (default). Specify scheduler_interval in seconds in common_site_config.json''' schedule.every(60).seconds.do(enqueue_events_for_all_sites) while True: schedule.run_pending() time.sleep(1) def enqueue_events_for_all_sites(): '''Loop through sites and enqueue events that are not already queued'''
def read_config(self): config = frappe.get_conf() or {} curr_site = os.path.join("currentsite.txt") config.default_site = frappe.read_file(curr_site) or frappe.local.site return config
def getProductionOutput(): es = Elasticsearch([frappe.get_conf().get("elastic_server")],scheme="https", port=443,) doc = {"size":0,"aggs":{"machine_performance":{"date_histogram":{"field":"id","interval":"8h","format":"yyyy-MM-dd HH:mm:ss","time_zone":"+07:00","offset":"+0h"},"aggs":{"max_output1":{"max":{"field":"192_168_1_128.M1_Product_Output"}},"max_output2":{"max":{"field":"192_168_1_128.M2_Product_Output"}},"adder":{"bucket_script":{"buckets_path":{"tmax_output1":"max_output1","tmax_output2":"max_output2"},"script":"params.tmax_output1 + params.tmax_output2"}}}}}} res = es.search(index="lionwings", body=doc) return res
"0 0 * * 0", "weekly_long": "0 0 * * 0", "daily": "0 0 * * *", "daily_long": "0 0 * * *", "midnight": "0 0 * * *", "hourly": "0 * * * *", "hourly_long": "0 * * * *", "all": "0/" + str( (frappe.get_conf().scheduler_interval or 240) // 60) + " * * * *", } def start_scheduler(): '''Run enqueue_events_for_all_sites every 2 minutes (default). Specify scheduler_interval in seconds in common_site_config.json''' schedule.every(60).seconds.do(enqueue_events_for_all_sites) while True: schedule.run_pending() time.sleep(1) def enqueue_events_for_all_sites():
def validate_bearer_with_introspection(token): is_valid = False email = None cached_token = frappe.cache().get_value(f"cc_bearer|{token}") now = datetime.datetime.now() form_dict = frappe.local.form_dict try: if cached_token: token_json = json.loads(cached_token) exp = token_json.get("exp") email = frappe.get_value("User", token_json.get("email"), "email") if exp: exp = datetime.datetime.fromtimestamp(int(token_json.get("exp"))) else: exp = now if now < exp and email: is_valid = True else: frappe.cache().delete_key(f"cc_bearer|{token}") else: client_id = frappe.get_conf().get("castlecraft_client_id") client_secret = frappe.get_conf().get("castlecraft_client_secret") introspect_url = frappe.get_conf().get("castlecraft_introspect_url") introspect_token_key = frappe.get_conf().get( "castlecraft_introspect_token_key", "token" ) if not introspect_url or not client_id or not client_secret: return data = {} data[introspect_token_key] = token r = requests.post( introspect_url, data=data, auth=HTTPBasicAuth(client_id, client_secret), headers={"Content-Type": "application/x-www-form-urlencoded"}, ) token_response = r.json() exp = token_response.get("exp") if exp: exp = datetime.datetime.fromtimestamp(int(token_response.get("exp"))) else: exp = now + datetime.timedelta( 0, int(token_response.get("expires_in")) or 0 ) if now < exp: email = frappe.get_value("User", token_response.get("email"), "email") p = requests.get( frappe.get_conf().get("castlecraft_userinfo_url"), headers={"Authorization": f"Bearer {token}"}, ) body = p.json() body["exp"] = round(exp.timestamp()) if email and body.get("email"): frappe.cache().set_value( f"cc_bearer|{token}", json.dumps(body), expires_in_sec=exp - now ) is_valid = True elif frappe.get_conf().get( "castlecraft_create_user_on_auth" ) and token_response.get("email"): user = create_and_save_user(body) frappe.cache().set_value( f"cc_bearer|{token}", json.dumps(body), expires_in_sec=exp - now ) is_valid = True email = user.email if is_valid: frappe.set_user(email) frappe.local.form_dict = form_dict except: frappe.log_error(traceback.format_exc(), "castlecraft_bearer_auth_failed")