-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3046 from karm1000/api_requests_report
feat: India Compliance API Usage Report (cherry picked from commit 67c2ee6) # Conflicts: # india_compliance/gst_india/workspace/gst_india/gst_india.json
- Loading branch information
1 parent
ebcf249
commit 0656064
Showing
5 changed files
with
276 additions
and
1 deletion.
There are no files selected for viewing
Empty file.
42 changes: 42 additions & 0 deletions
42
india_compliance/gst_india/report/india_compliance_api_usage/india_compliance_api_usage.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright (c) 2025, Resilient Tech and contributors | ||
// For license information, please see license.txt | ||
|
||
frappe.query_reports["India Compliance API Usage"] = { | ||
filters: [ | ||
{ | ||
"fieldname": "from_date", | ||
"label": __("From"), | ||
"fieldtype": "Date", | ||
"reqd": 1, | ||
"default": frappe.datetime.add_months(frappe.datetime.now_date(), -6) | ||
}, | ||
{ | ||
"fieldname": "to_date", | ||
"label": __("To"), | ||
"fieldtype": "Date", | ||
"reqd": 1, | ||
"default": frappe.datetime.now_date() | ||
}, | ||
{ | ||
"fieldname": "report_by", | ||
"label": __("Report by"), | ||
"fieldtype": "Select", | ||
"options": [ | ||
"Endpoint", | ||
"Linked Document", | ||
"Date" | ||
], | ||
"default": "Endpoint" | ||
}, | ||
], | ||
|
||
onload(query_report) { | ||
query_report.filters.forEach(filter => { | ||
if (filter.fieldtype === "Date") { | ||
filter.datepicker.update( | ||
{ maxDate: new Date() } | ||
) | ||
} | ||
}); | ||
} | ||
}; |
27 changes: 27 additions & 0 deletions
27
india_compliance/gst_india/report/india_compliance_api_usage/india_compliance_api_usage.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"add_total_row": 0, | ||
"columns": [], | ||
"creation": "2025-02-11 12:15:09.380757", | ||
"disabled": 0, | ||
"docstatus": 0, | ||
"doctype": "Report", | ||
"filters": [], | ||
"idx": 0, | ||
"is_standard": "Yes", | ||
"letterhead": null, | ||
"modified": "2025-02-11 12:17:30.611180", | ||
"modified_by": "Administrator", | ||
"module": "GST India", | ||
"name": "India Compliance API Usage", | ||
"owner": "Administrator", | ||
"prepared_report": 0, | ||
"ref_doctype": "Integration Request", | ||
"report_name": "India Compliance API Usage", | ||
"report_type": "Script Report", | ||
"roles": [ | ||
{ | ||
"role": "System Manager" | ||
} | ||
], | ||
"timeout": 0 | ||
} |
156 changes: 156 additions & 0 deletions
156
india_compliance/gst_india/report/india_compliance_api_usage/india_compliance_api_usage.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
# Copyright (c) 2025, Resilient Tech and contributors | ||
# For license information, please see license.txt | ||
|
||
import frappe | ||
from frappe import _ | ||
from frappe.query_builder.functions import Count, Date, Replace | ||
|
||
from india_compliance.gst_india.api_classes.base import BASE_URL | ||
|
||
|
||
def execute(filters: dict | None = None): | ||
report = IndiaComplianceAPIUsageReport(filters=filters) | ||
columns = report.get_columns() | ||
data = report.get_data() | ||
|
||
return columns, data | ||
|
||
|
||
class IndiaComplianceAPIUsageReport: | ||
def __init__(self, filters: dict | None = None): | ||
self.from_data = filters.from_date | ||
self.to_date = filters.to_date | ||
self.report_by = filters.report_by | ||
|
||
def get_columns(self) -> list[dict]: | ||
if self.report_by == "Endpoint": | ||
return self._get_columns_by_endpoint() | ||
|
||
if self.report_by == "Date": | ||
return self._get_columns_by_date() | ||
|
||
return self._get_columns_by_linked_doctype() | ||
|
||
def _get_columns_by_endpoint(self): | ||
columns = [ | ||
{ | ||
"label": _("Endpoint"), | ||
"fieldname": "endpoint", | ||
"fieldtype": "Data", | ||
"width": 400, | ||
}, | ||
{ | ||
"label": _("API Requests Count"), | ||
"fieldname": "api_requests_count", | ||
"fieldtype": "Int", | ||
"width": 200, | ||
}, | ||
] | ||
|
||
return columns | ||
|
||
def _get_columns_by_date(self): | ||
columns = [ | ||
{ | ||
"label": _("Date"), | ||
"fieldname": "date", | ||
"fieldtype": "Date", | ||
"width": 250, | ||
}, | ||
{ | ||
"label": _("API Requests Count"), | ||
"fieldname": "api_requests_count", | ||
"fieldtype": "Int", | ||
"width": 200, | ||
}, | ||
] | ||
|
||
return columns | ||
|
||
def _get_columns_by_linked_doctype(self): | ||
columns = [ | ||
{ | ||
"label": _("Reference DocType"), | ||
"fieldname": "reference_doctype", | ||
"fieldtype": "Link", | ||
"options": "DocType", | ||
"width": 200, | ||
}, | ||
{ | ||
"label": _("Reference Document"), | ||
"fieldname": "reference_docname", | ||
"fieldtype": "Dynamic Link", | ||
"options": "reference_doctype", | ||
"width": 200, | ||
}, | ||
{ | ||
"label": _("API Requests Count"), | ||
"fieldname": "api_requests_count", | ||
"fieldtype": "Int", | ||
"width": 200, | ||
}, | ||
] | ||
|
||
return columns | ||
|
||
def get_data(self) -> list[dict]: | ||
if self.report_by == "Endpoint": | ||
return self._get_data_by_endpoint() | ||
|
||
if self.report_by == "Date": | ||
return self._get_data_by_date() | ||
|
||
return self._get_data_by_linked_doctype() | ||
|
||
def _get_data_by_endpoint(self): | ||
integration_requests = frappe.qb.DocType("Integration Request") | ||
|
||
query = ( | ||
frappe.qb.from_(integration_requests) | ||
.select( | ||
# Replace base url for all API endpoints | ||
Replace(integration_requests.url, BASE_URL, "").as_("endpoint"), | ||
Count("*").as_("api_requests_count"), | ||
) | ||
.where(integration_requests.creation >= self.from_data) | ||
.where(integration_requests.creation <= self.to_date) | ||
.groupby(integration_requests.url) | ||
) | ||
|
||
return query.run(as_dict=True) | ||
|
||
def _get_data_by_date(self): | ||
integration_requests = frappe.qb.DocType("Integration Request") | ||
|
||
query = ( | ||
frappe.qb.from_(integration_requests) | ||
.select( | ||
Date(integration_requests.creation).as_("date"), | ||
Count("*").as_("api_requests_count"), | ||
) | ||
.where(integration_requests.creation >= self.from_data) | ||
.where(integration_requests.creation <= self.to_date) | ||
.groupby("date") | ||
) | ||
|
||
return query.run(as_dict=True) | ||
|
||
def _get_data_by_linked_doctype(self): | ||
integration_requests = frappe.qb.DocType("Integration Request") | ||
|
||
query = ( | ||
frappe.qb.from_(integration_requests) | ||
.select( | ||
integration_requests.reference_doctype.as_("reference_doctype"), | ||
integration_requests.reference_docname.as_("reference_docname"), | ||
Count("*").as_("api_requests_count"), | ||
) | ||
.where(integration_requests.creation >= self.from_data) | ||
.where(integration_requests.creation <= self.to_date) | ||
.groupby( | ||
integration_requests.reference_doctype, | ||
integration_requests.reference_docname, | ||
) | ||
) | ||
|
||
return query.run(as_dict=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters