Projekt

Obecné

Profil

Stáhnout (3.32 KB) Statistiky
| Větev: | Revize:
1
from Utilities.Database import DatabaseDataLine
2
import pymongo
3

    
4
# specify mongodb connection
5
MONGODB_CONNECTION = "mongodb://root:root@database"
6
# mongodb account name
7
MONGODB_ACC_NAME = "root"
8
# mongodb account password
9
MONGODB_ACC_PASSWORD = "root"
10
# mongodb data database
11
MONGODB_DATA_DATABASE = "DATA"
12
# mongodb collection with aviable datasets
13
MONGODB_DATASET_COLLECTION = "DATASETS"
14

    
15
# Path to processed data
16
PROCESSED_DATA_PATH = "ProcessedData/"
17

    
18

    
19
def create_database_connection():
20
    """
21
    Creates connection to mongoDB
22
    
23
    Returns:
24
        Connection to mongoDB
25
    """
26
    client = pymongo.MongoClient(MONGODB_CONNECTION)
27

    
28
    # Authenticating
29
    client.admin.authenticate(MONGODB_ACC_NAME, MONGODB_ACC_PASSWORD)
30

    
31
    database = client[MONGODB_DATA_DATABASE]
32

    
33
    return database
34

    
35

    
36
def get_data_from_file(filename, config):
37
    """
38
        Opens processed file, reads it line by line
39
        name, ocurrence, date
40
        searches name in config and adds device map coordinates
41
        than creates a dictionary with date without hours as key
42
        and list of data lines as value.
43
    Args:
44
        filename: name of processed file
45
        config: loaded configuration file of dataset
46

    
47
    Returns:
48
        dictionary with date without hours as key
49
        and list of Datalines as value
50
    """
51
    dataset_name = config["dataset-name"]
52
    dataset_path = PROCESSED_DATA_PATH + dataset_name + '/'
53

    
54
    f = open(dataset_path + filename, "r")
55

    
56
    devices = config["devices"]
57
    date_dict = dict()
58

    
59
    for line in f:
60
        line = line[:-1]
61

    
62
        csv_column = line.split(";")
63

    
64
        name = csv_column[0]
65

    
66
        occurrence = csv_column[1]
67
        date = csv_column[2]
68

    
69

    
70

    
71
        database_data_line = DatabaseDataLine.DatabaseDataLine(name, devices[name]["x"]
72
                                                               , devices[name]["y"], date, occurrence)
73

    
74
        # if you want to change table split by hours or months change this
75
        date_without_hours = date[:-2]
76
        if date_without_hours not in date_dict:
77
            date_dict[date_without_hours] = list()
78

    
79
        date_dict[date_without_hours].append(database_data_line.to_dictionary())
80

    
81
    return date_dict
82

    
83

    
84
def load_data_to_database(dataset_name, data_dic):
85
    """
86
    Takes data_dic created in method get_data_from_file
87
    and loads into into database where collection name is dataset_name + data_dic key
88
    and data lines are line in collection
89

    
90
    Args:
91
        dataset_name: name of dataset that has existing configuration file
92
        data_dic: dictionary of data lines created in get_data_from_file
93
    """
94
    database = create_database_connection()
95

    
96
    # collection where are specified aviable datasets
97
    collection_datasets = database[MONGODB_DATASET_COLLECTION]
98

    
99
    # check if newly added data already have a dataset specified in collection
100
    dataset_present = collection_datasets.find_one({'name': dataset_name})
101

    
102
    if dataset_present is None:
103
        collection_datasets.insert_one({'name': dataset_name})
104

    
105
    for date in data_dic:
106
        dataset_collections = database[dataset_name]
107
        dataset_collections.insert_one({'name': dataset_name+date})
108
        date_dataset = database[dataset_name + date]
109
        date_dataset.insert_many(data_dic[date])
(2-2/2)