Esempio n. 1
0
def get_db_clients(source_url, dest_url, source_key, dest_key, db_name):
    # Create cosmosdb client for source db
    source_cosmos_client = CosmosClient(source_url, credential=source_key)
    if not source_cosmos_client:
        logging.error("Failed to create cosmos client for source db")
        exit(1)

    # Create cosmosdb client for destination db
    dest_cosmos_client = CosmosClient(dest_url, credential=dest_key)
    if not dest_cosmos_client:
        logging.error("Failed to create cosmos client for destination db")
        exit(1)

    logging.info("Created cosmos client for source and destination cosmosdb")
    # Create database client object using CosmosClient
    src_db_client = None
    dest_db_client = None
    try:
        src_db_client = source_cosmos_client.get_database_client(db_name)
        dest_db_client = dest_cosmos_client.get_database_client(db_name)
        logging.info("Created database clients")
    except:
        logging.error("Failed to create databae client using CosmosClient")
        traceback.print_exc()
        exit(1)
    return src_db_client, dest_db_client
Esempio n. 2
0
def get_db_container(container_id):
    endpoint = "https://kawin.documents.azure.com:443/"
    key = 'U6VUfkGeu2vzNVKumpXsTWHkPyjbYi9ffmHmmsz9XUz6mqAwwcFTs7FAAzmb4ZF3SptDLFfs2vbCALrzimiJNQ=='

    client = CosmosClient(endpoint, key)

    id = "hpsdb"
    try:
        db = client.get_database_client(id)
        print('Database with id \'{0}\' was found, it\'s link is {1}'.format(
            id, db.database_link))

    except exceptions.CosmosResourceNotFoundError:
        print('A database with id \'{0}\' does not exist'.format(id))

    id = container_id
    try:
        container = db.get_container_client(id)
        print('Container with id \'{0}\' was found, it\'s link is {1}'.format(
            container.id, container.container_link))

    except exceptions.CosmosResourceNotFoundError:
        print('A container with id \'{0}\' does not exist'.format(id))

    return container
Esempio n. 3
0
def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    url = os.environ['COSMOS_URL']
    key = os.environ['COSMOS_KEY']

    # Create connection to cosmos
    client = CosmosClient(url, credential=key)
    db = client.get_database_client('database1')
    container = db.get_container_client('container1')

    # Query to select all records or to select only a particular patient's records
    sql_query = 'SELECT * FROM container1'
    patient_id = req.params.get('patient_id')
    if patient_id:
        sql_query += f' WHERE container1.patient_id = "{patient_id}"'

    # enable_cross_partition_query should be set to True as the container is partitioned
    items = list(
        container.query_items(query=sql_query,
                              enable_cross_partition_query=True))

    return func.HttpResponse(
        json.dumps(items),
        status_code=200,
        mimetype="application/json",
    )
Esempio n. 4
0
def main(req: func.HttpRequest) -> func.HttpResponse:
    # fetch the farm_id from the request
    try:
        farm_id = req.params['farm_id']
        run_id = req.params['run_id']
        pred_type = req.params['pred_type']
    except (ValueError, KeyError) as e:
        return func.HttpResponse(
            "Incorrect request body",
            status_code=422
        )

    client = CosmosClient(Cosmos.URL, Cosmos.KEY)
    pred_container = client.get_database_client(Cosmos.DATABASE).get_container_client(Cosmos.PRED_CONTAINER)

    results, timestamp = get_predictions_from_runId(pred_container, farm_id, run_id, pred_type)

    payload = {
        'results': results,
        'timestamp': timestamp
    }

    return func.HttpResponse(
        json.dumps(payload),
        mimetype="application/json",
        status_code=200
    )
