-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4825 from GSA/ga-metrics
- Loading branch information
Showing
14 changed files
with
1,000 additions
and
6 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
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 |
---|---|---|
|
@@ -62,3 +62,7 @@ ansible/roles/vendor | |
.vscode/settings.json | ||
|
||
output | ||
|
||
## dont ignore | ||
# metrics | ||
!metrics/.env |
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
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 @@ | ||
AWS_S3_BUCKET_METRICS=cg-baa85e06-1bdd-4672-9e3a-36333c05c6ce |
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,22 @@ | ||
{ | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": "Poetry debug", | ||
"type": "debugpy", | ||
"request": "launch", | ||
"pythonPath": "${workspaceFolder}/.venv/bin/python", | ||
"cwd": "${workspaceFolder}", | ||
"module": "datagov_metrics.ga", | ||
"justMyCode": false, | ||
"args": [ | ||
"src.main:app", | ||
"--host", | ||
"0.0.0.0", | ||
"--port", | ||
"8000", | ||
"--reload" | ||
] | ||
} | ||
] | ||
} |
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,12 @@ | ||
.DEFAULT_GOAL := help | ||
SHELL := /bin/bash | ||
|
||
.PHONY: py-lint | ||
py-lint: ## Run python linting scanners and black | ||
poetry run ruff check . --fix | ||
|
||
# Output documentation for top-level targets | ||
# Thanks to https://marmelab.com/blog/2016/02/29/auto-documented-makefile.html | ||
.PHONY: help | ||
help: | ||
@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {printf "\033[36m%-10s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST) |
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,11 @@ | ||
# datagov-metrics | ||
|
||
A python module to fetch metrics from various sources (GA & CKAN) and push them to S3 | ||
|
||
Reports are then available at the path: | ||
|
||
https://s3-us-gov-west-1.amazonaws.com/cg-baa85e06-1bdd-4672-9e3a-36333c05c6ce/{file_name} | ||
|
||
Ex. | ||
https://s3-us-gov-west-1.amazonaws.com/cg-baa85e06-1bdd-4672-9e3a-36333c05c6ce/global__datasets_per_org.csv | ||
|
Empty file.
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,4 @@ | ||
from datagov_metrics import ckan, ga | ||
|
||
ga.main() | ||
ckan.main() |
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,43 @@ | ||
import requests | ||
import csv | ||
import io | ||
from datagov_metrics.s3_util import put_data_to_s3 | ||
|
||
CKAN_BASE_URL = "https://catalog.data.gov/api/action/package_search" | ||
QUERIES = { | ||
"harvest_sources": '?fq=dataset_type:harvest&facet.field=["organization"]&facet.limit=200&rows=0', | ||
"datasets_per_org": '?q=*:*&facet.field=["organization"]&facet.limit=200&rows=0', | ||
} | ||
|
||
|
||
def get_data(): | ||
query_dict = {} | ||
for k, v in QUERIES.items(): | ||
url = f"{CKAN_BASE_URL}{v}" | ||
repo = requests.get(url) | ||
data = repo.json() | ||
|
||
raw_data = data["result"]["facets"]["organization"] | ||
|
||
query_dict[k] = [[k, v] for (k, v) in raw_data.items()] | ||
return query_dict | ||
|
||
|
||
def write_data_to_csv(response): | ||
"""Reshape the response CSV.""" | ||
with io.StringIO() as csv_buffer: | ||
writer = csv.writer(csv_buffer, delimiter=",") | ||
writer.writerow(["organization", "count"]) # write header | ||
writer.writerows(response) | ||
return csv_buffer.getvalue() | ||
|
||
|
||
def main(): | ||
data = get_data() | ||
for k, v in data.items(): | ||
csv_data = write_data_to_csv(v) | ||
put_data_to_s3(f"global__{k}.csv", csv_data) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
Oops, something went wrong.