Skip to content

Commit af475f3

Browse files
committed
[llvm-advisor] add web server API infrastructure
- implement HTTP server with RESTful API endpoints - add base API classes
1 parent 6d2cf10 commit af475f3

File tree

6 files changed

+462
-0
lines changed

6 files changed

+462
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# ===----------------------------------------------------------------------===//
2+
#
3+
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
# See https://llvm.org/LICENSE.txt for license information.
5+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
#
7+
# ===----------------------------------------------------------------------===//
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# ===----------------------------------------------------------------------===//
2+
#
3+
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
# See https://llvm.org/LICENSE.txt for license information.
5+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
#
7+
# ===----------------------------------------------------------------------===//
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# ===----------------------------------------------------------------------===//
2+
#
3+
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
# See https://llvm.org/LICENSE.txt for license information.
5+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
#
7+
# ===----------------------------------------------------------------------===//
8+
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# ===----------------------------------------------------------------------===//
2+
#
3+
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
# See https://llvm.org/LICENSE.txt for license information.
5+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
#
7+
# ===----------------------------------------------------------------------===//
8+
9+
import json
10+
from abc import ABC, abstractmethod
11+
from typing import Dict, Any, Optional
12+
13+
14+
class BaseEndpoint(ABC):
15+
def __init__(self, data_dir: str, collector):
16+
self.data_dir = data_dir
17+
self.collector = collector
18+
self._cache = {}
19+
20+
@abstractmethod
21+
def handle(self, path_parts: list, query_params: Dict[str, list]) -> Dict[str, Any]:
22+
pass
23+
24+
def get_compilation_units(self):
25+
if "units" not in self._cache:
26+
self._cache["units"] = self.collector.discover_compilation_units(
27+
self.data_dir
28+
)
29+
return self._cache["units"]
30+
31+
def get_parsed_data(self):
32+
if "parsed_data" not in self._cache:
33+
self._cache["parsed_data"] = self.collector.parse_all_units(self.data_dir)
34+
return self._cache["parsed_data"]
35+
36+
def clear_cache(self):
37+
self._cache.clear()
38+
39+
40+
class APIResponse:
41+
@staticmethod
42+
def success(data: Any, status: int = 200) -> Dict[str, Any]:
43+
return {"success": True, "data": data, "status": status}
44+
45+
@staticmethod
46+
def error(message: str, status: int = 400) -> Dict[str, Any]:
47+
return {"success": False, "error": message, "status": status}
48+
49+
@staticmethod
50+
def not_found(resource: str) -> Dict[str, Any]:
51+
return APIResponse.error(f"{resource} not found", 404)
52+
53+
@staticmethod
54+
def invalid_request(message: str) -> Dict[str, Any]:
55+
return APIResponse.error(f"Invalid request: {message}", 400)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# ===----------------------------------------------------------------------===//
2+
#
3+
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
# See https://llvm.org/LICENSE.txt for license information.
5+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
#
7+
# ===----------------------------------------------------------------------===//
8+
9+
import os
10+
from typing import Dict, Any
11+
from .base import BaseEndpoint, APIResponse
12+
13+
14+
class HealthEndpoint(BaseEndpoint):
15+
"""GET /api/health - System health and data directory status"""
16+
17+
def handle(self, path_parts: list, query_params: Dict[str, list]) -> Dict[str, Any]:
18+
data_dir_exists = os.path.exists(self.data_dir)
19+
20+
# Count units and files if data directory exists
21+
unit_count = 0
22+
file_count = 0
23+
24+
if data_dir_exists:
25+
try:
26+
units = self.get_compilation_units()
27+
unit_count = len(units)
28+
file_count = sum(
29+
sum(len(files) for files in unit.artifacts.values())
30+
for unit in units
31+
)
32+
except Exception:
33+
pass
34+
35+
health_data = {
36+
"status": "healthy" if data_dir_exists else "no_data",
37+
"data_dir": self.data_dir,
38+
"data_dir_exists": data_dir_exists,
39+
"compilation_units": unit_count,
40+
"total_files": file_count,
41+
}
42+
43+
return APIResponse.success(health_data)

0 commit comments

Comments
 (0)