Exemple #1
0
def table(request, cid, **kwargs):
    my_auth = MyBasicAuthentication()
    is_login = my_auth.is_authenticated(request, **kwargs)
    if is_login:
        try:
            my_var = MyVariable()
            data_path = my_var.get_var('path', 'data_path')
            config_path = data_path + "custom"
            real_file = config_path + '/tables.json'
            info = {}
            with open(real_file, 'r') as f:
                config = json.load(f)
            tables = config["tables"]
            for item in tables:
                if item["id"] == cid:
                    info = item
        except Exception, e:
            print e
            info = {}
        page_data = {
            "active": "applications",
            "subnav": "overview",
            "user": is_login["u"],
            "email": is_login["e"],
            "token": is_login["t"],
            "userid": is_login["i"],
            "table_info": info,
            "role": is_login["r"]
        }
        return render(request, 'application/custom/table.html',
                      {"page_data": json.dumps(page_data)})
Exemple #2
0
    def report_files(self, request, **kwargs):
        self.method_check(request, allowed=['get'])
        dummy_data = {}

        my_auth = MyBasicAuthentication()
        es_check = my_auth.is_authenticated(request, **kwargs)
        if es_check:
            domain = es_check["d"]
            owner = str(es_check["i"]) + "|" + str(es_check["u"]) + "|" + str(
                es_check["t"])
            my_var = MyVariable()
            data_path = my_var.get_var('path', 'report_path')
            tmp_path = data_path + "yottaweb_reports/" + es_check[
                "t"] + "/" + str(es_check["i"]) + "/"
            tmp_files = os.listdir(tmp_path)
            file_list = []
            for one_file in tmp_files:
                file_list.append({
                    'name': one_file,
                    'owner': es_check['u'],
                    'created_time': one_file.split('_')[-1]
                })
            dummy_data["list"] = file_list
            dummy_data["status"] = "1"
        else:
            data = err_data.build_error({}, "auth error!")
            data["location"] = "/auth/login/"
            dummy_data = data
        bundle = self.build_bundle(obj=dummy_data,
                                   data=dummy_data,
                                   request=request)
        response_data = bundle
        resp = self.create_response(request, response_data)
        return resp
Exemple #3
0
    def table_info(self, request, **kwargs):
        self.method_check(request, allowed=['get'])

        sid = kwargs.get('sid')

        dummy_data = {}
        my_auth = MyBasicAuthentication()
        es_check = my_auth.is_authenticated(request, **kwargs)
        if es_check:
            info = {}
            try:
                my_var = MyVariable()
                data_path = my_var.get_var('path', 'data_path')
                config_path = data_path + "custom"
                real_file = config_path + '/tables.json'
                with open(real_file, 'r') as f:
                    config = json.load(f)
                tables = config["tables"]
                for item in tables:
                    if item["id"] == sid:
                        info = item
            except Exception, e:
                print e
                info = {}
            dummy_data["status"] = "1"
            dummy_data["table"] = info.get("content", {})
Exemple #4
0
def overview(request, **kwargs):
    my_auth = MyBasicAuthentication()
    is_login = my_auth.is_authenticated(request, **kwargs)
    if is_login:
        with_sg = check_with_sourcegroup(is_login)
        cf_per = check_with_permission(is_login)
        my_var = MyVariable()
        data_path = my_var.get_var('path', 'data_path')
        config_path = data_path + "custom"
        real_file = config_path + '/apps.json'
        try:
            with open(real_file, 'r') as f:
                config = json.load(f)
            apps = config["apps"]
        except Exception, e:
            print e
            apps = []
            config = {}
        table_file = config_path + '/tables.json'
        try:
            with open(table_file, 'r') as f:
                config = json.load(f)
            tables = config["tables"]
        except Exception, e:
            print e
            tables = []
            config = {}
Exemple #5
0
    def tables_delete(self, request, **kwargs):
        self.method_check(request, allowed=['post'])
        dummy_data = {}
        id = kwargs.get("aid", "")
        my_auth = MyBasicAuthentication()
        es_check = my_auth.is_authenticated(request, **kwargs)

        if es_check:
            my_var = MyVariable()
            data_path = my_var.get_var('path', 'data_path')
            config_path = data_path + "custom"
            real_file = config_path + '/tables.json'

            try:
                with open(real_file, 'r') as f:
                    config = json.load(f)
                f.close()
                tables = config.get("tables", [])

            except Exception, e:
                print e
                tables = []

            cur_id = -1
            for i in range(len(tables)):
                if tables[i]["id"] == id:
                    cur_id = i
            try:
                if cur_id > -1:
                    del(tables[cur_id])
            except Exception, e:
                print e
Exemple #6
0
    def steps(self, request, **kwargs):
        self.method_check(request, allowed=['get'])

        sid = kwargs.get('sid')

        dummy_data = {}
        my_auth = MyBasicAuthentication()
        es_check = my_auth.is_authenticated(request, **kwargs)
        if es_check:
            info = {}
            try:
                my_var = MyVariable()
                data_path = my_var.get_var('path', 'data_path')
                config_path = data_path + "custom"
                real_file = config_path + '/apps.json'
                with open(real_file, 'r') as f:
                    config = json.load(f)
                apps = config["apps"]
                for item in apps:
                    if item["id"] == sid:
                        info = item
            except Exception, e:
                print e
                info = {}
            dummy_data["status"] = "1"
            dummy_data["name"] = info.get("name", "")
            dummy_data["auto_search"] = info.get("auto_search", "no")
            dummy_data["sumSection"] = info.get("sumSection", {})
            dummy_data["steps"] = info.get("content", [])
Exemple #7
0
    def csv_download(self, request, **kwargs):
        self.method_check(request, allowed=['get'])
        data = []
        dummy_data = {}
        my_auth = MyBasicAuthentication()
        es_check = my_auth.is_authenticated(request, **kwargs)
        if es_check:
            file_name = kwargs['fid']
            root_path = os.getcwd()
            my_var = MyVariable()
            data_path = my_var.get_var('path', 'data_path')
            tmp_path = data_path + "yottaweb_tmp/" + es_check["d"] + "/" + es_check["u"] + "/"
            filename = tmp_path + file_name
            wrapper = FileWrapper(file(filename))
            resp = HttpResponse(wrapper, content_type='text/plain')

            # resp = self.create_response(request, wrapper)
            resp['Content-Length'] = os.path.getsize(filename)
            resp['Content-Encoding'] = 'utf-8'
            resp['Content-Disposition'] = 'attachment;filename=%s' % file_name
        else:
            data = err_data.build_error({}, "auth error!")
            data["location"] = "/auth/login/"
            dummy_data = data
            bundle = self.build_bundle(obj=dummy_data, data=dummy_data, request=request)
            response_data = bundle
            resp = self.create_response(request, response_data)
        return resp
Exemple #8
0
    def dashboards_update(self, request, **kwargs):
        self.method_check(request, allowed=['post', 'get'])
        dummy_data = {}
        id = kwargs.get("did", "")
        my_auth = MyBasicAuthentication()
        es_check = my_auth.is_authenticated(request, **kwargs)

        if es_check:
            my_var = MyVariable()
            data_path = my_var.get_var('path', 'data_path')
            config_path = data_path + "custom"
            real_file = config_path + '/dashboard.json'
            if request.method == "POST":
                post_data = json.loads(request.POST.get("data"))

                try:
                    with open(real_file, 'r') as f:
                        config = json.load(f)
                    f.close()
                    if not config:
                        config = []

                except Exception, e:
                    print e
                    config = []

                cur_id = -1
                for i in range(len(config)):
                    if config[i]["custom_config_id"] == id:
                        cur_id = i
                if cur_id > -1:
                    config[cur_id] = post_data

                    try:
                        with open(real_file, 'w') as f:
                            json.dump(config, f)
                        f.close()
                        dummy_data["status"] = "1"
                        dummy_data["url"] = "/system/custom/dashboard/"
                    except Exception, e:
                        print e
                        dummy_data["status"] = "0"
                        dummy_data["msg"] = "update custom dashboard error!"
                else:
                    dummy_data["status"] = "0"
                    dummy_data["msg"] = "Can't find dashboard by id"+id+" !"
