-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilities.py
71 lines (56 loc) · 2.24 KB
/
utilities.py
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
68
69
70
71
from datetime import datetime, timedelta
from collections import defaultdict
from typing import List, Dict
from CodeFile import CodeFile
def get_dates_between(start_date_str, end_date, interval):
date_format = "%Y-%m-%d"
output_format = "%Y-%m-%dT%H:%M:%SZ"
# Parse the input date string
start_date = datetime.strptime(start_date_str, date_format).date()
# Convert end date to date if string
if isinstance(end_date, str):
end_date = datetime.strptime(end_date, '%Y-%m-%d').date()
# Calculate the difference in days
days_diff = (end_date - start_date).days
# Generate the list of dates
date_list = set()
for i in range(0, days_diff + 1, int(interval)):
date_item = start_date + timedelta(days=i)
start_date_str = date_item.strftime(output_format)
date_list.add(start_date_str)
# include end of today in the set if end_date is today
if end_date == datetime.now().date():
date_list.add((datetime.now() + timedelta(days=1)).strftime(output_format))
return date_list
def count_by_file_extension(files: List[str], languages: List[str]) -> dict:
file_counts = defaultdict(int)
for file in files:
for ext in languages:
extension = f".{ext.lower()}"
if file.endswith(extension):
file_counts[ext] += 1
return file_counts
def count_by_language_and_file_extension(files: List[CodeFile], languages: List[str]) -> Dict[str, Dict[str, int]]:
file_counts = defaultdict(int)
file_sizes = defaultdict(int)
for file in files:
file_parts = file.path.split('/')
if len(file_parts) < 3:
continue
instrumentation = file_parts[1]
extension = file_parts[-1].split('.')[-1]
if extension in languages:
file_counts[instrumentation] += 1
file_sizes[instrumentation] += file.size
return file_counts, file_sizes
def convert_to_plot(input_dict: dict, items):
result = {}
dates = []
for entry in input_dict.values():
dates.append(entry["date"][:10])
for item in items:
try:
result[item].append(entry[item])
except KeyError:
result[item] = [entry[item]]
return dates, result