Skip to content

Commit

Permalink
Merge pull request #20 from weni-ai/feature/adding-timezone
Browse files Browse the repository at this point in the history
Feature/adding timezone
  • Loading branch information
AlanJaeger authored Jul 2, 2024
2 parents 1d8babf + 6afb003 commit e32d1e9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
4 changes: 2 additions & 2 deletions insights/sources/agents/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ def __init__(self, project) -> None:

def list(self, query_filters: dict):
if query_filters.get("created_on__gte", None):
query_filters["start_date"] = query_filters.pop("created_on__gte")[0]
query_filters["start_date"] = query_filters.pop("created_on__gte")
if query_filters.get("created_on__lte", None):
query_filters["end_date"] = query_filters.pop("created_on__lte")[0]
query_filters["end_date"] = query_filters.pop("created_on__lte")

response = requests.get(
url=self.url, headers=self.headers, params=query_filters
Expand Down
28 changes: 23 additions & 5 deletions insights/widgets/usecases/get_source_data.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
from datetime import datetime, time

import pytz

from insights.projects.parsers import parse_dict_to_json
from insights.shared.viewsets import get_source
from insights.widgets.models import Widget


def apply_timezone_to_filters(default_filters, project_timezone_str):
project_timezone = pytz.timezone(project_timezone_str)
for key in default_filters.keys():
if key.endswith("__gte") or key.endswith("__lte"):
date_str = default_filters[key][0]
date_obj = datetime.strptime(date_str, "%Y-%m-%d")
date_obj_with_tz = project_timezone.localize(date_obj)
default_filters[key] = date_obj_with_tz.isoformat()


def get_source_data_from_widget(
widget: Widget, is_report: bool = False, filters: dict = {}, user_email: str = ""
):
Expand All @@ -19,21 +33,25 @@ def get_source_data_from_widget(

default_filters, operation, op_field, limit = widget.source_config(
sub_widget=filters.pop("slug", [None])[0]
) # implement a dynamic handler for each widget
)

default_filters.update(filters)

project_timezone = widget.project.timezone
apply_timezone_to_filters(default_filters, project_timezone)

filters.update(default_filters)
if operation == "list":
tags = filters.pop("tags", [None])[0]
tags = default_filters.pop("tags", [None])[0]
if tags:
filters["tags"] = tags.split(",")
default_filters["tags"] = tags.split(",")

if op_field:
query_kwargs["op_field"] = op_field
if limit:
query_kwargs["limit"] = limit

serialized_source = SourceQuery.execute(
filters=filters,
filters=default_filters,
operation=operation,
parser=parse_dict_to_json,
project=widget.project,
Expand Down

0 comments on commit e32d1e9

Please sign in to comment.