Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge ooni/data API and new measurements service #908

Merged
merged 3 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ooniapi/common/src/common/clickhouse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from clickhouse_sqlalchemy import get_declarative_base


Base = get_declarative_base()
1 change: 1 addition & 0 deletions ooniapi/common/src/common/routers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from datetime import date, datetime
from typing import Union
from pydantic import BaseModel as PydandicBaseModel
from pydantic import ConfigDict

Expand Down
2 changes: 2 additions & 0 deletions ooniapi/common/src/common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ def commasplit(p: str) -> List[str]:
def convert_to_csv(r) -> str:
"""Convert aggregation result dict/list to CSV"""
csvf = StringIO()
if len(r) == 0:
return ""
if isinstance(r, dict):
# 0-dimensional data
fieldnames = sorted(r.keys())
Expand Down
3 changes: 3 additions & 0 deletions ooniapi/services/oonifindings/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,7 @@ clean:
run:
hatch run uvicorn $(SERVICE_NAME).main:app

apidocs:
hatch run python -m oonifindings.mkapidocs apidocs.json

.PHONY: init test build clean docker print-labels
3 changes: 3 additions & 0 deletions ooniapi/services/oonifindings/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ dependencies = [
"psycopg2 ~= 2.9.9",
"pyjwt ~= 2.8.0",
"alembic ~= 1.13.1",
"python-dateutil ~= 2.9.0",
"prometheus-fastapi-instrumentator ~= 6.1.0",
"prometheus-client",
]
Expand Down Expand Up @@ -65,6 +66,8 @@ dependencies = [
"black",
"pytest-asyncio",
"pytest-postgresql",
"pytest-docker",
"requests"
]
path = ".venv/"

Expand Down
10 changes: 10 additions & 0 deletions ooniapi/services/oonifindings/src/oonifindings/dependencies.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from typing import Annotated

from clickhouse_driver import Client as Clickhouse

from fastapi import Depends

from sqlalchemy import create_engine
Expand All @@ -18,3 +20,11 @@ def get_postgresql_session(settings: Annotated[Settings, Depends(get_settings)])
yield db
finally:
db.close()


def get_clickhouse_session(settings: Annotated[Settings, Depends(get_settings)]):
db = Clickhouse.from_url(settings.clickhouse_url)
try:
yield db
finally:
db.disconnect()
1 change: 1 addition & 0 deletions ooniapi/services/oonifindings/src/oonifindings/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from . import models
from .routers import v1


from .dependencies import get_settings, get_postgresql_session
from .common.version import get_build_label, get_pkg_version
from .common.metrics import mount_metrics
Expand Down
13 changes: 13 additions & 0 deletions ooniapi/services/oonifindings/src/oonifindings/mkapidocs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import sys
import json

from fastapi.openapi.utils import get_openapi
from .main import app
from .__about__ import VERSION

if __name__ == "__main__":
openapi = get_openapi(title="OONI Findings", version=VERSION, routes=app.routes)
assert len(sys.argv) == 2, "must specify outfile file"
with open(sys.argv[1], "w") as out_file:
out_file.write(json.dumps(openapi))
out_file.write("\n")
8 changes: 4 additions & 4 deletions ooniapi/services/oonifindings/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from pathlib import Path
import pytest

import time

import pytest
import jwt

from fastapi.testclient import TestClient
Expand Down Expand Up @@ -56,7 +56,7 @@ def client(alembic_migration):
postgresql_url=alembic_migration,
jwt_encryption_key="super_secure",
prometheus_metrics_password="super_secure",
account_id_hashing_key="super_secure"
account_id_hashing_key="super_secure",
)

client = TestClient(app)
Expand Down Expand Up @@ -100,7 +100,7 @@ def client_with_admin_role(client):

@pytest.fixture
def client_with_hashed_email(client):

def _hashed_email(email: str, role: str):
client = TestClient(app)
account_id = hash_email_address(email, "super_secure")
Expand Down
1 change: 1 addition & 0 deletions ooniapi/services/oonimeasurements/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ dependencies = [
"fastapi ~= 0.108.0",
"psycopg2 ~= 2.9.5",
"clickhouse-driver ~= 0.2.6",
"clickhouse-sqlalchemy ~= 0.3.2",
"sqlalchemy ~= 2.0.27",
"ujson ~= 5.9.0",
"urllib3 ~= 2.1.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
from .common.config import Settings
from .common.dependencies import get_settings


def get_clickhouse_session(settings: Annotated[Settings, Depends(get_settings)]):
db = Clickhouse.from_url(settings.clickhouse_url)
try:
yield db
finally:
finally:
db.disconnect()
13 changes: 12 additions & 1 deletion ooniapi/services/oonimeasurements/src/oonimeasurements/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@

from prometheus_fastapi_instrumentator import Instrumentator

from .routers import aggregation, measurements
from .routers.v1 import aggregation
from .routers.v1 import measurements
from .routers.data import (
list_analysis,
list_observations,
aggregate_observations,
aggregate_analysis,
)

from .dependencies import get_clickhouse_session
from .common.dependencies import get_settings
Expand Down Expand Up @@ -49,6 +56,10 @@ async def lifespan(app: FastAPI):

app.include_router(measurements.router, prefix="/api")
app.include_router(aggregation.router, prefix="/api")
app.include_router(list_analysis.router, prefix="/api")
app.include_router(list_observations.router, prefix="/api")
app.include_router(aggregate_observations.router, prefix="/api")
app.include_router(aggregate_analysis.router, prefix="/api")


@app.get("/version")
Expand Down
Loading
Loading