Esempio n. 5
0
def main(req: func.HttpRequest) -> func.HttpResponse:
    URL = "https://junctionx.documents.azure.com:443/"
    KEY = "BCw9nhNiTSl9ndUokCw6uHhzZSDgsSGoMjPUa0Ech6E9yiuGKCrsswFIXRatcguQYcNLnYdhFPWPhuaRtLfoxg=="
    DATABASE_NAME = "MitoSoftCorp"
    CONTAINER_NAME = "workType"
    client = CosmosClient(URL, credential=KEY)

    work_type = req.params.get("work_type")

    try:
        database = client.create_database(DATABASE_NAME)
    except exceptions.CosmosResourceExistsError:
        database = client.get_database_client(DATABASE_NAME)

    try:
        container = database.create_container(
            id=CONTAINER_NAME, partition_key=PartitionKey(path="/name"))
    except exceptions.CosmosResourceExistsError:
        container = database.get_container_client(CONTAINER_NAME)

    user_list = []
    for item in container.query_items(
            query='SELECT * FROM {} w WHERE w.work_type = "{}"'.format(
                CONTAINER_NAME, work_type),
            enable_cross_partition_query=True):
        user_list.append(item.get("user_id"))

    return_data = dict(id=user_list)

    return func.HttpResponse(json.dumps(return_data),
                             headers={"Content-Type": "application/json"},
                             status_code=200)
Esempio n. 6
0
def get_farm_data(device_id):    
    # query farm data correspoding to device
    client = CosmosClient(Cosmos.URL, Cosmos.KEY)
    farms_container = client.get_database_client(Cosmos.DATABASE).get_container_client(Cosmos.FARMS_CONTAINER)

    query = """SELECT c
                FROM farms f
                JOIN c IN f.farms_arr
                WHERE c.base_station.deviceId = @dev_id"""

    items = list(farms_container.query_items(
        query=query,
        parameters=[{ "name":"@dev_id", "value": device_id }],
        enable_cross_partition_query=True
    ))

    props = {
        'TYPE': 'RESPONSE',
        'EVENT': 'FARM_DATA_LOADED'
    }

    payload = json.dumps(items[0]['c'])
    # write back response to the device
    registry_manager = IoTHubRegistryManager(IoTHub.CONNECTION_STRING)
    registry_manager.send_c2d_message(device_id, payload, properties=props)
Esempio n. 7
0
def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')
    uid = uuid.uuid4()

    url = os.environ['ACCOUNT_URI']
    key = os.environ['ACCOUNT_KEY']
    client = CosmosClient(url, credential=key)
    database_name = 'ToDoDB'
    database = client.get_database_client(database_name)
    container_name = 'Actions'
    container = database.get_container_client(container_name)

    try:
        req_body = req.get_json()
        title = req_body.get('title')

        container.upsert_item({
            'id': str(uid),
            'title': title,
            'location': 'SouthAfrica',
            'complete': False
        })

        return func.HttpResponse(
            "Saved",
            status_code=201
        )

    except Exception:
        return func.HttpResponse("Error", status_code=500)
Esempio n. 8
0
def get_latest_sensor_data(farm_id):
    client = CosmosClient(Cosmos.URL, Cosmos.KEY)
    pred_container = client.get_database_client(Cosmos.DATABASE).get_container_client(Cosmos.TELEMETRY)

    query = """SELECT c.data
            FROM c
            WHERE c.farmId=@farm_id AND c.deviceType=@device_type
            ORDER BY c._ts DESC
            OFFSET 0 LIMIT 1"""

    
    # loop through each device and fetch latest sensor reading for each device
    agg_result = {}
    for device in DEVICE_TYPES:
        items = list(pred_container.query_items(
            query=query,
            parameters=[{ "name":"@farm_id", "value": farm_id },{ "name":"@device_type", "value": device }],
            enable_cross_partition_query=True
        ))
        try:

            agg_result[device] = {
                'unit': items[0]['data']['unit'],
                'value': items[0]['data']['value']
            }
        except: 
            agg_result[device] = None
    
    return agg_result
