Discourse API を使用して Data Explorer クエリを実行する

<div data-theme-toc="true"> </div>

> :bookmark: このガイドでは、Discourse API を使用して、[Data Explorer](https://meta.discourse.org/t/32566?silent=true) プラグインでクエリを作成、実行、管理する方法について説明します。
>
> :person_raising_hand: 必要なユーザーレベル: 管理者

Discourse ユーザーインターフェースから実行できるほぼすべての操作は、Discourse API を使用してトリガーすることもできます。

このドキュメントでは、特に [Data Explorer](https://meta.discourse.org/t/32566?silent=true) プラグインと連携して API を利用するための包括的な概要を提供します。

アクションに適した API リクエストを見つける方法の一般的な概要については、https://meta.discourse.org/t/how-to-reverse-engineer-the-discourse-api/20576 を参照してください。

## [Data Explorer](https://meta.discourse.org/t/32566?silent=true) クエリの実行

API を介して [Data Explorer](https://meta.discourse.org/t/32566?silent=true) クエリを実行するには、`/admin/plugins/explorer/queries/<query-id>/run` に対して `POST` リクエストを送信します。クエリ ID は、Discourse サイトからアクセスし、アドレスバーの `id` パラメータを確認することで見つけることができます。

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

``` sql
--[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/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 プラグインは、デフォルトで結果を 1000 行に制限します。より大きなデータセットをページングするには、以下のクエリ例を使用できます。

--[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/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/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 クエリの実行にのみ使用する場合、「Scope」ドロップダウンメニューから「Granular」を選択し、「run queries」スコープを選択できます。

FAQ

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

はい、認証された GET リクエストを /admin/plugins/explorer/queries.json に対して行うと、サイト上のすべてのクエリのリストを取得できます。

API 経由でクエリを作成することは可能ですか?

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

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

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

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

JSON 出力が標準ですが、結果を手動で CSV に変換することは可能です。ネイティブの CSV エクスポートはサポートされなくなりました。

追加リソース

「いいね!」 39
Watching API
"DataExplorer::ValidationError: Missing parameter end_date of type string
Get total list of topics and their view counts from Discourse API
Best API for All First Posts in a Category
Category API request downloads all topics
Get Latest topic for Current user
TimeStamp of Tag
How can I get the list of Discourse Topic IDs dynamically
Passing params to Data Explorer using API requires enclosing a value
Reports by Discourse
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
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
Validation error even when parameter passed while running data explorer API with Curl
How to fetch posts/topics by multiple usernames
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
Ability to have granular scope for data explorer?
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?
Identifying users in multiple groups using AND rather than OR?
Restrict moderator access to only the stats panel on the admin dashboard?
How to get all the deleted posts for a specific topic
Discourse forum traffic query data
Download a user's posting history via Discourse API?
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
How to get a full list of badges of all users