From 543e12e41a3cf35c7b1179b025a15996715891b7 Mon Sep 17 00:00:00 2001 From: mandarons Date: Fri, 17 Mar 2023 03:32:52 +0000 Subject: [PATCH] deploy: 7220e1404f750e3bf63bd7bebab35b2bc5a091c4 --- test-coverage/index.html | 10 +- test-coverage/src___init___py.html | 2 +- test-coverage/src_config_parser_py.html | 2 +- test-coverage/src_email_message_py.html | 2 +- test-coverage/src_notify_py.html | 2 +- test-coverage/src_sync_drive_py.html | 2 +- test-coverage/src_sync_photos_py.html | 2 +- test-coverage/src_sync_py.html | 2 +- test-coverage/src_usage_py.html | 218 +++++++++++++----------- test-coverage/status.json | 2 +- 10 files changed, 135 insertions(+), 109 deletions(-) diff --git a/test-coverage/index.html b/test-coverage/index.html index 2fab40aac..99406ac65 100644 --- a/test-coverage/index.html +++ b/test-coverage/index.html @@ -53,10 +53,10 @@

Coverage report: Total - 756 + 775 0 0 - 100% + 100% @@ -111,10 +111,10 @@

Coverage report: src/usage.py - 67 + 86 0 0 - 100% + 100% @@ -126,7 +126,7 @@

Coverage report:

coverage.py v5.4, - created at 2023-03-16 16:28 +0000 + created at 2023-03-17 03:31 +0000

diff --git a/test-coverage/src___init___py.html b/test-coverage/src___init___py.html index d617feb8b..53352a015 100644 --- a/test-coverage/src___init___py.html +++ b/test-coverage/src___init___py.html @@ -194,7 +194,7 @@

« index     coverage.py v5.4, - created at 2023-03-16 16:28 +0000 + created at 2023-03-17 03:31 +0000

diff --git a/test-coverage/src_config_parser_py.html b/test-coverage/src_config_parser_py.html index 80d9b4dd3..bb22d2990 100644 --- a/test-coverage/src_config_parser_py.html +++ b/test-coverage/src_config_parser_py.html @@ -404,7 +404,7 @@

« index     coverage.py v5.4, - created at 2023-03-16 16:28 +0000 + created at 2023-03-17 03:31 +0000

diff --git a/test-coverage/src_email_message_py.html b/test-coverage/src_email_message_py.html index e6bf9c195..fb5fe8462 100644 --- a/test-coverage/src_email_message_py.html +++ b/test-coverage/src_email_message_py.html @@ -108,7 +108,7 @@

« index     coverage.py v5.4, - created at 2023-03-16 16:28 +0000 + created at 2023-03-17 03:31 +0000

diff --git a/test-coverage/src_notify_py.html b/test-coverage/src_notify_py.html index 7ec90ad59..2a6338802 100644 --- a/test-coverage/src_notify_py.html +++ b/test-coverage/src_notify_py.html @@ -119,7 +119,7 @@

« index     coverage.py v5.4, - created at 2023-03-16 16:28 +0000 + created at 2023-03-17 03:31 +0000

diff --git a/test-coverage/src_sync_drive_py.html b/test-coverage/src_sync_drive_py.html index 3e9253a2e..dae3dd788 100644 --- a/test-coverage/src_sync_drive_py.html +++ b/test-coverage/src_sync_drive_py.html @@ -384,7 +384,7 @@

« index     coverage.py v5.4, - created at 2023-03-16 16:28 +0000 + created at 2023-03-17 03:31 +0000

diff --git a/test-coverage/src_sync_photos_py.html b/test-coverage/src_sync_photos_py.html index 70a0ce114..2668e87e8 100644 --- a/test-coverage/src_sync_photos_py.html +++ b/test-coverage/src_sync_photos_py.html @@ -284,7 +284,7 @@

« index     coverage.py v5.4, - created at 2023-03-16 16:28 +0000 + created at 2023-03-17 03:31 +0000

diff --git a/test-coverage/src_sync_py.html b/test-coverage/src_sync_py.html index 1025b324b..98cdc11ff 100644 --- a/test-coverage/src_sync_py.html +++ b/test-coverage/src_sync_py.html @@ -205,7 +205,7 @@

« index     coverage.py v5.4, - created at 2023-03-16 16:28 +0000 + created at 2023-03-17 03:31 +0000

diff --git a/test-coverage/src_usage_py.html b/test-coverage/src_usage_py.html index 8e6c2cee2..ae21c4747 100644 --- a/test-coverage/src_usage_py.html +++ b/test-coverage/src_usage_py.html @@ -22,8 +22,8 @@

Coverage for src/usage.py :

Show keyboard shortcuts

- 67 statements   - + 86 statements   +

@@ -55,115 +55,141 @@

1"""To record usage of the app.""" 

2import json 

3import os 

-

4 

-

5import requests 

-

6 

-

7from src.config_parser import prepare_root_destination 

-

8 

-

9CACHE_FILE_NAME = ".data" 

-

10NEW_INSTALLATION_ENDPOINT = os.environ.get("NEW_INSTALLATION_ENDPOINT", None) 

-

11NEW_HEARTBEAT_ENDPOINT = os.environ.get("NEW_HEARTBEAT_ENDPOINT", None) 

-

12APP_NAME = "icloud-drive-docker" 

-

13APP_VERSION = os.environ.get("APP_VERSION", "dev") 

-

14NEW_INSTALLATION_DATA = {"appName": APP_NAME, "appVersion": APP_VERSION} 

-

15 

+

4from datetime import datetime, timedelta 

+

5 

+

6import requests 

+

7 

+

8from src.config_parser import prepare_root_destination 

+

9 

+

10CACHE_FILE_NAME = ".data" 

+

11NEW_INSTALLATION_ENDPOINT = os.environ.get("NEW_INSTALLATION_ENDPOINT", None) 

+

12NEW_HEARTBEAT_ENDPOINT = os.environ.get("NEW_HEARTBEAT_ENDPOINT", None) 

+

13APP_NAME = "icloud-drive-docker" 

+

14APP_VERSION = os.environ.get("APP_VERSION", "dev") 

+

15NEW_INSTALLATION_DATA = {"appName": APP_NAME, "appVersion": APP_VERSION} 

16 

-

17def init_cache(config): 

-

18 """Initialize the cache file.""" 

-

19 root_destination_path = prepare_root_destination(config=config) 

-

20 cache_file_path = os.path.join(root_destination_path, CACHE_FILE_NAME) 

-

21 return cache_file_path 

-

22 

+

17 

+

18def init_cache(config): 

+

19 """Initialize the cache file.""" 

+

20 root_destination_path = prepare_root_destination(config=config) 

+

21 cache_file_path = os.path.join(root_destination_path, CACHE_FILE_NAME) 

+

22 return cache_file_path 

23 

-

24def load_cache(file_path: str): 

-

25 """Load the cache file.""" 

-

26 data = {} 

-

27 if os.path.isfile(file_path): 

-

28 with open(file_path) as f: 

-

29 data = json.load(f) 

-

30 else: 

-

31 save_cache(file_path=file_path, data={}) 

-

32 return data 

-

33 

+

24 

+

25def load_cache(file_path: str): 

+

26 """Load the cache file.""" 

+

27 data = {} 

+

28 if os.path.isfile(file_path): 

+

29 with open(file_path) as f: 

+

30 data = json.load(f) 

+

31 else: 

+

32 save_cache(file_path=file_path, data={}) 

+

33 return data 

34 

-

35def save_cache(file_path: str, data: object): 

-

36 """Save data to the cache file.""" 

-

37 with open(file_path, "w") as f: 

-

38 json.dump(data, f) 

-

39 return True 

-

40 

+

35 

+

36def save_cache(file_path: str, data: object): 

+

37 """Save data to the cache file.""" 

+

38 with open(file_path, "w") as f: 

+

39 json.dump(data, f) 

+

40 return True 

41 

-

42def post_new_installation(data, endpoint=NEW_INSTALLATION_ENDPOINT): 

-

43 """Post new installation to server.""" 

-

44 try: 

-

45 response = requests.post(endpoint, data) 

-

46 if response.ok: 

-

47 data = response.json() 

-

48 return data["id"] 

-

49 except Exception: 

-

50 pass 

-

51 return None 

-

52 

+

42 

+

43def post_new_installation(data, endpoint=NEW_INSTALLATION_ENDPOINT): 

+

44 """Post new installation to server.""" 

+

45 try: 

+

46 response = requests.post(endpoint, data) 

+

47 if response.ok: 

+

48 data = response.json() 

+

49 return data["id"] 

+

50 except Exception: 

+

51 pass 

+

52 return None 

53 

-

54def record_new_installation(previous_id=None): 

-

55 """Record new or upgrade existing installation.""" 

-

56 data = dict(NEW_INSTALLATION_DATA) 

-

57 if previous_id: 

-

58 data["previousId"] = previous_id 

-

59 return post_new_installation(data) 

-