Esempio n. 9
0
def update_run_id(device_id, run_id):
    # first get the appropriate farm
    client = CosmosClient(Cosmos.URL, Cosmos.KEY)
    farms_container = client.get_database_client(
        Cosmos.DATABASE).get_container_client(Cosmos.FARMS_CONTAINER)

    query = """SELECT c.id as farm_id, f
        FROM farms f
        JOIN c IN f.farms_arr
        WHERE c.base_station.deviceId = @dev_id"""

    items = farms_container.query_items(query=query,
                                        parameters=[{
                                            "name": "@dev_id",
                                            "value": device_id
                                        }],
                                        enable_cross_partition_query=True)

    item = next(items)
    farm_id = item['farm_id']
    document = item['f']

    # loop through the farms and find the correct farm to update run id
    for farm_obj in document['farms_arr']:
        if farm_obj['id'] == farm_id:
            farm_obj['last_run'] = run_id

    # finally update document
    farms_container.upsert_item(document)

    return farm_id
Esempio n. 10
0
def get_records_from_cosmos():

    latest_2_query_results = []

    print ("**** GET RECORDS FROM COSMOS ****")
    uri = os.environ.get('ACCOUNT_URI')
    key = os.environ.get('ACCOUNT_KEY')
    database_id = os.environ.get('DATABASE_ID')
    results_container_id = os.environ.get('RESULTS_CONTAINER_ID')
    dummy_container_id = os.environ.get('DUMMY_CONTAINER_ID')
   
    client = CosmosClient(uri, {'masterKey': key})
    print (client)

    database = client.get_database_client(database_id)
    tango_container = database.get_container_client(results_container_id) # Results limited to those submitted by UUID
    netcraft_container = database.get_container_client(dummy_container_id) # Results include redirects, all results reported in portal 

    latest_2_query_results_tango = list(tango_container.query_items(query = 'SELECT TOP 2 * FROM c ORDER BY c._ts DESC', enable_cross_partition_query = True))    
    latest_2_query_results_netcraft = list(netcraft_container.query_items(query = 'SELECT TOP 2 * FROM c ORDER BY c._ts DESC', enable_cross_partition_query = True))

    for result in latest_2_query_results_tango:
        print (json.dumps(result, indent=True))

    for result in latest_2_query_results_netcraft:
        print (json.dumps(result, indent=True))

    return latest_2_query_results_tango, latest_2_query_results_netcraft
def store_url_counts(url_list, unique_url_list):

    print ("**** COUNT URLS ****\n")
    print(url_list)
    url_counts = dict(Counter(url_list))
    print(url_counts)

    uri          = os.environ.get('ACCOUNT_URI')
    key          = os.environ.get('ACCOUNT_KEY')
    database_id  = os.environ.get('DATABASE_ID')
    container_id = os.environ.get('URL_CONTAINER_ID')

    client    = CosmosClient(uri, {'masterKey': key})
    database  = client.get_database_client(database_id)
    container = database.get_container_client(container_id)

    date_str    = datetime.today().strftime('%Y-%m-%d %H:%M:%S')
    id_date     = int((datetime.utcnow()).timestamp())
    id_date_str = str(id_date)

    output = []
    for k,v in url_counts.items():
        output.append({'url':k, 'count':v})

    container.upsert_item({'id': id_date_str,
                           'date_time': id_date_str,
                           'date': date_str,
                           'urls_and_counts': output})
Esempio n. 12
0
class CosmosQueryClient():
    def __init__(self):

        self.auth_key = os.getenv("COSMOS_KEY")
        self.account_name = os.getenv("COSMOS_DB_ACCOUNT_NAME")
        self.db_name = os.getenv("COSMOS_DB_DATABASE_NAME")
        self.detail_coll_name = os.getenv("DETAIL_COLLECTION")
        self.user_rec_coll_name = os.getenv("USER_REC_COLLECTION")
        self.url = f"https://{self.account_name}.documents.azure.com:443/"
        try:
            self.client = CosmosClient(self.url, credential=self.auth_key)
            self.db_client = self.client.get_database_client(self.db_name)
            self.prod_detail_container = self.db_client.get_container_client(
                self.detail_coll_name)
            self.user_rec_container = self.db_client.get_container_client(
                self.user_rec_coll_name)
        except Exception as e:
            print(e)
            print(
                "Default Settings Required By Demo:\n(1) Database Named: product_data\n(2) Product Detail Collection Named: product_details\n(3) User Recommendation Collection Named: user_recommendations\n\nOther Potential Issues:\n(1) Incorrect key provided in local.settings.json and/or the Function App settings.\n(2) Incorrect Account Name for the Cosmos Account Name parameter in local.settings.json and/or the Function App settings."
            )

    def getProductDetails(self, productIDs):
        if (len(productIDs) == 0):
            product_id_query_string = f"c.productID = '{productIDs[0]}'"
        else:
            product_id_query_string = " ".join([
                f"OR c.productID = '{x}'"
                if idx != 0 else f"c.productID = '{x}'"
                for idx, x in enumerate(productIDs)
            ])

        query_statement = f"SELECT * FROM c WHERE {product_id_query_string}"
        logging.info(query_statement)
        query_result_items = self.prod_detail_container.query_items(
            query=f'{query_statement}', enable_cross_partition_query=True)

        product_details = [x for x in query_result_items]

        return product_details

    def getUserRecommendations(self, userID):
        query_statement = f"SELECT * FROM c WHERE c.user_id = '{userID}'"
        try:

            query_result_items = self.user_rec_container.query_items(
                query=f'{query_statement}', enable_cross_partition_query=True)
            logging.info(query_statement)
        except Exception as e:
            print(e)
            logging.error(f"ISSUE {e}")
        productIDs = json.loads([json.dumps(x) for x in query_result_items
                                 ][0])["recommendations"]
        user_rec_object = {
            "user_id": userID,
            "items": self.getProductDetails(productIDs)
        }
        return user_rec_object
Esempio n. 13
0
 def __init__(self, config: ConfigParser):
     config = config
     CosmosURI = config['AzureCosmos']['URI']
     CosmosKey = config['AzureCosmos']['Key']
     client = CosmosClient(CosmosURI, credential=CosmosKey)
     database = client.get_database_client(config['AzureCosmos']['DatabaseName'])
     self.users = database.get_container_client(config['AzureCosmos']['UsersContainerName'])
     self.events = database.get_container_client(config['AzureCosmos']['EventsContainerName'])
     self.counters = database.get_container_client(config['AzureCosmos']['CountersContainerName'])
Esempio n. 14
0
def store_deltas(delta_tango, delta_netcraft):

    print ("**** STORE DELTAS IN COSMOS DB ****")
    uri          = os.environ.get('ACCOUNT_URI')
    key          = os.environ.get('ACCOUNT_KEY')
    database_id  = os.environ.get('DATABASE_ID')
    container_id = os.environ.get('DELTA_CONTAINER_ID')

    client = CosmosClient(uri, {'masterKey': key})

    database = client.get_database_client(database_id)
    container = database.get_container_client(container_id)

    # Get date
    date_str = datetime.today().strftime('%Y-%m-%d %H:%M:%S')
    id_date  = int((datetime.utcnow()).timestamp())
    id_date_str = str(id_date)

    phishing_delta = list(delta_tango['phishing'])
    already_blocked_delta = list(delta_tango['already_blocked'])
    suspicious_delta = list(delta_tango['suspicious'])
    malware_delta = list(delta_tango['malware'])

    tango_unique_list = list(set(phishing_delta) | set(already_blocked_delta) | set(suspicious_delta) | set(malware_delta))

    #print (type(tango_unique_list))
    #print (type(delta_netcraft))

    delta_netcraft_list = list(delta_netcraft)
    all_unique_list = tango_unique_list + delta_netcraft_list

    all_unique_list_str           = ' '.join(map(str, all_unique_list))
    all_phishing_delta_str        = ' '.join(map(str, phishing_delta))
    all_already_blocked_delta_str = ' '.join(map(str, already_blocked_delta))
    all_suspicious_delta_str      = ' '.join(map(str, suspicious_delta))
    all_malware_delta_str         = ' '.join(map(str, malware_delta))
    all_unique_str                = ' '.join(map(str, all_unique_list))
    all_netcraft_delta_str        = ' '.join(map(str, delta_netcraft_list))

    container.upsert_item( { 'id': id_date_str,
                             'date_time': id_date_str,
                             'date': date_str,
                             'n_unique': str(len(all_unique_list)),
                             'unique' : all_unique_list_str,
                             'n_phishing': str(len(phishing_delta)),
                             'phishing_delta': all_phishing_delta_str,
                             'n_blocked_delta': str(len(already_blocked_delta)),
                             'already_blocked_delta': all_already_blocked_delta_str,
                             'n_suspicious_delta': str(len(suspicious_delta)),
                             'suspicious_delta': all_suspicious_delta_str,
                             'n_malware_delta': str(len(malware_delta)),
                             'malware_delta': all_malware_delta_str,
                             'n_netcraft_delta': str(len(delta_netcraft_list)),
                             'netcraft_delta': all_netcraft_delta_str })

    
    write_attack_urls_to_output(all_unique_list, tango_unique_list, delta_netcraft_list, date_str)
