示例#1
0
class Covid19DateDataRTL:
    """
    Facade class that abstracts away all the logic for requesting, transforming,
    and loading new CSV date data into MongoDB.
    """

    logger = build_logger("Covid19DateDataRTL")
    dates_service = DatesService()
    csv_requester = CSVRequester()
    csv_date_transformer = CSVDateTransformer()

    def execute_rtl(self) -> str:
        """
        Entry point for the class.
        """
        urls_and_dates = self.csv_requester.check_for_new_csv()

        date_documents = []
        for url, file in urls_and_dates:
            requested_data = self._request(url, file)
            if requested_data:
                date_documents.extend(self._transform(requested_data))

        if not date_documents:
            self.logger.info("No new data!")
            return "No new data!"

        dates = [d["date"].strftime("%d-%m-%Y") for d in date_documents]
        self.logger.info("Loading new transformed data.")
        result = self._load(date_documents)
        if result and result.acknowledged:
            return "Data updated for:\n" + "\n".join(dates)

    def _request(self, url: str, filename: str) -> Dict:
        """
        Requests new CSV data from github, should there be any.

        :return:
        """
        return self.csv_requester.request_new_csv(url, filename)

    def _transform(self, data: dict):
        """
        Transforms new CSV data into structure suitable for MongoDB.
        """
        transformed_data = self.csv_date_transformer.transform_csv_data(data)
        if transformed_data:
            return transformed_data

    def _load(self, date_documents: list) -> InsertManyResult:
        """
        Persists new date data as MongoDB documents in covid-19 db.
        """
        return self.dates_service.insert_multiple_dates(date_documents)
示例#2
0
 def __init__(self):
     self.logger = build_logger("CountriesService")
     self.mongo_dates_dao = MongoDAO("dates")
     self.mongo_countries_dao = MongoDAO("countries")
示例#3
0
"""
Contains the class acting as the direct interface to a standalone MongoDB
'covid-19'.
"""
import datetime
from typing import Dict, List

from flask import current_app
from pymongo import MongoClient, WriteConcern
from pymongo.command_cursor import CommandCursor
from pymongo.cursor import Cursor
from pymongo.results import InsertManyResult, InsertOneResult

from webapp.utils.loggers import build_logger

LOGGER = build_logger("MongoDAO")


class MongoDAO:
    """
    Data access class that uses the PyMongo package to interact directly with
    a standalone mongo instance to retrieve and insert documents to and from the
    'covid-19' db.
    """

    client = MongoClient(
        username=current_app.config["MONGO_USERNAME"],
        password=current_app.config["MONGO_PASSWORD"],
    )
    db = client.get_database("covid-19")
示例#4
0
"""
Contains the class acting as intermediary between the Mongo dates collection and
calling code.
"""
import datetime
from typing import Dict, List, Optional

from pymongo.results import InsertManyResult

from webapp.data.mongo.mongo_dao import MongoDAO
from webapp.utils.loggers import build_logger

LOGGER = build_logger("DatesService")


class DatesService:
    """
    Acts as intermediary service layer between a MongoDAO and calling code at
    the view layer.
    """

    def __init__(self):
        self.mongo_dao = MongoDAO("dates")

    def get_dates_data(self, country: str) -> List[Dict]:
        """
        Uses MongoDAO to retrieve date data for a specified country and pass to
        the list constructor. Also filters out dates with no cases.

        :param country: String representing country.
        :return: A list of date documents
示例#5
0
 def __init__(self, dataframe: DataFrame):
     self.dataframe = dataframe
     self.logger = build_logger("SeabornPlotter")
示例#6
0
 def __init__(self):
     self.logger = build_logger("CSVDateTransformer")
示例#7
0
from typing import Dict, List

import pandas
from pandas import DataFrame

from webapp.utils.loggers import build_logger

LOGGER = build_logger("DocumentConverter")


class DocumentConverter:
    """
    todo
    """
    def __init__(self, data: List[Dict]):
        self.data = data

    def convert_dates_to_dataframe(self):
        """
        TODO

        :return:
        """
        temp_dict = {
            "dates": [],
            "confirmed": [],
            "recovered": [],
            "deaths": [],
        }

        for document in self.data:
示例#8
0
 def __init__(self):
     self.logger = build_logger("CSVRequester")
     self.repo_url = current_app.config["GIT_COVID_REPO_URL"]
     self.root_url = current_app.config["GITHUB_RAW_ROOT_URL"]