#!/usr/bin/env python

__author__ = "Yaroslav Litvinov"
__copyright__ = "Copyright 2016, Rackspace Inc."
__email__ = "yaroslav.litvinov@rackspace.com"

from logging import getLogger
from collections import namedtuple
from datetime import datetime
from bson.json_util import loads

# initial load completed --init-load-save-ts ts, status=1
# init load fail --init-load-fail, status=0
# sync ok --ts-sync-ok ts, status=2
# sync bad --ts-sync-fail ts, status=0
# oplog handle ok --ts-hanlde-ok ts, status=3
# oplog handle bad --ts-hanlde-fail ts, status=0

STATUS_INITIAL_LOAD = 0
STATUS_OPLOG_SYNC = 1
STATUS_OPLOG_APPLY = 2
STATUS_OPLOG_RESYNC = 3

def timestamp_str_to_object(timestamp_str):
    """ Return bson Timestamp object
    timestamp_str -- str like 'Timestamp(1464278289, 1)' """
    if timestamp_str == 'None':
        return None
    if timestamp_str and len(timestamp_str):
        spl = timestamp_str.split(',')
        tmstmp = spl[0].split('(')[1].strip()
        increm = spl[1].split(')')[0].strip()
        fmt = '{"$timestamp": {"t": %s, "i": %s}}'
        res_str = fmt % (tmstmp, increm)
        return loads(res_str)
    else: