# -*- coding: utf-8 -*-
"""部署后回读校验：线上 workbench_data.json 是否就是本次合并产物。"""
import hashlib
import json
import urllib.parse
import urllib.request

BASE = 'https://bier-sales-d0gatbvlx288724e9-1458438710.tcloudbaseapp.com/'
LOCAL = '/Users/liuxinyuan/Desktop/Hermes输出-工作类/数据/workbench_data.json'
PATH = urllib.parse.quote('数据/workbench_data.json')

with open(LOCAL, 'rb') as f:
    raw = f.read()
local_md5 = hashlib.md5(raw).hexdigest()
local = json.loads(raw.decode('utf-8'))

req = urllib.request.Request(BASE + PATH, headers={'Cache-Control': 'no-cache'})
with urllib.request.urlopen(req, timeout=120) as r:
    body = r.read()
remote_md5 = hashlib.md5(body).hexdigest()
remote = json.loads(body.decode('utf-8'))

print('local  bytes=%d md5=%s merged_at=%s' % (len(raw), local_md5, local.get('_merged_at')))
print('remote bytes=%d md5=%s merged_at=%s' % (len(body), remote_md5, remote.get('_merged_at')))
print('MD5 MATCH:', local_md5 == remote_md5)
print('remote mcpCustomers:', len((remote.get('mcpCustomers') or {}).get('customers') or []))
print('remote complaints:', len((remote.get('complaints') or {}).get('items') or []))
print('remote shipments total:', (remote.get('shipments') or {}).get('total'))
print('remote keys:', len(remote.keys()))
