瀏覽代碼

Merge branch 'master' of http://git.choozmo.com:3000/choozmo/kw_tools into master

Your Name 3 年之前
父節點
當前提交
84df69c902
共有 2 個文件被更改,包括 37 次插入15 次删除
  1. 25 15
      monitor/GA_Daily.py
  2. 12 0
      monitor/choozmo-ga-beee24b7a4c1.json

+ 25 - 15
monitor/GA_Daily.py

@@ -18,7 +18,7 @@ from googleapiclient.discovery import build
 from oauth2client.service_account import ServiceAccountCredentials
 import dataset
 from datetime import datetime
-import schedule
+
 
 db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/hhh?charset=utf8mb4')
 db.query('delete from ga_pagepath')
@@ -27,7 +27,7 @@ db.begin()
 table=db['ga_pagepath']
 
 SCOPES = ['https://www.googleapis.com/auth/analytics.readonly']
-KEY_FILE_LOCATION = 'c:\\keys\\choozmo-ga-beee24b7a4c1.json' #'c:\\keys\\choozmo-ga-beee24b7a4c1.json'
+KEY_FILE_LOCATION = '/root/src/kw_tools/monitor/choozmo-ga-beee24b7a4c1.json' #'c:\\keys\\choozmo-ga-beee24b7a4c1.json'
 VIEW_ID = '188916214'
 
 # line notify header
@@ -36,25 +36,32 @@ headers = {
         "Content-Type": "application/x-www-form-urlencoded"
 }
 
+com_table = []
+def creat_table():
+  for i in range(0,24):
+    com_table.append([i,2650*i])
+  com_table.append([24,62000])
+  print(com_table)
+
 def send_msg(kw):
   # line notify send message
     current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S') #現在時間
+    hour = datetime.now().strftime('%H') 
     # 判斷是否達標
     complet="否"
-    if int(kw) > 56000 : 
-      complet="是"
+    for i in range(0,25):
+      if int(hour)+1==com_table[i][0]:
+        print(i)
+        if int(kw) > com_table[i][1] : 
+          complet="是"
+      elif int(hour) == 24:
+        if int(kw) > 56000 : 
+          complet="是"
     params = {"message": "\n現在時間: " + current_time + "\n當前pageViews: "+kw + "\n是否達標: " + complet}  
     print(params)
     r = requests.post("https://notify-api.line.me/api/notify",headers=headers, params=params)
     print(r.text)
 
-def scheduleMonitor():
-  # 清空任務
-  schedule.clear()
-  # 建立一個按1小時隔執行任務
-  schedule.every(1).hours.do(main)
-  while True:
-    schedule.run_pending()
 
 def initialize_analyticsreporting():
   """Initializes an Analytics Reporting API V4 service object.
@@ -119,9 +126,9 @@ def print_response(response):
 def main():
   analytics = initialize_analyticsreporting()
 #(FB_|facebook|IG_|LINE_|LINEMP_|qsear.ch)
-
+  current_time = datetime.now().strftime('%Y-%m-%d') #現在時間
   body=[{ 'viewId': VIEW_ID,
-  'dateRanges': [{'startDate': '2021-11-03', 'endDate': '2021-11-03'}],
+  'dateRanges': [{'startDate': current_time, 'endDate': current_time}],
 
   'metrics': [{'expression': 'ga:users'},{'expression': 'ga:newusers'},{'expression': 'ga:sessions'},{'expression': 'ga:pageviews'},{'expression': 'ga:bounceRate'},{'expression': 'ga:pageviewsPerSession'}],
 #  'dimensions': [{'name': 'ga:pagePath'}],
@@ -135,9 +142,12 @@ def main():
   result=[]
   for elmt in ga_dict:
       print(elmt)
-      send_msg(elmt['ga:pageviews'])
+      hour = datetime.now().strftime('%H')
+      if int(hour)+1 > 8 :
+      	send_msg(elmt['ga:pageviews'])
 #      result.append(elmt)
   print('inserting.....')
 
 if __name__ == '__main__':
-  scheduleMonitor()
+  creat_table()
+  main()

+ 12 - 0
monitor/choozmo-ga-beee24b7a4c1.json

@@ -0,0 +1,12 @@
+{
+  "type": "service_account",
+  "project_id": "choozmo-ga",
+  "private_key_id": "beee24b7a4c14b052a7be0db09062d555a1f7faf",
+  "private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQC7X6pGPpaDG5iY\nYdYbaVVRAo2WEvVLIUrDu4MOdIy1H2h8vPvuZiXJ8G/npQ3V8vAY8dZI39Xpv0JS\nyeP5CRM1i3ANcS8Tmw8Eyi6eoejOYqZl1T1rLyD76tgihXG0mUFjWogwCikdmpMF\nGd2c57bN5jADs/VtC9/tGNHud1pSsn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QQQKBgD48\ntMxdnn31uKDdEuba9uXd2nZ0r8sjd8NdrRul9V6mD9wjhB+xSGLUffRY/yuKLMoU\nT2Kf3OPt0MPmJWVO/vLeKdYZmutx+afBniDWgNLKZB4IjbvS+Ie47SB0kwDqDSfC\nYmRV4juEc31pkLZpLX1mTOMZxTBXeOGH/lKJMwCdAoGBAL0Kp57I7+8ihc2izD4s\nEajUMsft8ZCkp4nZjR6HVtvfxTN2lbjvwPL8B8g71usxJ+4j9zdTnRSRGSrNQKzN\nzFIbX46vBx97hGdDlTNx063DravTpNP6BhZQ7GXxqWwKz5DMXTTAoV7NMKgUs7bk\nloIJP31z1HcHYjErKa65SnhE\n-----END PRIVATE KEY-----\n",
+  "client_email": "ga-reporter@choozmo-ga.iam.gserviceaccount.com",
+  "client_id": "100209334727047751767",
+  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
+  "token_uri": "https://oauth2.googleapis.com/token",
+  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
+  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/ga-reporter%40choozmo-ga.iam.gserviceaccount.com"
+}