<commit_before>#!/usr/bin/env python
import tornado.httpserver
import tornado.ioloop
import tornado.web
import optparse
import logging
import traceback
import os
from os import listdir
from os.path import isfile, join
import json

"""
Logging settings
"""
logger = logging.getLogger('DynIPReport')
logger.setLevel(logging.INFO)
fileHandler = logging.FileHandler('dyn_report_service.log')
fileHandler.setLevel(logging.INFO)
# console handler
consoleHandler = logging.StreamHandler()
consoleHandler.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s',
                                datefmt='%Y-%m-%d %H:%M:%S')
fileHandler.setFormatter(formatter)
consoleHandler.setFormatter(formatter)
logger.addHandler(fileHandler)
logger.addHandler(consoleHandler)

# global variables
workingFolderPath = os.getcwd()

"""
Handles the report of IP address from client.
"""
class DynIpReportHanlder(tornado.web.RequestHandler):
    def post(self):
        try:
            self.set_header("Content-Type", "application/json; charset=UTF-8")

            # parse received json
            jsonobj = json.loads(self.request.body)

            hostname = jsonobj['hostname']
            ip = jsonobj['ip']
            ssh_port = jsonobj['ssh_port']
            update_time = jsonobj['update_time']

            print "Received report: " + hostname + ", " + ip + ", " + ssh_port + ", " + update_time

            # create temp folder if not exits
            tmpFileFolder = workingFolderPath + "/temp"
            if not os.path.exists(tmpFileFolder):
                os.makedirs(tmpFileFolder)

            # write to text file
            f = open(tmpFileFolder + "/" + hostname + ".txt", "w")
            try:
                f.write(ip + ',' + ssh_port + ',' + update_time + '\n')
            finally:
                f.close()

            # send response json
            response = { 'result': 'OK' }
            self.write(response)

            print response

        except:
            tb = traceback.format_exc()
            errMsg = { 'error': tb }
            self.write(errMsg)
            logger.error(tb)

"""
Handles the summary data for adminstrator.
"""
class SummaryDataHanlder(tornado.web.RequestHandler):
    def get(self):
        try:
            self.set_header("Content-Type", "text/html; charset=UTF-8")

            responseData = []
            # read from text file
            tmpFileFolder = workingFolderPath + "/temp"
            filePathList = [ f for f in listdir(tmpFileFolder) if isfile(join(tmpFileFolder, f)) ]

            for fileFullName in filePathList:
                rowData = None
                fileName = fileFullName.replace(".txt", "")
                f = open(tmpFileFolder + "/" + fileFullName, "r")
                try:
                    # Read the entire contents of a file at once.
                    raw = f.read()
                    if raw is not None:
                        data = raw.split(',')
                        rowData = { 'hostname': fileName, 'ip': data[0], 'ssh_port': data[1], 'update_time': data[2] }
                        responseData.append(rowData)
                finally:
                    f.close()

            responseMsg = { 'result': responseData }

            self.write(responseMsg)

        except IOError:
            tb = traceback.format_exc()
            errMsg = { 'error': tb }
            self.write(errMsg)
            logger.error(tb)
        except:
            tb = traceback.format_exc()
            errMsg = { 'error': tb }
            self.write(errMsg)
            logger.error(tb)

"""<commit_msg>Revise file listing bug.<commit_after>#!/usr/bin/env python
import tornado.httpserver
import tornado.ioloop
import tornado.web
import optparse
import logging
import traceback
import os
from os import listdir
from os.path import isfile, join
import json

"""
Logging settings
"""
logger = logging.getLogger('DynIPReport')
logger.setLevel(logging.INFO)
fileHandler = logging.FileHandler('dyn_report_service.log')
fileHandler.setLevel(logging.INFO)
# console handler
consoleHandler = logging.StreamHandler()
consoleHandler.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s',
                                datefmt='%Y-%m-%d %H:%M:%S')
fileHandler.setFormatter(formatter)
consoleHandler.setFormatter(formatter)
logger.addHandler(fileHandler)
logger.addHandler(consoleHandler)

# global variables
workingFolderPath = os.getcwd()

"""
Handles the report of IP address from client.
"""
class DynIpReportHanlder(tornado.web.RequestHandler):
    def post(self):
        try:
            self.set_header("Content-Type", "application/json; charset=UTF-8")

            # parse received json
            jsonobj = json.loads(self.request.body)

            hostname = jsonobj['hostname']
            ip = jsonobj['ip']
            ssh_port = jsonobj['ssh_port']
            update_time = jsonobj['update_time']

            print "Received report: " + hostname + ", " + ip + ", " + ssh_port + ", " + update_time

            # create temp folder if not exits
            tmpFileFolder = workingFolderPath + "/temp"
            if not os.path.exists(tmpFileFolder):
                os.makedirs(tmpFileFolder)

            # write to text file
            f = open(tmpFileFolder + "/" + hostname + ".txt", "w")
            try:
                f.write(ip + ',' + ssh_port + ',' + update_time + '\n')
            finally:
                f.close()

            # send response json
            response = { 'result': 'OK' }
            self.write(response)

            print response

        except:
            tb = traceback.format_exc()
            errMsg = { 'error': tb }
            self.write(errMsg)
            logger.error(tb)

"""
Handles the summary data for adminstrator.
"""
class SummaryDataHanlder(tornado.web.RequestHandler):
    def get(self):
        try:
            self.set_header("Content-Type", "text/html; charset=UTF-8")

            responseData = []
            # read from text file
            tmpFileFolder = workingFolderPath + "/temp"
            os.chdir(tmpFileFolder)
            filePathList = os.listdir(".")
            filePathList.sort()

            for fileFullName in filePathList:
                rowData = None
                fileName = fileFullName.replace(".txt", "")
                f = open(tmpFileFolder + "/" + fileFullName, "r")
                try:
                    # Read the entire contents of a file at once.
                    raw = f.read()
                    if raw is not None:
                        data = raw.split(',')
                        rowData = { 'hostname': fileName, 'ip': data[0], 'ssh_port': data[1], 'update_time': data[2] }
                        responseData.append(rowData)
                finally:
                    f.close()

            responseMsg = { 'result': responseData }

            self.write(responseMsg)

        except IOError:
            tb = traceback.format_exc()
            errMsg = { 'error': tb }
            self.write(errMsg)
            logger.error(tb)
        except:
            tb = traceback.format_exc()
            errMsg = { 'error': tb }
            self.write(errMsg)
            logger.error(tb)

"""
{'fraction_non_alphanumeric': 0.11525331148436434, 'fraction_numerical': 0.0013655605626109518, 'mean_word_length': 1.913285600636436, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4892516', 'n_tokens_mistral': 2089, 'n_tokens_neox': 2000, 'n_words': 462}