-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLanguageJson.py
More file actions
67 lines (56 loc) · 2.35 KB
/
LanguageJson.py
File metadata and controls
67 lines (56 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import json
import gitlab
from datetime import datetime
import time
from concurrent.futures import ThreadPoolExecutor, as_completed
def get_project_languages(project):
"""Récupère les langages pour un projet donné, en gérant les exceptions."""
try:
return project.id, project.languages()
except Exception as e:
print(f"Erreur pour le projet {project.id}: {e}")
return project.id, {}
def json_number_language(projects, max_workers=10) -> json:
"""Crée le JSON des langues utilisées sur tous les projets GitLab, en parallèle."""
result = {}
# Appels parallèles aux projets GitLab
with ThreadPoolExecutor(max_workers=max_workers) as executor:
futures = [executor.submit(get_project_languages, project) for project in projects]
for future in as_completed(futures):
project_id, languages = future.result()
for language, lines in languages.items():
result[language] = result.get(language, 0) + lines
total_lines = sum(result.values())
result_percentage = {
language: (lines / total_lines) * 100 for language, lines in result.items()
}
sorted_result = sorted(result_percentage.items(), key=lambda item: item[1], reverse=True)
current_date = datetime.now().strftime("%Y-%m-%d")
json_structure = [{
"date": current_date,
"state": [item[0] for item in sorted_result],
"count": [item[1] for item in sorted_result],
}]
try:
with open("json/Language/cumulate.json", "r") as file:
existing_data = json.load(file)
except (FileNotFoundError, json.JSONDecodeError):
existing_data = []
if isinstance(existing_data, list):
existing_data.extend(json_structure)
else:
existing_data = json_structure
with open("json/Language/cumulate.json", "w") as file:
json.dump(existing_data, file, indent=4)
return json.dumps(existing_data, indent=4)
# if __name__ == "__main__":
# print("fetch")
# gl = gitlab.Gitlab(GITLAB_URL, private_token=ACCESS_TOKEN)
# projects = gl.projects.list(all=True)
# print("start")
# start_time = time.time()
# output_json = json_number_language(projects)
# print(output_json)
# print(f"Temps d'exécution : {time.time() - start_time:.2f} secondes")
# 6s pour environ 900projets
# soit 0,006 s par projets