Skip to content
This repository has been archived by the owner on Nov 14, 2023. It is now read-only.

Stats users #51

Merged
merged 1 commit into from
Nov 8, 2021
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
7 changes: 3 additions & 4 deletions API/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@
from .data.routers import router as data_router
from .auth.routers import router as auth_router
from .mapathon.routers import router as mapathon_router
from .osm_users import router as osm_users_router


app = FastAPI()


origins = [
"*"
]
origins = ["*"]

app.add_middleware(
CORSMiddleware,
Expand All @@ -28,3 +26,4 @@
app.include_router(auth_router)
app.include_router(mapathon_router)
app.include_router(data_router)
app.include_router(osm_users_router)
21 changes: 21 additions & 0 deletions API/osm_users.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from fastapi import APIRouter
from typing import List

from src.galaxy.validation.models import UsersListParams, User, UserStatsParams, MappedFeature
from src.galaxy.app import UserStats

router = APIRouter(prefix="/osm-users")

@router.post("/ids", response_model=List[User])
def list_users(params: UsersListParams):
return UserStats().list_users(params)


@router.post("/statistics/", response_model=List[MappedFeature])
def user_statistics(params: UserStatsParams):
user_stats = UserStats()

if len(params.hashtags) > 0:
return user_stats.get_statistics_with_hashtags(params)

return user_stats.get_statistics(params)
74 changes: 73 additions & 1 deletion src/galaxy/app.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'''Main page contains class for database mapathon and funtion for error printing '''

import sys
from psycopg2 import connect
from API import config
from psycopg2 import connect, sql
from psycopg2.extras import DictCursor
from psycopg2 import OperationalError, errorcodes, errors
from pydantic import validator
Expand Down Expand Up @@ -231,4 +232,75 @@ def dataframe(self):
return self.dataframe


class UserStats:
def __init__(self):
self.db = Database(dict(config.items("INSIGHTS_PG")))
self.con, self.cur = self.db.connect()

def list_users(self, params):
user_names_str = ",".join(["%s" for n in range(len(params.user_names))])

query = sql.SQL(f"""SELECT DISTINCT user_id, user_name from osm_changeset
WHERE created_at between %s AND %s AND user_name IN ({user_names_str})
""")

items = (params.from_timestamp, params.to_timestamp, *params.user_names)
list_users_query = self.cur.mogrify(query, items)

result = self.db.executequery(list_users_query)

users_list = [User(**r) for r in result]

return users_list

def get_statistics(self, params):
query = """
SELECT (each(tags)).key as feature, action, count(distinct id)
FROM osm_element_history
WHERE timestamp BETWEEN %s AND %s
AND uid = %s
AND type in ('way','relation')
GROUP BY feature, action
"""

items = (params.from_timestamp, params.to_timestamp, params.user_id)
query = self.cur.mogrify(query, items)

result = self.db.executequery(query)
summary = [MappedFeature(**r) for r in result]

return summary

def get_statistics_with_hashtags(self, params):
changeset_query, _, _ = create_changeset_query(params, self.con, self.cur)

# Include user_id filter.
changeset_query = f"{changeset_query} AND user_id = {params.user_id}"

base_query = """
SELECT (each(osh.tags)).key as feature, osh.action, count(distinct osh.id)
FROM osm_element_history AS osh, T1
WHERE osh.timestamp BETWEEN %s AND %s
AND osh.uid = %s
AND osh.type in ('way','relation')
AND T1.changeset_id = osh.changeset
GROUP BY feature, action
"""
items = (params.from_timestamp, params.to_timestamp, params.user_id)
base_query = self.cur.mogrify(base_query, items).decode()

query = f"""
WITH T1 AS (
{changeset_query}
)
{base_query}
"""

result = self.db.executequery(query)

summary = [MappedFeature(**r) for r in result]

return summary



19 changes: 19 additions & 0 deletions src/galaxy/validation/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,22 @@ def check_hashtag_filter(cls, value, values, **kwargs):
"Empty lists found for both hashtags and project_ids params")

return value


class UsersListParams(BaseModel):
user_names: List[str]
from_timestamp: Union[datetime, date]
to_timestamp: Union[datetime, date]


class UserStatsParams(BaseModel):
user_id: int
from_timestamp: Union[datetime, date]
to_timestamp: Union[datetime, date]
hashtags: List[str]
project_ids: List[int] = []


class User(BaseModel):
user_id: int
user_name: str