Skip to content

Commit

Permalink
Add project versions to monthly contributors
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcelGeo committed Nov 4, 2024
1 parent 52862d1 commit d45724e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 7 deletions.
25 changes: 21 additions & 4 deletions server/mergin/sync/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@
#
# SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-MerginMaps-Commercial

from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone
from typing import Dict, Tuple, Optional, Set, List
from flask_login import current_user
from sqlalchemy import or_, and_, Column, literal
from sqlalchemy import or_, and_, Column, literal, extract
from sqlalchemy.orm import joinedload

from .errors import UpdateProjectAccessError
from .models import Project, ProjectAccess, AccessRequest, ProjectAccessDetail
from .models import (
Project,
ProjectAccess,
AccessRequest,
ProjectAccessDetail,
ProjectVersion,
)
from .permissions import projects_query, ProjectPermissions
from .public_api_controller import parse_project_access_update_request
from .. import db
Expand Down Expand Up @@ -250,7 +256,18 @@ def workspace_count():

@staticmethod
def monthly_contributors_count():
return 0
today = datetime.now(timezone.utc)
year = today.year
month = today.month
return (
db.session.query(ProjectVersion.author_id)
.filter(
extract("year", ProjectVersion.created) == year,
extract("month", ProjectVersion.created) == month,
)
.group_by(ProjectVersion.author_id)
.count()
)

def projects_query(self, name=None, workspace=None):
ws = self.factory_method()
Expand Down
2 changes: 1 addition & 1 deletion server/mergin/tests/test_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def test_send_statistics(app, caplog):
assert data["workspaces_count"] == 1
assert data["service_uuid"] == app.config["SERVICE_ID"]
assert data["licence"] == "ce"
assert data["monthly_contributors"] == 0
assert data["monthly_contributors"] == 1

# repeated action does not do anything
task = send_statistics.s().apply()
Expand Down
16 changes: 15 additions & 1 deletion server/mergin/tests/test_workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ def test_workspace_implementation(client):
diff=null(),
change=PushChangeType.CREATE,
)
file_history.version = project.get_latest_version()
latest_version = project.get_latest_version()
file_history.version = latest_version
file_history.project_version_name = file_history.version.name
default_project_usage = ws.disk_usage()
db.session.add(file_history)
Expand All @@ -69,6 +70,19 @@ def test_workspace_implementation(client):
db.session.commit()
assert ws.disk_usage() == default_project_usage

current_time = datetime.datetime.now(datetime.timezone.utc)
latest_version.created = datetime.datetime.combine(
current_time.replace(day=1), datetime.time.max
) - datetime.timedelta(days=1)
db.session.commit()
assert handler.monthly_contributors_count() == 1

# test group by author_id
create_project("test_second_project", ws, user)
latest_version.created = current_time
db.session.commit()
assert handler.monthly_contributors_count() == 2


def test_workspace(client):
"""Test get global workspace"""
Expand Down
1 change: 0 additions & 1 deletion server/mergin/tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ def initialize():
"added": project_files,
"updated": [],
"removed": [],
"renamed": [],
}
)
pv = ProjectVersion(p, 1, user.id, upload_changes, "127.0.0.1")
Expand Down

0 comments on commit d45724e

Please sign in to comment.