Skip to content

Commit

Permalink
Merge pull request #29 from open-AIMS/julia-format
Browse files Browse the repository at this point in the history
JuliaFormatter config, reformat project, doc improvements.
  • Loading branch information
arlowhite authored Oct 2, 2024
2 parents c49be35 + b8a6665 commit aa4c092
Show file tree
Hide file tree
Showing 14 changed files with 195 additions and 107 deletions.
20 changes: 20 additions & 0 deletions .JuliaFormatter.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
style="blue"
indent=4
margin=92
always_for_in="nothing"
for_in_replacement=""
whitespace_typedefs=false
import_to_using=true
align_struct_field=true
align_assignment=false
align_conditional=true
align_pair_arrow=false
normalize_line_endings="unix"
align_matrix=true
join_lines_based_on_source=true
indent_submodule=true
surround_whereop_typeparameters=false
yas_style_nesting=true
trailing_comma=false
short_to_long_function_def=false
conditional_to_if=false
13 changes: 13 additions & 0 deletions .github/workflows/Format.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: Format suggestions
on:
pull_request:
# this argument is not required if you don't use the `suggestion-label` input
types: [ opened, reopened, synchronize, labeled, unlabeled ]
jobs:
code-style:
runs-on: ubuntu-latest
steps:
- uses: julia-actions/julia-format@v3
with:
version: '1' # Set `version` to '1.0.54' if you need to use JuliaFormatter.jl v1.0.54 (default: '1')
suggestion-label: 'format-suggest' # leave this unset or empty to show suggestions for all PRs
4 changes: 2 additions & 2 deletions docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ makedocs(;
modules=[ReefGuideAPI],
clean=true,
doctest=true,
format=DocumenterVitepress.MarkdownVitepress(
format=DocumenterVitepress.MarkdownVitepress(;
repo="github.com/open-AIMS/ReefGuideAPI.jl",
devbranch="main",
devurl = "dev"
devurl="dev"
),
draft=false,
source="src",
Expand Down
8 changes: 4 additions & 4 deletions docs/makelocal.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ makedocs(;
doctest=true,
authors="Iwanaga et al.",
checkdocs=:all,
format=DocumenterVitepress.MarkdownVitepress(
format=DocumenterVitepress.MarkdownVitepress(;
repo="github.com/open-AIMS/ReefGuideAPI.jl", # this must be the full URL!
devbranch="main",
devurl="dev",
build_vitepress=false;
build_vitepress=false
),
remotes=nothing,
draft=true,
Expand All @@ -31,7 +31,7 @@ makedocs(;
deploydocs(;
repo="github.com/open-AIMS/ReefGuideAPI.jl.git",
target="build", # this is where Vitepress stores its output
branch = "gh-pages",
branch="gh-pages",
devbranch="main",
push_preview = true
push_preview=true
)
15 changes: 9 additions & 6 deletions src/Middleware.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ const CORS_HEADERS = [

# https://juliaweb.github.io/HTTP.jl/stable/examples/#Cors-Server
function CorsMiddleware(handler)
return function(req::HTTP.Request)
return function (req::HTTP.Request)
@debug "CORS middleware"
# determine if this is a pre-flight request from the browser
if HTTP.method(req)=="OPTIONS"
if HTTP.method(req) == "OPTIONS"
return HTTP.Response(200, CORS_HEADERS)
else
# passes the request to the AuthMiddleware and route function.
Expand All @@ -28,7 +28,6 @@ function CorsMiddleware(handler)
end
end


function setup_jwt_middleware(config::Dict)
if !get(config["jwt_auth"], "JWT_ENABLED", false)
return identity # Return a pass-through middleware if JWT is not enabled
Expand All @@ -46,10 +45,12 @@ function setup_jwt_middleware(config::Dict)
rsa_public = JSONWebTokens.RS256(public_key)

return function jwt_auth_middleware(handler)
return function(req::HTTP.Request)
return function (req::HTTP.Request)
auth_header = HTTP.header(req, "Authorization", "")
if !startswith(auth_header, "Bearer ")
return HTTP.Response(401, "Unauthorized: Missing or invalid Authorization header.")
return HTTP.Response(
401, "Unauthorized: Missing or invalid Authorization header."
)
end

token = strip(auth_header[8:end]) # Remove "Bearer " prefix
Expand Down Expand Up @@ -77,7 +78,9 @@ function setup_jwt_middleware(config::Dict)
return HTTP.Response(401, "Unauthorized: Invalid token format")
else
# Log the error for debugging
@error "Unexpected error during JWT validation" exception=(e, catch_backtrace())
@error "Unexpected error during JWT validation" exception = (
e, catch_backtrace()
)
return HTTP.Response(500, "Internal Server Error")
end
end
Expand Down
10 changes: 4 additions & 6 deletions src/ReefGuideAPI.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ include("site_assessment/best_fit_polygons.jl")

include("Middleware.jl")


function get_regions()
# TODO: Comes from config?
regions = String[
Expand Down Expand Up @@ -75,7 +74,7 @@ function setup_regional_data(config::Dict)

reef_data_path = config["prepped_data"]["PREPPED_DATA_DIR"]

regional_assessment_data = OrderedDict{String, RegionalCriteria}()
regional_assessment_data = OrderedDict{String,RegionalCriteria}()
for reg in get_regions()
data_paths = String[]
data_names = String[]
Expand All @@ -93,7 +92,7 @@ function setup_regional_data(config::Dict)
push!(data_names, string(k))
if occursin("valid", string(dp))
# Load up Parquet files
parq_file = replace(first(g), ".tif"=>"_lookup.parq")
parq_file = replace(first(g), ".tif" => "_lookup.parq")

if occursin("slope", string(dp))
slope_table = GeoParquet.read(parq_file)
Expand Down Expand Up @@ -197,10 +196,9 @@ Invokes warm up of regional data cache to reduce later spin up times.
"""
function warmup_cache(config_path::String)
config = TOML.parsefile(config_path)
setup_regional_data(config)
return setup_regional_data(config)
end


function start_server(config_path)
@info "Launching server... please wait"

Expand All @@ -219,7 +217,7 @@ function start_server(config_path)
port = 8000
@info "Initialisation complete, starting server on port $(port) with $(Threads.nthreads()) threads."

serve(
return serve(;
middleware=[CorsMiddleware],
host="0.0.0.0",
port=port,
Expand Down
3 changes: 1 addition & 2 deletions src/criteria_assessment/_generators_dev.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@


abstract type DeploymentCriteria end

"""
Expand Down Expand Up @@ -48,4 +47,4 @@ function generate_criteria_structs()::Nothing
end

# function generate_criteria_state()::Nothing
# end
# end
49 changes: 24 additions & 25 deletions src/criteria_assessment/criteria.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ const COG_HEADERS = [
"Cache-Control" => "max-age=86400, no-transform"
]


function criteria_data_map()
# TODO: Load from config?
return OrderedDict(
Expand Down Expand Up @@ -59,6 +58,7 @@ function Base.show(io::IO, ::MIME"text/plain", z::RegionalCriteria)
Number of valid slope locations: $(nrow(z.valid_slopes))
Number of valid flat locations: $(nrow(z.valid_flats))
""")
return nothing
end

struct CriteriaBounds{F<:Function}
Expand All @@ -79,17 +79,16 @@ end
# Define struct type definition to auto-serialize/deserialize to JSON
StructTypes.StructType(::Type{CriteriaBounds}) = StructTypes.Struct()


"""
criteria_middleware(handle)
Creates middleware that parses a criteria query before reaching an endpoint
# Example
https:://somewhere:8000/suitability/assess/region-name/reeftype?criteria_names=Depth,Slope&lb=-9.0,0.0&ub=-2.0,40
`https://somewhere:8000/suitability/assess/region-name/reeftype?criteria_names=Depth,Slope&lb=-9.0,0.0&ub=-2.0,40`
"""
function criteria_middleware(handle)
function(req)
function (req)
fd = queryparams(req)

criteria_names = string.(split(fd["criteria_names"], ","))
Expand All @@ -110,10 +109,10 @@ function setup_region_routes(config, auth)
qp = queryparams(req)
file_id = string(hash(qp))
mask_temp_path = _cache_location(config)
mask_path = joinpath(mask_temp_path, file_id*".tiff")
mask_path = joinpath(mask_temp_path, file_id * ".tiff")

if isfile(mask_path)
return file(mask_path, headers=COG_HEADERS)
return file(mask_path; headers=COG_HEADERS)
end

criteria_names, lbs, ubs = remove_rugosity(reg, parse_criteria_query(qp)...)
Expand All @@ -137,16 +136,16 @@ function setup_region_routes(config, auth)
source="gdal",
driver="COG",
options=Dict{String,String}(
"COMPRESS"=>"DEFLATE",
"SPARSE_OK"=>"TRUE",
"OVERVIEW_COUNT"=>"5",
"BLOCKSIZE"=>"256",
"NUM_THREADS"=>n_gdal_threads(config)
"COMPRESS" => "DEFLATE",
"SPARSE_OK" => "TRUE",
"OVERVIEW_COUNT" => "5",
"BLOCKSIZE" => "256",
"NUM_THREADS" => n_gdal_threads(config)
),
force=true
)

return file(mask_path, headers=COG_HEADERS)
return file(mask_path; headers=COG_HEADERS)
end

@get auth("/bounds/{reg}") function (req::Request, reg::String)
Expand All @@ -157,25 +156,25 @@ function setup_region_routes(config, auth)

# Form for testing/dev
# https:://somewhere:8000/suitability/assess/region-name/reeftype?criteria_names=Depth,Slope&lb=-9.0,0.0&ub=-2.0,40
@get "/" function()
html("""
<form action="/assess/Cairns-Cooktown/slopes" method="post">
<label for="criteria_names">Criteria Names:</label><br>
<input type="text" id="criteria_names" name="criteria"><br>
@get "/" function ()
return html("""
<form action="/assess/Cairns-Cooktown/slopes" method="post">
<label for="criteria_names">Criteria Names:</label><br>
<input type="text" id="criteria_names" name="criteria"><br>
<label for="lb">Lower Bound:</label><br>
<input type="text" id="lb" name="lower_bound"><br><br>
<label for="lb">Lower Bound:</label><br>
<input type="text" id="lb" name="lower_bound"><br><br>
<label for="ub">Upper Bound:</label><br>
<input type="text" id="ub" name="upper_bound"><br><br>
<label for="ub">Upper Bound:</label><br>
<input type="text" id="ub" name="upper_bound"><br><br>
<input type="submit" value="Submit">
</form>
""")
<input type="submit" value="Submit">
</form>
""")
end

# Parse the form data and return it
@post auth("/form") function(req)
@post auth("/form") function (req)
data = formdata(req)
return data
end
Expand Down
1 change: 0 additions & 1 deletion src/criteria_assessment/query_parser.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ Remove rugosity layer from consideration if region is not Townsville.
Rugosity data currently only exists for the Townsville region.
"""
function remove_rugosity(reg, criteria_names, lbs, ubs)

if !Base.contains(reg, "Townsville")
# Remove rugosity layer from consideration as it doesn't exist for regions
# outside of Townsville.
Expand Down
22 changes: 13 additions & 9 deletions src/criteria_assessment/query_thresholds.jl
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,7 @@ end
Obtain the pixel positions of valid data.
Intended for use in something like:
https://developers.arcgis.com/javascript/latest/sample-code/layers-imagery-pixelvalues/
Intended for use in applications similar to [ImageryLayer - client side pixel filter](https://developers.arcgis.com/javascript/latest/sample-code/layers-imagery-pixelvalues/).
# Arguments
- `data` :
Expand Down Expand Up @@ -200,7 +199,7 @@ Generate mask for a given region and reef type (slopes or flats) according to th
applied to a set of criteria.
# Notes
- Zero values indicate locations to mask **out**.
- Zeros indicate locations to mask **out**.
- Ones indicate locations to **keep**.
# Arguments
Expand Down Expand Up @@ -260,7 +259,7 @@ Generate mask file for a given region and reef type (slopes or flats) according
applied to a set of criteria.
# Notes
- Zero values indicate locations to mask **out**.
- Zeros indicate locations to mask **out**.
- Ones indicate locations to **keep**.
# Arguments
Expand All @@ -272,7 +271,12 @@ applied to a set of criteria.
# Returns
Nothing
"""
function generate_criteria_mask!(fn::String, rst_stack::RasterStack, lookup::DataFrame, ruleset::Vector{CriteriaBounds{Function}})::Nothing
function generate_criteria_mask!(
fn::String,
rst_stack::RasterStack,
lookup::DataFrame,
ruleset::Vector{CriteriaBounds{Function}}
)::Nothing
# Create the geotiff
res = spzeros(size(rst_stack))
tmp_rst = Raster(
Expand All @@ -291,14 +295,14 @@ function generate_criteria_mask!(fn::String, rst_stack::RasterStack, lookup::Dat

write(
fn,
UInt8.(tmp_rst),
UInt8.(tmp_rst);
ext=".tiff",
source="gdal",
driver="COG", # GTiff
options=Dict{String,String}(
"COMPRESS"=>"LZW",
"SPARSE_OK"=>"TRUE",
"OVERVIEW_COUNT"=>"5"
"COMPRESS" => "LZW",
"SPARSE_OK" => "TRUE",
"OVERVIEW_COUNT" => "5"
)
)

Expand Down
Loading

0 comments on commit aa4c092

Please sign in to comment.