Discourse API で Data Explorer クエリを実行する

:bookmark: このガイドでは、Discourse API を使用して、Data Explorer プラグインでクエリを作成、実行、管理する方法について説明します。

:person_raising_hand: 必要なユーザーレベル: 管理者

Discourse ユーザーインターフェースから実行できるアクションは、事実上すべて Discourse API を使用してトリガーできます。

このドキュメントでは、特に Data Explorer プラグインと連携して API を利用するための包括的な概要を提供します。

アクションに適した API リクエストを見つける方法の一般的な概要については、Reverse engineer the Discourse API を参照してください。

Data Explorer クエリの実行

API を介して Data Explorer クエリを実行するには、/admin/plugins/discourse-data-explorer/queries/<query-id>/run に対して POST リクエストを送信します。クエリ ID は、Discourse サイトでアクセスし、アドレスバーの id パラメータを確認することで見つけることができます。

以下は、ID が 20 のクエリの例で、指定された日付の閲覧数に基づいてトピックを返します。

--[params]
-- date :viewed_at

SELECT
topic_id,
COUNT(1) AS views_for_date
FROM topic_views
WHERE viewed_at = :viewed_at
GROUP BY topic_id
ORDER BY views_for_date DESC

このクエリは、ターミナルから次のコマンドで実行できます。

curl -X POST "https://your-site-url/admin/plugins/discourse-data-explorer/queries/20/run" \
-H "Content-Type: multipart/form-data;" \
-H "Api-Key: <api-key>" \
-H "Api-Username: system" \
-F 'params={"viewed_at":"2019-06-10"}'

<api-key><your-site-url> を、ご自身の API キーとドメインに置き換える必要があることに注意してください。

大規模データセットの処理

Data Explorer プラグインは、JSON の結果をデフォルトで 1000 行に制限しています(非表示のサイト設定 data_explorer_query_result_limit で制御されます)。リクエストごとに limit パラメータを渡すことで、これを上書きでき、最大 10,000 行まで指定できます。

curl -X POST "https://your-site-url/admin/plugins/discourse-data-explorer/queries/20/run" \
-H "Content-Type: multipart/form-data;" \
-H "Api-Key: <api-key>" \
-H "Api-Username: system" \
-F "limit=5000"

10,000 行を超えるデータセットの場合、SQL レベルでページネーションを行う必要があります。以下の例のクエリを使用できます。

--[params]
-- integer :limit = 100
-- integer :page = 0
SELECT * 
FROM generate_series(1, 10000)
OFFSET :page * :limit 
LIMIT :limit

結果をページごとに取得するには、リクエスト内の page パラメータをインクリメントします。

curl -X POST "https://your-site-url/admin/plugins/discourse-data-explorer/queries/27/run" \
-H "Content-Type: multipart/form-data;" \
-H "Api-Key: <api-key>" \
-H "Api-Username: system" \
-F 'params={"page":"0"}'

result_count がゼロになったら停止します。

大規模データセットの処理に関する追加情報については、Result Limits and Exporting Queries を参照してください。

結果からの relations データの削除

Data Explorer クエリがユーザーインターフェースを介して実行されると、結果に relations オブジェクトが追加されます。このデータは UI 結果でユーザーを描画するために使用されますが、API を介してクエリを実行する場合には必要になることはほとんどありません。

そのデータを結果から削除するには、リクエストに download=true パラメータを追加します。

curl -X POST "https://your-site-url/admin/plugins/discourse-data-explorer/queries/27/run" \
-H "Content-Type: multipart/form-data;" \
-H "Api-Key: <api-key>" \
-H "Api-Username: system" \
-F 'params={"page":"0"}' \
-F "download=true"

API 認証

リクエスト用の API キーを生成する詳細については、こちらを参照してください: Create and configure an API key

API キーが Data Explorer クエリの実行にのみ使用される場合、スコープのドロップダウンメニューで「Granular」を選択し、次に「run queries」スコープを選択できます。

FAQ

レポートのリストとその ID 番号を取得できる API エンドポイントはありますか?ドロップダウンを作成したいのです。

はい、認証済みの GET リクエストを /admin/plugins/discourse-data-explorer/queries.json に対して送信すると、サイト上のすべてのクエリのリストを取得できます。

API を介してクエリを作成することは可能ですか?

はい。その方法に関するドキュメントは、Create a Data Explorer query using the API にあります。

POST リクエストでパラメータを送信することは可能ですか?

はい。例に示されているように、-F オプションを使用して SQL パラメータを含めます。

クエリの CSV エクスポートは API でサポートされていますか?

はい。結果を CSV 形式で取得するには、実行エンドポイントの URL に .csv を追加します。

curl -X POST "https://your-site-url/admin/plugins/discourse-data-explorer/queries/20/run.csv" \
-H "Api-Key: <api-key>" \
-H "Api-Username: system" \
-F 'params={"viewed_at":"2019-06-10"}'

CSV レスポンスは、デフォルトで最大 10,000 行を返します。limit パラメータを渡して、この数を減らすことができます。

追加リソース

「いいね!」 40
Watching API
"DataExplorer::ValidationError: Missing parameter end_date of type string
Get total list of topics and their view counts from Discourse API
Reports by Discourse
Passing params to Data Explorer using API requires enclosing a value
Category API request downloads all topics
Best API for All First Posts in a Category
Get Latest topic for Current user
TimeStamp of Tag
How can I get the list of Discourse Topic IDs dynamically
How to get a full list of badges of all users
API rate limits
Getting recently updated posts using the REST API
`DataExplorer::ValidationError: Missing parameter` when running Data Explorer queries with [params] via API
`DataExplorer::ValidationError: Missing parameter` when running Data Explorer queries with [params] via API
Validation error even when parameter passed while running data explorer API with Curl
Backend data retrieve for analytics
Discourse-user-notes API
Admin dashboard report reference guide
How to query the topics_with_no_response.json API with filters
Use API to get topics for a period using js
Access Discourse database with n8n
Why getUserById doesn't return the user's email?
Grant a custom badge through the API
Is there an API endpoint for recently edited posts
How to query gamification score via the API?
1.5X cheers on specific TL's or groups
Page Publishing
Identifying users in multiple groups using AND rather than OR?
How to fetch posts/topics by multiple usernames
Restrict moderator access to only the stats panel on the admin dashboard?
How to change the response default_limit in data explorer?
How to change the response default_limit in data explorer?
Order/Filter searched topics by latest update to First Post
API Filter users by emails, including secondary emails
Download a user's posting history via Discourse API?
Ability to have granular scope for data explorer?
Discourse forum traffic query data
Interact with discourse from Python?
Daily, weekly, or total stats by user over a specified time range
Looking for help posting automating data explorer reports to my forum
How to get all topics from a specific category using offset/page param in the API query?
Discourse 有哪个接口能直接获取某个帖子的最后一条评论信息
想得到活跃的用户——通过api
API endpoint to create invite links has moved to /invites.json
How to get a password from database?
Can I send an external URL to the Discourse API for it to return topics linking to that URL?
How to get all the deleted posts for a specific topic
Discourse Data Explorer Query Response to Slack
Filter topics in category containing file attachments
Discord Integration with Webhooks
Download result of queries into Google Spreadsheet
Who's online "API"?
Is there any endpoint that would provide a user's external account IDs from their Discourse ID?
API post request without an Accept header returns 406
Best way to get (via API) a list of users from a group, and their bios
Create a Data Explorer query using the API
Automate the syncing of Discourse queries to Google Sheets