Kaynağa Gözat

增加定时任务

panwang 3 yıl önce
ebeveyn
işleme
174031285d
5 değiştirilmiş dosya ile 85 ekleme ve 49 silme
  1. BIN
      __pycache__/app.cpython-37.pyc
  2. BIN
      __pycache__/utils.cpython-37.pyc
  3. 1 0
      app.py
  4. 4 24
      test.py
  5. 80 25
      utils.py

BIN
__pycache__/app.cpython-37.pyc


BIN
__pycache__/utils.cpython-37.pyc


+ 1 - 0
app.py

@@ -24,6 +24,7 @@ registry = CollectorRegistry(auto_describe=False)
 # 定义后台执行调度器
 scheduler = BackgroundScheduler()
 scheduler.add_job(func=update_request_url_list, trigger="interval", seconds=300)
+scheduler.add_job(func=produce_video_ratio_cnt, trigger="interval", seconds=86400)
 scheduler.start()
 atexit.register(lambda: scheduler.shutdown())
 

+ 4 - 24
test.py

@@ -290,29 +290,9 @@ if __name__ == '__main__':
     # if res[0] is not None:
     #     print(res[0])
 
+    from datetime import date, timedelta
 
-    start_time = int(time.strftime("%Y%m%d", time.localtime())) * 1000000000000000
-    end_time = int(time.strftime("%Y%m%d%H%M", time.localtime())) * 100000000000
-    query_sql = ("select count(*) as totalCount,"
-                 "sum(case when produce_status in (5,6,7,8) then 1 else 0 end) as successCount,"
-                 "sum(case when produce_status = 99 then 1 else 0 end) as failCount , "
-                 "sum(case when produce_status in(5,6,7,8) then 1 else 0 end) as processingCount,"
-                 "sum(case when produce_status in(5,6,7,8) and (rate < 0.5 or rate is null) then 1 else 0 end) processingCount1,"
-                 "sum(case when produce_status in(5,6,7,8) and rate >= 0.5 and  rate < 0.7 then 1 else 0 end) processingCount2  ,"
-                 "sum(case when produce_status in(5,6,7,8) and rate >= 0.7 and  rate < 1 then 1 else 0 end) processingCount3 ,"
-                 "sum(case when produce_status in(5,6,7,8) and rate >= 1 and  rate < 1.5 then 1 else 0 end) processingCount4  ,"
-                 "sum(case when produce_status in(5,6,7,8) and rate >= 1.5 and  rate < 2 then 2 else 0 end) processingCount5 ,"
-                 "sum(case when produce_status in(5,6,7,8) and  rate >2  then 1 else 0 end) processingCount6  from  "
-                 "(select t1.project_id, t1.produce_status, round((t2.last_connect_timestamp - t1.submit_timestamp) / (t1.video_duration/1000), 1) as rate  from produce_video_project t1  "
-                 "left join produce_video_project_connect_time t2 on t1.project_id = t2.project_id "
-                 "where t1.project_id > %s and t1.project_id < %s and t1.app_type not in (1,13,15)) s1 " %(start_time ,end_time)
-
-                )
-    res = db_query(query_sql)
-    print(res)
-    if res[0] is not None:
-
-        print(res[0],res[1],res[2],res[3],res[4], res[5],res[6],res[7],res[8],res[9])
-    print(start_time, end_time)
-
+    yesterday = (date.today() + timedelta(days=-1)).strftime("%Y%m%d")
+    today = date.today().strftime("%Y%m%d")
+    print(yesterday,today)
 

+ 80 - 25
utils.py

@@ -15,6 +15,8 @@ import  datetime
 import  pymysql
 from sqlalchemy.sql import func
 from model_longvideo import produce_video_project
+from datetime import date, timedelta
+
 client = AcsClient('LTAI4GBWbFvvXoXsSVBe1o9f', 'kRAikWitb4kDxaAyBqNrmLmllMEDO3', 'cn-hangzhou')
 
 
@@ -274,8 +276,21 @@ def  db_query(sql):
     cursor.execute(sql)
     res = cursor.fetchone()
 
+
+
     return  res
+def  db_query_prod(sql):
+    HOST = 'rr-bp1x9785e8h5452bi157.mysql.rds.aliyuncs.com'
+    PORT = '3306'
+    DATABASE = 'longvideo'
+    USERNAME = 'devops'
+    PASSWORD = 'devops@123456'
+    conn = pymysql.connect(host=HOST, user=USERNAME, password=PASSWORD, database=DATABASE, charset="utf8")
+    cursor = conn.cursor()
+    cursor.execute(sql)
+    res = cursor.fetchone()
 
+    return  res
 
 def  count_qps_avgtime(appType):
     endpoint = 'cn-hangzhou.log.aliyuncs.com'
