Exemple #1
0
def main():

    database = r"/Users/oskarsakol/PycharmProjects/bombardier/obiekt_kolejowy.tdb2"
    database_results = r"/Users/oskarsakol/PycharmProjects/bombardier/obiekt_kolejowy_results.db"

    conn = create_connection(database)

    with conn:
        all_courses = select_all_tasks(conn)

    if conn:
        conn.close()

    conn = create_connection(database_results)

    delete_all_tasks(conn)

    c = conn.cursor()

    c.execute('''CREATE TABLE IF NOT EXISTS results
                 ([route_id] INTEGER PRIMARY KEY,[courses_ids] text, [test_result] text, [log] text)'''
              )

    if conn:
        conn.close()

    generated_routes = generate_routes(all_courses)

    signal_test(generated_routes, database_results)
Exemple #2
0
def scan_cycle(my_path, my_ext, database):
    # The scan begins by creating a list of files in path of valid extension
    my_list = absolute_file_paths(my_path, my_ext)
    db_management.create_connection(database)
    # we then create a user report
    report = pd.DataFrame(columns=['Full Path', 'Extension', 'Timestamp', 'Original Hash', 'New Hash'])
    # main work loop
    dict_positives = {}
    for i in my_list:
        # for clarity, create a str variable of i
        fullpath_var = str(i)
        # we begin hashing our files one by one
        new_hash = md5(i)
        print("Working on: " + fullpath_var)
        # if the hash exists in our db
        if db_management.entry_exists(fullpath_var):
            # if it is equal to the previous hash, we update the timestamp
            # Strip the data received from the database
            old_hash = str(db_management.get_hash(fullpath_var)).strip(",""("")""'"" ")
            check = old_hash == new_hash
            if check:
                update_dict = {"timestamp": time.time()}
                db_management.update_hash(fullpath_var, update_dict)
            # else we save the filename, timestamp and hash in the user report
            else:
                new_row = {'Full Path': fullpath_var,
                           'Extension': pathlib.Path(fullpath_var).suffix,
                           'Timestamp': datetime.datetime.now(),
                           'Original Hash': old_hash,
                           'New Hash': new_hash}
                dict_positives[fullpath_var] = new_hash
                report = report.append(new_row, ignore_index=True)
                update_dict = {"timestamp": time.time(), "hash_val": new_hash}
                db_management.update_hash(fullpath_var, update_dict)
                print("ATTENTION: File at "+fullpath_var+" was different from last scan cycle!")
        # if the hash doesn't exist we add it to the db
        else:
            db_management.insert_hash(str(i), time.time(), new_hash)
    if not report.empty:
        # sort the report by extension first, timestamps second
        report = report.sort_values(by=['Extension', 'Timestamp'], ignore_index=True)
        # build a path where to save the report, same location as database
        # and format datetime to be filename friendly
        datetime_formatted = str(datetime.datetime.now())
        datetime_formatted = datetime_formatted.replace(":", "_")
        report_dir_path = str(os.getcwd()) + sep + 'Reports'
        if not os.path.isdir(report_dir_path):
            try:
                os.mkdir(report_dir_path)
            except OSError:
                exit(1)
        report_path = pathlib.Path(str(os.getcwd()) + sep + 'Reports' + sep + 'Report_' +
                                   datetime_formatted + '.csv')
        # save the report as csv
        report.to_csv(report_path)
        print("A report for modified files has been generated at "+str(report_path))
        # we then return a control code and the report path
        return True, report_path
    else:
        return False, '-'
