Skip to content

Instantly share code, notes, and snippets.

@diggzhang
Last active September 22, 2016 01:38
Show Gist options
  • Save diggzhang/ccd56155bf9d037f8e8557237a698658 to your computer and use it in GitHub Desktop.
Save diggzhang/ccd56155bf9d037f8e8557237a698658 to your computer and use it in GitHub Desktop.
# _*_ coding:utf-8 _*_
"""
该脚本用户修改表每日更新备份状态
"""
from pymongo import MongoClient
import sys
import datetime
onion_v4_DB_instance = MongoClient('10.8.8.111', 27017)['eventsV4']
backup_status_collection = onion_v4_DB_instance['backupStatus']
def main(collectionName, collectionUpdateStatus):
if list(backup_status_collection.find({"collectionName": collectionName})) == []:
print("%s 是第一次更新")%(collectionName)
backup_status_collection.update_one({"collectionName": collectionName}, {"$set": {"state": int(collectionUpdateStatus)}}, upsert=True)
backup_status_collection.update_one({"collectionName": collectionName}, {"$set": {"lastUpdateTime": datetime.datetime.now()}})
backup_status_collection.update_one({"collectionName": collectionName}, {"$set": {"updateTime": datetime.datetime.now()}})
else:
print("更新 %s 状态为 %s")%(collectionName, collectionUpdateStatus)
lastUpdateTime = list(backup_status_collection.find({"collectionName": collectionName}))[0]['updateTime']
backup_status_collection.update_one({"collectionName": collectionName}, {"$set": {"state": int(collectionUpdateStatus)}})
backup_status_collection.update_one({"collectionName": collectionName}, {"$set": {"lastUpdateTime": lastUpdateTime}})
backup_status_collection.update_one({"collectionName": collectionName}, {"$set": {"updateTime": datetime.datetime.now()}})
try:
if len(sys.argv) != 3:
print("传参错误 arg[0] collection arg[1] status ")
print("$ python collectionStatus.py orderEvents 1")
else:
collectionName = sys.argv[1]
collectionUpdateStatus = sys.argv[2]
main(collectionName, collectionUpdateStatus)
except:
raise
# _*_ coding:utf-8 _*_
"""
该脚本用户修改表每日更新备份状态
备份地址: https://gist.github.com/diggzhang/ccd56155bf9d037f8e8557237a698658
"""
from pymongo import MongoClient
import pymongo
import sys
import datetime
onion_v4_DB_instance = MongoClient('10.8.8.111', 27017)['eventsV4']
backup_status_collection = onion_v4_DB_instance['backupStatus']
# 为了获取订单表的最新状态,这个脚本只能执行在订单表完全清洗好后
order_events_colllection = onion_v4_DB_instance['orderEvents']
def main(collectionName, collectionUpdateStatus):
if list(backup_status_collection.find({"collectionName": collectionName})) == []:
print("%s 是第一次更新")%(collectionName)
backup_status_collection.update_one({"collectionName": collectionName}, {"$set": {"state": int(collectionUpdateStatus)}}, upsert=True)
backup_status_collection.update_one({"collectionName": collectionName}, {"$set": {"lastUpdateTime": datetime.datetime.now()}})
backup_status_collection.update_one({"collectionName": collectionName}, {"$set": {"updateTime": datetime.datetime.now()}})
else:
print("更新 %s 状态为 %s")%(collectionName, collectionUpdateStatus)
lastUpdateTime = list(backup_status_collection.find({"collectionName": collectionName}))[0]['updateTime']
backup_status_collection.update_one({"collectionName": collectionName}, {"$set": {"state": int(collectionUpdateStatus)}})
backup_status_collection.update_one({"collectionName": collectionName}, {"$set": {"lastUpdateTime": lastUpdateTime}})
backup_status_collection.update_one({"collectionName": collectionName}, {"$set": {"updateTime": datetime.datetime.now()}})
try:
if len(sys.argv) != 3:
print("传参错误 arg[0] collection arg[1] status ")
print("$ python collectionStatus.py <表名字> <状态0/1>")
print("$ python collectionStatus.py orderEvents 1")
else:
collectionName = sys.argv[1]
collectionUpdateStatus = sys.argv[2]
main(collectionName, collectionUpdateStatus)
# 针对orderEvents做特殊处理,查找到orderEvents的最后一条记录时间,并添加到backupStatus表内,需求详情@国杰
if collectionName == 'orderEvents':
last_order_event_doc_orderTime = list(order_events_colllection.find({"orderTime": {"$exists": True}}).sort('_id', pymongo.DESCENDING))[:1][0]['orderTime']
backup_status_collection.update_one({"collectionName": collectionName}, {"$set": {"lastOrderTime": last_order_event_doc_orderTime}})
except:
raise
@diggzhang
Copy link
Author

以ordertime排序

# _*_ coding:utf-8 _*_

"""
    该脚本用户修改表每日更新备份状态
    备份地址: https://gist.github.com/diggzhang/ccd56155bf9d037f8e8557237a698658
"""

from pymongo import MongoClient
import pymongo
import sys
import datetime

onion_v4_DB_instance = MongoClient('10.8.8.111', 27017)['eventsV4']
backup_status_collection = onion_v4_DB_instance['backupStatus']
# 为了获取订单表的最新状态,这个脚本只能执行在订单表完全清洗好后
order_events_colllection = onion_v4_DB_instance['orderEvents']

def main(collectionName, collectionUpdateStatus):
    if list(backup_status_collection.find({"collectionName": collectionName})) == []:
        print("%s 是第一次更新")%(collectionName)
        backup_status_collection.update_one({"collectionName": collectionName}, {"$set": {"state": int(collectionUpdateStatus)}}, upsert=True)
        backup_status_collection.update_one({"collectionName": collectionName}, {"$set": {"lastUpdateTime": datetime.datetime.now()}})
        backup_status_collection.update_one({"collectionName": collectionName}, {"$set": {"updateTime": datetime.datetime.now()}})
    else:
        print("更新 %s 状态为 %s")%(collectionName, collectionUpdateStatus)
        lastUpdateTime = list(backup_status_collection.find({"collectionName": collectionName}))[0]['updateTime']
        backup_status_collection.update_one({"collectionName": collectionName}, {"$set": {"state": int(collectionUpdateStatus)}})
        backup_status_collection.update_one({"collectionName": collectionName}, {"$set": {"lastUpdateTime": lastUpdateTime}})
        backup_status_collection.update_one({"collectionName": collectionName}, {"$set": {"updateTime": datetime.datetime.now()}})

try:
    if len(sys.argv) != 3:
        print("传参错误 arg[0] collection arg[1] status ")
        print("$ python collectionStatus.py <表名字> <状态0/1>")
        print("$ python collectionStatus.py orderEvents 1")
    else:
        collectionName = sys.argv[1]
        collectionUpdateStatus = sys.argv[2]
        main(collectionName, collectionUpdateStatus)

        # 针对orderEvents做特殊处理,查找到orderEvents的最后一条记录时间,并添加到backupStatus表内,需求详情@国杰
        if collectionName == 'orderEvents':
            last_order_event_doc_orderTime = list(order_events_colllection.find({"orderTime": {"$exists": True}}).sort('orderTime', pymongo.DESCENDING))[:1][0]['orderTime']
            backup_status_collection.update_one({"collectionName": collectionName}, {"$set": {"lastOrderTime": last_order_event_doc_orderTime}})
except:
    raise

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment