Skip to content

Commit

Permalink
feat: Implement top datasets in main
Browse files Browse the repository at this point in the history
  • Loading branch information
drorganvidez committed Oct 15, 2024
1 parent ab52a4c commit ce1edc1
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 18 deletions.
35 changes: 30 additions & 5 deletions app/modules/dataset/repositories.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from datetime import datetime
import logging
from flask_login import current_user
from typing import Optional
from typing import List, Optional

import pytz
from sqlalchemy import desc, func
Expand Down Expand Up @@ -90,23 +90,39 @@ def is_synchronized(self, dataset_id: int) -> bool:
return True
return False

def get_synchronized(self, current_user_id: int) -> DataSet:
def get_synchronized_datasets(self) -> List[DataSet]:
return (
self.model.query.join(DSMetaData)
.filter(DSMetaData.dataset_doi.isnot(None))
.order_by(self.model.created_at.desc())
.all()
)

def get_unsynchronized_datasets(self) -> List[DataSet]:
return (
self.model.query.join(DSMetaData)
.filter(DSMetaData.dataset_doi.is_(None))
.order_by(self.model.created_at.desc())
.all()
)

def get_synchronized_datasets_by_user(self, current_user_id: int) -> List[DataSet]:
return (
self.model.query.join(DSMetaData)
.filter(DataSet.user_id == current_user_id, DSMetaData.dataset_doi.isnot(None))
.order_by(self.model.created_at.desc())
.all()
)

def get_unsynchronized(self, current_user_id: int) -> DataSet:
def get_unsynchronized_datasets_by_user(self, current_user_id: int) -> List[DataSet]:
return (
self.model.query.join(DSMetaData)
.filter(DataSet.user_id == current_user_id, DSMetaData.dataset_doi.is_(None))
.order_by(self.model.created_at.desc())
.all()
)

def get_unsynchronized_dataset(self, current_user_id: int, dataset_id: int) -> DataSet:
def get_unsynchronized_dataset_by_user(self, current_user_id: int, dataset_id: int) -> DataSet:
return (
self.model.query.join(DSMetaData)
.filter(DataSet.user_id == current_user_id, DataSet.id == dataset_id, DSMetaData.dataset_doi.is_(None))
Expand All @@ -127,7 +143,7 @@ def count_unsynchronized_datasets(self):
.count()
)

def latest_synchronized(self):
def latest_synchronized(self) -> List[DataSet]:
return (
self.model.query.join(DSMetaData)
.filter(DSMetaData.dataset_doi.isnot(None))
Expand All @@ -136,6 +152,15 @@ def latest_synchronized(self):
.all()
)

def get_top_5_datasets_by_feature_model_count(self) -> List[DataSet]:
return (
self.model.query
.order_by(self.model.feature_model_count.desc())
.order_by(desc(self.model.created_at))
.limit(5)
.all()
)


class DOIMappingRepository(BaseRepository):
def __init__(self):
Expand Down
4 changes: 2 additions & 2 deletions app/modules/dataset/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ def update_dataset():
def list_dataset():
return render_template(
"dataset/list_datasets.html",
datasets=dataset_service.get_synchronized(current_user.id),
local_datasets=dataset_service.get_unsynchronized(current_user.id),
datasets=dataset_service.get_synchronized_datasets_by_user(current_user.id),
local_datasets=dataset_service.get_unsynchronized_datasets_by_user(current_user.id),
)


Expand Down
23 changes: 16 additions & 7 deletions app/modules/dataset/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import hashlib
import shutil
import tempfile
from typing import Optional
from typing import List, Optional
import uuid
from zipfile import ZipFile

Expand Down Expand Up @@ -69,19 +69,28 @@ def move_feature_models(self, dataset: DataSet):
def is_synchronized(self, dataset_id: int) -> bool:
return self.repository.is_synchronized(dataset_id)

def get_synchronized(self, current_user_id: int) -> DataSet:
return self.repository.get_synchronized(current_user_id)
def get_synchronized_datasets(self) -> List[DataSet]:
return self.repository.get_synchronized_datasets()

def get_unsynchronized(self, current_user_id: int) -> DataSet:
return self.repository.get_unsynchronized(current_user_id)
def get_unsynchronized_datasets(self) -> List[DataSet]:
return self.repository.get_unsynchronized_datasets()

def get_synchronized_datasets_by_user(self, current_user_id: int) -> List[DataSet]:
return self.repository.get_synchronized_datasets_by_user(current_user_id)

def get_unsynchronized_datasets_by_user(self, current_user_id: int) -> List[DataSet]:
return self.repository.get_unsynchronized_datasets_by_user(current_user_id)

def get_unsynchronized_dataset(self, current_user_id: int, dataset_id: int) -> DataSet:
return self.repository.get_unsynchronized_dataset(current_user_id, dataset_id)

def latest_synchronized(self):
def latest_synchronized(self) -> List[DataSet]:
return self.repository.latest_synchronized()

def count_synchronized_datasets(self):
def get_top_5_datasets_by_feature_model_count(self) -> List[DataSet]:
return self.repository.get_top_5_datasets_by_feature_model_count()

def count_synchronized_datasets(self) -> int:
return self.repository.count_synchronized_datasets()

def count_feature_models(self, dataset_id: int) -> int:
Expand Down
9 changes: 8 additions & 1 deletion app/modules/featuremodel/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@ def total_feature_model_downloads(self) -> int:
return self.hubfile_service.total_hubfile_downloads()

def count_feature_models(self):
return self.repository.count_feature_models()

from app.modules.dataset.services import DataSetService
dataset_service = DataSetService()
synchronized_datasets = dataset_service.get_synchronized_datasets()

total_feature_models = sum(dataset.feature_model_count for dataset in synchronized_datasets)

return total_feature_models

class FMMetaDataService(BaseService):
def __init__(self):
Expand Down
2 changes: 1 addition & 1 deletion app/modules/public/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def index():

return render_template(
"public/index.html",
datasets=dataset_service.latest_synchronized(),
datasets=dataset_service.get_top_5_datasets_by_feature_model_count(),
datasets_counter=datasets_counter,
feature_models_counter=feature_models_counter,
total_dataset_downloads=total_dataset_downloads,
Expand Down
3 changes: 1 addition & 2 deletions app/modules/public/templates/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
{% block content %}

<h1 class="h2 mb-3">

<b>Latest</b> datasets
<b>Top datasets</b> with the most feature models
</h1>

<div class="row">
Expand Down

0 comments on commit ce1edc1

Please sign in to comment.