Skip to content
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

Feature add start date option #4

Merged
merged 2 commits into from
Dec 17, 2024
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
13 changes: 12 additions & 1 deletion component_config/configSchema.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,18 @@
},
"required": true,
"description": "Full Sync downloads all data from the source every run, Incremental Sync downloads data (tickets, ticket_comments and ticket_audits) by parameter start_time described <a href='https://developer.zendesk.com/api-reference/ticketing/ticket-management/incremental_exports/#per_page'>here</a>. The start time is taken from the last successful run.",
"propertyOrder": 20
"propertyOrder": 10
},
"date_from": {
"propertyOrder": 20,
"type": "string",
"title": "From date",
"description": "Date from. Date in YYYY-MM-DD format or a string i.e. 5 days ago, 1 month ago, yesterday, etc. If left empty, all records from 2000-01-01 are downloaded.",
"options": {
"dependencies": {
"sync_mode": "incremental_sync"
}
}
}
}
},
Expand Down
20 changes: 19 additions & 1 deletion src/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from collections import OrderedDict
from typing import List

import dateparser
import dlt
from dlt.common import pendulum
from dlt.common.time import ensure_pendulum_datetime
Expand Down Expand Up @@ -45,7 +46,7 @@ def run(self):

# get the previous start time
if self.params.sync_options.is_incremental:
previous_start: int = self.get_state_file().get("time", {}).get("previousStart", DEFAULT_START_DATE)
previous_start = self._def_start_timestamp()
logging.info("Incremental mode")
else:
previous_start = DEFAULT_START_DATE
Expand All @@ -72,6 +73,23 @@ def run(self):
logging.info(f"Saving the state file with the actual start date {actual_start}")
self.write_state_file({"time": {"previousStart": actual_start}})

def _def_start_timestamp(self):
if self.params.sync_options.date_from:
return self._parse_date(self.params.sync_options.date_from)
else:
return int(self.get_state_file().get("time", {}).get("previousStart", DEFAULT_START_DATE))

@staticmethod
def _parse_date(date_to_parse: str) -> int:
try:
parsed_date = dateparser.parse(date_to_parse)
if parsed_date.tzinfo is None:
parsed_date = parsed_date.replace(tzinfo=pendulum.UTC)
return int(parsed_date.timestamp())
except (AttributeError, TypeError) as err:
raise UserException(f"Failed to parse date {date_to_parse}, make sure the date is either in YYYY-MM-DD "
f"format or relative date i.e. 5 days ago, 1 month ago, yesterday, etc.") from err

def _set_dlt(self):
# prepare the temporary directories
os.makedirs(DLT_TMP_DIR, exist_ok=True)
Expand Down
1 change: 1 addition & 0 deletions src/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Authentication(BaseModel):

class SyncOptions(BaseModel):
sync_mode: str
date_from: str = Field(default=None)

@computed_field
def is_incremental(self) -> bool:
Expand Down
Loading