<commit_before>#  Remote Weather Access - Client/server solution for distributed weather networks
#   Copyright (C) 2013-2020 <NAME> (<EMAIL>)
#
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU Affero General Public License as
#   published by the Free Software Foundation, either version 3 of the
#   License, or (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU Affero General Public License for more details.
#
#   You should have received a copy of the GNU Affero General Public License
#   along with this program.  If not, see <https://www.gnu.org/licenses/>.

import json
import os
from http import HTTPStatus
from pathlib import Path

import requests
from requests.adapters import Retry

from frontend.src.cache import cache
from frontend.src.utils import Singleton, TimeoutHTTPAdapter, TEMP_SENSOR_MARKER, HUMID_SENSOR_MARKER
from frontend.src.utils import is_temp_sensor, is_humidity_sensor, IsoDatetimeJSONEncoder


class CachedBackendProxy(object, metaclass=Singleton):
    def __init__(self, backend_url, backend_port, app):
        self._backend = BackendProxy(backend_url, backend_port)
        self._app = app

    @cache.memoize(timeout=5 * 60)  # caching period in seconds
    def time_limits(self):
        limits = self._backend.get_available_time_limits()
        self._app.logger.info('Received the current minimum and maximum time limits of the data from the backend')
        return limits['first_timepoint'], limits['last_timepoint']

    @cache.memoize(timeout=5 * 60)  # caching period in seconds
    def available_sensors(self):
        available_sensors_data = self._backend.get_all_available_sensors()
        available_sensors = []
        for curr_sensor_id, cur__sensor_data in available_sensors_data.items():
            available_sensors.append({'label': cur__sensor_data['description'], 'value': curr_sensor_id})

        self._app.logger.info('Received data for all available sensors from the backend')
        return available_sensors, available_sensors_data
<commit_msg>Gevent patching of the backend proxy within the frontend.<commit_after>#  Remote Weather Access - Client/server solution for distributed weather networks
#   Copyright (C) 2013-2020 <NAME> (<EMAIL>)
#
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU Affero General Public License as
#   published by the Free Software Foundation, either version 3 of the
#   License, or (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU Affero General Public License for more details.
#
#   You should have received a copy of the GNU Affero General Public License
#   along with this program.  If not, see <https://www.gnu.org/licenses/>.

import json
import os
from http import HTTPStatus
from pathlib import Path

import gevent.monkey
gevent.monkey.patch_all()

import requests
from requests.adapters import Retry

from frontend.src.cache import cache
from frontend.src.utils import Singleton, TimeoutHTTPAdapter, TEMP_SENSOR_MARKER, HUMID_SENSOR_MARKER
from frontend.src.utils import is_temp_sensor, is_humidity_sensor, IsoDatetimeJSONEncoder


class CachedBackendProxy(object, metaclass=Singleton):
    def __init__(self, backend_url, backend_port, app):
        self._backend = BackendProxy(backend_url, backend_port)
        self._app = app

    @cache.memoize(timeout=5 * 60)  # caching period in seconds
    def time_limits(self):
        limits = self._backend.get_available_time_limits()
        self._app.logger.info('Received the current minimum and maximum time limits of the data from the backend')
        return limits['first_timepoint'], limits['last_timepoint']

    @cache.memoize(timeout=5 * 60)  # caching period in seconds
    def available_sensors(self):
        available_sensors_data = self._backend.get_all_available_sensors()
        available_sensors = []
        for curr_sensor_id, cur__sensor_data in available_sensors_data.items():
            available_sensors.append({'label': cur__sensor_data['description'], 'value': curr_sensor_id})

        self._app.logger.info('Received data for all available sensors from the backend')
        return available_sensors, available_sensors_data

{'fraction_non_alphanumeric': 0.07056277056277056, 'fraction_numerical': 0.006493506493506494, 'mean_word_length': 4.221468926553673, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '335189', 'n_tokens_mistral': 1297, 'n_tokens_neox': 1244, 'n_words': 459}