Exemple #9
0
    def tables_update(self, request, **kwargs):
        self.method_check(request, allowed=['post', 'get'])
        dummy_data = {}
        id = kwargs.get("aid", "")
        my_auth = MyBasicAuthentication()
        es_check = my_auth.is_authenticated(request, **kwargs)

        if es_check:
            my_var = MyVariable()
            data_path = my_var.get_var('path', 'data_path')
            config_path = data_path + "custom"
            real_file = config_path + '/tables.json'

            if request.method == "POST":
                post_data = json.loads(request.POST.get("data"))

                try:
                    with open(real_file, 'r') as f:
                        config = json.load(f)
                    f.close()
                    tables = config.get("tables", [])

                except Exception, e:
                    print e
                    tables = []

                cur_id = -1
                for i in range(len(tables)):
                    if tables[i]["id"] == id:
                        cur_id = i
                if cur_id > -1:
                    tables[cur_id] = post_data

                app_content = {
                    "tables": tables
                }
                try:
                    with open(real_file, 'w') as f:
                        json.dump(app_content, f)
                    f.close()
                    dummy_data["status"] = "1"
                    dummy_data["url"] = "/system/custom/table/"
                except Exception, e:
                    print e
                    dummy_data["status"] = "0"
                    dummy_data["msg"] = "update custom app error!"
Exemple #10
0
    def app_list(self, request, **kwargs):
        self.method_check(request, allowed=['get'])

        dummy_data = {}
        my_auth = MyBasicAuthentication()
        es_check = my_auth.is_authenticated(request, **kwargs)
        if es_check:
            my_var = MyVariable()
            data_path = my_var.get_var('path', 'data_path')
            config_path = data_path + "custom"
            real_file = config_path + '/apps.json'
            traces = []
            tables = []
            try:
                with open(real_file, 'r') as f:
                    config = json.load(f)
                _tmp_traces = config["apps"]
                for item in _tmp_traces:
                    print item
                    traces.append({
                        "name":
                        item.get("name", ""),
                        "link":
                        "/app/custom/" + item.get("id", "") + "/",
                        "permission":
                        "true"
                    })
            except Exception, e:
                print e
            table_file = config_path + '/tables.json'
            try:
                with open(table_file, 'r') as f:
                    config = json.load(f)
                _tmp_tables = config["tables"]
                for item in _tmp_tables:
                    tables.append({
                        "name": item.get("name", ""),
                        "link": "/app/table/" + item.get("id", "") + "/",
                        "permission": "true"
                    })
            except Exception, e:
                print e
                tables = []
Exemple #11
0
 def dashboards(self, request, **kwargs):
     self.method_check(request, allowed=['get'])
     my_auth = MyBasicAuthentication()
     es_check = my_auth.is_authenticated(request, **kwargs)
     dummy_data = {}
     if es_check:
         my_var = MyVariable()
         data_path = my_var.get_var('path', 'data_path')
         config_path = data_path + "custom"
         real_file = config_path + '/dashboard.json'
         try:
             with open(real_file, 'r') as f:
                 config = json.load(f)
             f.close()
         except Exception, e:
             print e
             config = []
         dummy_data["status"] = "1"
         dummy_data["dashboards"] = config
Exemple #12
0
    def tables(self, request, **kwargs):
        self.method_check(request, allowed=['get'])
        my_auth = MyBasicAuthentication()
        es_check = my_auth.is_authenticated(request, **kwargs)
        dummy_data = {}
        if es_check:
            my_var = MyVariable()
            data_path = my_var.get_var('path', 'data_path')
            config_path = data_path + "custom"
            try:
                real_path = config_path + '/tables.json'
                with open(real_path, 'r') as f:
                    config = json.load(f)
                tables = config.get("tables", [])

            except Exception, e:
                print e
                tables = []
            dummy_data["status"] = "1"
            dummy_data["tables"] = tables