Exemple #3
0
def most_expensive_car():
    """
    Method that can help to decide which type of car is the most expensive
    :return: the most expensive type of car in usage
    """
    conn = create_connection(DB_FILE)
    cursor = conn.cursor()
    sql = '''select AVG(average_per_day) as average,type
            from (select AVG(repair_avg) + AVG(charge_avg) as average_per_day, charge.car_id from
              (select AVG(repair_car.cost) as repair_avg, date, car_id
              from repair_car
              group by date,car_id )as repair
            join
              (select AVG(charge_car_history.cost) as charge_avg,date, car_id
              from charge_car_history
              group by date,car_id ) as charge
            on repair.car_id = charge.car_id
            group by charge.car_id) as s,cars, models
            where cars.car_id = s.car_id and cars.model_id = models.model_id
            group by type
            order by average desc
    '''
    cursor.execute(sql)
    s = cursor.fetchall()
    response = {s[0][1]: s[0][0]}
    close_connection(conn)
    return response
Exemple #4
0
def search_duplicates(data):
    '''
    Method for searching duplicate order_ids which should be unique in transaction table
    Search are done for a last month
    :param data: username to search duplicate payment
    :return: date when duplicate occurred and cost of that order
    '''
    username = data['username']
    conn = create_connection(DB_FILE)
    cursor = conn.cursor()

    date_month_ago = '2018-09-29'

    # It's also work but for well-looked (big table) result we defined month by our hands
    # date_month_ago = (datetime.datetime.now() - datetime.timedelta(30)).date()

    try:
        task = (username, date_month_ago)
        sql = '''SELECT b.date, b.cost
                  FROM (select order_id from 
                  (SELECT order_id, COUNT(*) FROM transactions GROUP BY order_id HAVING COUNT(*) > 1))as a
                  inner join
                   (select order_id, date, cost from orders where username = ? and date > ?) as b 
                   on a.order_id = b.order_id;'''
        print("THERE")
        cursor.execute(sql, task)
        response = cursor.fetchall()
        close_connection(conn)
        return response
    except sqlite3.Error:
        logging.info("Error")
    return "No such username"
Exemple #5
0
def top_locations_search():
    """
    Method that search top 3 pick-up and destination locations
    :return: list of tuples: morning start and finish point, afternoon start and finish point, evening start and finish point
    """
    conn = create_connection(DB_FILE)
    cursor = conn.cursor()

    try:

        morning_start_load = '''SELECT starting_point FROM orders WHERE time >= '07:00' and time <= '10:00' 
        GROUP BY starting_point ORDER BY count(starting_point) DESC LIMIT 3 '''

        morning_finish_load = '''SELECT destination FROM orders WHERE time >= '07:00' and time <= '10:00'
         GROUP BY destination ORDER BY count(destination) DESC LIMIT 3'''

        afternoon_start_load = '''SELECT starting_point FROM orders WHERE time >= '12:00' and time <= '14:00' 
                GROUP BY starting_point ORDER BY count(starting_point) DESC LIMIT 3 '''

        afternoon_finish_load = '''SELECT destination FROM orders WHERE time >= '12:00' and time <= '14:00' 
                        GROUP BY destination ORDER BY count(destination) DESC LIMIT 3 '''

        evening_start_load = '''SELECT starting_point FROM orders WHERE time >= '17:00' and time <= '19:00' 
                        GROUP BY starting_point ORDER BY count(starting_point) DESC LIMIT 3 '''

        evening_finish_load = '''SELECT destination FROM orders WHERE time >= '17:00' and time <= '19:00' 
                                GROUP BY destination ORDER BY count(destination) DESC LIMIT 3 '''

        def fetch_load(query):
            cursor.execute(query)
            return [x[0] for x in cursor.fetchall()]

        top_morning_start_point = fetch_load(morning_start_load)

        top_morning_finish_point = fetch_load(morning_finish_load)

        top_afternoon_start_point = fetch_load(afternoon_start_load)

        top_afternoon_finish_point = fetch_load(afternoon_finish_load)

        top_evening_start_point = fetch_load(evening_start_load)

        top_evening_finish_point = fetch_load(evening_finish_load)

        close_connection(conn)

        return (
            (top_morning_start_point, top_morning_finish_point),
            (top_afternoon_start_point, top_afternoon_finish_point),
            (top_evening_start_point, top_evening_finish_point),
        )
    except sqlite3.Error:
        logging.info("Error")
    return "Error while searching was occured"