Esempio n. 15
0
def main(data):
    client = CosmosClient(data['uri'], credential=data['pkey'])
    db = client.get_database_client(data['db'])
    container = db.get_container_client(data['container'])
    query = 'SELECT * FROM c WHERE c.id="1"'
    for item in container.query_items(query=query,
                                      enable_cross_partition_query=True):
        payload = json.loads(json.dumps(item, indent=True))
        print(payload)
        print(payload['po'])
Esempio n. 16
0
def get_records_from_cosmos():

    print("**** GET RECORDS FROM COSMOS ****")
    print("**** QUERY RESULTS CONTAINER ****")
    uri = os.environ.get('ACCOUNT_URI')
    key = os.environ.get('ACCOUNT_KEY')
    database_id = os.environ.get('DATABASE_ID')
    results_container_id = os.environ.get('RESULTS_CONTAINER_ID')

    client = CosmosClient(uri, {'masterKey': key})
    print(client)

    database = client.get_database_client(database_id)
    results_container = database.get_container_client(results_container_id)

    reporting_results = list(
        results_container.query_items(
            query='SELECT TOP 1 * FROM c ORDER BY c._ts DESC',
            enable_cross_partition_query=True))

    print("**** QUERY SUBMISSION CONTAINER ***")

    submission_container_id = os.environ.get('SUBMISSION_CONTAINER_ID')

    client = CosmosClient(uri, {'masterKey': key})
    print(client)

    database = client.get_database_client(database_id)
    submission_container = database.get_container_client(
        submission_container_id)

    date_str = datetime.today().strftime('%Y-%m-%d %H:%M:%S')
    current_date = datetime.now()

    yesterday = int((datetime.utcnow() - relativedelta(days=1)).timestamp())

    query = 'SELECT * FROM c WHERE c._ts > {}'.format(str(yesterday))
    submission_results = list(
        submission_container.query_items(query,
                                         enable_cross_partition_query=True))

    return submission_results, reporting_results
Esempio n. 17
0
    def __init__(self):
        endpoint = "https://synapsel1nk1.documents.azure.com:443/"
        key = "zM8dZNCWIi03JWziWAr2JGBlU2uEsmU1651YA4p5HCKp1DpGeQ0fH3gNDCHVEId5JIhHXhTI7Rkfnl4pZurqfg=="
        # create_cosmos_client
        client = CosmosClient(endpoint, key)

        database_name = 'lvtn_database'
        client.create_database_if_not_exists(database_name)
        database = client.get_database_client(database_name)
        container_name = 'parsed_data'
        self.container = database.get_container_client(container_name)
Esempio n. 18
0
    def __init__(self):
        endpoint = "https://synapsel1nk.documents.azure.com:443/"
        key = "r0EEApAfBwKARscLmgjPzdAYVVxFbLy5pOf2AU0yLL6FrcHFjySI3NYnb5zpHSvVPFkvRKI4yUTTRIZTmt4mCg=="
        # create_cosmos_client
        client = CosmosClient(endpoint, key)

        database_name = 'lvtn_database'
        client.create_database_if_not_exists(database_name)
        database = client.get_database_client(database_name)
        container_name = 'parsed_data'
        self.container = database.get_container_client(container_name)