Exemple #13
0
    def report_files_download_directly(self, request, **kwargs):
        self.method_check(request, allowed=['get'])
        dummy_data = {}

        fid_encoded = kwargs['fid_encoded']

        if fid_encoded:
            my_utils = MyUtils()
            _decrypt_path = my_utils.b64Decrypt(fid_encoded.encode("utf-8"))
            re_logger.info("report_files_download_directly decrypted_path: %s",
                           _decrypt_path)
            file_path = base64.urlsafe_b64decode(
                _decrypt_path) if _decrypt_path else ""
            re_logger.info("report_files_download_directly: %s", file_path)
            my_var = MyVariable()
            data_path = my_var.get_var('path', 'report_path')
            tmp_path = data_path + "yottaweb_reports/" + file_path
            if file_path:
                file_name = file_path.split("/")[-1]
                wrapper = FileWrapper(file(file_path))
                resp = HttpResponse(wrapper, content_type='text/plain')

                # resp = self.create_response(request, wrapper)
                resp['Content-Length'] = os.path.getsize(file_path)
                resp['Content-Encoding'] = 'utf-8'
                resp[
                    'Content-Disposition'] = 'attachment;filename=%s' % file_name
                return resp
            else:
                data = err_data.build_error({}, "Download report file error!")
                dummy_data = data
        else:
            data = err_data.build_error({}, "auth error!")
            data["location"] = "/auth/login/"
            dummy_data = data
        bundle = self.build_bundle(obj=dummy_data,
                                   data=dummy_data,
                                   request=request)
        response_data = bundle
        resp = self.create_response(request, response_data)
        return resp
Exemple #14
0
    def tables_new(self, request, **kwargs):
        self.method_check(request, allowed=['post'])
        dummy_data = {}
        my_auth = MyBasicAuthentication()
        es_check = my_auth.is_authenticated(request, **kwargs)

        if es_check:
            post_data = json.loads(request.POST.get("data"))

            my_var = MyVariable()
            data_path = my_var.get_var('path', 'data_path')
            config_path = data_path + "custom"
            real_file = config_path + '/tables.json'
            try:
                if not os.path.exists(config_path):
                    os.makedirs(config_path)

                with open(real_file, 'rw') as f:
                    config = json.load(f)
                f.close()
                tables = config.get("tables", [])

            except Exception, e:
                print e
                tables = []

            tables.append(post_data)

            app_content = {
                "tables": tables
            }
            try:
                with open(real_file, 'w') as f:
                    json.dump(app_content, f)
                f.close()
                dummy_data["status"] = "1"
                dummy_data["url"] = "/system/custom/table/"
            except Exception, e:
                print e
                dummy_data["status"] = "0"
                dummy_data["msg"] = "create custom table error!"
Exemple #15
0
    def dashboards_delete(self, request, **kwargs):
        self.method_check(request, allowed=['post'])
        dummy_data = {}
        id = kwargs.get("did", "")
        my_auth = MyBasicAuthentication()
        es_check = my_auth.is_authenticated(request, **kwargs)

        if es_check:
            my_var = MyVariable()
            data_path = my_var.get_var('path', 'data_path')
            config_path = data_path + "custom"
            real_file = config_path + '/dashboard.json'
            try:
                with open(real_file, 'r') as f:
                    config = json.load(f)
                f.close()
                if not config:
                    config = []

            except Exception, e:
                print e
                config = []

            cur_id = -1
            for i in range(len(config)):
                if config[i]["custom_config_id"] == id:
                    cur_id = i
            if cur_id > -1:
                del(config[cur_id])
                try:
                    with open(real_file, 'w') as f:
                        json.dump(config, f)
                    f.close()
                    dummy_data["status"] = "1"
                    dummy_data["dashboards"] = config
                except Exception, e:
                    print e
                    dummy_data["status"] = "0"
                    dummy_data["msg"] = "delete custom dashboard error!"