Exemple #6
0
def stat_of_busy_cars(data):
    """
    :param data: json file which contains start date to calculate statistic
    :return: load of cars in percentage by 1 week in different daytime periods
    """
    conn = create_connection(DB_FILE)
    cursor = conn.cursor()
    date = data['date']

    try:
        task = (date, date)
        sql_cnt_cars = '''SELECT count(DISTINCT cars.car_id)
                 from cars
              '''
        morning_load = '''SELECT DISTINCT car_id,date, date(?,'+7 day') as date_end
                          FROM orders where time >='07:00' and time <= '10:00' 
                          AND date>=? AND date <= date_end
                       '''
        afternoon_load = '''
                          SELECT DISTINCT car_id, date, date(?,'+7 day') as date_end
                          FROM orders where time >='12:00' and time <= '14:00' 
                          AND date>=? AND date <= date_end
                         '''
        evening_load = '''
                          SELECT DISTINCT car_id, date, date(?,'+7 day') as date_end
                          FROM orders where time >='17:00' and time <= '19:00' 
                          AND date=? AND date <= date_end
                       '''
        cursor.execute(sql_cnt_cars)
        cnt = cursor.fetchall()[0][0] * 7
        morning_load = len(cursor.execute(morning_load,
                                          task).fetchall()) / cnt * 100
        afternoon_load = len(cursor.execute(afternoon_load,
                                            task).fetchall()) / cnt * 100
        evening_load = len(cursor.execute(evening_load,
                                          task).fetchall()) / cnt * 100
        response = {
            'Morning': morning_load,
            'Afternoon': afternoon_load,
            'Evening': evening_load
        }
        close_connection(conn)
        return response
    except sqlite3.Error:
        logging.info("Error")

    return "Error while searching was occured or doesn't found in database"
Exemple #7
0
def most_relevant_part_by_workshop():
    """
    Method that returns the most relevant part that workshop needs
    :return: type of detail that separate workshop needs mostly.
    """
    conn = create_connection(DB_FILE)
    cursor = conn.cursor()
    sql = '''select type_of_detail, WID
            from(select part_id, WID , MAX(amount_week_ago - amount) as diff
            from workshop_have_parts
            group by  WID) as s, parts
            where s.part_id = parts.part_id
          '''
    cursor.execute(sql)
    response = {}
    for info in cursor.fetchall():
        response[info[1]] = info[0]
    close_connection(conn)
    return response
Exemple #8
0
def average_distance(data):
    '''
    Calculating average distance of car to a pickup point for orders in one particular day
    :param data: date to calculate
    :return: average distance to a pickup point for a particular date
    '''
    date = data['date']
    conn = create_connection(DB_FILE)
    cursor = conn.cursor()

    try:
        task = [date]
        sql = "select avg(car_distance) from orders where date = ?"
        cursor.execute(sql, task)
        response = cursor.fetchall()[0]
        close_connection(conn)
        return response
    except sqlite3.Error:
        logging.info("Error")
    return "There is no orders for such period"
Exemple #9
0
def stat_least_amount_cars():
    """
    Method that search for least frequently used cars (10 % of cars in company)
    :return: list of cars id
    """
    conn = create_connection(DB_FILE)
    cursor = conn.cursor()
    try:
        cars_id = []
        least_car_id = '''SELECT car_id FROM orders GROUP BY (car_id) 
        ORDER BY count(*) LIMIT (SELECT DISTINCT count(car_id)/10 FROM cars)'''
        cursor.execute(least_car_id)
        cars = cursor.fetchall()
        close_connection(conn)

        for car in cars:
            cars_id.append(car[0])
        return cars_id
    except sqlite3.Error:
        logging.info("Error")
    return "Error while searching was occured"