60 

+

54 

+

55def record_new_installation(previous_id=None): 

+

56 """Record new or upgrade existing installation.""" 

+

57 data = dict(NEW_INSTALLATION_DATA) 

+

58 if previous_id: 

+

59 data["previousId"] = previous_id 

+

60 return post_new_installation(data) 

61 

-

62def already_installed(cached_data): 

-

63 """Check if already installed.""" 

-

64 return ( 

-

65 "id" in cached_data 

-

66 and "app_version" in cached_data 

-

67 and cached_data["app_version"] == APP_VERSION 

-

68 ) 

-

69 

+

62 

+

63def already_installed(cached_data): 

+

64 """Check if already installed.""" 

+

65 return ( 

+

66 "id" in cached_data 

+

67 and "app_version" in cached_data 

+

68 and cached_data["app_version"] == APP_VERSION 

+

69 ) 

70 

-

71def install(cached_data): 

-

72 """Install the app.""" 

-

73 new_id = record_new_installation(cached_data.get("id", None)) 

-

74 if new_id: 

-

75 cached_data["id"] = new_id 

-

76 cached_data["app_version"] = APP_VERSION 

-

77 return cached_data 

-

78 return False 

-

79 

+

71 

+

72def install(cached_data): 

+

73 """Install the app.""" 

+

74 new_id = record_new_installation(cached_data.get("id", None)) 

+

75 if new_id: 

+

76 cached_data["id"] = new_id 

+

77 cached_data["app_version"] = APP_VERSION 

+

78 return cached_data 

+

79 return False 

80 

-

81def post_new_heartbeat(data, endpoint=NEW_HEARTBEAT_ENDPOINT): 

-

82 """Post the heartbeat to server.""" 

-

83 try: 

-

84 response = requests.post(endpoint, data) 

-

85 if response.ok: 

-

86 return True 

-

87 except Exception: 

-

88 pass 

-

89 return False 

-

90 

+

81 

+

82def post_new_heartbeat(data, endpoint=NEW_HEARTBEAT_ENDPOINT): 

+

83 """Post the heartbeat to server.""" 

+

84 try: 

+

85 response = requests.post(endpoint, data) 

+

86 if response.ok: 

+

87 return True 

+

88 except Exception: 

+

89 pass 

+

90 return False 

91 

-

92def send_heartbeat(app_id, data=None): 

-

93 """Prepare and send heartbeat to server.""" 

-

94 data = {"installationId": app_id, "data": data} 

-

95 return post_new_heartbeat(data) 

-

96 

+

92 

+

93def send_heartbeat(app_id, data=None): 

+

94 """Prepare and send heartbeat to server.""" 

+

95 data = {"installationId": app_id, "data": data} 

+

96 return post_new_heartbeat(data) 

97 

-

98def alive(config, data=None): 

-

99 """Record liveliness.""" 

-

100 cache_file_path = init_cache(config=config) 

-

101 cached_data = load_cache(cache_file_path) 

-

102 if not already_installed(cached_data=cached_data): 

-

103 installed_data = install(cached_data=cached_data) 

-

104 if installed_data: 

-

105 return save_cache(file_path=cache_file_path, data=installed_data) 

-

106 return send_heartbeat(app_id=cached_data.get("id"), data=data) 

+

98 

+

99def current_time(): 

+

100 """Get current time.""" 

+

101 return datetime.now() 

+

102 

+

103 

+

104def heartbeat(cached_data, data): 

+

105 """Send heartbeat.""" 

+

106 previous_heartbeat = cached_data.get("heartbeat_timestamp", None) 

+

107 current = current_time() 

+

108 if previous_heartbeat: 

+

109 previous = datetime.strptime(previous_heartbeat, "%Y-%m-%d %H:%M:%S.%f") 

+

110 if previous < (current - timedelta(hours=24)): 

+

111 if send_heartbeat(cached_data.get("id"), data=data): 

+

112 cached_data["heartbeat_timestamp"] = str(current) 

+

113 return cached_data 

+

114 else: 

+

115 return False 

+

116 elif send_heartbeat(cached_data.get("id"), data=data): 

+

117 cached_data["heartbeat_timestamp"] = str(current) 

+

118 return cached_data 

+

119 

+

120 

+

121def alive(config, data=None): 

+

122 """Record liveliness.""" 

+

123 cache_file_path = init_cache(config=config) 

+

124 cached_data = load_cache(cache_file_path) 

+

125 if not already_installed(cached_data=cached_data): 

+

126 installed_data = install(cached_data=cached_data) 

+

