Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions shard.lock
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ shards:

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

office365:
git: https://github.com/placeos/office365.git
Expand Down Expand Up @@ -211,7 +211,7 @@ shards:

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

placeos-log-backend:
git: https://github.com/place-labs/log-backend.git
Expand Down Expand Up @@ -267,7 +267,7 @@ shards:

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

secrets-env: # Overridden
git: https://github.com/spider-gazelle/secrets-env.git
Expand Down
2 changes: 1 addition & 1 deletion shard.override.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ dependencies:

neuroplastic:
github: place-labs/neuroplastic
commit: 2c156ab8ce688d676ca912b25ccdc631b265952c
commit: b8549eb1e61a6f893ae08e219f53e2a5d48777ec

redis:
github: stefanwille/crystal-redis
68 changes: 68 additions & 0 deletions spec/controllers/zones_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,74 @@ module PlaceOS::Api

describe "index", tags: "search" do
Spec.test_base_index(klass: Model::Zone, controller_klass: Zones)

it "filters by single parent_id" do
parent = Model::Generator.zone.save!
child1 = Model::Generator.zone
child1.parent_id = parent.id
child1.save!
child2 = Model::Generator.zone
child2.parent_id = parent.id
child2.save!

sleep 1.second
refresh_elastic(Model::Zone.table_name)

params = HTTP::Params.encode({"parent_id" => parent.id.as(String)})
path = "#{Zones.base_route}?#{params}"
result = client.get(path, headers: Spec::Authentication.headers)

result.success?.should be_true
zones = Array(Hash(String, JSON::Any)).from_json(result.body)
zone_ids = zones.map(&.["id"].as_s)
zone_ids.should contain(child1.id)
zone_ids.should contain(child2.id)

parent.destroy
child1.destroy
child2.destroy
end

it "filters by multiple parent_ids (comma-separated)" do
parent1 = Model::Generator.zone.save!
parent2 = Model::Generator.zone.save!
parent3 = Model::Generator.zone.save!

child1 = Model::Generator.zone
child1.parent_id = parent1.id
child1.save!

child2 = Model::Generator.zone
child2.parent_id = parent2.id
child2.save!

child3 = Model::Generator.zone
child3.parent_id = parent3.id
child3.save!

sleep 1.second
refresh_elastic(Model::Zone.table_name)

# Query for children of parent1 and parent2 (should not include child3)
parent_ids = "#{parent1.id},#{parent2.id}"
params = HTTP::Params.encode({"parent_id" => parent_ids})
path = "#{Zones.base_route}?#{params}"
result = client.get(path, headers: Spec::Authentication.headers)

result.success?.should be_true
zones = Array(Hash(String, JSON::Any)).from_json(result.body)
zone_ids = zones.map(&.["id"].as_s)
zone_ids.should contain(child1.id)
zone_ids.should contain(child2.id)
zone_ids.should_not contain(child3.id)

parent1.destroy
parent2.destroy
parent3.destroy
child1.destroy
child2.destroy
child3.destroy
end
end

describe "tags", tags: "search" do
Expand Down
2 changes: 1 addition & 1 deletion src/placeos-rest-api/controllers/modules.cr
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ module PlaceOS::Api
client = Loki::Client.from_env
labels = client.list_labels.data
stream = labels.try &.includes?("container") ? "container" : "app"
query = %({#{stream}="core"} | logfmt | source = "#{current_module.id}" | level = "[E]")
query = %({#{stream}="core"} | source = "#{current_module.id}" |~ "(?i)exception" | level = "ERROR|[E]")
results = client.query_range(query, 20, error_timestamp - 1.hour, error_timestamp, Loki::Direction::Backward)
entries = Array(String).new
results.response_data.result.as(Loki::Model::Streams).each do |res_stream|
Expand Down
15 changes: 8 additions & 7 deletions src/placeos-rest-api/controllers/zones.cr
Original file line number Diff line number Diff line change
Expand Up @@ -94,22 +94,23 @@ module PlaceOS::Api
###############################################################################################

# list the configured zones
@[AC::Route::GET("/", converters: {tags: ConvertStringArray})]
@[AC::Route::GET("/", converters: {tags: ConvertStringArray, parent_id: ConvertStringArray})]
def index(
@[AC::Param::Info(description: "only return zones who have this zone as a parent", example: "zone-1234")]
parent_id : String? = nil,
@[AC::Param::Info(description: "only return zones who have this zone as a parent (supports comma-separated list)", example: "zone-1234,zone-5678")]
parent_id : Array(String)? = nil,
@[AC::Param::Info(description: "return zones with particular tags", example: "building,level")]
tags : Array(String)? = nil,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should be using the converter helpers to support multiple parent_ids

@[AC::Route::GET("/", converters: {tags: ConvertStringArray, parent_id: ConvertStringArray})]
def index(
      @[AC::Param::Info(description: "only return zones who have this zone as a parent (supports comma-separated list)", example: "zone-1234,zone-5678")]
      parent_id : Array(String)? = nil,

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

refactored that endpoint

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

simplified it a bit more as the converter already does some of this
image

) : Array(::PlaceOS::Model::Zone)
elastic = ::PlaceOS::Model::Zone.elastic
query = elastic.query(search_params)
query.sort(NAME_SORT_ASC)

# Limit results to the children of this parent
if parent = parent_id
query.must({
"parent_id" => [parent],
# Limit results to the children of these parents (OR logic)
if parent_id
query.should({
"parent_id" => parent_id,
})
query.minimum_should_match(1)
end

# Limit results to zones containing the passed list of tags
Expand Down
Loading