-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add aggregate hourly downloads function
- Loading branch information
Showing
7 changed files
with
628 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,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 ` |
3 changes: 3 additions & 0 deletions
3
...ons/aggregate_hourly_downloads/cicd/env.aggregate_hourly_downloads.arxiv-development.yaml
This file contains 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,3 @@ | ||
ENVIRONMENT: DEVELOPMENT | ||
LOG_LEVEL: INFO | ||
DOWNLOAD_TABLE: arxiv-development.arxiv_stats.papers_downloaded_by_ip_recently |
3 changes: 3 additions & 0 deletions
3
...ions/aggregate_hourly_downloads/cicd/env.aggregate_hourly_downloads.arxiv-production.yaml
This file contains 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,3 @@ | ||
ENVIRONMENT: PRODUCTION | ||
LOG_LEVEL: INFO | ||
DOWNLOAD_TABLE: arxiv-production.arxiv_stats.papers_downloaded_by_ip_recently |
20 changes: 20 additions & 0 deletions
20
gcp/cloud_functions/aggregate_hourly_downloads/cicd/pubsub_steps.yaml
This file contains 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,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 |
Oops, something went wrong.