Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
geosphere-mapserver
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
cspp_geo
geosphere
geosphere-mapserver
Commits
353dd905
Verified
Commit
353dd905
authored
4 years ago
by
David Hoese
Browse files
Options
Downloads
Patches
Plain Diff
First attempt at a postgres times script handles start/end time and limit
parent
ec778cf9
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Pipeline
#19883
passed
4 years ago
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
mapserver/cgi-bin/layer_times_postgres.py
+71
-14
71 additions, 14 deletions
mapserver/cgi-bin/layer_times_postgres.py
with
71 additions
and
14 deletions
mapserver/cgi-bin/layer_times_postgres.py
+
71
−
14
View file @
353dd905
...
...
@@ -3,37 +3,94 @@ import os
import
sys
import
cgi
import
json
from
datetime
import
datetime
import
psycopg2
from
psycopg2
import
sql
pw_filename
=
"
__POSTGRES_PASSWORD_FILE__
"
connect_str
=
"
host=__POSTGRES_HOST__
"
\
"
port=__POSTGRES_PORT__
"
\
"
dbname=__POSTGRES_DBNAME__
"
\
"
user=__POSTGRES_USER__
"
if
not
os
.
path
.
isfile
(
pw_filename
)
or
'
__
'
in
connect_str
:
print
(
"""
Status: 500 Backend Misconfigured
\n
def
bad_request
(
msg
):
print
(
"""
Status: 400 Bad Request
Content-type: text/plain
Access-Control-Allow-Origin: *
{}
"""
.
format
(
msg
))
sys
.
exit
(
1
)
def
get_connect_str
():
pw_filename
=
"
__POSTGRES_PASSWORD_FILE__
"
connect_str
=
"
host=__POSTGRES_HOST__
"
\
"
port=__POSTGRES_PORT__
"
\
"
dbname=__POSTGRES_DBNAME__
"
\
"
user=__POSTGRES_USER__
"
if
not
os
.
path
.
isfile
(
pw_filename
)
or
'
__
'
in
connect_str
:
print
(
"""
Status: 500 Backend Misconfigured
\n
Content-type: text/plain
Access-Control-Allow-Origin: *
Backend has not been configured properly and doesn
'
t know how to communicate with the database.
Please contact site administrator.
"""
)
sys
.
exit
(
1
)
"""
)
sys
.
exit
(
1
)
with
open
(
pw_filename
,
'
r
'
)
as
pw_file
:
password
=
pw_file
.
read
().
strip
()
connect_str
+=
"
password={}
"
.
format
(
password
)
with
open
(
pw_filename
,
'
r
'
)
as
pw_file
:
password
=
pw_file
.
read
().
strip
()
connect_str
+=
"
password={}
"
.
format
(
password
)
return
connect_str
connect_str
=
get_connect_str
()
form
=
cgi
.
FieldStorage
()
table_name
=
form
[
'
layer
'
].
value
table_name
=
form
.
getvalue
(
'
layer
'
)
start_time
=
form
.
getvalue
(
'
start_time
'
)
end_time
=
form
.
getvalue
(
'
end_time
'
)
num_times
=
form
.
getvalue
(
'
num_times
'
,
0
)
order
=
form
.
getvalue
(
'
order
'
,
'
ASC
'
)
if
table_name
is
None
:
bad_request
(
"
Missing required parameter
'
layer
'
.
"
)
if
start_time
is
not
None
:
try
:
start_time
=
datetime
.
strptime
(
start_time
,
"
%Y-%m-%dT%H:%M:%S
"
)
except
ValueError
:
bad_request
(
"""
Bad
'
start_time
'
format. Expected:
'
YYYY-MM-DDTHH:MM:SS
'"""
)
if
end_time
is
not
None
:
try
:
end_time
=
datetime
.
strptime
(
end_time
,
"
%Y-%m-%dT%H:%M:%S
"
)
except
ValueError
:
bad_request
(
"""
Bad
'
end_time
'
format. Expected:
'
YYYY-MM-DDTHH:MM:SS
'"""
)
try
:
num_times
=
int
(
num_times
)
if
num_times
<
0
:
raise
ValueError
(
"'
num_times
'
must greater or equal to 0.
"
)
except
ValueError
:
bad_request
(
"""
Invalid integer for
'
num_times
'
.
"""
)
order
=
order
.
upper
()
if
order
not
in
(
'
ASC
'
,
'
DESC
'
):
bad_request
(
"""'
order
'
must be either
'
ASC
'
or
'
DESC
'
.
"""
)
query_str
=
"
SELECT start_time FROM {}
"
if
start_time
is
not
None
or
end_time
is
not
None
:
query_str
+=
"
WHERE
"
if
start_time
is
not
None
:
query_str
+=
"
start_time >= {}
"
.
format
(
start_time
.
strftime
(
'
%Y-%m-%dT%H:%M:%S
'
))
if
start_time
is
not
None
and
end_time
is
not
None
:
query_str
+=
"
AND
"
if
end_time
is
not
None
:
query_str
+=
"
start_time <= {}
"
.
format
(
end_time
.
strftime
(
'
%Y-%m-%dT%H:%M:%S
'
))
query_str
+=
"
ORDER BY start_time {}
"
.
format
(
order
)
if
num_times
!=
0
:
query_str
+=
"
LIMIT {}
"
.
format
(
num_times
)
try
:
conn
=
psycopg2
.
connect
(
connect_str
)
with
conn
:
with
conn
.
cursor
()
as
cur
:
cur
.
execute
(
sql
.
SQL
(
"
SELECT start_time FROM {} ORDER BY start_time
"
).
format
(
sql
.
Identifier
(
table_name
)))
cur
.
execute
(
sql
.
SQL
(
query_str
).
format
(
sql
.
Identifier
(
table_name
)))
times
=
cur
.
fetchall
()
except
psycopg2
.
errors
.
DataBaseError
:
print
(
"""
Status: 500 Database Error
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment