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

feat : Mgramseva expense stream incremental #115

Closed
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"todo-stream-name": {
"todo-field-name": "value"
"mgramseva_tenant_expenses": {
"toDate": "2024-08-09"
}
}
90 changes: 45 additions & 45 deletions airbyte-integrations/connectors/source-mgramseva/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
},
"data": {
"type": "object"
},
"toDate":{
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we must have cursor field in schema

"type": "string"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import pytz
from airbyte_cdk.models import SyncMode
from airbyte_cdk.sources import AbstractSource
from airbyte_cdk.sources.streams import Stream
from airbyte_cdk.sources.streams import Stream, CheckpointMixin
from airbyte_cdk.sources.streams.http import HttpStream
from airbyte_cdk.sources.streams.core import StreamData

Expand Down Expand Up @@ -216,12 +216,15 @@ def parse_response(self, response: requests.Response, **kwargs) -> Iterable[Mapp
expenses["toDate"] = self.month_end.strftime("%Y-%m-%d")
combined_string = f"{self.tenantid}{expenses['fromDate']}{expenses['toDate']}"
id_hash = hashlib.sha256(combined_string.encode())
return [{"data": expenses, "id": id_hash.hexdigest()}]
return [{"data": expenses, "id": id_hash.hexdigest(),"toDate":expenses["toDate"]}]


class MgramsevaTenantExpenses(MgramsevaStream):
class MgramsevaTenantExpenses(MgramsevaStream, CheckpointMixin):
"""object for tenant payments"""

cursor_field = "toDate"
_cursor_value = None

def __init__(
self, headers: dict, request_info: dict, user_request: dict, tenantid_list: list, fromdate: datetime, todate: datetime, **kwargs
): # pylint: disable=super-init-not-called
Expand All @@ -234,8 +237,20 @@ def __init__(
self.request_info = request_info
self.user_request = user_request
self.tenantid_list = tenantid_list
self.fromdate = fromdate
self.todate = todate
self.fromdate = fromdate.replace(hour=0, minute=0, second=0, microsecond=0)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trying to fetch record on month basis

self.todate = todate.replace(hour=0, minute=0, second=0, microsecond=0)

@property
def state(self) -> Mapping[str, Any]:
if self._cursor_value:
return {self.cursor_field: str(self._cursor_value)}
else:
return {self.cursor_field: str(self.fromdate)}

@state.setter
def state(self, value: Mapping[str, Any]):
if self.cursor_field in value:
self._cursor_value = value[self.cursor_field]

def read_records(
self,
Expand All @@ -248,25 +263,30 @@ def read_records(

for tenantid in self.tenantid_list:

month_start = self.fromdate.replace(day=1)
state_value = self.state[self.cursor_field]

while month_start < self.todate:
month_start = datetime.fromisoformat(state_value).replace(day=1)

next_month_start = month_start + relativedelta(months=1) - timedelta(milliseconds=1)
to_date = self.todate.replace(day=1)

while month_start < to_date:

next_month_start = month_start + relativedelta(months=1)
stream = MgramsevaTenantExpense(
"echallan-services/eChallan/v1/_expenseDashboard",
self.headers,
self.request_info,
self.user_request,
tenantid,
month_start,
next_month_start,
next_month_start - timedelta(milliseconds=1),
"ExpenseDashboard",
)
yield from stream.read_records(sync_mode, cursor_field, stream_slice, stream_state)

for record in stream.read_records(sync_mode, cursor_field, stream_slice, stream_state):
yield record
month_start = next_month_start

self._cursor_value = str(self.todate.replace(day=1))


class MgramsevaPayments(MgramsevaStream):
Expand Down
Loading