127 if installed_data: 

+

128 return save_cache(file_path=cache_file_path, data=installed_data) 

+

129 heartbeat_data = heartbeat(cached_data=cached_data, data=data) 

+

130 if heartbeat_data: 

+

131 return save_cache(file_path=cache_file_path, data=heartbeat_data) 

+

132 return False 

diff --git a/test-coverage/status.json b/test-coverage/status.json index ed453c5e6..8ea51695d 100644 --- a/test-coverage/status.json +++ b/test-coverage/status.json @@ -1 +1 @@ -{"format":2,"version":"5.4","globals":"39d500336f42ed1e3d49ba0d1ecd0eea","files":{"src___init___py":{"hash":"620001c02d5bdfe7860d7b8016f7e65c","index":{"nums":[1,76,0,0,0,0,0],"html_filename":"src___init___py.html","relative_filename":"src/__init__.py"}},"src_config_parser_py":{"hash":"62783d262359b3042984c4c6816bf721","index":{"nums":[1,179,0,0,0,0,0],"html_filename":"src_config_parser_py.html","relative_filename":"src/config_parser.py"}},"src_email_message_py":{"hash":"f1cd58e66273dace4abeac30f6057ce9","index":{"nums":[1,33,0,0,0,0,0],"html_filename":"src_email_message_py.html","relative_filename":"src/email_message.py"}},"src_notify_py":{"hash":"b294a25ff4e7cdbfb567868bb29c803e","index":{"nums":[1,44,0,0,0,0,0],"html_filename":"src_notify_py.html","relative_filename":"src/notify.py"}},"src_sync_py":{"hash":"f2fbc097ddece783aad5358cc8452069","index":{"nums":[1,78,0,0,0,0,0],"html_filename":"src_sync_py.html","relative_filename":"src/sync.py"}},"src_sync_drive_py":{"hash":"3c9197fa45f7830fb91ff878d4ad5e7a","index":{"nums":[1,184,0,0,0,0,0],"html_filename":"src_sync_drive_py.html","relative_filename":"src/sync_drive.py"}},"src_sync_photos_py":{"hash":"d3589a16c7365971945c88bcf13fb422","index":{"nums":[1,95,0,0,0,0,0],"html_filename":"src_sync_photos_py.html","relative_filename":"src/sync_photos.py"}},"src_usage_py":{"hash":"c07c88c1e3887697f13992856e453076","index":{"nums":[1,67,0,0,0,0,0],"html_filename":"src_usage_py.html","relative_filename":"src/usage.py"}}}} \ No newline at end of file +{"format":2,"version":"5.4","globals":"39d500336f42ed1e3d49ba0d1ecd0eea","files":{"src___init___py":{"hash":"620001c02d5bdfe7860d7b8016f7e65c","index":{"nums":[1,76,0,0,0,0,0],"html_filename":"src___init___py.html","relative_filename":"src/__init__.py"}},"src_config_parser_py":{"hash":"62783d262359b3042984c4c6816bf721","index":{"nums":[1,179,0,0,0,0,0],"html_filename":"src_config_parser_py.html","relative_filename":"src/config_parser.py"}},"src_email_message_py":{"hash":"f1cd58e66273dace4abeac30f6057ce9","index":{"nums":[1,33,0,0,0,0,0],"html_filename":"src_email_message_py.html","relative_filename":"src/email_message.py"}},"src_notify_py":{"hash":"b294a25ff4e7cdbfb567868bb29c803e","index":{"nums":[1,44,0,0,0,0,0],"html_filename":"src_notify_py.html","relative_filename":"src/notify.py"}},"src_sync_py":{"hash":"f2fbc097ddece783aad5358cc8452069","index":{"nums":[1,78,0,0,0,0,0],"html_filename":"src_sync_py.html","relative_filename":"src/sync.py"}},"src_sync_drive_py":{"hash":"3c9197fa45f7830fb91ff878d4ad5e7a","index":{"nums":[1,184,0,0,0,0,0],"html_filename":"src_sync_drive_py.html","relative_filename":"src/sync_drive.py"}},"src_sync_photos_py":{"hash":"d3589a16c7365971945c88bcf13fb422","index":{"nums":[1,95,0,0,0,0,0],"html_filename":"src_sync_photos_py.html","relative_filename":"src/sync_photos.py"}},"src_usage_py":{"hash":"5d0eecf971798ee26fea1aa677c761aa","index":{"nums":[1,86,0,0,0,0,0],"html_filename":"src_usage_py.html","relative_filename":"src/usage.py"}}}} \ No newline at end of file