Skip to content

Commit 6b1b8cc

Browse files
authored
feat: [PPT-2295] zone queries to support multiple parent_ids (#422)
1 parent 3e7ddeb commit 6b1b8cc

File tree

5 files changed

+81
-12
lines changed

5 files changed

+81
-12
lines changed

shard.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ shards:
155155

156156
neuroplastic: # Overridden
157157
git: https://github.com/place-labs/neuroplastic.git
158-
version: 1.13.2+git.commit.2c156ab8ce688d676ca912b25ccdc631b265952c
158+
version: 1.13.3+git.commit.b8549eb1e61a6f893ae08e219f53e2a5d48777ec
159159

160160
office365:
161161
git: https://github.com/placeos/office365.git
@@ -211,7 +211,7 @@ shards:
211211

212212
placeos-frontend-loader:
213213
git: https://github.com/placeos/frontend-loader.git
214-
version: 2.7.1+git.commit.ad484f46c8af671421f7f5b4704eccca7e30250e
214+
version: 2.7.1+git.commit.d47bdbc18c04052f6eacd463d105716c8796e863
215215

216216
placeos-log-backend:
217217
git: https://github.com/place-labs/log-backend.git
@@ -267,7 +267,7 @@ shards:
267267

268268
search-ingest:
269269
git: https://github.com/placeos/search-ingest.git
270-
version: 2.11.3+git.commit.89ff9805b666f6dbb55fed4b8d8f5d6cdb49edfa
270+
version: 2.11.3+git.commit.8dabc64759d45a202393f6d000fd6ec05d50ec17
271271

272272
secrets-env: # Overridden
273273
git: https://github.com/spider-gazelle/secrets-env.git

shard.override.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ dependencies:
2020

2121
neuroplastic:
2222
github: place-labs/neuroplastic
23-
commit: 2c156ab8ce688d676ca912b25ccdc631b265952c
23+
commit: b8549eb1e61a6f893ae08e219f53e2a5d48777ec
2424

2525
redis:
2626
github: stefanwille/crystal-redis

spec/controllers/zones_spec.cr

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,74 @@ module PlaceOS::Api
66

77
describe "index", tags: "search" do
88
Spec.test_base_index(klass: Model::Zone, controller_klass: Zones)
9+
10+
it "filters by single parent_id" do
11+
parent = Model::Generator.zone.save!
12+
child1 = Model::Generator.zone
13+
child1.parent_id = parent.id
14+
child1.save!
15+
child2 = Model::Generator.zone
16+
child2.parent_id = parent.id
17+
child2.save!
18+
19+
sleep 1.second
20+
refresh_elastic(Model::Zone.table_name)
21+
22+
params = HTTP::Params.encode({"parent_id" => parent.id.as(String)})
23+
path = "#{Zones.base_route}?#{params}"
24+
result = client.get(path, headers: Spec::Authentication.headers)
25+
26+
result.success?.should be_true
27+
zones = Array(Hash(String, JSON::Any)).from_json(result.body)
28+
zone_ids = zones.map(&.["id"].as_s)
29+
zone_ids.should contain(child1.id)
30+
zone_ids.should contain(child2.id)
31+
32+
parent.destroy
33+
child1.destroy
34+
child2.destroy
35+
end
36+
37+
it "filters by multiple parent_ids (comma-separated)" do
38+
parent1 = Model::Generator.zone.save!
39+
parent2 = Model::Generator.zone.save!
40+
parent3 = Model::Generator.zone.save!
41+
42+
child1 = Model::Generator.zone
43+
child1.parent_id = parent1.id
44+
child1.save!
45+
46+
child2 = Model::Generator.zone
47+
child2.parent_id = parent2.id
48+
child2.save!
49+
50+
child3 = Model::Generator.zone
51+
child3.parent_id = parent3.id
52+
child3.save!
53+
54+
sleep 1.second
55+
refresh_elastic(Model::Zone.table_name)
56+
57+
# Query for children of parent1 and parent2 (should not include child3)
58+
parent_ids = "#{parent1.id},#{parent2.id}"
59+
params = HTTP::Params.encode({"parent_id" => parent_ids})
60+
path = "#{Zones.base_route}?#{params}"
61+
result = client.get(path, headers: Spec::Authentication.headers)
62+
63+
result.success?.should be_true
64+
zones = Array(Hash(String, JSON::Any)).from_json(result.body)
65+
zone_ids = zones.map(&.["id"].as_s)
66+
zone_ids.should contain(child1.id)
67+
zone_ids.should contain(child2.id)
68+
zone_ids.should_not contain(child3.id)
69+
70+
parent1.destroy
71+
parent2.destroy
72+
parent3.destroy
73+
child1.destroy
74+
child2.destroy
75+
child3.destroy
76+
end
977
end
1078

1179
describe "tags", tags: "search" do

src/placeos-rest-api/controllers/modules.cr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ module PlaceOS::Api
246246
client = Loki::Client.from_env
247247
labels = client.list_labels.data
248248
stream = labels.try &.includes?("container") ? "container" : "app"
249-
query = %({#{stream}="core"} | logfmt | source = "#{current_module.id}" | level = "[E]")
249+
query = %({#{stream}="core"} | source = "#{current_module.id}" |~ "(?i)exception" | level = "ERROR|[E]")
250250
results = client.query_range(query, 20, error_timestamp - 1.hour, error_timestamp, Loki::Direction::Backward)
251251
entries = Array(String).new
252252
results.response_data.result.as(Loki::Model::Streams).each do |res_stream|

src/placeos-rest-api/controllers/zones.cr

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,22 +94,23 @@ module PlaceOS::Api
9494
###############################################################################################
9595

9696
# list the configured zones
97-
@[AC::Route::GET("/", converters: {tags: ConvertStringArray})]
97+
@[AC::Route::GET("/", converters: {tags: ConvertStringArray, parent_id: ConvertStringArray})]
9898
def index(
99-
@[AC::Param::Info(description: "only return zones who have this zone as a parent", example: "zone-1234")]
100-
parent_id : String? = nil,
99+
@[AC::Param::Info(description: "only return zones who have this zone as a parent (supports comma-separated list)", example: "zone-1234,zone-5678")]
100+
parent_id : Array(String)? = nil,
101101
@[AC::Param::Info(description: "return zones with particular tags", example: "building,level")]
102102
tags : Array(String)? = nil,
103103
) : Array(::PlaceOS::Model::Zone)
104104
elastic = ::PlaceOS::Model::Zone.elastic
105105
query = elastic.query(search_params)
106106
query.sort(NAME_SORT_ASC)
107107

108-
# Limit results to the children of this parent
109-
if parent = parent_id
110-
query.must({
111-
"parent_id" => [parent],
108+
# Limit results to the children of these parents (OR logic)
109+
if parent_id
110+
query.should({
111+
"parent_id" => parent_id,
112112
})
113+
query.minimum_should_match(1)
113114
end
114115

115116
# Limit results to zones containing the passed list of tags

0 commit comments

Comments
 (0)