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

Add cost callback for rest calls #142

Merged
merged 7 commits into from
Jun 22, 2022
Merged
Show file tree
Hide file tree
Changes from 6 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
337 changes: 179 additions & 158 deletions poetry.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ license = "Apache 2.0"
PyJWT = "2.4.0"
python = "<3.11,>=3.7.2"
requests = "^2.25.1"
singer-sdk = "^0.4.9"
# singer-sdk = "^0.4.9"
# For local SDK dev:
# singer-sdk = {path = "../singer-sdk", develop = true}
# singer-sdk = {git = "https://gitlab.com/meltano/singer-sdk.git", rev = "97-hierarchical-streams"}
singer-sdk = {git = "https://github.com/oviohub/meltano-sdk.git", rev = "040a8383f3994f47d5a324da81e2cb7b559ee23c"}
types-simplejson = "^3.17.2"
types-python-dateutil = "^2.8.6"
nested-lookup = "^0.2.23"
Expand Down
20 changes: 20 additions & 0 deletions tap_github/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,15 @@ def backoff_handler(self, details: dict) -> None:
self.authenticator.get_next_auth_token()
prepared_request.headers.update(self.authenticator.auth_headers or {})

def calculate_sync_cost(
self,
request: requests.PreparedRequest,
response: requests.Response,
context: Optional[dict],
) -> Dict[str, int]:
"""Return the cost of the last REST API call."""
return {"rest": 1, "graphql": 0, "search": 0}


class GitHubGraphqlStream(GraphQLStream, GitHubRestStream):
"""GitHub Graphql stream class."""
Expand Down Expand Up @@ -354,3 +363,14 @@ def get_url_params(
params["since"] = str(since)

return params

def calculate_sync_cost(
self,
request: requests.PreparedRequest,
response: requests.Response,
context: Optional[dict],
) -> Dict[str, int]:
"""Return the cost of the last graphql API call."""
costgen = extract_jsonpath("$.data.rateLimit.cost", input=response.json())
cost = next(costgen)
return {"rest": 0, "graphql": int(cost), "search": 0}
10 changes: 9 additions & 1 deletion tap_github/repository_streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def query(self) -> str:
f'repo{i}: repository(name: "{repo[1]}", owner: "{repo[0]}") '
"{ nameWithOwner databaseId }"
)
return "query {" + " ".join(chunks) + " }"
return "query {" + " ".join(chunks) + " rateLimit { cost } }"

repos_with_ids: list = list()
temp_stream = TempStream(self._tap, list(repo_list))
Expand All @@ -101,6 +101,8 @@ def query(self) -> str:
# the line.
for record in temp_stream.request_records({}):
for item in record.keys():
if item == "rateLimit":
continue
try:
org, repo = record[item]["nameWithOwner"].split("/")
except TypeError:
Expand Down Expand Up @@ -1612,6 +1614,9 @@ def query(self) -> str:
}
}
}
rateLimit {
cost
}
}
"""

Expand Down Expand Up @@ -2103,6 +2108,9 @@ def query(self) -> str:
}
}
}
rateLimit {
cost
}
}

"""
Expand Down
2 changes: 2 additions & 0 deletions tap_github/streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
RepositoryStream,
ReviewCommentsStream,
ReviewsStream,
StargazersGraphqlStream,
StargazersStream,
StatsContributorsStream,
WorkflowRunJobsStream,
Expand Down Expand Up @@ -86,6 +87,7 @@ def __init__(self, valid_queries: Set[str], streams: List[Type[Stream]]):
RepositoryStream,
ReviewCommentsStream,
ReviewsStream,
StargazersGraphqlStream,
StargazersStream,
StatsContributorsStream,
WorkflowRunJobsStream,
Expand Down
7 changes: 6 additions & 1 deletion tap_github/user_streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def query(self) -> str:
f'user{i}: repositoryOwner(login: "{user}") '
"{ login avatarUrl}"
)
return "query {" + " ".join(chunks) + " }"
return "query {" + " ".join(chunks) + " rateLimit { cost } }"

users_with_ids: list = list()
temp_stream = TempStream(self._tap, list(user_list))
Expand All @@ -88,6 +88,8 @@ def query(self) -> str:
# the line.
for record in temp_stream.request_records({}):
for item in record.keys():
if item == "rateLimit":
continue
try:
username = record[item]["login"]
except TypeError:
Expand Down Expand Up @@ -293,6 +295,9 @@ def query(self) -> str:
}
}
}
rateLimit {
cost
}
}
"""

Expand Down