Exemple #10
0
def times_using_ch_station(data):
    """
    Method that calculate how many times charging station was used by user
    :param data: json file which contain start date
    :return: how many times user used charging station in his orders
    """
    conn = create_connection(DB_FILE)
    cursor = conn.cursor()
    task = (data['start_date'])
    sql = '''select count(car_id) as charging_times,username
             from(select charge_car_history.date, date(?,'+1 month') as end_period,orders.car_id,username
             from charge_car_history,orders
             where orders.car_id = charge_car_history.car_id and charge_car_history.date = orders.date
             and start_time between orders.time and time(orders.time,'+'|| cast(orders.duration as text)||' minutes'))
             group by username;'''
    cursor.execute(sql, task)
    response = {}
    for info in cursor.fetchall():
        response[info[1]] = info[0]
    close_connection(conn)
    return response
Exemple #11
0
def find_car(data):
    """
    Method that search a car for given parameters
    :param data: json file which contains date of the order, colour and registration
                 number  of the car and username
    :return: list of tuples of the car id, colour, registration number
    """
    conn = create_connection(DB_FILE)
    cursor = conn.cursor()

    try:
        task = (data['date'], data['colour'], data['username'],
                '%' + data['reg_num'] + '%')
        sql = '''SELECT cars.car_id,colour,reg_num 
        from cars,orders 
        where cars.car_id=orders.car_id and  date = ? AND colour = ? 
        AND username = ? AND reg_num LIKE ?;'''
        cursor.execute(sql, task)
        response = cursor.fetchall()
        close_connection(conn)
        return response
    except sqlite3.Error:
        logging.info("Error")
    return "Error while searching was occured"
Exemple #12
0
def efficiency_ch_stations(data):
    """
    Method that calculate efficiency of charging station utilization for given date
    :param data: json file which contains date
    :return: returns list of charging stations and how many times they were used hourly
    """
    conn = create_connection(DB_FILE)
    cursor = conn.cursor()
    date = data['date']
    response = defaultdict(dict)
    for i in range(0, 24):
        st_time = ''
        end_time = ''
        if i < 9:
            st_time = '0{}:00'.format(i)
            end_time = '0{}:00'.format(i + 1)
            task = (st_time, end_time, date)
            sql = '''SELECT UID,count(charge_car_id)
                     FROM charge_car_history where start_time >= ?  and start_time< ?
                     and date=?
                     group by UID;
                  '''
        elif i == 9:
            st_time = '0{}:00'.format(i)
            end_time = '{}:00'.format(i + 1)
            task = (st_time, end_time, date)
            sql = '''SELECT UID,count(charge_car_id)
                     FROM charge_car_history where start_time>=? and start_time<?
                     and date=?
                     group by UID;
                  '''
        else:
            st_time = '{}:00'.format(i)
            end_time = '{}:00'.format(i + 1)
            task = (st_time, end_time, date)
            sql = '''SELECT UID,count(charge_car_id)
                     FROM charge_car_history where start_time >= ? and start_time<?
                     and date=?
                     group by UID;
                  '''
        cursor.execute(sql, task)
        data = cursor.fetchall()

        for value in data:
            response[value[0]][st_time + "-" + end_time] = value[1]

    close_connection(conn)
    for i in range(0, 24):
        st_time = ''
        end_time = ''
        s = defaultdict(dict)
        if i < 9:
            st_time = '0{}:00'.format(i)
            end_time = '0{}:00'.format(i + 1)
        elif i == 9:
            st_time = '0{}:00'.format(i)
            end_time = '{}:00'.format(i + 1)
        else:
            st_time = '{}:00'.format(i)
            end_time = '{}:00'.format(i + 1)
        for residual in response.keys():
            if (st_time + "-" + end_time) not in response[residual].keys():
                response[residual][st_time + "-" + end_time] = 0
    for residual in response.keys():
        s[residual] = sorted(response[residual].items())
    return s