Esempio n. 19
0
def sendToCosmos(farmid, runid, result_list):
    client = CosmosClient(CosmosClientConstants.URL, CosmosClientConstants.KEY)
    container = client.get_database_client(CosmosClientConstants.DATABASE_STR)\
                      .get_container_client(CosmosClientConstants.CONTAINER_STR)
    test_string = {
        "id": str(farmid) + "_" + str(runid),
        "farmId": str(farmid),
        "runId": runid,
        "result": result_list
    }
    container.upsert_item(test_string)
    logging.info("Upload to Cosmos Succesful")
def upload_cosmos(race_data):

    client = CosmosClient(url, credential=key)
    database = client.get_database_client(database_name)
    container = database.get_container_client(container_name)

    # upload race data
    for driver_data in race_data:
        if driver_data["driver_name"] not in ['easy', 'medium', 'hard']:
            container.upsert_item(driver_data)

    print("\nDriver data uploaded to Azure Cosmos DB")
Esempio n. 21
0
class DBClient:
    def __init__(self, endpoint=None, key=None):
        if not endpoint:
            endpoint = os.getenv("endpoint")
        if not key:
            key = os.getenv("key")
        self.WEATHER = "weather_data"
        self.TWEETS = "tweets"
        self.database_name = "urban_climate"
        self.client = CosmosClient(endpoint, key)
        self.database = self.client.get_database_client(self.database_name)
        self.weather_container = self.database.get_container_client(self.WEATHER)
        self.tweet_container = self.database.get_container_client(self.TWEETS)
Esempio n. 22
0
def getPrediction(user_id):
    url = "https://adbms-cosmos.documents.azure.com:443/"
    key = 'MjyGDQH9W1ZRAPUkhDzSyKHhPQS3nmUIXOm2MpE8tIDx7EYXx5sLkBkpWXUUoAlfVHDXZzcQm461NTyUU2GUmA=='
    client = CosmosClient(url, credential=key)
    database_name = 'HDIdatabase'
    database = client.get_database_client(database_name)
    container_name = 'ProductRecommendations'
    container = database.get_container_client(container_name)
    str_user = "******"+user_id
    result = []
    for item in container.query_items( query='SELECT * FROM c["%s"]'%(str_user), enable_cross_partition_query=True):
        result = item
    
    return {"message":"Prediction Data from UserId", "prediction_data":result}
Esempio n. 23
0
def update_cosmos_db(uuid, num_urls, url_list):

    print("\n***** Add UUID to the COSMOS DB *****\n")
    uri = os.environ.get('ACCOUNT_URI')
    key = os.environ.get('ACCOUNT_KEY')
    database_id = os.environ.get('DATABASE_ID')
    container_id = os.environ.get('CONTAINER_ID')

    #client = cosmos_client.CosmosClient(uri, {'masterKey': key})
    client = CosmosClient(uri, {'masterKey': key})

    database = client.get_database_client(database_id)
    container = database.get_container_client(container_id)

    # Get date
    date_str = datetime.today().strftime('%Y-%m-%d %H:%M:%S')
    all_uuid_str = uuid  #' '.join(map(str, netcraft_uuids))

    #for uuid in netcraft_uuids:
    uuid_str = str(uuid)
    print(uuid_str)

    url_list_str = ' '.join(map(str, url_list))

    print("Informaton for new record: ")
    print("    uuid: " + uuid_str)
    print("    date: " + date_str)
    print("    num URLs: " + str(num_urls))
    print("    associated uuids: " + all_uuid_str)
    print("    url list: " + url_list_str)

    # information to include:
    # - uuid
    # - associated uuids
    # - date
    # - number of unique URLs
    # - number of valid URLs subitted to Netcraft
    # - list URLs received from client

    # statement to insert record
    container.upsert_item({
        'id': uuid_str,
        'date': date_str,
        'uuid': uuid_str,
        'assoc_uuids': all_uuid_str,
        'n_urls': num_urls,
        'urls_str': url_list_str
    })
