Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
MetObsAPI
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
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
MetObs
MetObsAPI
Commits
5a33af55
Verified
Commit
5a33af55
authored
2 years ago
by
David Hoese
Browse files
Options
Downloads
Patches
Plain Diff
Fix v2 date string query format
parent
d00ce373
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
metobsapi/common_config.py
+1
-0
1 addition, 0 deletions
metobsapi/common_config.py
metobsapi/tests/test_data_api.py
+25
-7
25 additions, 7 deletions
metobsapi/tests/test_data_api.py
metobsapi/util/query_influx.py
+3
-2
3 additions, 2 deletions
metobsapi/util/query_influx.py
with
29 additions
and
9 deletions
metobsapi/common_config.py
+
1
−
0
View file @
5a33af55
...
...
@@ -19,3 +19,4 @@ INFLUXDB_PASS = "root" # noqa: S105
INFLUXDB_TOKEN
=
""
INFLUXDB_ORG
=
"
metobs
"
INFLUXDB_DB
=
"
metobs
"
INFLUXDB_BUCKET
=
"
metobs_realtime
"
This diff is collapsed.
Click to expand it.
metobsapi/tests/test_data_api.py
+
25
−
7
View file @
5a33af55
...
...
@@ -27,6 +27,9 @@ if TYPE_CHECKING:
from
metobsapi.util.query_influx
import
QueryResult
FAKE_DATA_START
=
datetime
(
2017
,
3
,
5
,
19
,
0
,
0
)
@pytest.fixture
(
params
=
[
"
1
"
,
"
2
"
],
ids
=
[
"
influxdbv1
"
,
"
influxdbv2
"
])
def
influxdb_version
(
request
):
return
request
.
param
...
...
@@ -112,21 +115,21 @@ def _fake_data_to_v1_resultset(fake_data: list[dict]) -> QueryResult:
@pytest.fixture
()
def
influxdb_air_temp_9_values
(
mock_influxdb_query
)
->
Iterable
[
None
]:
def
influxdb_air_temp_9_values
(
mock_influxdb_query
)
->
Iterable
[
mock
.
Mock
]:
fake_result
=
_fake_data
(
"
1m
"
,
{(
"
aoss
"
,
"
tower
"
):
[
"
time
"
,
"
air_temp
"
]},
9
)
with
mock_influxdb_query
(
fake_result
)
as
query_func
:
yield
query_func
@pytest.fixture
()
def
influxdb_3_symbols_9_values
(
mock_influxdb_query
)
->
Iterable
[
None
]:
def
influxdb_3_symbols_9_values
(
mock_influxdb_query
)
->
Iterable
[
mock
.
Mock
]:
fake_result
=
_fake_data
(
"
1m
"
,
{(
"
aoss
"
,
"
tower
"
):
[
"
time
"
,
"
air_temp
"
,
"
rel_hum
"
,
"
wind_speed
"
]},
9
)
with
mock_influxdb_query
(
fake_result
)
as
query_func
:
yield
query_func
@pytest.fixture
()
def
influxdb_wind_fields_9_values
(
mock_influxdb_query
)
->
Iterable
[
None
]:
def
influxdb_wind_fields_9_values
(
mock_influxdb_query
)
->
Iterable
[
mock
.
Mock
]:
fake_result
=
_fake_data
(
"
1m
"
,
{(
"
aoss
"
,
"
tower
"
):
[
"
time
"
,
"
wind_speed
"
,
"
wind_direction
"
,
"
wind_east
"
,
"
wind_north
"
]},
...
...
@@ -137,7 +140,7 @@ def influxdb_wind_fields_9_values(mock_influxdb_query) -> Iterable[None]:
def
_fake_data
(
interval
:
str
,
symbols
:
dict
[
tuple
[
str
,
str
],
list
[
str
]],
num_vals
:
int
)
->
list
[
dict
[
str
,
Any
]]:
now
=
datetime
(
2017
,
3
,
5
,
19
,
0
,
0
)
now
=
FAKE_DATA_START
measurement_name
=
"
metobs_
"
+
interval
series
=
[]
for
(
site
,
inst
),
columns
in
symbols
.
items
():
...
...
@@ -295,11 +298,26 @@ def test_too_many_points(client):
assert
res
[
"
status
"
]
==
"
fail
"
@pytest.mark.usefixtures
(
"
influxdb_air_temp_9_values
"
)
def
test_shorthand_one_symbol_json_row
(
client
):
@pytest.mark.parametrize
(
(
"
begin
"
,
"
end
"
,
"
exp_query_time
"
),
[
(
"
-00:10:00
"
,
None
,
"
-600s
"
),
(
FAKE_DATA_START
.
strftime
(
"
%Y-%m-%dT%H:%M:%S
"
),
(
FAKE_DATA_START
+
timedelta
(
minutes
=
10
)).
strftime
(
"
%Y-%m-%dT%H:%M:%S
"
),
"
range(start: 2017-03-05T19:00:00Z, stop: 2017-03-05T19:10:00Z)
"
,
),
],
)
def
test_shorthand_one_symbol_json_row
(
client
,
influxdb_air_temp_9_values
,
begin
,
end
,
exp_query_time
):
end
=
""
if
not
end
else
f
"
&end=
{
end
}
"
# row should be the default
res
=
client
.
get
(
"
/api/data.json?site=aoss&inst=tower&symbols=air_temp&begin=
-00:10:00
"
)
res
=
client
.
get
(
f
"
/api/data.json?site=aoss&inst=tower&symbols=air_temp&begin=
{
begin
}{
end
}
"
)
res
=
json
.
loads
(
res
.
data
.
decode
())
influxdb_air_temp_9_values
.
assert_called_once
()
query_str_arg
=
influxdb_air_temp_9_values
.
call_args
[
0
][
0
]
if
not
query_str_arg
.
startswith
(
"
SELECT
"
):
assert
exp_query_time
in
query_str_arg
assert
res
[
"
code
"
]
==
200
assert
res
[
"
num_results
"
]
==
9
assert
res
[
"
results
"
][
"
symbols
"
]
==
[
"
air_temp
"
]
...
...
This diff is collapsed.
Click to expand it.
metobsapi/util/query_influx.py
+
3
−
2
View file @
5a33af55
...
...
@@ -134,7 +134,7 @@ def parse_dt_v2(d):
if
isinstance
(
d
,
timedelta
):
total_seconds
=
int
(
d
.
total_seconds
())
return
f
"
-
{
total_seconds
:
d
}
s
"
return
d
.
strftime
(
"
'
%Y-%m-%dT%H:%M:%SZ
'
"
)
return
d
.
strftime
(
"
%Y-%m-%dT%H:%M:%SZ
"
)
def
_query_influxdbv1
(
query_str
,
epoch
)
->
QueryResult
:
...
...
@@ -167,7 +167,7 @@ def _build_queries_v2(
interval
:
str
,
)
->
str
:
query_format
=
"""
from(bucket:
"
metobs/forever
"
)
from(bucket:
"
{bucket}
"
)
|> range(start: {start}, stop: {stop})
|> filter(fn: (r) => r[
"
_measurement
"
] ==
"
metobs_{interval}
"
and r[
"
site
"
] ==
"
{site}
"
and r[
"
inst
"
] ==
"
{inst}
"
)
|> filter(fn: (r) => contains(value: r[
"
_field
"
], set: [
"
_time
"
,
"
site
"
,
"
inst
"
, {symbols_csv}]))
...
...
@@ -182,6 +182,7 @@ from(bucket:"metobs/forever")
for
(
site
,
inst
),
symbol_names
in
symbols
.
items
():
symbol_csv_str
=
"
,
"
.
join
(
f
'"
{
sname
}
"'
for
sname
in
symbol_names
)
this_query
=
query_format
.
format
(
bucket
=
current_app
.
config
[
"
INFLUXDB_BUCKET
"
],
interval
=
interval
,
symbols_csv
=
symbol_csv_str
,
site
=
site
,
...
...
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