-
-
Notifications
You must be signed in to change notification settings - Fork 278
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
feat: new active projects api for export tool #6153
Changes from 1 commit
348de44
0a3e152
c9fb313
3aee592
4d59f9c
c3f5aca
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1164,3 +1164,39 @@ def get(self, project_id): | |
project_id, authenticated_user_id, preferred_locale, limit | ||
) | ||
return projects_dto.to_primitive(), 200 | ||
|
||
|
||
class ProjectQueriesActiveProjectsAPI(Resource): | ||
@token_auth.login_required(optional=True) | ||
def get(self): | ||
""" | ||
Get active projects | ||
--- | ||
tags: | ||
- projects | ||
produces: | ||
- application/json | ||
parameters: | ||
- in: header | ||
name: Authorization | ||
description: Base64 encoded session token | ||
required: false | ||
type: string | ||
default: Token sessionTokenHere== | ||
- name: interval | ||
in: path | ||
description: Time interval to get active project | ||
required: false | ||
type: integer | ||
default: 24 | ||
responses: | ||
200: | ||
description: Active projects geojson | ||
404: | ||
description: Project not found or project is not published | ||
500: | ||
description: Internal Server Error | ||
""" | ||
interval = int(request.args.get("interval", 24)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to add validation for the interval |
||
projects_dto = ProjectService.get_active_projects(interval) | ||
return projects_dto, 200 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
import threading | ||
from cachetools import TTLCache, cached | ||
from flask import current_app | ||
import geojson | ||
from datetime import datetime, timedelta | ||
|
||
from backend.exceptions import NotFound | ||
from backend.models.dtos.mapping_dto import TaskDTOs | ||
|
@@ -615,3 +617,37 @@ def send_email_on_project_progress(project_id): | |
project_completion, | ||
), | ||
).start() | ||
|
||
@staticmethod | ||
def get_active_projects(interval): | ||
action_date = datetime.now() - timedelta(hours=interval) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I assume datetime.now() is in UTC ? |
||
result = ( | ||
TaskHistory.query.with_entities(TaskHistory.project_id) | ||
.distinct() | ||
.filter(TaskHistory.action_date >= action_date) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using this , test this query with dump of production database , you might need action_date btree timestamp index |
||
.all() | ||
) | ||
project_ids = [row.project_id for row in result] | ||
projects = ( | ||
Project.query.with_entities( | ||
Project.id, | ||
Project.mapping_types, | ||
Project.geometry.ST_AsGeoJSON().label("geometry"), | ||
) | ||
.filter( | ||
Project.status == ProjectStatus.PUBLISHED.value, | ||
Project.id.in_(project_ids), | ||
) | ||
.all() | ||
) | ||
features = [] | ||
for project in projects: | ||
properties = { | ||
"project_id": project.id, | ||
"mapping_types": project.mapping_types, | ||
} | ||
feature = geojson.Feature( | ||
geometry=geojson.loads(project.geometry), properties=properties | ||
) | ||
features.append(feature) | ||
return geojson.FeatureCollection(features) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
time interval in hours