Esempio n. 24
0
def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info(f'Python HTTP trigger function processed a request.')
    cosmosDbUrl = os.environ["cosmosurl"]
    cosmosDbKey = os.environ["cosmoskey"]
    client = CosmosClient(cosmosDbUrl, credential=cosmosDbKey)
    database_name = 'todos-python'
    database = client.get_database_client(database_name)
    container_name = 'todos-python-container'
    container = database.get_container_client(container_name)
    itemId = req.route_params['id']
    try:
        container.delete_item(item=itemId, partition_key=itemId)
    except:
        return func.HttpResponse("Todo not found", status_code=404)

    return func.HttpResponse(
        f"Hello,. This HTTP triggered function executed successfully.")
Esempio n. 25
0
def get_submission_info_from_cosmos():

    print("**** GET NETCRAFT UUIDs FROM COSMOS ****")
    print("**** QUERY RESULTS CONTAINER ****")
    uri = os.environ.get('ACCOUNT_URI')
    key = os.environ.get('ACCOUNT_KEY')
    database_id = os.environ.get('DATABASE_ID')
    submission_container_id = os.environ.get('SUBMISSION_CONTAINER_ID')

    client = CosmosClient(uri, {'masterKey': key})
    #print (client)

    database = client.get_database_client(database_id)
    submission_container = database.get_container_client(
        submission_container_id)

    last_week = int((datetime.utcnow() - relativedelta(weeks=1)).timestamp())

    #print('Today: ' + current_date.strftime('%Y-%m-%d %H:%M:%S'))
    #print('Yesterday: ' + date_yesterday.strftime('%Y-%m-%d %H:%M:%S'))

    print("Query db for UUIDs since yesterday\n")

    #print(str(yesterday))

    # Get list of UUIDs
    query = 'SELECT DISTINCT VALUE c.id FROM c WHERE c._ts > {}'.format(
        str(last_week))
    uuid_query_results = list(
        submission_container.query_items(query,
                                         enable_cross_partition_query=True))

    query = 'SELECT * FROM c WHERE c._ts > {}'.format(str(last_week))
    submission_results = list(
        submission_container.query_items(query,
                                         enable_cross_partition_query=True))

    # From submission_results, pull: urls_in, urls_net
    urls_received = 0
    urls_submitted = 0

    for record in submission_results:
        urls_received += int(record['n_urls_in'])
        urls_submitted += int(record['n_urls_unq'])

    return uuid_query_results, urls_received, urls_submitted
Esempio n. 26
0
def database_write(apt_num):
    db_endpoint = "https://fall-detection-logs.documents.azure.com:443/"
    key = "wNZuwOXUGBnFxbqcEfY1KLIHn7Pq6C1kRG3HhONZydyHaxyPLXBWwyBPK4yQ0HuW545njp84ho300wLsf2BBFA=="
    client = CosmosClient(db_endpoint, {'masterKey': key})
    database_id = "fall-detection-logs1"
    container_id = "logs1-container-1"
    database = client.get_database_client(database_id)
    container = database.get_container_client(container_id)
    now = datetime.now()
    current_time = str(now)
    document = {
        'id': current_time,
        'apartmentNumber': apt_num,
        'time': current_time,
        'description': 'Fallen resident'
    }
    container.create_item(document)
