back-end/utils/descriptive_analysis.py
def top_n(results_cursor, n, key, barplot_file_name, path):
    """ It calculates the frequency of appearance for values of a given key in query result.
        Then returns the top n most common values with the number of appearance.
        This result gets stored in json and csv files but also gets returned by the function.
        @parameters
            dataset_name        (string)        the name of the data set to be created WITHOUT EXTENSION (.csv, .json)
            n                   (integer)       the number of first entities to be returned
            key                 (str)           the field in mongoDB's to base the analysis on
            barplot_file_name   (str)
            path                (str)           the path of file to be saved
        @returns
            highcharts_results  (list)          a list of sublists. In each sublist the first element is a value and the
                                                second is the number of attacks
    """

    # aggregate categories
    type_of_attacks = list([])
    try:
        for doc in results_cursor.rewind():
            type_of_attacks.append(doc[key])

        # find most common, results example [('Unknown', 6631), ('China', 1339), ('Russian Federation', 614)]
        results = Counter(type_of_attacks).most_common(n)

        # typecast tuples in lists
        highcharts_results = list([])
        for result in results:
            # result is updated in results and later used, highcharts_results example
            # [['Unknown', 6631], ['China', 1339], ['Russian Federation', 614], ['United States', 364]]
            # now results will be a list of lists adequate for Highcharts Barplot
            highcharts_results.append(list(result))

        # store in files for bar plot
        json_barplot = path + barplot_file_name + '.json'
        csv_barplot = path + barplot_file_name + '.csv'

        # files will be overwritten
        with open(json_barplot, 'w') as json_file:
            json.dump(highcharts_results, json_file, separators=(',', ': '), indent=4)

        with open(csv_barplot, 'w') as csv_file:
            for result in highcharts_results:
                line = str(result[0]) + str(',') + str(result[1]) + str("\n")
                csv_file.write(line)

        return highcharts_results

    except Exception as e:
        print("descriptive_analysis module > top_n > Store in file Error: ", e)
        return None

    # -----------------------------------------------------------------------------------------------------------------#

 It calculates the frequency of appearance for values of a given key in query result.
        Then returns the top n most common values with the number of appearance.
        This result gets stored in json and csv files but also gets returned by the function.
        @parameters
            dataset_name        (string)        the name of the data set to be created WITHOUT EXTENSION (.csv, .json)
            n                   (integer)       the number of first entities to be returned
            key                 (str)           the field in mongoDB's to base the analysis on
            barplot_file_name   (str)
            path                (str)           the path of file to be saved
        @returns
            highcharts_results  (list)          a list of sublists. In each sublist the first element is a value and the
                                                second is the number of attacks
    
It calculates the frequency of appearance for values of a given key in query result.
Then returns the top n most common values with the number of appearance.
This result gets stored in json and csv files but also gets returned by the function.
It calculates the frequency of appearance for values of a given key in query result . Then returns the top n most common values with the number of appearance . This result gets stored in json and csv files but also gets returned by the function .
def top_n(results_cursor, n, key, barplot_file_name, path):
    type_of_attacks = list([])
    try:
        for doc in results_cursor.rewind():
            type_of_attacks.append(doc[key])
        results = Counter(type_of_attacks).most_common(n)
        highcharts_results = list([])
        for result in results:
            highcharts_results.append(list(result))
        json_barplot = path + barplot_file_name + '.json'
        csv_barplot = path + barplot_file_name + '.csv'
        with open(json_barplot, 'w') as json_file:
            json.dump(highcharts_results, json_file, separators=(',', ': '), indent=4)
        with open(csv_barplot, 'w') as csv_file:
            for result in highcharts_results:
                line = str(result[0]) + str(',') + str(result[1]) + str("\n")
                csv_file.write(line)
        return highcharts_results
    except Exception as e:
        print("descriptive_analysis module > top_n > Store in file Error: ", e)
        return None
def top_n ( results_cursor , n , key , barplot_file_name , path ) : type_of_attacks = list ( [ ] ) try : for doc in results_cursor . rewind ( ) : type_of_attacks . append ( doc [ key ] ) results = Counter ( type_of_attacks ) . most_common ( n ) highcharts_results = list ( [ ] ) for result in results : highcharts_results . append ( list ( result ) ) json_barplot = path + barplot_file_name + '.json' csv_barplot = path + barplot_file_name + '.csv' with open ( json_barplot , 'w' ) as json_file : json . dump ( highcharts_results , json_file , separators = ( ',' , ': ' ) , indent = 4 ) with open ( csv_barplot , 'w' ) as csv_file : for result in highcharts_results : line = str ( result [ 0 ] ) + str ( ',' ) + str ( result [ 1 ] ) + str ( "\n" ) csv_file . write ( line ) return highcharts_results except Exception as e : print ( "descriptive_analysis module > top_n > Store in file Error: " , e ) return None
It calculates the frequency of appearance for values of a given key in query result.
It calculates the frequency of appearance for values of a given key in query result .
""" It calculates the frequency of appearance for values of a given key in query result.
        Then returns the top n most common values with the number of appearance.
        This result gets stored in json and csv files but also gets returned by the function.
        @parameters
            dataset_name        (string)        the name of the data set to be created WITHOUT EXTENSION (.csv, .json)
            n                   (integer)       the number of first entities to be returned
            key                 (str)           the field in mongoDB's to base the analysis on
            barplot_file_name   (str)
            path                (str)           the path of file to be saved
        @returns
            highcharts_results  (list)          a list of sublists. In each sublist the first element is a value and the
                                                second is the number of attacks
    """ # aggregate categories
 # find most common, results example [('Unknown', 6631), ('China', 1339), ('Russian Federation', 614)]
 # typecast tuples in lists
 # result is updated in results and later used, highcharts_results example
 # [['Unknown', 6631], ['China', 1339], ['Russian Federation', 614], ['United States', 364]]
 # now results will be a list of lists adequate for Highcharts Barplot
 # store in files for bar plot
 # files will be overwritten
 # -----------------------------------------------------------------------------------------------------------------#

results_cursor
n
key
barplot_file_name
path