-
Notifications
You must be signed in to change notification settings - Fork 181
Initial PR to add support for stackoverflow teams #78
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
Open
allen-munsch
wants to merge
11
commits into
GerevAI:main
Choose a base branch
from
allen-munsch:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
2b33e4f
initial attempt to add stackoverflow support
dfebfad
initial attempt to add stackoverflow support
6d0b810
initial attempt to add stackoverflow support
1f73af8
test the download, add the help text to the data-source-panel.tsx
dd575dc
test the download, add the help text to the data-source-panel.tsx, up…
036e7e5
comment out the test function
e52ec76
try to address code review, move io bound to queue, check for last in…
5f14fc3
rate limit the requests
05ae7c9
add a rate limiter
ec910fa
wire up async sqlite, add rate limiter, fix rendering issues on DataS…
1e4f56e
Merge branch 'main' into main
allen-munsch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
sqlite3 ~/.gerev/storage/tasks.sqlite3/data.db 'delete from ack_queue_task where _id in (select _id from ack_queue_task);' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
sqlite3 ~/.gerev/storage/db.sqlite3 'delete from data_source where id in (select id from data_source);' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
import logging | ||
import time | ||
from dataclasses import dataclass | ||
from datetime import datetime | ||
from typing import Dict, List, Optional | ||
import requests | ||
|
||
from data_source.api.base_data_source import BaseDataSource, ConfigField, HTMLInputType, BaseDataSourceConfig | ||
from data_source.api.basic_document import DocumentType, BasicDocument | ||
from queues.index_queue import IndexQueue | ||
|
||
from data_source.api.utils import rate_limit | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
@dataclass | ||
class StackOverflowPost: | ||
link: str | ||
score: int | ||
last_activity_date: int | ||
creation_date: int | ||
post_id: Optional[int] = None | ||
post_type: Optional[str] = None | ||
body_markdown: Optional[str] = None | ||
owner_account_id: Optional[int] = None | ||
owner_reputation: Optional[int] = None | ||
owner_user_id: Optional[int] = None | ||
owner_user_type: Optional[str] = None | ||
owner_profile_image: Optional[str] = None | ||
owner_display_name: Optional[str] = None | ||
owner_link: Optional[str] = None | ||
title: Optional[str] = None | ||
last_edit_date: Optional[str] = None | ||
tags: Optional[List[str]] = None | ||
view_count: Optional[int] = None | ||
article_id: Optional[int] = None | ||
article_type: Optional[str] = None | ||
|
||
class StackOverflowConfig(BaseDataSourceConfig): | ||
api_key: str | ||
team_name: str | ||
|
||
|
||
@rate_limit(allowed_per_second=15) | ||
def rate_limited_get(url, headers): | ||
''' | ||
https://api.stackoverflowteams.com/docs/throttle | ||
https://api.stackexchange.com/docs/throttle | ||
Every application is subject to an IP based concurrent request throttle. | ||
If a single IP is making more than 30 requests a second, new requests will be dropped. | ||
The exact ban period is subject to change, but will be on the order of 30 seconds to a few minutes typically. | ||
Note that exactly what response an application gets (in terms of HTTP code, text, and so on) | ||
is undefined when subject to this ban; we consider > 30 request/sec per IP to be very abusive and thus cut the requests off very harshly. | ||
''' | ||
resp = requests.get(url, headers=headers) | ||
if resp.status_code == 429: | ||
logger.warning('Rate limited, sleeping for 5 minutes') | ||
time.sleep(300) | ||
return rate_limited_get(url, headers) | ||
return resp | ||
|
||
|
||
class StackOverflowDataSource(BaseDataSource): | ||
|
||
@staticmethod | ||
def get_config_fields() -> List[ConfigField]: | ||
return [ | ||
ConfigField(label="PAT API Key", name="api_key", type=HTMLInputType.TEXT), | ||
ConfigField(label="Team Name", name="team_name", type=HTMLInputType.TEXT), | ||
] | ||
|
||
@staticmethod | ||
async def validate_config(config: Dict) -> None: | ||
so_config = StackOverflowConfig(**config) | ||
url = f'https://api.stackoverflowteams.com/2.3/questions?&team={so_config.team_name}' | ||
response = rate_limited_get(url, headers={'X-API-Access-Token': so_config.api_key}) | ||
response.raise_for_status() | ||
|
||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
so_config = StackOverflowConfig(**self._raw_config) | ||
self._api_key = so_config.api_key | ||
self._team_name = so_config.team_name | ||
|
||
def _fetch_posts(self, *, api_key: str, team_name: str, page: int, doc_type: str) -> None: | ||
team_fragment = f'&team={team_name}' | ||
# this is a filter for "body markdown" inclusion, all filters are unique and static | ||
# i am not entirely sure if this is per account, or usable by everyone | ||
filter_fragment = '&filter=!nOedRLbqzB' | ||
page_fragment = f'&page={page}' | ||
# it looked like the timestamp was 10 digits, lets only look at stuff that is newer than the last index time | ||
from_date_fragment = f'&fromdate={self._last_index_time.timestamp():.10n}' | ||
url = f'https://api.stackoverflowteams.com/2.3/{doc_type}?{team_fragment}{filter_fragment}{page_fragment}{from_date_fragment}' | ||
response = rate_limited_get(url, headers={'X-API-Access-Token': api_key}) | ||
response.raise_for_status() | ||
response = response.json() | ||
has_more = response['has_more'] | ||
items = response['items'] | ||
logger.info(f'Fetched {len(items)} {doc_type} from Stack Overflow') | ||
for item_dict in items: | ||
owner_fields = {} | ||
if 'owner' in item_dict: | ||
owner_fields = {f"owner_{k}": v for k, v in item_dict.pop('owner').items()} | ||
if 'title' not in item_dict: | ||
item_dict['title'] = item_dict['link'] | ||
post = StackOverflowPost(**item_dict, **owner_fields) | ||
last_modified = datetime.fromtimestamp(post.last_edit_date or post.last_activity_date) | ||
if last_modified < self._last_index_time: | ||
return | ||
logger.info(f'Feeding {doc_type} {post.title}') | ||
post_document = BasicDocument(title=post.title, content=post.body_markdown, author=post.owner_display_name, | ||
timestamp=datetime.fromtimestamp(post.creation_date), id=post.post_id, | ||
data_source_id=self._data_source_id, location=post.link, | ||
url=post.link, author_image_url=post.owner_profile_image, | ||
type=DocumentType.MESSAGE) | ||
IndexQueue.get_instance().put_single(doc=post_document) | ||
if has_more: | ||
# paginate onto the queue | ||
self.add_task_to_queue(self._fetch_posts, api_key=self._api_key, team_name=self._team_name, page=page + 1, doc_type=doc_type) | ||
|
||
def _feed_new_documents(self) -> None: | ||
self.add_task_to_queue(self._fetch_posts, api_key=self._api_key, team_name=self._team_name, page=1, doc_type='posts') | ||
# TODO: figure out how to get articles | ||
# self.add_task_to_queue(self._fetch_posts, api_key=self._api_key, team_name=self._team_name, page=1, doc_type='articles') | ||
|
||
|
||
# def test(): | ||
# import os | ||
# config = {"api_key": os.environ['SO_API_KEY'], "team_name": os.environ['SO_TEAM_NAME']} | ||
# so = StackOverflowDataSource(config=config, data_source_id=1) | ||
# so._feed_new_documents() | ||
# | ||
# | ||
# if __name__ == '__main__': | ||
# test() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.