ALPR Stats (#2)

* set up lambda to get total ALPR counts

* show total counts on map, updated hourly
This commit is contained in:
Will Freeman
2024-11-20 14:49:19 -06:00
committed by GitHub
parent 6bb2c1ea99
commit 0d062296a3
9 changed files with 215 additions and 175 deletions

9
.gitignore vendored
View File

@@ -36,5 +36,14 @@ terraform.tfstate.d/
.terraform/
.terraform.lock.hcl
terraform.tfvars
terraform.tfstate*
.venv/
# Lambda Python Stuff
serverless/*/lambda.zip
serverless/*/src/*
!serverless/*/src/alpr_counts.py
!serverless/*/src/requirements.txt
# TODO: need a better way to handle python packages

View File

@@ -0,0 +1,48 @@
import json
import requests
import boto3
def fetch_alpr_surveillance_nodes(usOnly=False):
overpass_url = "http://overpass-api.de/api/interpreter"
overpass_query = f"""
[out:json];
{'area["ISO3166-1"="US"]->.searchArea;' if usOnly else ''}
node["man_made"="surveillance"]["surveillance:type"="ALPR"]{f'(area.searchArea)' if usOnly else ''};
out count;
"""
response = requests.get(overpass_url, params={'data': overpass_query})
if response.status_code == 200:
response_json = response.json()
try:
return response_json['elements'][0]['tags']['nodes']
except (IndexError, KeyError) as e:
return {"error": "Could not find 'elements[0].tags.nodes' in the response."}
else:
return {"error": f"Failed to fetch data from Overpass API. Status code: {response.status_code}"}
def lambda_handler(event, context):
us_alprs = fetch_alpr_surveillance_nodes('(area["ISO3166-1"="US"])')
worldwide_alprs = fetch_alpr_surveillance_nodes()
all_alprs = {
'us': us_alprs,
'worldwide': worldwide_alprs
}
s3 = boto3.client('s3')
bucket = 'deflock-clusters'
key = 'alpr-counts.json'
s3.put_object(
Bucket=bucket,
Key=key,
Body=json.dumps(all_alprs),
ContentType='application/json'
)
return {
'statusCode': 200,
'body': 'Successfully fetched ALPR counts.',
}

View File

@@ -0,0 +1,2 @@
requests
boto3

101
terraform/alpr-counts.tf Normal file
View File

@@ -0,0 +1,101 @@
locals {
alpr_counts_filename = "alpr-counts.json"
}
provider "aws" {
region = "us-east-1"
}
resource "aws_iam_role" "lambda_role" {
name = "lambda_s3_write_role"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "sts:AssumeRole"
Effect = "Allow"
Principal = {
Service = "lambda.amazonaws.com"
}
}
]
})
}
resource "aws_iam_policy" "lambda_s3_write_policy" {
name = "lambda_s3_write_policy"
description = "Policy for Lambda to write to S3 bucket deflock-clusters"
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = [
"s3:PutObject",
"s3:PutObjectAcl"
]
Effect = "Allow"
Resource = "arn:aws:s3:::deflock-clusters/${local.alpr_counts_filename}"
}
]
})
}
resource "aws_iam_role_policy_attachment" "lambda_s3_write_attachment" {
role = aws_iam_role.lambda_role.name
policy_arn = aws_iam_policy.lambda_s3_write_policy.arn
}
resource "null_resource" "pip_install" {
provisioner "local-exec" {
command = <<EOT
cd ${path.module}/../serverless/alpr-counts/src
pip3 install -r requirements.txt -t .
EOT
}
triggers = {
# Re-run the provisioner if the file changes
file_hash = "${filemd5("${path.module}/../serverless/alpr-counts/src/alpr_counts.py")}"
}
}
data "archive_file" "python_lambda_package" {
type = "zip"
source_dir = "${path.module}/../serverless/alpr-counts/src"
output_path = "${path.module}/../serverless/alpr-counts/lambda.zip"
depends_on = [ null_resource.pip_install ]
}
resource "aws_lambda_function" "overpass_lambda" {
filename = data.archive_file.python_lambda_package.output_path
function_name = "alpr_counts"
role = aws_iam_role.lambda_role.arn
handler = "alpr_counts.lambda_handler"
runtime = "python3.9"
source_code_hash = data.archive_file.python_lambda_package.output_base64sha256
timeout = 30
}
resource "aws_cloudwatch_event_rule" "lambda_rule" {
name = "alpr_counts_rule"
description = "Rule to trigger alpr_counts lambda"
schedule_expression = "rate(60 minutes)"
}
resource "aws_cloudwatch_event_target" "lambda_target" {
target_id = "alpr_counts_target"
rule = aws_cloudwatch_event_rule.lambda_rule.name
arn = aws_lambda_function.overpass_lambda.arn
}
resource "aws_lambda_permission" "allow_cloudwatch_to_call_lambda" {
statement_id = "AllowExecutionFromCloudWatch"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.overpass_lambda.function_name
principal = "events.amazonaws.com"
source_arn = aws_cloudwatch_event_rule.lambda_rule.arn
}

View File

@@ -1,8 +0,0 @@
variable "domain_name" {
type = string
description = "Domain name"
}
variable "bucket_name" {
type = string
description = "S3 bucket name for the static site"
}

View File

@@ -1,167 +0,0 @@
# Provider Configuration
provider "aws" {
region = "us-east-1" # ACM certificates for CloudFront must be in us-east-1
}
# Route 53 Zone for Domain
resource "aws_route53_zone" "deflock_me" {
name = var.domain_name
}
# S3 Bucket for Static Site Hosting
resource "aws_s3_bucket" "vue_app" {
bucket = var.bucket_name
tags = {
Name = "Vue App Static Site Bucket"
}
}
resource "aws_s3_bucket_acl" "vue_app_acl" {
bucket = aws_s3_bucket.vue_app.id
acl = "private"
}
resource "aws_s3_bucket_website_configuration" "vue_app" {
bucket = aws_s3_bucket.vue_app.id
index_document {
suffix = "index.html"
}
error_document {
key = "index.html"
}
}
resource "aws_s3_bucket_policy" "s3_access_policy" {
bucket = aws_s3_bucket.vue_app.id
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Sid = "AllowPublic"
Effect = "Allow"
Principal = "*"
Action = "s3:GetObject"
Resource = "${aws_s3_bucket.vue_app.arn}/*"
},
]
})
}
# ACM Certificate for HTTPS
resource "aws_acm_certificate" "deflock_me_cert" {
domain_name = var.domain_name
validation_method = "DNS"
subject_alternative_names = [
"www.${var.domain_name}"
]
lifecycle {
create_before_destroy = true
}
}
# DNS Validation Records for ACM Certificate
resource "aws_route53_record" "deflock_me_cert_validation" {
for_each = {
for dvo in aws_acm_certificate.deflock_me_cert.domain_validation_options : dvo.domain_name => {
name = dvo.resource_record_name
type = dvo.resource_record_type
value = dvo.resource_record_value
zone_id = aws_route53_zone.deflock_me.zone_id
}
}
zone_id = each.value.zone_id
name = each.value.name
type = each.value.type
ttl = 60
records = [each.value.value]
}
# ACM Certificate Validation Completion
resource "aws_acm_certificate_validation" "deflock_me_cert_validation" {
certificate_arn = aws_acm_certificate.deflock_me_cert.arn
validation_record_fqdns = [for record in aws_route53_record.deflock_me_cert_validation : record.fqdn]
}
# CloudFront Distribution for CDN and HTTPS
resource "aws_cloudfront_distribution" "vue_app_cdn" {
origin {
domain_name = aws_s3_bucket.vue_app.website_endpoint
origin_id = "S3-VueApp"
custom_origin_config {
http_port = 80
https_port = 443
origin_protocol_policy = "http-only"
origin_ssl_protocols = ["TLSv1.2"]
}
}
enabled = true
is_ipv6_enabled = true
comment = "CDN for ${var.domain_name}"
default_root_object = "index.html"
aliases = ["${var.domain_name}", "www.${var.domain_name}"]
default_cache_behavior {
allowed_methods = ["GET", "HEAD"]
cached_methods = ["GET", "HEAD"]
target_origin_id = "S3-VueApp"
forwarded_values {
query_string = false
cookies {
forward = "none"
}
}
viewer_protocol_policy = "redirect-to-https"
min_ttl = 0
default_ttl = 3600
max_ttl = 86400
}
restrictions {
geo_restriction {
restriction_type = "none"
}
}
viewer_certificate {
acm_certificate_arn = aws_acm_certificate_validation.deflock_me_cert_validation.certificate_arn
ssl_support_method = "sni-only"
minimum_protocol_version = "TLSv1.2_2021"
}
tags = {
Name = "CloudFront Vue App CDN"
}
}
# Route 53 Records for Domain
resource "aws_route53_record" "deflock_me_root" {
zone_id = aws_route53_zone.deflock_me.zone_id
name = var.domain_name
type = "A"
alias {
name = aws_cloudfront_distribution.vue_app_cdn.domain_name
zone_id = aws_cloudfront_distribution.vue_app_cdn.hosted_zone_id
evaluate_target_health = false
}
}
resource "aws_route53_record" "deflock_me_www" {
zone_id = aws_route53_zone.deflock_me.zone_id
name = "www.${var.domain_name}"
type = "A"
alias {
name = aws_cloudfront_distribution.vue_app_cdn.domain_name
zone_id = aws_cloudfront_distribution.vue_app_cdn.hosted_zone_id
evaluate_target_health = false
}
}

View File

@@ -0,0 +1,44 @@
<template>
<v-card>
<v-card-text class="text-center">
<div class="d-flex flex-row justify-space-between">
<div class="px-2">
<h6>US</h6>
<h4>{{ formatCount(counts.us) }}</h4>
</div>
<v-divider vertical></v-divider>
<div class="px-2">
<h6>Worldwide</h6>
<h4>{{ formatCount(counts.worldwide) }}</h4>
</div>
</div>
</v-card-text>
</v-card>
</template>
<script setup lang="ts">
import { onMounted, ref } from 'vue';
import { getALPRCounts } from '@/services/apiService';
const counts = ref({
us: null,
worldwide: null,
});
onMounted(() => {
getALPRCounts().then((response) => {
counts.value = response;
});
});
function formatCount(count: number | null): string {
if (count === null) {
return '-';
}
if (count < 1000) {
return Math.round(count / 10) * 10 + '';
}
const rounded = Math.round(count / 100) / 10;
return `${rounded}k+`;
}
</script>

View File

@@ -65,6 +65,12 @@ export const getALPRs = async (boundingBox: BoundingBox) => {
return response.data;
}
export const getALPRCounts = async () => {
const s3Url = "https://deflock-clusters.s3.us-east-1.amazonaws.com/alpr-counts.json";
const response = await apiService.get(s3Url);
return response.data;
}
export const getClusters = async () => {
const s3Url = "https://deflock-clusters.s3.us-east-1.amazonaws.com/alpr_clusters.json";
const response = await apiService.get(s3Url);

View File

@@ -17,6 +17,10 @@
@ready="mapLoaded"
:options="{ zoomControl: false, attributionControl: false }"
>
<l-control position="bottomleft">
<ALPRCounter />
</l-control>
<l-control position="topleft">
<form @submit.prevent="onSearch">
<v-text-field
@@ -78,6 +82,7 @@ import { useDisplay, useTheme } from 'vuetify';
import DFMapMarker from '@/components/DFMapMarker.vue';
import DFMarkerCluster from '@/components/DFMarkerCluster.vue';
import NewVisitor from '@/components/NewVisitor.vue';
import ALPRCounter from '@/components/ALPRCounter.vue';
import type { ALPR } from '@/types';
const DEFAULT_ZOOM = 12;