Exemple #16
0
    def _build_csv(self, type, data, domain, user):
        if type == "sourcegroup":
            heads = ["sourcegroup", "appname", "hostname"]
        else:
            heads = ["appname", "sourcegroup", "hostname"]
        rows = []
        for item in data:
            if item.get("infos", {}):
                for (k, v) in item["infos"].items():
                    if v:
                        for hostname in v:
                            rows.append([item["name"], k, hostname])
                    else:
                        rows.append([item["name"], k, ""])
                # todo
            else:
                rows.append([item["name"], "", ""])

        root_path = os.getcwd()
        my_var = MyVariable()
        data_path = my_var.get_var('path', 'data_path')
        tmp_path = data_path + "yottaweb_tmp/" + domain + "/" + user + "/"
        if not os.path.exists(tmp_path):
            os.makedirs(tmp_path)
        # remove old file first
        name = 'log_resource_' + domain + "_" + user.encode("utf-8")
        file_name = str(name) + ".csv"
        file_path = tmp_path + file_name
        if os.path.isfile(file_path):
            os.remove(file_path)
        f = open(file_path, "wb+")
        # write bom to resolve unreadable chinese problem
        f.write("\xEF\xBB\xBF")
        writer = csv.writer(f)
        writer.writerow(heads)
        for line in rows:
            writer.writerow(line)
        return file_name
Exemple #17
0
    def dashboards_new(self, request, **kwargs):
        self.method_check(request, allowed=['post'])
        dummy_data = {}
        my_auth = MyBasicAuthentication()
        es_check = my_auth.is_authenticated(request, **kwargs)

        if es_check:
            post_data = json.loads(request.POST.get("data"))
            my_var = MyVariable()
            data_path = my_var.get_var('path', 'data_path')
            config_path = data_path + "custom"
            real_file = config_path + '/dashboard.json'
            try:
                if not os.path.exists(config_path):
                    os.makedirs(config_path)
                with open(real_file, 'r') as f:
                    config = json.load(f)
                f.close()
                if not config:
                    config = []

            except Exception, e:
                print e
                config = []

            config.append(post_data)

            try:
                with open(real_file, 'w') as f:
                    json.dump(config, f)
                f.close()
                dummy_data["status"] = "1"
                dummy_data["url"] = "/system/custom/dashboard/"
            except Exception, e:
                print e
                dummy_data["status"] = "0"
                dummy_data["msg"] = "create custom dashboard error!"
Exemple #18
0
    def report_files_download(self, request, **kwargs):
        self.method_check(request, allowed=['get'])
        dummy_data = {}

        my_auth = MyBasicAuthentication()
        es_check = my_auth.is_authenticated(request, **kwargs)
        if es_check:
            domain = es_check["d"]
            my_var = MyVariable()
            data_path = my_var.get_var('path', 'report_path')
            tmp_path = data_path + "yottaweb_reports/" + es_check[
                "t"] + "/" + str(es_check["i"]) + "/"
            file_name = kwargs['fid']
            file_path = tmp_path + kwargs['fid']
            if file_name:
                wrapper = FileWrapper(file(file_path))
                resp = HttpResponse(wrapper, content_type='text/plain')

                # resp = self.create_response(request, wrapper)
                resp['Content-Length'] = os.path.getsize(file_path)
                resp['Content-Encoding'] = 'utf-8'
                resp[
                    'Content-Disposition'] = 'attachment;filename=%s' % file_name
                return resp
            else:
                data = err_data.build_error({}, "Delete report file error!")
                dummy_data = data
        else:
            data = err_data.build_error({}, "auth error!")
            data["location"] = "/auth/login/"
            dummy_data = data
        bundle = self.build_bundle(obj=dummy_data,
                                   data=dummy_data,
                                   request=request)
        response_data = bundle
        resp = self.create_response(request, response_data)
        return resp