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

Incident management: add "mine" and "test_names" fields #700

Merged
merged 3 commits into from
Aug 9, 2023
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
6 changes: 6 additions & 0 deletions api/debian/changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
ooni-api (1.0.63) unstable; urgency=medium

* Add "mine" to incident API

-- Federico Ceratto <[email protected]> Tue, 08 Aug 2023 12:28:29 +0200

ooni-api (1.0.62) unstable; urgency=medium

* Add translation support for OONI Run v2
Expand Down
24 changes: 17 additions & 7 deletions api/ooniapi/incidents.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,17 @@ def search_list_incidents() -> Response:
if account_id is None:
return nocachejson(incidents=[])
where += "\nAND creator_account_id = %(account_id)s"
query_params["account_id"] = account_id

if account_id is None:
# non-published incidents are not exposed to anon users
where += "\nAND published = 1"
query_params["account_id"] = "never-match"
else:
query_params["account_id"] = account_id

query = f"""SELECT id, update_time, start_time, end_time, reported_by,
title, event_type, published, CCs, ASNs, domains, tags,
links
title, event_type, published, CCs, ASNs, domains, tags, test_names,
links, creator_account_id = %(account_id)s AS mine
FROM incidents FINAL
{where}
ORDER BY title
Expand Down Expand Up @@ -115,15 +117,17 @@ def show_incident(incident_id: str) -> Response:
log.debug("showing incident")
try:
where = "WHERE id = %(id)s AND deleted != 1"
query_params = {"id": incident_id}
account_id = get_account_id_or_none()
if account_id is None:
# non-published incidents are not exposed to anon users
where += "\nAND published = 1"
query_params = {"id": incident_id, "account_id": "never-match"}
else:
query_params = {"id": incident_id, "account_id": account_id}

query = f"""SELECT id, update_time, start_time, end_time, reported_by,
title, text, event_type, published, CCs, ASNs, domains, tags,
links
title, text, event_type, published, CCs, ASNs, domains, tags, test_names,
links, creator_account_id = %(account_id)s AS mine
FROM incidents FINAL
{where}
LIMIT 1
Expand Down Expand Up @@ -154,6 +158,7 @@ def prepare_incident_dict(d: dict):
"reported_by",
"start_time",
"tags",
"test_names",
"text",
"title",
]
Expand Down Expand Up @@ -228,6 +233,10 @@ def post_update_incident(action: str) -> Response:
type: array
items:
type: string
test_names:
type: array
items:
type: string
domains:
type: array
items:
Expand Down Expand Up @@ -288,7 +297,8 @@ def post_update_incident(action: str) -> Response:

ins_sql = """INSERT INTO incidents
(id, start_time, end_time, creator_account_id, reported_by, title,
text, event_type, published, CCs, ASNs, domains, tags, links)
text, event_type, published, CCs, ASNs, domains, tags, links,
test_names)
VALUES
"""
prepare_incident_dict(req)
Expand Down
3 changes: 2 additions & 1 deletion api/tests/integ/clickhouse_1_schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,8 @@ CREATE TABLE IF NOT EXISTS default.incidents
`ASNs` Array(UInt32),
`domains` Array(String),
`tags` Array(String),
`links` Array(String)
`links` Array(String),
`test_names` Array(String)
)
ENGINE = ReplacingMergeTree(update_time)
ORDER BY (id)
Expand Down
7 changes: 7 additions & 0 deletions api/tests/integ/test_incidents.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ def test_crud_general(cleanup, client, adminsession, usersession):
ASNs=[1, 2],
domains=[],
tags=["integ-test"],
test_names=["web_connectivity", "signal"],
links=[
"https://explorer.ooni.org/chart/mat?test_name=web_connectivity&axis_x=measurement_start_day&since=2023-04-16&until=2023-05-16&time_grain=day"
],
Expand Down Expand Up @@ -79,7 +80,9 @@ def test_crud_general(cleanup, client, adminsession, usersession):
"start_time": "2020-01-01T00:00:00Z",
"tags": ["integ-test"],
"title": "integ-test-1",
"test_names": ["web_connectivity", "signal"],
"event_type": "incident",
"mine": 1,
}
assert i == expected

Expand Down Expand Up @@ -132,6 +135,7 @@ def test_crud_general(cleanup, client, adminsession, usersession):
i.pop("update_time")
expected["start_time"] = "2020-01-02T00:00:00Z"
expected["published"] = True
expected["mine"] = 0
assert i == expected

# Delete as admin
Expand Down Expand Up @@ -161,6 +165,7 @@ def test_crud_user_create(cleanup, client, adminsession, usersession):
event_type="incident",
published=True,
CCs=["UK", "FR"],
test_names=["web_connectivity"],
ASNs=[1, 2],
domains=[],
tags=["integ-test"],
Expand Down Expand Up @@ -194,6 +199,8 @@ def test_crud_user_create(cleanup, client, adminsession, usersession):
"start_time": "2020-01-01T00:00:00Z",
"tags": ["integ-test"],
"title": title,
"test_names": ["web_connectivity"],
"mine": 1,
"event_type": "incident",
}
assert i == expected
Expand Down
6 changes: 3 additions & 3 deletions api/tests/integ/test_private_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def privapi(client, subpath):
def test_private_api_asn_by_month(client):
url = "asn_by_month"
response = privapi(client, url)
assert len(response) > 1
assert len(response) > 0, response
r = response[0]
assert sorted(r.keys()) == ["date", "value"]
assert r["value"] > 10
Expand All @@ -32,10 +32,10 @@ def test_private_api_asn_by_month(client):
def test_private_api_countries_by_month(client):
url = "countries_by_month"
response = privapi(client, url)
assert len(response) > 1
assert len(response) > 0, response
r = response[0]
assert sorted(r.keys()) == ["date", "value"]
assert r["value"] > 10
assert r["value"] > 1
assert r["value"] < 1000
assert r["date"].endswith("T00:00:00+00:00")

Expand Down
Loading