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

Send USGS Data to Kafka #7

Merged
merged 17 commits into from
Jun 26, 2024
78 changes: 77 additions & 1 deletion src/sasquatchbackpack/cli.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
from datetime import timedelta

import click
Expand All @@ -10,6 +11,8 @@

DEFAULT_MAGNITUDE_BOUNDS = (2, 10)

DEFAULT_TEST = False


def check_duration(
ctx: click.Context, param: dict, value: tuple[int, int]
Expand Down Expand Up @@ -155,11 +158,20 @@ def main() -> None:
show_default=True,
callback=check_magnitude_bounds,
)
@click.option(
"-t",
"--test",
help="set to True to echo API results without sending them",
default=DEFAULT_TEST,
type=bool,
show_default=True,
)
def usgs_earthquake_data(
duration: tuple[int, int],
radius: int,
coords: tuple[float, float],
magnitude_bounds: tuple[int, int],
test: bool,
) -> None:
"""Seaches USGS databases for relevant earthquake data and prints it
to console
Expand All @@ -180,10 +192,74 @@ def usgs_earthquake_data(
click.echo(result)
click.echo("------")
else:
click.secho("SUCCESS", fg="orange")
click.secho("SUCCESS! (kinda)", fg="yellow")
click.echo("------")
click.echo("No results found for the provided criteria :(")
click.echo("------")
return

if not test:
click.echo("Sending data...")

# sasquatch_rest_proxy_url = (
# "https://data-int.lsst.cloud/sasquatch-rest-proxy"
# )

namespace = "lsst.example"
topic_name = "usgs-earthquake-data"

value_schema = json.dumps(
Fireye04 marked this conversation as resolved.
Show resolved Hide resolved
{
"namespace": f"{namespace}",
"type": "record",
"name": f"{topic_name}",
"description": "Collection of earthquakes near the "
+ "summit",
"fields": [
{"name": "timestamp", "type": "long"},
{"name": "id", "type": "str"},
{"name": "latitude", "type": "float"},
{"name": "longitude", "type": "float"},
{"name": "depth", "type": "float", "units": "Km"},
{"name": "magnitude", "type": "float"},
],
}
)

# url = f"{sasquatch_rest_proxy_url}/topics/{namespace}.{topic_name}"

# headers = {
# "Content-Type": "application/vnd.kafka.avro.v2+json",
# "Accept": "application/vnd.kafka.v2+json",
# }

records = []

for result in results:
records.append(
{
"value": {
"timestamp": result.time.strftime("%s"),
"id": result.id,
"latitude": result.latitude,
"longitude": result.longitude,
"depth": float(result.depth),
"magnitude": float(result.magnitude),
}
}
)

payload = {"value_schema": value_schema, "records": records}

print(payload)

# response = requests.request("POST",
# url,
# json=payload,
# headers=headers
# )

# print(response.text)


if __name__ == "__main__":
Fireye04 marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
30 changes: 30 additions & 0 deletions test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"namespace": "lsst.example",
Fireye04 marked this conversation as resolved.
Show resolved Hide resolved
"type": "record",
"name": "usgs-earthquake-data",
"description": "Collection of earthquakes near the summit",
"fields": [
{
"name": "timestamp",
"type": "long"
Fireye04 marked this conversation as resolved.
Show resolved Hide resolved
},
{
"name": "latitude",
"type": "float"
},
{
"name": "longitude",
"type": "float"
},
{
"name": "distance",
"type": "float",
"description": "Distance from the summit",
"units": "Km"
},
{
"name": "magnitude",
"type": "float"
}
]
}