Skip to content

Commit

Permalink
Cleaning up
Browse files Browse the repository at this point in the history
  • Loading branch information
manasaV3 committed Sep 14, 2023
1 parent 2476dc1 commit 0cd2bb7
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 31 deletions.
15 changes: 10 additions & 5 deletions backend/api/_tests/test_home.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from unittest.mock import Mock
import pytest
from api import home
from nhcommons.models.plugin_utils import PluginVisibility


def filter_plugins(plugins: List[Dict]):
Expand Down Expand Up @@ -79,22 +80,24 @@ def test_invalid_section_names(self, mock_get_index):
assert {} == home.get_plugin_sections({"foo"})
mock_get_index.assert_not_called()

@pytest.mark.parametrize("section, key", [
@pytest.mark.parametrize("section, sort_key", [
("newest", "first_released"),
("recently_updated", "release_date"),
("top_installed", "total_installs"),
])
def test_valid_section_names(
self, section, key, index_data, mock_get_index
self, section, sort_key, index_data, mock_get_index
):
actual = home.get_plugin_sections({section})

plugins = sorted(
index_data, key=lambda item: item.get(key), reverse=True
index_data, key=lambda item: item.get(sort_key), reverse=True
)[0:3]
expected = {section: {"plugins": filter_plugins(plugins)}}
assert expected == actual
mock_get_index.assert_called_with({"PUBLIC"}, True)
mock_get_index.assert_called_once_with(
include_total_installs=True, visibility_filter={PluginVisibility.PUBLIC}
)

@pytest.mark.parametrize(
"plugin_type, minute, expected", [
Expand Down Expand Up @@ -128,4 +131,6 @@ def test_valid_plugin_types_section(
actual["plugin_types"]["plugins"], key=get_name
)
assert expected_result == actual
mock_get_index.assert_called_once_with({"PUBLIC"}, True)
mock_get_index.assert_called_once_with(
include_total_installs=True, visibility_filter={PluginVisibility.PUBLIC}
)
31 changes: 25 additions & 6 deletions backend/api/_tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from nhcommons.models import plugin, install_activity
from api import model
from nhcommons.models.plugin_utils import PluginVisibility


class TestModel:
Expand Down Expand Up @@ -60,21 +61,39 @@ def plugin_index_with_total_installs(
return result

@pytest.mark.parametrize("visibility, include_total_installs, expected", [
({"PUBLIC"}, True, "plugin_index_with_total_installs"),
({"PUBLIC", "HIDDEN"}, True, "plugin_index_with_total_installs"),
({"PUBLIC"}, False, "plugin_get_index_result"),
({"PUBLIC", "HIDDEN"}, False, "plugin_get_index_result"),
({PluginVisibility.PUBLIC}, True, "plugin_index_with_total_installs"),
(
{PluginVisibility.PUBLIC, PluginVisibility.HIDDEN},
True,
"plugin_index_with_total_installs"
),
(
None,
True,
"plugin_index_with_total_installs"
),
({PluginVisibility.PUBLIC}, False, "plugin_get_index_result"),
(
{PluginVisibility.PUBLIC, PluginVisibility.HIDDEN},
False,
"plugin_get_index_result"
),
(
None,
False,
"plugin_get_index_result"
),
])
def test_get_index_with_include_installs(
self,
visibility: Optional[Set[str]],
visibility: Optional[Set[PluginVisibility]],
include_total_installs: bool,
expected: str,
mock_get_index: Mock,
mock_total_installs: Mock,
request: pytest.FixtureRequest,
):
actual = model.get_index(visibility, include_total_installs)
actual = model.get_index(include_total_installs, visibility)

assert request.getfixturevalue(expected) == actual
mock_get_index.assert_called_with(visibility)
Expand Down
8 changes: 6 additions & 2 deletions backend/api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from api.metrics import get_metrics_for_plugin
from nhcommons.models import (category as categories)
from api.shield import get_shield
from nhcommons.models.plugin_utils import PluginVisibility
from utils.utils import send_alert

app = Flask(__name__)
Expand Down Expand Up @@ -43,12 +44,15 @@ def swagger():

@app.route("/plugins/index")
def plugin_index() -> Response:
return jsonify(get_index({"PUBLIC"}, True))
index = get_index(
include_total_installs=True, visibility_filter={PluginVisibility.PUBLIC}
)
return jsonify(index)


@app.route("/plugins/index/all")
def plugin_index_all() -> Response:
return jsonify(get_index(None, False))
return jsonify(get_index())


@app.route("/plugins/<plugin>", defaults={"version": None})
Expand Down
6 changes: 5 additions & 1 deletion backend/api/home.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from random import sample
from datetime import datetime

from nhcommons.models.plugin_utils import PluginVisibility

DEFAULT_FIELDS = {
"authors", "display_name", "first_released", "name", "release_date",
"summary", "total_installs",
Expand All @@ -21,7 +23,9 @@ def get_plugin_sections(sections: Set[str], limit: int = 3) -> Dict[str, Dict]:
logger.warning("No processing as there are no valid sections")
return response

index = get_index({"PUBLIC"}, True)
index = get_index(
include_total_installs=True, visibility_filter={PluginVisibility.PUBLIC}
)
for name, handler in _get_handler_by_section_name().items():
if name in sections:
response[name] = handler(index, limit, plugins_encountered)
Expand Down
8 changes: 5 additions & 3 deletions backend/api/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,17 @@ def get_manifest(name: str, version: str = None) -> dict:


def get_index(
visibility: Optional[Set[str]], include_total_installs: bool
include_total_installs: bool = False,
visibility_filter: Optional[Set[PluginVisibility]] = None
) -> List[Dict[str, Any]]:
"""
Get the index page related metadata for all plugins.
:params visibility: visibilities to filter results by
:params visibility: visibilities to filter results by, if None all plugins are
returned
:params include_total_installs: include total_installs in result
:return: dict for index page metadata
"""
plugins = plugin_model.get_index(visibility)
plugins = plugin_model.get_index(visibility_filter)
if include_total_installs:
total_installs = install_activity.get_total_installs_by_plugins()
for item in plugins:
Expand Down
35 changes: 23 additions & 12 deletions napari-hub-commons/src/nhcommons/models/github_activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,29 +82,40 @@ def get_timeline(plugin: str, repo: str, month_delta: int) -> List[Dict[str, int
:param str repo: Name of the GitHub repo.
:param int month_delta: Number of months in timeline.
"""
results = _query_for_timeline(plugin, repo, month_delta)
start_datetime = datetime.combine(
get_first_of_last_month(), datetime.min.time(), timezone.utc
)
dates = [
int((start_datetime - relativedelta(months=i)).timestamp()) * 1000
for i in range(month_delta - 1, -1, -1)
]
return list(
map(lambda ts: {"timestamp": ts, "commits": results.get(ts, 0)}, dates)
)


def get_first_of_last_month():
return datetime.today().replace(day=1) - relativedelta(months=1)


def _query_for_timeline(plugin: str, repo: str, month_delta: int) -> Dict[int, int]:
if not repo:
logger.info(f"Skipping timeline query for {plugin} as repo={repo}")
return {}
type_identifier_format = f"MONTH:{{timestamp:%Y%m}}:{repo}"
start_date = datetime.today().replace(day=1) - relativedelta(months=1)
start_date = get_first_of_last_month()
end_date = start_date - relativedelta(months=month_delta - 1)
condition = _GitHubActivity.type_identifier.between(
type_identifier_format.format(timestamp=end_date),
type_identifier_format.format(timestamp=start_date)
)

query_params = {
"hash_key": plugin.lower(),
"range_key_condition": condition,
"filter_condition": _GitHubActivity.repo == repo
}
results = {row.timestamp: row.commit_count for row in _query_table(query_params)}

start_datetime = datetime.combine(start_date, datetime.min.time(), timezone.utc)
dates = [
int((start_datetime - relativedelta(months=i)).timestamp()) * 1000
for i in range(month_delta - 1, -1, -1)
]
return list(
map(lambda ts: {"timestamp": ts, "commits": results.get(ts, 0)}, dates)
)
return {row.timestamp: row.commit_count for row in _query_table(query_params)}


def _get_item(plugin: str, type_identifier: str) -> Optional[_GitHubActivity]:
Expand Down
4 changes: 2 additions & 2 deletions napari-hub-commons/src/nhcommons/models/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ class Meta:
}


def get_index(visibility: Optional[Set[PluginVisibility]]) -> List[Dict[str, Any]]:
def get_index(visibility_filter: Optional[Set[PluginVisibility]]) -> List[Dict[str, Any]]:
return _scan_latest_plugins_index(
attributes=["name", "version", "data", "visibility"],
mapper=_index_list_mapper(),
filter_conditions=_to_visibility_condition(visibility),
filter_conditions=_to_visibility_condition(visibility_filter),
)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,11 @@ def test_get_latest_commit(self, seed_data, plugin_name, repo, expected):
("Plugin-1", "Foo/Bar", 0, {}),
("Plugin-1", "foo/bar", 0, {}),
("Plugin-1", "Foo/Bar", 12, {4: 10, 9: 20}),
("Plugin-1", None, 12, {}),
("Plugin-1", "foo/bar", 12, {}),
("Plugin-7", "Foo/Bar", 12, {}),
("Plugin-7", "Bar/Baz", 12, {}),
("Plugin-7", None, 12, {}),
])
def test_get_timeline(
self, seed_data, generate_timeline, plugin_name, repo, month_delta, expected
Expand Down

0 comments on commit 0cd2bb7

Please sign in to comment.