dongzhaorui 3 years ago
parent
commit
4143378424
2 changed files with 26 additions and 22 deletions
  1. 11 6
      find_source/common/databases.py
  2. 15 16
      find_source/common/tools.py

+ 11 - 6
find_source/common/databases.py

@@ -4,22 +4,27 @@ import redis
 import requests
 from elasticsearch import Elasticsearch
 from pymongo.collection import Collection
-
+from pymongo.uri_parser import SCHEME
 from config.load import mongo_conf, redis_conf, es_conf, analyze_url
 
 # ---------------------------------- mongo ----------------------------------
 MONGO_URI_CLIENTS = {}    # a dictionary hold all client with uri as key
 
 
-def mongo_client(cfg=None, host=None, port=None, fork=False, **kwargs):
-    if host is not None and port is not None:
-        uri = f'mongodb://{host}:{port}'
+def mongo_client(host=None, port=None, fork=False, **kwargs):
+    if host is None:
+        host = mongo_conf["host"]
+    if port is None:
+        port = mongo_conf["port"]
+
+    if host is not None and host.startswith(SCHEME):
+        uri = host
     else:
-        _cfg = (cfg or mongo_conf)
-        uri = f'mongodb://{_cfg["host"]}:{_cfg["port"]}'
+        uri = f'mongodb://{host}:{port}'
 
     if fork:
         return pymongo.MongoClient(uri, **kwargs)
+
     global MONGO_URI_CLIENTS
     matched_client = MONGO_URI_CLIENTS.get(uri)
     if matched_client is None:

+ 15 - 16
find_source/common/tools.py

@@ -24,34 +24,33 @@ def get_current_date(fmt="%Y-%m-%d"):
     return datetime.datetime.now().strftime(fmt)
 
 
-def ms2date(ms: int, fmt="%Y-%m-%d %H:%M:%S"):
-    """毫秒转日期"""
-    timestamp = float(ms / 1000)
-    time_array = time.localtime(timestamp)
-    return time.strftime(fmt, time_array)
-
-
-def convert2type(ts_str):
-    """字符串类型时间戳转成整型"""
-    return int(float(ts_str) / 1000)
+def convert2type(ts: str):
+    """时间字符串转换成整型"""
+    return int(float(ts) / 1000)
 
 
-def ts2date(ts_str, fmt="%Y-%m-%d %H:%M:%S") -> str:
+def ts2date(ts, fmt="%Y-%m-%d %H:%M:%S") -> str:
     """
     时间戳转成日期
 
-    :param ts_str: 毫秒级时间戳
+    :param ts: 时间戳
     :param fmt: 日期格式
     :return: 日期
     """
-    timestamp = int(float(ts_str) / 1000)
-    time_array = time.localtime(timestamp)
+    # ms = int(ts / 1000)
+    time_array = time.localtime(int(ts))
     return time.strftime(fmt, time_array)
 
 
-def date2ts(date_str: str, fmt="%Y-%m-%d"):
+def ms2date(ms, fmt="%Y-%m-%d %H:%M:%S"):
+    """毫秒转日期"""
+    timestamp = float(ms / 1000)
+    return ts2date(timestamp, fmt)
+
+
+def date2ts(date: str, fmt="%Y-%m-%d"):
     """日期转成时间戳"""
-    time_array = time.strptime(date_str, fmt)
+    time_array = time.strptime(date, fmt)
     timestamp = int(time.mktime(time_array))
     return timestamp