تشغيل استعلامات Data Explorer باستخدام Discourse API

:bookmark: يشرح هذا الدليل كيفية استخدام واجهة برمجة تطبيقات Discourse لإنشاء الاستعلامات وتشغيلها وإدارتها باستخدام المكون الإضافي Data Explorer.

:person_raising_hand: مستوى المستخدم المطلوب: مسؤول

يمكن تشغيل أي إجراء يمكن تنفيذه من خلال واجهة مستخدم Discourse تقريبًا باستخدام واجهة برمجة تطبيقات Discourse.

توفر هذه الوثيقة نظرة عامة شاملة لاستخدام واجهة برمجة التطبيقات بالتحديد بالاقتران مع المكون الإضافي Data Explorer.

للحصول على نظرة عامة عامة حول كيفية العثور على طلب واجهة برمجة التطبيقات الصحيح لإجراء ما، راجع: Reverse engineer the Discourse API .

تشغيل استعلام Data Explorer

لتشغيل استعلام Data Explorer عبر واجهة برمجة التطبيقات، قم بإجراء طلب POST إلى /admin/plugins/explorer/queries/<query-id>/run. يمكنك العثور على معرّف الاستعلام عن طريق زيارته عبر موقع Discourse الخاص بك والتحقق من المعلمة 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/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> بمفتاح واجهة برمجة التطبيقات والنطاق الخاص بك.

التعامل مع مجموعات البيانات الكبيرة

يحد المكون الإضافي 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 إلى النتائج. تُستخدم هذه البيانات لعرض المستخدم في نتائج واجهة المستخدم، ولكن من غير المرجح أن تحتاج إليها عند تشغيل الاستعلامات عبر واجهة برمجة التطبيقات.

لإزالة تلك البيانات من النتائج، أضف المعلمة 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"

مصادقة واجهة برمجة التطبيقات

يمكن العثور على تفاصيل حول إنشاء مفتاح واجهة برمجة تطبيقات للطلبات هنا: Create and configure an API key .

إذا كان سيتم استخدام مفتاح واجهة برمجة التطبيقات فقط لتشغيل استعلامات Data Explorer، يمكنك تحديد “Granular” من القائمة المنسدلة Scope، ثم تحديد نطاق “run queries”.

الأسئلة الشائعة

هل هناك أي نقطة نهاية لواجهة برمجة التطبيقات يمكنني استخدامها للحصول على قائمة التقارير وأرقام المعرّفات؟ أريد إنشاء قائمة منسدلة تحتوي على القائمة؟

نعم، يمكنك إجراء طلب GET مصادق عليه إلى /admin/plugins/explorer/queries.json للحصول على قائمة بجميع الاستعلامات على الموقع.

هل من الممكن إنشاء استعلامات عبر واجهة برمجة التطبيقات؟

نعم. توجد وثائق حول كيفية القيام بذلك على Create a Data Explorer query using the API

هل من الممكن إرسال معلمات مع طلب النشر؟

نعم، قم بتضمين معلمات SQL باستخدام الخيار -F، كما هو موضح في الأمثلة.

هل يتم دعم تصدير CSV للاستعلامات بواسطة واجهة برمجة التطبيقات؟

على الرغم من أن إخراج 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