ops(prd): add MES-57 report default 30-day JS and LATERAL SQL optimization
This commit is contained in:
parent
0b6e3e2d92
commit
d15d4f7f47
78
ops/prd-stability/apply-report-definition.py
Normal file
78
ops/prd-stability/apply-report-definition.py
Normal file
@ -0,0 +1,78 @@
|
||||
"""Apply the two exact MES-57 configuration fields atomically; credentials arrive on stdin."""
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
import pathlib
|
||||
import re
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
plan = json.load(sys.stdin)
|
||||
EXPECTED_BEFORE = '13c54b7ea288d4d20f2405bbb519cd531cb92b2de76faef4c20df6dcec21ce16'
|
||||
EXPECTED_SQL = 'ed5dee6feb08650cfa5db8f40fa6bf223b30f91ed19d985b80eaa22fde736c52'
|
||||
EXPECTED_JS = 'e0078f25e806649a3ac3531d21a5394d7197ce2771675b16dedf186fc0b89110'
|
||||
DB_ID = '1041616224854937600'
|
||||
REPORT_ID = '1041614769821519872'
|
||||
assert hashlib.sha256(plan['sql'].encode()).hexdigest() == EXPECTED_SQL
|
||||
assert hashlib.sha256(plan['js'].encode()).hexdigest() == EXPECTED_JS
|
||||
credentials = plan.pop('database')
|
||||
environment = dict(os.environ, MYSQL_PWD=credentials['password'])
|
||||
folder = pathlib.Path('/home/mes/tongzhou-stability-20261105/report-migration')
|
||||
folder.mkdir(parents=True, mode=0o700, exist_ok=True)
|
||||
|
||||
|
||||
def sql(statement):
|
||||
result = subprocess.run(['mysql', '--connect-timeout=8', '-h', '127.0.0.1', '-P', '3306',
|
||||
'-u', credentials['username'], '-N', '-B', '--default-character-set=utf8mb4'],
|
||||
input=statement, env=environment, text=True, capture_output=True, timeout=25)
|
||||
assert result.returncode == 0, f'Report configuration operation failed: {result.stderr}'
|
||||
return result.stdout.strip()
|
||||
|
||||
|
||||
def row(table, identifier):
|
||||
columns = sql("SELECT COLUMN_NAME FROM information_schema.COLUMNS WHERE TABLE_SCHEMA='momx_report' "
|
||||
"AND TABLE_NAME='" + table + "' ORDER BY ORDINAL_POSITION").splitlines()
|
||||
assert columns and all(re.fullmatch(r'[A-Za-z_0-9]+', c) for c in columns)
|
||||
args = ','.join("'" + c + "',`" + c + "`" for c in columns)
|
||||
encoded = sql('SELECT HEX(JSON_OBJECT(' + args + ')) FROM momx_report.' + table + " WHERE id='" + identifier + "'")
|
||||
return json.loads(bytes.fromhex(encoded).decode())
|
||||
|
||||
|
||||
def other_definitions():
|
||||
return sql("SELECT id,COALESCE(SHA2(db_dyn_sql,256),'NULL') FROM momx_report.fw_report_db "
|
||||
"WHERE id<>'" + DB_ID + "' ORDER BY id") + '\n' + sql(
|
||||
"SELECT id,COALESCE(SHA2(JS_STR,256),'NULL') FROM momx_report.fw_report "
|
||||
"WHERE id<>'" + REPORT_ID + "' ORDER BY id")
|
||||
|
||||
|
||||
assert sql('SELECT @@read_only') == '0'
|
||||
assert sql("SELECT COUNT(*) FROM information_schema.TABLES WHERE TABLE_SCHEMA='momx_report' "
|
||||
"AND TABLE_NAME IN ('fw_report','fw_report_db') AND ENGINE='InnoDB'") == '2'
|
||||
before = {'db': row('fw_report_db', DB_ID), 'report': row('fw_report', REPORT_ID)}
|
||||
assert before['db']['fw_report_id'] == REPORT_ID and before['db']['db_code'] == 'LOT_QTY'
|
||||
assert hashlib.sha256(before['db']['db_dyn_sql'].encode()).hexdigest() == EXPECTED_BEFORE
|
||||
assert before['report']['JS_STR'] is None
|
||||
backup = folder / 'definition-before-private.json'
|
||||
assert not backup.exists(), 'A definition backup exists; inspect state before resuming'
|
||||
backup.write_text(json.dumps(before, ensure_ascii=False, indent=2))
|
||||
backup.chmod(0o600)
|
||||
others = other_definitions()
|
||||
new_sql = "CONVERT(UNHEX('" + plan['sql'].encode().hex() + "') USING utf8mb4)"
|
||||
new_js = "CONVERT(UNHEX('" + plan['js'].encode().hex() + "') USING utf8mb4)"
|
||||
changed = sql("SET SESSION lock_wait_timeout=5; SET SESSION innodb_lock_wait_timeout=5; "
|
||||
"UPDATE momx_report.fw_report_db D JOIN momx_report.fw_report R ON R.id=D.fw_report_id "
|
||||
"SET D.db_dyn_sql=" + new_sql + ",R.JS_STR=" + new_js + ","
|
||||
"D.update_by='tongzhou-stability-20261105',D.update_time=NOW(),"
|
||||
"R.update_by='tongzhou-stability-20261105',R.update_time=NOW() "
|
||||
"WHERE D.id='" + DB_ID + "' AND R.id='" + REPORT_ID + "' "
|
||||
"AND SHA2(D.db_dyn_sql,256)='" + EXPECTED_BEFORE + "' AND R.JS_STR IS NULL; SELECT ROW_COUNT();")
|
||||
assert changed == '2', f'Atomic compare-and-set did not update both configuration rows (got {changed})'
|
||||
after = {'db': row('fw_report_db', DB_ID), 'report': row('fw_report', REPORT_ID)}
|
||||
assert hashlib.sha256(after['db']['db_dyn_sql'].encode()).hexdigest() == EXPECTED_SQL
|
||||
assert hashlib.sha256(after['report']['JS_STR'].encode()).hexdigest() == EXPECTED_JS
|
||||
assert others == other_definitions(), 'Another report definition changed during verification'
|
||||
report = {'sourceCommit': plan['sourceCommit'], 'reportId': REPORT_ID, 'dataSetId': DB_ID,
|
||||
'sqlSha256': EXPECTED_SQL, 'defaultJsSha256': EXPECTED_JS, 'updatedConfigurationRows': 2,
|
||||
'otherDefinitionsUnchanged': True, 'businessRowsModified': 0, 'completed': True}
|
||||
(folder / 'definition-result.json').write_text(json.dumps(report, indent=2))
|
||||
print(json.dumps(report))
|
||||
25
ops/prd-stability/report-shoporder-consumption-defaults.js
Normal file
25
ops/prd-stability/report-shoporder-consumption-defaults.js
Normal file
@ -0,0 +1,25 @@
|
||||
function () {
|
||||
var key = 'LOT_QTY__计划开始日期';
|
||||
var compKey = 'LOT_QTY__计划完成日期';
|
||||
var query = this.queryInfo;
|
||||
var params = getRequestUrlParam();
|
||||
if (query[key] || query[key + '_begin'] || query[key + '_end'] ||
|
||||
query[compKey] || query[compKey + '_begin'] || query[compKey + '_end'] ||
|
||||
params[key] || params[key + '_begin'] || params[key + '_end'] ||
|
||||
params[compKey] || params[compKey + '_begin'] || params[compKey + '_end'] ||
|
||||
query['LOT_QTY__工单'] || query['LOT_QTY__产出物料编号'] ||
|
||||
query['LOT_QTY__车间'] || query['LOT_QTY__消耗物料编号'] ||
|
||||
params['SHOP_ORDER'] || params['LOT_QTY__工单']) return;
|
||||
var now = new Date();
|
||||
var begin = new Date(now.getFullYear(), now.getMonth(), now.getDate() - 29);
|
||||
var end = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 23, 59, 59);
|
||||
function format(date) {
|
||||
function pad(value) { return ('0' + value).slice(-2); }
|
||||
return date.getFullYear() + '-' + pad(date.getMonth() + 1) + '-' + pad(date.getDate()) +
|
||||
' ' + pad(date.getHours()) + ':' + pad(date.getMinutes()) + ':' + pad(date.getSeconds());
|
||||
}
|
||||
var field = this.configQueryList.find(function (item) { return item.key === key; });
|
||||
if (!field) throw new Error('Shop order material consumption query date field is missing');
|
||||
handleConfigToQueryInfo(field, query, format(begin) + '|' + format(end));
|
||||
this.queryInfo = Object.assign({}, query);
|
||||
}
|
||||
68
ops/prd-stability/report-shoporder-consumption-query.sql
Normal file
68
ops/prd-stability/report-shoporder-consumption-query.sql
Normal file
@ -0,0 +1,68 @@
|
||||
select
|
||||
so.SHOP_ORDER as '工单',
|
||||
DATE_FORMAT(so.PLANNED_START_DATE, '%Y-%m-%d %H:%i:%s') as '计划开始日期',
|
||||
DATE_FORMAT(so.PLANNED_COMP_DATE, '%Y-%m-%d %H:%i:%s') as '计划完成日期',
|
||||
s.STATUS as '工单状态',
|
||||
s.STATUS_DESCRIPTION as '工单状态描述',
|
||||
out_i.ITEM as '产出物料编号',
|
||||
out_i.DESCRIPTION as '产出物料描述',
|
||||
out_i.UNIT_OF_MEASURE as '产出物料单位',
|
||||
so.QTY as '计划数量',
|
||||
lot_out.OUTPUT_QTY as '产出数量',
|
||||
lot_out.ALL_OUTPUT_QTY as '产出数量总和',
|
||||
substring_index(substring_index(wc.FAB_BASIS_BO, ',', -1), ',', 1) as '车间',
|
||||
lm.CONSUME_ITEM_BO as '消耗物料主键',
|
||||
con_i.ITEM as '消耗物料编号',
|
||||
con_i.DESCRIPTION as '消耗物料描述',
|
||||
con_i.UNIT_OF_MEASURE as '消耗物料单位',
|
||||
lm.CONSUME_QTY as '消耗数量',
|
||||
t4.MAIN_QTY as '冲销数量',
|
||||
lm.CONSUME_QTY - round((t1.qty_re1 * lot_out.ALL_OUTPUT_QTY), 3) as '消耗偏差数量',
|
||||
round((t1.qty_re1 * lot_out.ALL_OUTPUT_QTY), 3) as '理论消耗数量',
|
||||
round(((lm.CONSUME_QTY - round((t1.qty_re1 * lot_out.ALL_OUTPUT_QTY), 3)) / round((t1.qty_re1 * lot_out.ALL_OUTPUT_QTY), 3)) * 100, 2) as '消耗偏差百分比'
|
||||
from wip.shop_order so
|
||||
inner join wip.work_center wc on wc.HANDLE = so.PLANNED_WORK_CENTER_BO
|
||||
left join wip.status s on s.HANDLE = so.STATUS_BO
|
||||
inner join lateral (
|
||||
select
|
||||
ll.ITEM_BO as OUT_ITEM_BO,
|
||||
sum(ll.QTY) as OUTPUT_QTY,
|
||||
sum(sum(case when ls.STATUS = 'C' then ll.QTY end)) over () as ALL_OUTPUT_QTY
|
||||
from wip.lot_basis ll use index (LOT_BASIS_IDX2)
|
||||
inner join wip.lot_state ls on ls.HANDLE = concat('LotState:', ll.HANDLE) and ls.LOT_BASIS_BO = ll.HANDLE and ls.STATUS in ('C','R','Q')
|
||||
where ll.SHOP_ORDER_BO = so.HANDLE
|
||||
group by ll.ITEM_BO
|
||||
) lot_out on true
|
||||
inner join wip.item out_i on out_i.SITE = so.SITE and out_i.HANDLE = lot_out.OUT_ITEM_BO
|
||||
left join lateral (
|
||||
select
|
||||
lm0.ITEM_BO as CONSUME_ITEM_BO,
|
||||
round(sum(lm0.QTY), 3) as CONSUME_QTY
|
||||
from wip.lot_material lm0 use index (idx_lot_material_shop_order)
|
||||
where lm0.SHOP_ORDER_BO = so.HANDLE and ifnull(lm0.REMOVED, 'N') <> 'Y'
|
||||
group by lm0.ITEM_BO
|
||||
) lm on true
|
||||
left join wip.item con_i on con_i.HANDLE = lm.CONSUME_ITEM_BO
|
||||
left join lateral (
|
||||
select
|
||||
round(bc.qty / bc.UNIT_QTY, 3) as qty_re1
|
||||
from wip.bom bom
|
||||
inner join wip.bom_component bc on bc.BOM_BO = bom.HANDLE
|
||||
inner join wip.item i on i.SITE = so.SITE and i.ITEM = con_i.ITEM and i.CURRENT_REVISION = 'Y'
|
||||
where bom.HANDLE = so.PLANNED_BOM_BO
|
||||
and bc.COMPONENT_GBO = concat('ItemBO:', so.SITE, ',', con_i.ITEM, ',#')
|
||||
group by bc.COMPONENT_GBO, bc.QTY, bc.UNIT_QTY, i.ITEM
|
||||
) t1 on true
|
||||
left join lateral (
|
||||
select
|
||||
zlmct.COMPONENT_ITEM,
|
||||
sum(zlmct.MAIN_QTY) as MAIN_QTY
|
||||
from wip.z_lot_material_consumption_transaction zlmct use index (idx_zlmct_shop_order)
|
||||
where zlmct.SITE = so.SITE
|
||||
and zlmct.SHOP_ORDER = so.SHOP_ORDER
|
||||
and zlmct.FAB_NO = substring_index(substring_index(wc.FAB_BASIS_BO, ',', -1), ',', 1)
|
||||
and zlmct.ERP_REVERSE = 'Y'
|
||||
group by zlmct.COMPONENT_ITEM
|
||||
) t4 on t4.COMPONENT_ITEM = con_i.ITEM
|
||||
where so.SITE = '2031'
|
||||
order by so.SHOP_ORDER, con_i.ITEM
|
||||
Loading…
Reference in New Issue
Block a user