From 42e9e3d707008b2b6a840d766ec827cd97988824 Mon Sep 17 00:00:00 2001 From: sosguns2002 Date: Thu, 1 Jun 2017 13:43:36 +0300 Subject: [PATCH] Download profile feature --- .../madoap/src/madserver.py | 152 +++++++++++++++- .../madoap/src/static/interactivemining.js | 165 ++++++++++++++---- .../src/templates/interactivemining.html | 13 ++ 3 files changed, 285 insertions(+), 45 deletions(-) diff --git a/interactive-mining-madoap/madoap/src/madserver.py b/interactive-mining-madoap/madoap/src/madserver.py index f21ce61..6632488 100755 --- a/interactive-mining-madoap/madoap/src/madserver.py +++ b/interactive-mining-madoap/madoap/src/madserver.py @@ -70,6 +70,9 @@ class Application(tornado.web.Application): (r"/", madAppQueryGenerator), (r"/importing-controller", importingControllerHandler), (r"/importing-text-controller", importingTextsControllerHandler), + (r"/save-config-controller", profileCreationHandler), + (r"/download-config-controller", profileServeHandler), + (r"/upload-profile-controller", profileUploadHandler), (r"/?$", madAppBarHandler), (r"/[^/]+/?$", madAppHandler), (r"/[^/]+/.+$", madAppDataHandler) @@ -316,6 +319,8 @@ class madAppQueryGenerator(BaseHandler): # Get the unique user id from the coookie set user_id = self.get_secure_cookie('madgikmining') + if user_id is None: + return # data to be sent data = {} try: @@ -409,19 +414,151 @@ class madAppQueryGenerator(BaseHandler): pass self.write(json.dumps(data)) self.flush() + self.finish() -class saveProfileHandler(BaseHandler): +class profileCreationHandler(BaseHandler): def post(self): - """Controls the importing job as follows: - *** load-raw-data (reading and saving them in the DB)""" try: user_id = self.get_secure_cookie('madgikmining') - if 'saveprofile' in self.request.arguments: - print "asda" + if user_id is None: + return + + import sys + sys.path.append(msettings.MADIS_PATH) + import madis + # get the database cursor + # profile file name + profile_file_name = "/tmp/OAMiningProfile_{0}.oamp".format(user_id) + cursor=madis.functions.Connection(profile_file_name).cursor() + # Create poswords table + cursor.execute("drop table if exists poswords", parse=False) + cursor.execute("create table poswords(c1,c2)", parse=False) + # Create negwords table + cursor.execute("drop table if exists negwords", parse=False) + cursor.execute("create table negwords(c1,c2)", parse=False) + # Create filters table + cursor.execute("drop table if exists filters", parse=False) + cursor.execute("create table filters(c1,c2)", parse=False) + # Create grants table + cursor.execute("drop table if exists grants", parse=False) + cursor.execute("create table grants(c1)", parse=False) + if 'poswords' in self.request.arguments and self.request.arguments['poswords'][0] != '{}': + # construct math string for positive words matching calculation with weights + pos_words = json.loads(self.request.arguments['poswords'][0]) + cursor.executemany("insert into poswords(c1,c2) values(?,?)", + ( + (key, value,) for key, value in pos_words.iteritems() + ) + ) + if 'negwords' in self.request.arguments and self.request.arguments['negwords'][0] != '{}': + # construct math string for negative words matching calculation with weights + neg_words = json.loads(self.request.arguments['negwords'][0]) + cursor.executemany("insert into negwords(c1,c2) values(?,?)", + ( + (key, value,) for key, value in neg_words.iteritems() + ) + ) + if 'filters' in self.request.arguments and self.request.arguments['filters'][0] != '{}': + # construct math string for negative words matching calculation with weights + filters = json.loads(self.request.arguments['filters'][0]) + cursor.executemany("insert into filters(c1,c2) values(?,?)", + ( + (key, value,) for key, value in filters.iteritems() + ) + ) + if numberOfGrantsUploaded(user_id, self.get_secure_cookie('madgikmining_grantsuploaded')) != 0: + cursor.execute("insert into grants select stripchars(c1) as c1 from (file '/tmp/p{0}.csv')".format(user_id)) + cursor.close() + + self.write(json.dumps({'respond': "File ploaded.
1 Code loaded! Please make sure that you separate each code with newline!"})) except Exception as ints: - self.write(json.dumps({'respond': "SOomething went very wrong!"})) + self.write(json.dumps({'respond': "Mining profile couldn't be saved!"})) + print ints + return + self.finish() + + +class profileServeHandler(BaseHandler): + def get(self): + try: + user_id = self.get_secure_cookie('madgikmining') + if user_id is None: + return + if 'saveprofile' in self.request.arguments: + print "asda" + profile_file_name = "/tmp/OAMiningProfile_{0}.oamp".format(user_id) + buf_size = 4096 + self.set_header('Content-Type', 'application/octet-stream') + self.set_header('Content-Disposition', 'attachment; filename=' + "OAMiningProfile_{0}.oamp".format(user_id)) + self.flush() + with open(profile_file_name, 'r') as f: + while True: + data = f.read(buf_size) + if not data: + break + self.write(data) + self.finish() + # TODO delete file after sending if needed + + except Exception as ints: + self.write(json.dumps({'respond': "Something went very wrong!"})) + print ints + return + +class profileUploadHandler(BaseHandler): + def post(self): + try: + user_id = self.get_secure_cookie('madgikmining') + if user_id is None: + return + # get file info and body from post data + fileinfo = self.request.files['upload'][0] + fname = fileinfo['filename'] + extn = os.path.splitext(fname)[1] + # must be .pdf or .json + if extn != ".oamp": + self.write(json.dumps({'respond': "File must be .oamp compatible profile"})) + return + # write data to physical file + cname = "/tmp/profile{0}.oamp".format(user_id) + fh = open(cname, 'w') + fh.write(fileinfo['body']) + fh.close() + # extract data from profile file + import sys + sys.path.append(msettings.MADIS_PATH) + import madis + # get the profile database cursor + cursor=madis.functions.Connection(cname).cursor() + + # data to be sent + data = {} + # Write to csv file the grants ids + if len([r for r in cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='grants'")]): + cursor.execute("output '/tmp/p{0}.csv' select * from grants".format(user_id)) + numberOfGrants = numberOfGrantsUploaded(user_id, "puppet_value") + self.set_secure_cookie('madgikmining_grantsuploaded', str(numberOfGrants)) + data['grants'] = numberOfGrants + # write to json the poswords + if len([r for r in cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='poswords'")]): + results = [r for r in cursor.execute("select c1, c2 from poswords")] + data['poswords'] = {value:key for value, key in results} + # write to json the negwords + if len([r for r in cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='negwords'")]): + results = [r for r in cursor.execute("select c1, c2 from negwords")] + data['negwords'] = {value:key for value, key in results} + # write to json the filters + if len([r for r in cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='filters'")]): + results = [r for r in cursor.execute("select c1, c2 from filters")] + data['filters'] = {value:key for value, key in results} + cursor.close() + self.write(json.dumps(data)) + self.finish() + + except Exception as ints: + self.write(json.dumps({'respond': "Something went very wrong!"})) print ints return @@ -434,7 +571,8 @@ class importingControllerHandler(BaseHandler): try: user_id = self.get_secure_cookie('madgikmining') - + if user_id is None: + return csv_file_name = "/tmp/p{0}.csv".format(user_id) csv_file = open(csv_file_name, 'w') diff --git a/interactive-mining-madoap/madoap/src/static/interactivemining.js b/interactive-mining-madoap/madoap/src/static/interactivemining.js index 0904a28..43ddff9 100644 --- a/interactive-mining-madoap/madoap/src/static/interactivemining.js +++ b/interactive-mining-madoap/madoap/src/static/interactivemining.js @@ -1,3 +1,34 @@ +function handleConfigDownload() { + var formData = new FormData(); + formData.append("poswords", $('#pos-words-text').val()); + formData.append("negwords", $('#neg-words-text').val()); + filters_list = {}; + filters_list["stopwords"] = $('#stop-words-filter').prop('checked')===true?1:0; + filters_list["lowercase"] = $('#lowercase-filter').prop('checked')===true?1:0; + filters_list["keywords"] = $('#keywords-filter').prop('checked')===true?1:0; + formData.append("filters", JSON.stringify(filters_list)); + $.ajax({ + url: "save-config-controller", + type: 'POST', + data: formData, + async: false, + success: function (data) { + $('#file-upload-response').html(JSON.parse(data).respond) + // if (data.indexOf('successfully!') != -1) { + // $('#file-uploaded')[0].checked = true; + // } + window.location="download-config-controller?saveprofile=1" + }, + error: function (xhr, ajaxOptions, thrownError) { + $('#file-upload-response').html('File Failed to Upload!'+xhr.status) + // $('#file-uploaded')[0].checked = false; + }, + cache: false, + contentType: false, + processData: false + }); +} + function handleLoadExampleFile() { $("#file-input-operation").val('example'); $("#file-title-text").html(''); @@ -65,6 +96,58 @@ $( window ).resize(function() { .on( 'blur', function(){ $input.removeClass( 'has-focus' ); }); }); + function handleProfileUploadButton() { + $("form#profile-input-form").submit(function(){ + if ($('#profile-input')[0].value === "") { + window.alert("You must specify a profile to import."); + return false; + } + // $('#user-id').val(getCookie("madgikmining")); + $("#profile-input-operation").val('normal'); + var formData = new FormData($(this)[0]); + $.ajax({ + url: "upload-profile-controller", + type: 'POST', + data: formData, + async: false, + success: function (data) { + $('#profile-upload-response').html(JSON.parse(data).respond); + obj = JSON && JSON.parse(data) || $.parseJSON(data); + console.log(obj); + for (var key1 in obj) { + if (obj.hasOwnProperty(key1)) { + if (key1==="poswords") { + // delete all poswords from the lists + deleteAllPosWords(0); + for (var key2 in obj[key1]) { + createWord(1, generateId(1), key2, obj[key1][key2]); + } + } else if (key1 === "negwords") { + deleteAllNegWords(0); + for (var key2 in obj[key1]) { + createWord(0, generateId(0), key2, obj[key1][key2]); + } + } else if (key1 === "filters") { + for (var key2 in obj[key1]) { + console.log(key2, obj[key1][key2]); + } + } + } + } + }, + error: function (xhr, ajaxOptions, thrownError) { + $('#profile-upload-response').html('File Failed to Upload!'+xhr.status) + // $('#profile-uploaded')[0].checked = false; + }, + cache: false, + contentType: false, + processData: false + }); + + return false; + }); + } + function handleFileUploadButton() { $("form#file-input-form").submit(function(){ if ($('#file-input')[0].value === "") { @@ -154,6 +237,7 @@ $( window ).resize(function() { } + /////////// LIST FUNCTIONS var count_pos = 0, count_neg = 0; @@ -445,46 +529,50 @@ $( window ).resize(function() { // } // } // }; + var deleteAllPosWords = function(warnUser = 1) { + if(!warnUser || confirm('Are you sure you want to delete all the items in the list? There is no turning back after that.')){ //remove items from DOM + var items = $('li[id ^= positive]'); + items.addClass('removed-item').one('webkitAnimationEnd oanimationend msAnimationEnd animationend', function(e) { + $(this).remove(); + }); + + //look for items in localStorage that start with word- and remove them + var keys = []; + for(var key in localStorage){ + if(key.indexOf('positive') === 0){ + localStorage.removeItem(key); + } + } + count_pos = 0; + updateCounter(1); + } + updatetextereas(); + }; + + var deleteAllNegWords = function(warnUser = 1) { + if(!warnUser || confirm('Are you sure you want to delete all the items in the list? There is no turning back after that.')){ //remove items from DOM + var items = $('li[id ^= negative]'); + items.addClass('removed-item').one('webkitAnimationEnd oanimationend msAnimationEnd animationend', function(e) { + $(this).remove(); + }); + + //look for items in localStorage that start with word- and remove them + var keys = []; + for(var key in localStorage){ + if(key.indexOf('negative') === 0){ + localStorage.removeItem(key); + } + } + count_neg = 0; + updateCounter(0); + } + updatetextereas(); + }; + //handler for the "delete all" button var handleDeleteButton = function(){ - $('#clear-all-pos').on('click', function(){ - if(confirm('Are you sure you want to delete all the items in the list? There is no turning back after that.')){ //remove items from DOM - var items = $('li[id ^= positive]'); - items.addClass('removed-item').one('webkitAnimationEnd oanimationend msAnimationEnd animationend', function(e) { - $(this).remove(); - }); - - //look for items in localStorage that start with word- and remove them - var keys = []; - for(var key in localStorage){ - if(key.indexOf('positive') === 0){ - localStorage.removeItem(key); - } - } - count_pos = 0; - updateCounter(1); - } - updatetextereas(); - }); - $('#clear-all-neg').on('click', function(){ - if(confirm('Are you sure you want to delete all the items in the list? There is no turning back after that.')){ //remove items from DOM - var items = $('li[id ^= negative]'); - items.addClass('removed-item').one('webkitAnimationEnd oanimationend msAnimationEnd animationend', function(e) { - $(this).remove(); - }); - - //look for items in localStorage that start with word- and remove them - var keys = []; - for(var key in localStorage){ - if(key.indexOf('negative') === 0){ - localStorage.removeItem(key); - } - } - count_neg = 0; - updateCounter(0); - } - updatetextereas(); - }); + $('#clear-all-pos').on('click', deleteAllPosWords); + $('#clear-all-neg').on('click', deleteAllNegWords); }; var init = function(){ @@ -499,6 +587,7 @@ $( window ).resize(function() { handleFileUploadButton(); handleZipFileUploadButton(); handleDocsUploadSelect(); + handleProfileUploadButton(); }; //start all init(); diff --git a/interactive-mining-madoap/madoap/src/templates/interactivemining.html b/interactive-mining-madoap/madoap/src/templates/interactivemining.html index 732b644..e46386a 100644 --- a/interactive-mining-madoap/madoap/src/templates/interactivemining.html +++ b/interactive-mining-madoap/madoap/src/templates/interactivemining.html @@ -33,6 +33,19 @@ -->
+ +
+
+
+ + + +
+
+ +
+
+

Add your Text File of codes