Esempio n. 27
0
def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')
    url = 'https://rmc-cosmosdb.documents.azure.com:443/'
    key = os.environ['COSMOS_ACCOUNT_KEY']
    client = CosmosClient(url, credential=key)
    database_name = 'rmc'
    #database = client.create_database(database_name)
    database = client.get_database_client(database_name)
    container_name = 'feedback'
    #container = database.create_container(id=container_name, partition_key=PartitionKey(path="/id"))
    container = database.get_container_client(container_name)

    req_body = req.get_json()
    toId = req_body.get('ToId')

    cookie = SimpleCookie()
    cookie.load(req.headers['Cookie'])
    accessToken = cookie['AccessToken'].value
    decodedAccessToken = jwt.decode(accessToken,
                                    os.environ["JWT_ENCODING_SECRET"],
                                    algorithms=['HS256'])
    fromId = decodedAccessToken['userId']
    exp = decodedAccessToken['exp']
    nowTime = time()

    if exp < nowTime:
        return func.HttpResponse("Expired token", status_code=401)

    newFeedback = {
        'id': f"{fromId}||{toId}",
        'toId': toId,
        'fromId': fromId,
        'lastEditDate': nowTime,
        'createDate': nowTime,
        'body': {
            'Technical': int(req_body.get('Technical')),
            'Leadership': int(req_body.get('Leadership')),
            'Communication': int(req_body.get('Communication'))
        }
    }
    container.upsert_item(body=newFeedback)

    return func.HttpResponse("Successfully uploaded feedback.",
                             status_code=200)
Esempio n. 28
0
def get_past_runs(farm_id):    
    # query for past runs and their timestamps
    client = CosmosClient(Cosmos.URL, Cosmos.KEY)
    pred_container = client.get_database_client(Cosmos.DATABASE).get_container_client(Cosmos.PRED_CONTAINER)

    query = """SELECT predictions.runId, predictions._ts
            FROM predictions
            WHERE predictions.farmId=@farm_id"""

    items = list(pred_container.query_items(
        query=query,
        parameters=[{ "name":"@farm_id", "value": farm_id }],
        enable_cross_partition_query=True
    ))

    try:
        return items
    except IndexError:
        return None
Esempio n. 29
0
def read_DBdata(today):
    # 아래 primarykey는 read-only 입니다.

    config = {
        "endpoint":
        "https://jang.documents.azure.com:443/",
        "primarykey":
        "vqAEaHFa2Ov79VlOlcf8zP1TLtsUsuZqD0LUASUqu0IFZ9pU1MfdNUkepMdvipkSziB80dUmyzyqyDwsx7ypZg=="
    }

    client = CosmosClient(config["endpoint"], config["primarykey"])

    database_name = 'MLStocking'
    database = client.get_database_client(database_name)
    container_name = 'daily_price'
    container = database.get_container_client(container_name)

    # SQL
    q = f'''
    SELECT p.Date, p.Open, p.High, p.Low, p.Close, p.Volume
    FROM daily_price p
    WHERE p.code = "005930" AND p.Date >= "2019-01-01" AND p.Date <= "{today}"
    '''

    # Get the number of items in daily_price container
    items = container.query_items(query=q, enable_cross_partition_query=True)

    json_list = []
    for item in items:
        json_list.append(item)

    keys = json_list[0].keys()

    DBdata = pd.DataFrame(json_list)
    DBdata['Date'] = pd.to_datetime(DBdata['Date'])
    DBdata.sort_values('Date', inplace=True)
    DBdata = DBdata.groupby('Date', as_index=False).first()  # 중복제거
    print("Today:", today)
    print("Last Data:", DBdata['Date'].dt.strftime("%Y-%m-%d").iloc[-1])
    DBdata.columns = DBdata.columns.str.lower()
    DBdata['date'] = DBdata['date'].dt.strftime('%Y%m%d')  # 날짜 포맷 변경

    return DBdata
def download_cosmos():
    client = CosmosClient(url, credential=key)
    database = client.get_database_client(database_name)
    container = database.get_container_client(container_name)
    fastest_drivers = pd.DataFrame(columns=[
        "driver_name", "conference_name", "average_lap", "fastest_lap"
    ])

    query_result = container.query_items(
        query=
        'SELECT TOP 10 c.driver_name,c.conference_name,c.average_lap,c.fastest_lap FROM c ORDER BY c.average_lap',
        enable_cross_partition_query=True)

    try:
        for item in query_result:
            fastest_drivers = fastest_drivers.append(item, ignore_index=True)
    except:
        raise Exception("Azure download error. Check connection URL and key")

    return fastest_drivers