Skip to content

Commit

Permalink
feat: implement export_timeline in client (#240)
Browse files Browse the repository at this point in the history
* feat: implement export_timeline in client

* fix: use enum instead of literal

* fix: use literal from typing_extensions
  • Loading branch information
edgarrmondragon committed Feb 17, 2022
1 parent 3c42fc7 commit 83340a9
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
2 changes: 1 addition & 1 deletion docs/api_coverage.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
| `export_responses` | Yes | Export responses in base64 encoded string |
| `export_responses_by_token` | Yes | Export token response in a survey. |
| `export_statistics` | Yes | Export survey statistics (RPC function) |
| `export_timeline` | No | Export submission timeline (RPC function) |
| `export_timeline` | Yes | Export submission timeline (RPC function) |
| `get_group_properties` | Yes | Get the properties of a group of a survey . |
| `get_language_properties` | Yes | Get survey language properties (RPC function) |
| `get_participant_properties` | Yes | Get settings of a survey participant (RPC function) |
Expand Down
33 changes: 33 additions & 0 deletions src/citric/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from __future__ import annotations

import base64
import datetime
import sys
from os import PathLike
from pathlib import Path
from types import TracebackType
Expand All @@ -13,6 +15,12 @@
from citric import enums
from citric.session import Session

if sys.version_info >= (3, 8):
from typing import Literal
else:
from typing_extensions import Literal


_T = TypeVar("_T", bound="Client")


Expand Down Expand Up @@ -485,6 +493,31 @@ def save_statistics(
)
)

def export_timeline(
self,
survey_id: int,
period: Literal["day", "hour"],
start: datetime.datetime,
end: datetime.datetime | None = None,
) -> dict[str, int]:
"""Export survey submission timeline.
Args:
survey_id: ID of the Survey.
period: Granularity level for aggregation submission counts.
start: Start datetime.
end: End datetime.
Returns:
Mapping of days/hours to submission counts.
"""
return self.session.export_timeline(
survey_id,
enums.TimelineAggregationPeriod(period),
start.isoformat(),
end.isoformat() if end else datetime.datetime.utcnow().isoformat(),
)

def get_group_properties(
self,
group_id: int,
Expand Down
7 changes: 7 additions & 0 deletions src/citric/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,10 @@ class ResponseType(str, enum.Enum):

LONG = "long"
SHORT = "short"


class TimelineAggregationPeriod(str, enum.Enum):
"""Timeline aggregation level."""

HOUR = "hour"
DAY = "day"
13 changes: 13 additions & 0 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from __future__ import annotations

import base64
import datetime
import random
import sys
from pathlib import Path
Expand Down Expand Up @@ -109,6 +110,10 @@ def export_statistics(self, *args: Any) -> bytes:
"""Mock statistics file content."""
return base64.b64encode(b"FILE CONTENTS")

def export_timeline(self, *args: Any) -> dict[str, int]:
"""Mock submission timeline."""
return {"2022-01-01": 4, "2022-01-02": 2}

def get_site_settings(self, setting_name: str) -> str:
"""Return the setting value or an empty string."""
return self.settings.get(setting_name, "")
Expand Down Expand Up @@ -326,6 +331,14 @@ def test_delete_participants(client: MockClient):
assert_client_session_call(client, "delete_participants", 1, participants)


def test_export_timeline(client: MockClient):
"""Test export_timeline client method."""
assert client.export_timeline(1, "hour", datetime.datetime(2020, 1, 1)) == {
"2022-01-01": 4,
"2022-01-02": 2,
}


def test_participant_properties(client: MockClient):
"""Test get_participant_properties client method."""
assert_client_session_call(client, "get_participant_properties", 1, 1, None)
Expand Down

0 comments on commit 83340a9

Please sign in to comment.