@@ -457,7 +472,7 @@ def  produce_video_task_cnt():
     query_sql = ("select count(*) as totalCount,"
                  "sum(case when produce_status in (5,6,7,8) then 1 else 0 end) as successCount,"
                  "sum(case when produce_status = 99 then 1 else 0 end) as failCount , "
-                 "sum(case when produce_status in(5,6,7,8) then 1 else 0 end) as processingCount,"
+                 "sum(case when produce_status in(0,1,2,3,4) then 1 else 0 end) as processingCount,"
                  "sum(case when produce_status in(5,6,7,8) and (rate < 0.5 or rate is null) then 1 else 0 end) processingCount1,"
                  "sum(case when produce_status in(5,6,7,8) and rate >= 0.5 and  rate < 0.7 then 1 else 0 end) processingCount2  ,"
                  "sum(case when produce_status in(5,6,7,8) and rate >= 0.7 and  rate < 1 then 1 else 0 end) processingCount3 ,"
@@ -473,28 +488,68 @@ def  produce_video_task_cnt():
 
     return  res
 
+
+
+def  produce_video_ratio_cnt():
+    HOST = 'rm-bp1k5853td1r25g3n690.mysql.rds.aliyuncs.com'
+    PORT = '3306'
+    DATABASE = 'devops'
+    USERNAME = 'devops'
+    PASSWORD = 'devops@123456'
+    conn = pymysql.connect(host=HOST, user=USERNAME, password=PASSWORD, database=DATABASE, charset="utf8")
+    cursor = conn.cursor()
+    start_time = int((date.today() + timedelta(days=-6)).strftime("%Y%m%d")) * 1000000000000000
+    # end_time = int(date.today().strftime("%Y%m%d")) * 1000000000000000
+    end_time = int((date.today() + timedelta(days=-5)).strftime("%Y%m%d")) * 1000000000000000
+
+    sql = ("select count(*) as totalCount, "
+            "sum(case when rate < 0.5 then 1 else 0 end) as r1,"
+            "sum(case when (rate >= 0.5 and rate < 0.7) then 1 else 0 end) as r2,"
+            "sum(case when (rate >= 0.7 and rate < 1) then 1 else 0 end) as r3,"
+            "sum(case when (rate >= 1 and rate < 1.5) then 1 else 0 end) as r4,"
+            "sum(case when (rate >= 1.5 and rate < 2) then 1 else 0 end) as r5,"
+            "sum(case when rate >= 2 then 1 else 0 end) as r6 from "
+            "(select project_id, round((video_duration/1000) / (produce_done_timestamp - submit_timestamp),1) as rate from "
+            "(select project_id, draftbox_id,video_duration,submit_timestamp,produce_done_timestamp from "
+            "(select project_id, draftbox_id,video_duration,submit_timestamp,produce_done_timestamp from "
+            "produce_video_project where project_id > %s and project_id < %s and app_type not in (1,13,15) and produce_status in (5,6,7,8) order by video_duration desc limit 100000) t1 group by draftbox_id) t2) t3"%(start_time, end_time)
+
+    )
+    print(sql)
+    y_date = (date.today() + timedelta(days=-1)).strftime("%Y%m%d")
+    res = db_query(sql)
+    if res[0]:
+        cnt = res[0]
+    else:
+        cnt = 0
+    if res[1]:
+        r1 = res[1]
+    else:
+        r1 = 0
+    if res[2]:
+        r2 = res[2]
+    else:
+        r2 = 0
+    if res[3]:
+        r3 = res[3]
+    else:
+        r3 = 0
+    if res[4]:
+        r4 = res[4]
+    else:
+        r4 = 0
+    if res[5]:
+        r5 = res[5]
+    else:
+        r5 = 0
+    if res[6]:
+        r6 = res[6]
+    else:
+        r6 = 0
+    sql = ("insert into produce_video_ratio (`cnt`,`r1`,`r2`,`r3`,`r4`,`r5`,`r6`,`date`) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)"%(cnt, r1, r2, r3, r4, r5, r6, y_date)
+
+    )
+    cursor.execute(sql)
+    conn.commit()
 if __name__ == '__main__':
-    res = produce_video_task_cnt()
-    if res:
-        if res[0]:
-            total_cnt = res[0]
-        if res[1]:
-            success_cnt = res[1]
-        if res[2]:
-            fail_cnt = res[2]
-        if res[3]:
-            process_cnt = res[3]
-        if res[4]:
-            r1 = res[4]
-        if res[5]:
-            r2 = res[5]
-        if res[5]:
-            r3 = res[6]
-        if res[5]:
-            r4 = res[7]
-        if res[5]:
-            r5 = res[8]
-        if res[5]:
-            r6 = res[9]
-
-    print(total_cnt, success_cnt,fail_cnt,process_cnt,r1)
+    produce_video_ratio_cnt()