Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/full-documented-m2ee.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,7 @@ metering:
output_file_path: /path/to/your/output/directory
# usage metrics will be exported to the Subscription Service via this endpoint (instead of export to
# local file) in case of your app is connected to the Internet
subscription_service_url: https://subscription-service-api-endpoint/activate #FIXME: replace with correct url when we have one
subscription_service_url: https://subscription-da-api-prod-2-eu-central-1.mendix.com/v3/activate
# timeout for Subscription Service in seconds (default is 30 sec)
subscription_service_timeout: 30
# If your app uses specializations of the 'System.User' entity to store users, use this variable
Expand Down
2 changes: 1 addition & 1 deletion src/m2ee/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ def get_metering_output_file_path(self):
return self._conf['metering'].get('output_file_path', self.get_app_data_tmp_path())

def get_metering_subscription_service_url(self):
return self._conf['metering'].get('subscription_service_url', '') #FIXME: replace default correct url when we have one
return self._conf['metering'].get('subscription_service_url', 'https://subscription-da-api-prod-2-eu-central-1.mendix.com/v3/activate')

def get_metering_subscription_service_timeout(self):
return self._conf['metering'].get('subscription_service_timeout', 30)
Expand Down
31 changes: 25 additions & 6 deletions src/m2ee/metering.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from time import time
from zipfile import ZipFile
from zipfile import ZIP_DEFLATED
from os.path import basename

try:
import psycopg2.sql
Expand Down Expand Up @@ -136,6 +137,10 @@ def prepare_db_cursor_for_usage_query(config, db_conn):
def check_subscription_service_availability(config):
url = config.get_metering_subscription_service_url()

if not url:
# no URL specified at all
return False

try:
response = requests.post(url)
if response.status_code == requests.codes.ok:
Expand Down Expand Up @@ -227,12 +232,21 @@ def send_to_subscription_service(config, server_id, usage_metrics, created_at):
def export_to_file(config, db_cursor, server_id):
# create file
file_suffix = str(int(time()))
output_file = path.join(config.get_metering_output_file_name() + "_" + file_suffix + ".json")
output_file = path.join(
config.get_metering_output_file_path(),
config.get_metering_output_file_name() + "_" + file_suffix + ".json"
)

with open(output_file, "w") as out_file:
# dump usage metering data to file
i = 1
out_file.write("[\n")
out_file.write("{\n")
out_file.write('"subscriptionSecret": "{}",\n'.format(server_id))
out_file.write('"environmentName": "",\n')
out_file.write('"projectID": "{}",\n'.format(config.get_project_id()))
# for incremental uploads and to prevent the same usage metrics processed more than once
out_file.write('"timestamp": "{}",\n'.format(str(datetime.datetime.now())))
out_file.write('"users": [\n')
for usage_metric in db_cursor:
export_data = convert_data_for_export(usage_metric, server_id, True)
# no comma before the first element in JSON array
Expand All @@ -245,11 +259,12 @@ def export_to_file(config, db_cursor, server_id):
# takes ~3Gb for 1 million users)
json.dump(export_data, out_file, indent=4, sort_keys=True)
out_file.write("\n]")
out_file.write("\n}")

logger.info("Usage metrics exported at {} to {}".format(
datetime.datetime.now(), output_file))

zip_file(output_file, file_suffix)
zip_file(output_file, file_suffix, config)


def convert_data_for_export(usage_metric, server_id, to_file=False):
Expand Down Expand Up @@ -515,7 +530,11 @@ def hash_data(name):
return h.hexdigest()


def zip_file(file_path, file_suffix):
archive_name = 'mendix_usage_metrics_' + file_suffix + '.zip'
def zip_file(file_path, file_suffix, config):
archive_name = path.join(
config.get_metering_output_file_path(),
'mendix_usage_metrics_' + file_suffix + '.zip'
)

with ZipFile(archive_name, 'w', ZIP_DEFLATED) as zip_archive:
zip_archive.write(file_path)
zip_archive.write(file_path, basename(file_path))