ganncamp
(G Ann Campbell)
2025 年3 月 5 日 19:08
1
我有一个数据浏览器查询,它接受一个“group_name”参数
我想自动化每周为每个相关组运行一次查询,并将结果发送给该组。
但我就是无法弄清楚参数的语法。这是我在一个全新的自动化中看到的:
我觉得这……没用。
我尝试了几种变体。当我尝试的查询没有产生错误(在错误日志中找到)时,我似乎什么也得不到。
如果我使用没有参数的查询,它运行得非常快。如何让它适用于需要参数的查询?
2 个赞
我也能重现这个问题。
在您的示例中,假设您想检查 moderators 和 admins 组,您应该有:
key: group_name
value: moderators,admins
技术细节供您参考,别介意
我尝试查看代码来理解,我认为这里有一个问题:
result = DataExplorer.run_query(query, params)
query.update!(last_run_at: Time.now)
return [] if opts[:skip_empty] && result[:pg_result].values.empty?
table =
ResultToMarkdown.convert(result[:pg_result], render_url_columns: opts[:render_url_columns])
build_report_pms(query, table, recipients, attach_csv: opts[:attach_csv], result:)
end
def self.generate_post(query_id, query_params, opts = {})
query = DiscourseDataExplorer::Query.find(query_id)
return {} if !query
params = params_to_hash(query_params)
result = DataExplorer.run_query(query, params)
query.update!(last_run_at: Time.now)
return {} if opts[:skip_empty] && result[:pg_result].values.empty?
build_report_post(query, table, attach_csv: opts[:attach_csv], result:)
end
def self.params_to_hash(query_params)
params = JSON.parse(query_params)
params.map { |p| p.is_a?(Hash) ? [p["key"], p["value"]] : p }.to_h
end
def self.build_report_pms(query, table = "", targets = [], attach_csv: false, result: nil)
pms = []
upload = create_csv_upload(query, result) if attach_csv
targets.each do |target|
name = target[0]
pm_type = "target_#{target[1]}s"
pm = {}
pm["title"] = I18n.t(
在调用 run_query 之前,参数被转换了。
假设您有这个原始值:
[{"key":"group_names","value":"admins,moderators"}]
转换后的值将是:
[{"key"=>"group_names", "value"=>"admins,moderators"}=>nil]
def self.run_query(query, req_params = {}, opts = {})
# Safety checks
# see test 'doesn't allow you to modify the database #2'
if query.sql =~ /;/
err = ValidationError.new(I18n.t("js.errors.explorer.no_semicolons"))
return { error: err, duration_nanos: 0 }
end
query_args = {}
begin
query_args = query.cast_params req_params
rescue ValidationError => e
return { error: e, duration_nanos: 0 }
end
time_start, time_end, explain, err, result = nil
begin
ActiveRecord::Base.connection.transaction do
# Setting transaction to read only prevents shoot-in-foot actions like SELECT FOR UPDATE
# see test 'doesn't allow you to modify the database #1'
DB.exec "SET TRANSACTION READ ONLY"
然而,cast_params 似乎期望的是 {"group_names"=>"admins,moderators"}
我尝试测试了这个简单的更改,参数就可以工作了。
def self.params_to_hash(query_params)
params = JSON.parse(query_params)
params_hash = {}
params.each do |param|
key = param["key"]
value = param["value"]
params_hash[key] = value
end
params_hash
end
7 个赞
ted
(Ted Johansson)
2025 年3 月 13 日 07:47
8
太棒的探洞了,@Arkshine !
我正在查看这个问题。当我阅读原始代码时,我感觉它期望参数是数组的数组,所以我有点担心直接丢弃现有内容。我会尝试弄清楚这可能在什么时候发生。
找到问题后我会在这里发帖。
1 个赞
ted
(Ted Johansson)
2025 年3 月 13 日 10:01
9
这里确实有一个 bug,部分归功于 @Arkshine 的调查,修复起来相对容易:
main ← fix/report-generator-parameters
opened 09:29AM - 13 Mar 25 UTC
### What is the problem?
When setting up an automation to create a DM or a po… st with a report on a recurring basis, and using a Data Explorer query that has parameters, we encounter an error.
### Why is this happening?
The `.params_to_hash` currently expects an array of arrays for the parameters, e.g.:
```ruby
[["group_name", "admins"]]
```
but in reality they seem to be arrays of hashes:
```ruby
[{ "key" => "group_name", "value" => "admins" }]
```
### How come we only found out now?
We do have tests for the report generator, and we do pass query parameters to it, but 1) we hard-code the parameters as an array of arrays and 2) the parameters aren't in the query used as a fixture.
### How does this fix it?
This change makes `ReportGenerator.params_to_hash` work with arrays of hashes. It also preserves the ability to work with nested arrays in case this is used somewhere else.
这已经合并了,所以一旦您的站点部署完毕,您应该就可以继续了 @ganncamp 。
如果我没理解错的话,您想要一个自动化,实现群组与其报告之间的一对一映射?(即 group_a 接收包含 group_a 数据的报告,依此类推)
目前收件人列表和报告参数之间没有连接,因此要实现这一点,您需要为每个群组设置一个自动化。在这种情况下,您可能需要将参数更改为 group_id。
3 个赞
ganncamp
(G Ann Campbell)
2025 年3 月 13 日 11:51
11
太棒了,@ted !
Ted Johansson:
要实现这一点,您需要为每个组设置一个自动化
是的,能够 foreach 一组组将是我的下一个请求
3 个赞
ted
(Ted Johansson)
2025 年3 月 14 日 02:19
12
这确实似乎是一件方便的事情,特别是对于拥有大量群组的网站。我会记下这个请求,但不能确定何时会处理。
3 个赞
j.jaffeux
(Joffrey Jaffeux)
关闭
2025 年3 月 17 日 10:09
14
此话题在最后回复后三天已被自动关闭,不再允许新的回复。