ganncamp
(G Ann Campbell)
2025 年 3 月 5 日午後 7:08
1
Data Explorer クエリに「group_name」パラメータがあります。
関連する各グループに対して自動的に実行し、週に一度グループに結果を送信したいと思います。
しかし、パラメータの構文をどうしても見つけられません。新しい自動化で提示されたのは次のとおりです。
これは…役に立たないと思います。
いくつかのバリエーションを試しました。(エラーログで見つかった)エラーが発生しない場合、何も得られないようです。
パラメータなしのクエリを使用すると、非常に高速に実行されます。パラメータが必要なクエリで機能させるにはどうすればよいですか?
「いいね!」 2
私も問題を再現できます。
例では、モデレーターと管理者グループを確認したいとします。次のようになっているはずです。
キー: group_name
値: 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 日午前 7:47
8
素晴らしい洞窟探検ですね、@Arkshineさん !
現在調査中です。元のコードを読むと、パラメータが配列の配列であることを期待しているような印象を受けるため、既存のものをすべて捨ててしまうことには少し懸念があります。これがいつ発生する可能性があるのか、解明しようと思います。
何か分かりましたら、こちらに投稿します。
「いいね!」 1
ted
(Ted Johansson)
2025 年 3 月 13 日午前 10:01
9
ここに確かにバグがあり、@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 。
私の理解が正しければ、グループとそのレポートの 1 対 1 のマッピングとなる自動化をご希望でしょうか? (つまり、group_a は group_a のデータを含むレポートを受信する、など)
現在、受信者リストとレポートパラメータの間には接続がないため、それを達成するにはグループごとに 1 つの自動化を設定する必要があります。この場合、パラメータを group_id に変更することをお勧めします。
「いいね!」 3
ganncamp
(G Ann Campbell)
2025 年 3 月 13 日午前 11:51
11
素晴らしいニュースですね、@ted さん!
はい、グループのセットを foreach できるようになることは、私の次のリクエストになる予定でした
「いいね!」 3
ted
(Ted Johansson)
2025 年 3 月 14 日午前 2:19
12
これは確かに便利な機能のように思えます。特に多くのグループを持つサイトでは。リクエストをメモしておきますが、いつ着手できるかは分かりません。
「いいね!」 3
j.jaffeux
(Joffrey Jaffeux)
クローズされました:
2025 年 3 月 17 日午前 10:09
14
このトピックは最後の返信から3日後に自動的に閉鎖されました。新しい返信は許可されていません。