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 aggregate hourly downloads function #304

Merged
merged 6 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ celerybeat-schedule
.env

# virtualenv
requirements.txt
.venv
venv/
ENV/
Expand Down
56 changes: 56 additions & 0 deletions gcp/cloud_functions/aggregate_hourly_downloads/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# summary
aggregates hourly download stats into a table grouping by category, country and download type. In response to a pubsub takes data from arxiv_stats.papers_downloaded_by_ip_recently. Looks up the categories for each paper from our main database and aggregates the data. Then writes this to another table which is configured by the WRITE_TABLE enviroment variable

# commands
this will need to be in its own dev enviroment seperate from base where you can install the dependencies. This way the function doesn't get surprised by changes made to the rest of base that it isnt ready for.

to install
` pip install -r src/requirements.txt `
and
` pip install -r src/requirements-dev.txt `

# enviroment variables
```
export ENVIRONMENT=DEVELOPMENT LOG_LEVEL=INFO DOWNLOAD_TABLE=arxiv-development.arxiv_stats.papers_downloaded_by_ip_recently WRITE_TABLE='sqlite:///../tests/output_test.db'

```

you need everything in the env file plus:

```
export CLASSIC_DB_URI='SECRET_HERE'
export WRITE_TABLE='SECRET_HERE'
```
the classic db URI always needs to be for the prodution database to get category data for recent papers

to run use this in the src folder
` functions-framework --target=aggregate_hourly_downloads --signature-type=cloudevent `

message data options:
currently the contents of the message don't matter any message triggers the run

to trigger run a curl command with a cloud event, heres an example you can use:
note that the data is base 64 encoded, and that return values from cloud functions seem to be useless
```
curl localhost:8080 \
-X POST \
-H "Content-Type: application/json" \
-H "ce-id: 123451234512345" \
-H "ce-specversion: 1.0" \
-H "ce-time: 2020-01-02T12:34:56.789Z" \
-H "ce-type: google.cloud.pubsub.topic.v1.messagePublished" \
-H "ce-source: //pubsub.googleapis.com/projects/MY-PROJECT/topics/MY-TOPIC" \
-d '{
"message": {
"data": "eyJwYXBlcl9pZCI6IjEwMDguMzIyMiIsICJvbGRfY2F0ZWdvcmllcyI6ImVlc3MuU1kgaGVwLWxhdCJ9",
"attributes": {
"attr1":"attr1-value"
}
},
"subscription": "projects/MY-PROJECT/subscriptions/MY-SUB"
}'

```

to run tests
` pytest tests `
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ENVIRONMENT: DEVELOPMENT
LOG_LEVEL: INFO
DOWNLOAD_TABLE: arxiv-development.arxiv_stats.papers_downloaded_by_ip_recently
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ENVIRONMENT: PRODUCTION
LOG_LEVEL: INFO
DOWNLOAD_TABLE: arxiv-production.arxiv_stats.papers_downloaded_by_ip_recently
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
steps:
# Deploy to Cloud Run
- name: 'google/cloud-sdk'
id: Deploy
entrypoint: gcloud
args: ['functions', 'deploy', '${_FUNCTION_NAME}',
'--retry',
'--gen2',
'--source', '${_SRCDIR}',
'--runtime', 'python311',
'--region', '${_REGION}',
'--trigger-topic', '${_PUBSUB_NAME}',
'--entry-point', '${_ENTRY_POINT}',
'--service-account', '${_RUN_ACCOUNT}',
'--env-vars-file', '${_ROOTDIR}/cicd/env.${_FUNCTION_NAME}.${_PROJECT_ID}.yaml',
'--allow-unauthenticated'
]
# Without this, build does not proceed
options:
logging: CLOUD_LOGGING_ONLY
Loading
Loading