-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustom_integrations_analytics.py
More file actions
139 lines (109 loc) · 3.59 KB
/
Copy pathcustom_integrations_analytics.py
File metadata and controls
139 lines (109 loc) · 3.59 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
"""Custom Integrations Analytics"""
import argparse
import json
from dataclasses import dataclass
from datetime import datetime, timezone
from pathlib import Path
from typing import Any
from urllib.parse import urljoin
import httpx
import jinja2
import pandas as pd
from pandas.io.formats.style import Styler
ANALYTICS_URL = "https://analytics.home-assistant.io/custom_integrations.json"
ANALYTICS_FILE = Path("custom_integrations.json")
TEMPLATE_FILE = Path("custom_integrations_analytics.html.j2")
REPORT_FILE = Path("custom_integrations.html")
TITLE = "Home Assistant Custom Integrations"
REPOSITORY_NAME = "jschlyter/custom-integrations-analytics"
REPOSITORY_URL = "https://github.com/jschlyter/custom-integrations-analytics"
HACS_DATA_URL = "https://data-v2.hacs.xyz/integration/data.json"
HACS_DATA_FILE = "hacs_data.json"
@dataclass(frozen=True)
class HacsIntegration:
domain: str
manifest_name: str
full_name: str
@property
def href(self) -> str:
return urljoin("https://github.com", self.full_name)
def read_dataset(filename: Path, url: str) -> dict[str, Any]:
"""Read dataset from file or URL"""
if filename.exists():
with open(filename) as fp:
return json.load(fp)
else:
return httpx.get(url).json()
def make_pretty(styler: Styler) -> Styler:
styler.format(thousands=",")
styler.set_uuid("integrations")
styler.set_table_attributes('class="display"')
return styler
def main() -> None:
"""Main function"""
parser = argparse.ArgumentParser()
parser.add_argument(
"--url",
type=str,
help="Input URL",
default=ANALYTICS_URL,
)
parser.add_argument(
"--input",
type=Path,
help="Input file",
default=ANALYTICS_FILE,
)
parser.add_argument(
"--output",
type=Path,
help="Output file",
default=REPORT_FILE,
)
parser.add_argument(
"--template",
type=Path,
help="Template file",
default=TEMPLATE_FILE,
)
args = parser.parse_args()
dataset = read_dataset(filename=Path(args.input), url=args.url)
# Read integration data from HACS
hacs: dict[str, HacsIntegration] = {}
hacs_dataset = read_dataset(filename=Path(HACS_DATA_FILE), url=HACS_DATA_URL)
for hacs_data in hacs_dataset.values():
i = HacsIntegration(
domain=hacs_data["domain"],
manifest_name=hacs_data["manifest_name"],
full_name=hacs_data["full_name"],
)
hacs[i.domain] = i
usage = []
for integration, data in dataset.items():
if hacs_data := hacs.get(integration):
description = f'<a href="{hacs_data.href}">{hacs_data.manifest_name}</a>'
else:
description = ""
usage.append([integration, description, data["total"]])
df = pd.DataFrame(
sorted(usage, key=lambda v: v[2], reverse=True),
columns=["Integration", "Description", "Count"],
)
env = jinja2.Environment(
loader=jinja2.FileSystemLoader("."), autoescape=jinja2.select_autoescape()
)
template = env.get_template(str(args.template))
with open(args.output, "w") as fp:
table_html = df.style.pipe(make_pretty).to_html()
fp.write(
template.render(
title=TITLE,
table=table_html,
now=datetime.now(tz=timezone.utc),
repository_url=REPOSITORY_URL,
repository_name=REPOSITORY_NAME,
)
)
print(f"Result written to {args.output}")
if __name__ == "__main__":
main()