예제 #1
0
 def test_context_manager_request(self):
     DataCollector().configure(
         RequestSkill.objects.create(path='/to/somewhere'))
     with silk_profile(name='test_profile'):
         sleep(0.1)
     profile = list(DataCollector().profiles.values())[0]
     self.assertEqual(DataCollector().request, profile['request'])
예제 #2
0
파일: views.py 프로젝트: CyberLight/silk
def index(request):
    @silk_profile()
    def do_something_long():
        sleep(1.345)

    with silk_profile(name='Why do this take so long?'):
        do_something_long()
    return render_to_response('example_app/index.html', {'blinds': models.Blind.objects.all()})
예제 #3
0
파일: question.py 프로젝트: webvul/crawler
def detail(request, pk):
    stackoverflowdb = mongodb["stackoverflow"]
    question = stackoverflowdb["question"].find_one(ObjectId(pk))
    if not question:
        raise Http404
    answers = stackoverflowdb["answer"].find(
        {"question_id": question["question_id"]})
    relate_questions = []
    with silk_profile(name='Search By Keywords #%s' % question["title"]):
        datas = es.search(index='it',
                          doc_type='stackoverflow_questions',
                          body={
                              "query": {
                                  "filtered": {
                                      "query": {
                                          "match": {
                                              "title": {
                                                  "query":
                                                  question["title"].lower(),
                                                  "minimum_should_match":
                                                  "30%",
                                                  "operator":
                                                  "or"
                                              }
                                          }
                                      }
                                  }
                              },
                              "from": 0,
                              "size": 10,
                              'sort': [{
                                  '_score': {
                                      'order': 'desc'
                                  }
                              }],
                          })
        hits, took = datas["hits"], datas["took"]
        total = hits["total"]
        for h in hits["hits"]:
            relate_questions.append({
                "id":
                h["_id"],
                "body":
                h["_source"]["body"],
                "title":
                h["_source"]["title"],
                "tags":
                h["_source"]["tags"],
                "created":
                time.strftime('%Y-%m-%d',
                              time.localtime(h["_source"]["creation_date"]))
            })
    return render_to_response(
        'ask/question/detail.html', {
            "question": question,
            "answers": answers,
            "relate_questions": relate_questions
        }, RequestContext(request))
예제 #4
0
def detail(request, pk):
    stackoverflowdb = mongodb["stackoverflow"]
    question = stackoverflowdb["question"].find_one(ObjectId(pk))
    obj = Question()
    obj.name = question["title"]
    obj.id = question["question_id"]
    if not question:
        raise Http404
    answers = stackoverflowdb["answer"].find({"question_id": question["question_id"]})
    relate_questions = []
    with silk_profile(name="Search By Keywords #%s" % question["title"]):
        datas = es.search(
            index="it",
            doc_type="stackoverflow_questions",
            body={
                "query": {
                    "filtered": {
                        "query": {
                            "match": {
                                "title": {
                                    "query": question["title"].lower(),
                                    "minimum_should_match": "30%",
                                    "operator": "or",
                                }
                            }
                        }
                    }
                },
                "from": 0,
                "size": 10,
                "sort": [{"_score": {"order": "desc"}}],
            },
        )
        hits, took = datas["hits"], datas["took"]
        total = hits["total"]
        for h in hits["hits"]:
            relate_questions.append(
                {
                    "id": h["_id"],
                    "body": h["_source"]["body"],
                    "title": h["_source"]["title"],
                    "tags": h["_source"]["tags"],
                    "created": time.strftime("%Y-%m-%d", time.localtime(h["_source"]["creation_date"])),
                }
            )
    print obj.get_absolute_url()
    return render_to_response(
        "search/detail.html",
        {
            "question": question,
            "answers": answers,
            "obj": obj,
            "answers_total": answers.count(),
            "relate_questions": relate_questions,
        },
        RequestContext(request),
    )
예제 #5
0
    def test_analyze_queries(self):
        DataCollector().configure(Request(reverse('example_app:index')))
        client = Client()

        with silk_profile(name='test_profile'):
            resp = client.get(reverse('example_app:index'))

        DataCollector().profiles.values()
        assert len(resp.context['blinds']) == 5
예제 #6
0
def index(request):
    @silk_profile()
    def do_something_long():
        sleep(1.345)

    with silk_profile(name='Why do this take so long?'):
        do_something_long()
    return render(request, 'example_app/index.html',
                  {'blinds': models.Blind.objects.all()})
예제 #7
0
 def test_no_queries_before(self):
     DataCollector().configure(Request.objects.create())
     with silk_profile(name='test_no_queries_before_profile'):
         mock_queries = MockSuite().mock_sql_queries(n=5, as_dict=True)
         DataCollector().register_query(*mock_queries)
     profile = list(DataCollector().profiles.values())[0]
     self.assertEqual(profile['name'], 'test_no_queries_before_profile')
     queries = profile['queries']
     self.assertEqual(len(queries), 5)
     for query in DataCollector().queries:
         self.assertIn(query, queries)
예제 #8
0
 def test_no_queries_before(self):
     DataCollector().configure(Request.objects.create())
     with silk_profile(name='test_no_queries_before_profile'):
         mock_queries = MockSuite().mock_sql_queries(n=5, as_dict=True)
         DataCollector().register_query(*mock_queries)
     profile = list(DataCollector().profiles.values())[0]
     self.assertEqual(profile['name'], 'test_no_queries_before_profile')
     queries = profile['queries']
     self.assertEqual(len(queries), 5)
     for query in DataCollector().queries:
         self.assertIn(query, queries)
예제 #9
0
 def test_no_queries_before(self):
     DataCollector().configure(Request.objects.create())
     with silk_profile(name="test_no_queries_before_profile"):
         mock_queries = MockSuite().mock_sql_queries(n=5, as_dict=True)
         DataCollector().register_query(*mock_queries)
     profile = list(DataCollector().profiles)[0]
     self.assertEqual(profile["name"], "test_no_queries_before_profile")
     queries = profile["queries"]
     self.assertEqual(len(queries), 5)
     for query in mock_queries:
         self.assertIn(query, queries)
예제 #10
0
 def test_queries_before(self):
     """test that any queries registered before profiling begins are ignored"""
     DataCollector().configure(Request.objects.create())
     DataCollector().register_query(
         *MockSuite().mock_sql_queries(n=2, as_dict=True))
     with silk_profile(name='test_no_queries_before_profile'):
         mock_queries = MockSuite().mock_sql_queries(n=5, as_dict=True)
         DataCollector().register_query(*mock_queries)
     profile = list(DataCollector().profiles)[0]
     self.assertEqual(profile['name'], 'test_no_queries_before_profile')
     queries = profile['queries']
     self.assertEqual(len(queries), 5)
     for query in mock_queries:
         self.assertIn(query, queries)
예제 #11
0
 def test_queries_before(self):
     """test that any queries registered before profiling begins are ignored"""
     DataCollector().configure(Request.objects.create())
     DataCollector().register_query(*MockSuite().mock_sql_queries(n=2, as_dict=True))
     before = [x for x in DataCollector().queries]
     with silk_profile(name='test_no_queries_before_profile'):
         mock_queries = MockSuite().mock_sql_queries(n=5, as_dict=True)
         DataCollector().register_query(*mock_queries)
     profile = list(DataCollector().profiles.values())[0]
     self.assertEqual(profile['name'], 'test_no_queries_before_profile')
     queries = profile['queries']
     self.assertEqual(len(queries), 5)
     for query in set(DataCollector().queries).difference(before):
         self.assertIn(query, queries)
예제 #12
0
파일: dynamic.py 프로젝트: kkaehler/silk
def profile_function_or_method(module, func, name=None):
    """
    Programmatically apply a decorator to a function in a given module [+ class]

    @param module: module object or module name in form 'path.to.module'
    @param func: function object or function name in form 'foo' or 'Class.method'
    """
    if type(module) is str or type(module) is unicode:
        module = _get_module(module)
    decorator = silk_profile(name, _dynamic=True)
    func_name = func
    cls, func = _get_func(module, func_name)
    wrapped_target = decorator(func)
    if cls:
        setattr(cls, func_name.split('.')[-1], wrapped_target)
    else:
        setattr(module, func_name, wrapped_target)
예제 #13
0
파일: dynamic.py 프로젝트: Gluriurl/silk
def profile_function_or_method(module, func, name=None):
    """
    Programmatically apply a decorator to a function in a given module [+ class]

    @param module: module object or module name in form 'path.to.module'
    @param func: function object or function name in form 'foo' or 'Class.method'
    """
    if type(module) is str or type(module) is unicode:
        module = _get_module(module)
    decorator = silk_profile(name, _dynamic=True)
    func_name = func
    cls, func = _get_func(module, func_name)
    wrapped_target = decorator(func)
    if cls:
        setattr(cls, func_name.split('.')[-1], wrapped_target)
    else:
        setattr(module, func_name, wrapped_target)
예제 #14
0
def blog_detail(request, pk):
    stackoverflowdb = mongodb["stackoverflow"]
    question = stackoverflowdb["question"].find_one(ObjectId(pk))
    if not question:
        raise Http404
    answers = stackoverflowdb["answer"].find({"question_id": question["question_id"]})
    relate_questions=[]
    with silk_profile(name='Search By Keywords #%s' % question["title"]):
        datas = es.search(index='it', doc_type='stackoverflow_questions', body={
            "query": {
                "filtered": {
                    "query": {
                        "match": {
                            "title": {
                                "query": question["title"].lower(),
                                "minimum_should_match": "30%",
                                "operator": "or"
                            }
                        }
                    }
                }
            },
            "from": 0,
            "size": 10,
            'sort': [
                {'_score': {'order': 'desc'}}
            ],
        })
        hits, took = datas["hits"], datas["took"]
        total = hits["total"]
        for h in hits["hits"]:
            relate_questions.append({
                "id": h["_id"],
                "body": h["_source"]["body"],
                "title": h["_source"]["title"],
                "tags": h["_source"]["tags"],
                "created": time.strftime('%Y-%m-%d',  time.localtime(h["_source"]["creation_date"]))
            })
    return render_to_response('blog/detail.html', {
        "question": question,
        "answers": answers,
        "answers_total": answers.count(),
        "relate_questions": relate_questions
    }, RequestContext(request))
예제 #15
0
    def save_model(self, request, user, form, change):
        super().save_model(request, user, form, change)

        if not (request.user.is_superuser or change):
            user.grant_permissions(request.user)
            user.grant_company_permissions()

        # Add company resources to the new user
        with silk_profile(name='Get user by company'):
            own_company_users = get_user_model().objects.filter(
                company=user.company_id)

            company_children = user.company.children
            external_company_users = get_user_model().objects.filter(
                company__in=company_children)

            users = own_company_users | external_company_users

            # Profiles with Admin Permissions Template
            if user.role == get_user_model().Role.ADMIN:
                user.bulk_grant_permissions('Admin Permissions Template',
                                            users)
예제 #16
0
def calculate_averages(request):
    """Kick off the various functions that will the average stats given a threshold"""
    items = range(100)
    with silk_profile(name='Recursive average (%d items)' % len(items)):
        recursive_average(items)
    with silk_profile(name='faster average (%d items)' % len(items)):
        faster_average(items)
    with silk_profile(name='standard library average (%d items)' % len(items)):
        statistics.mean(items)

    items = range(900)
    with silk_profile(name='Recursive average (%d items)' % len(items)):
        recursive_average(items)
    with silk_profile(name='faster average (%d items)' % len(items)):
        faster_average(items)
    with silk_profile(name='standard library average (%d items)' % len(items)):
        statistics.mean(items)

    items = range(10000)
    with silk_profile(name='faster average (%d items)' % len(items)):
        faster_average(items)
    with silk_profile(name='standard library average (%d items)' % len(items)):
        statistics.mean(items)
    return HttpResponse('Averages now calculated, to see how long it took go to <a href="/silk">/silk</a> url')
예제 #17
0
    def bulk_grant_permissions(self, template, users):
        # Grant permissions to a queryset of users
        with silk_profile(name='ADD resources to user'):

            if template == 'Admin Permissions Template':
                groups = Group.objects.filter(
                    user__in=users).exclude(name__contains='Template')
                self.groups.add(*groups)

                # -----------------
                # Allow the existing users to access this user instance
                read_permissions = Group.objects.get(
                    name=f"{self.username}: Read")
                read_permissions.user_set.add(*users)


        if template == 'Agent Permissions Template':
            read_permissions.user_set.add(*users)
            write_permissions.user_set.add(*users)
            write_permissions.user_set.add(*users)

        if template == 'Employee Permissions Template':
            read_permissions.user_set.add(*users)
            write_permissions.user_set.add(*users)
예제 #18
0
 def test_context_manager_no_request(self):
     DataCollector().configure()
     with silk_profile(name='test_profile'):
         sleep(0.1)
     self.assertFalse(DataCollector().profiles)
예제 #19
0
 def test_context_manager_request(self):
     DataCollector().configure(Request.objects.create(path='/to/somewhere'))
     with silk_profile(name='test_profile'):
         sleep(0.1)
     profile = list(DataCollector().profiles.values())[0]
     self.assertEqual(DataCollector().request, profile['request'])
예제 #20
0
 def setUpClass(cls):
     r = Request.objects.create()
     DataCollector().configure(r)
     with silk_profile(name='test_profile'):
         sleep(0.1)
예제 #21
0
 def setUpClass(cls):
     super(TestProfilertContextManager, cls).setUpClass()
     r = Request.objects.create()
     DataCollector().configure(r)
     with silk_profile(name='test_profile'):
         sleep(0.1)
예제 #22
0
 def test_context_manager_no_request(self):
     DataCollector().configure()
     with silk_profile(name='test_profile'):
         sleep(0.1)
     self.assertFalse(DataCollector().profiles)
예제 #23
0
 def test_context_manager_request(self):
     DataCollector().configure(Request.objects.create(path="/to/somewhere"))
     with silk_profile(name="test_profile"):
         sleep(0.1)
     profile = list(DataCollector().profiles)[0]
     self.assertEqual(DataCollector().request, profile["request"])
예제 #24
0
 def setUpClass(cls):
     super(TestProfilertContextManager, cls).setUpClass()
     r = Request.objects.create()
     DataCollector().configure(r)
     with silk_profile(name='test_profile'):
         sleep(0.1)
예제 #25
0
def search(request):
    keywords = request.GET.get("keywords", None)
    page = int(request.GET.get("page", 1))
    if page < 1:
        page = 1
    elif page > PAGE_COUNTER:
        page = PAGE_COUNTER
    tags = set()
    results = []
    total = 0
    if keywords and keywords.strip():
        keywords = strip_tags(keywords.strip()).replace("-", " ")
        with silk_profile(name='Search By Keywords #%s' % keywords):
            datas = es.search(index='it',
                              doc_type='stackoverflow_questions',
                              body={
                                  "query": {
                                      "filtered": {
                                          "query": {
                                              "match": {
                                                  "title": {
                                                      "query":
                                                      keywords.lower(),
                                                      "minimum_should_match":
                                                      "75%",
                                                      "operator": "and"
                                                  }
                                              }
                                          }
                                      }
                                  },
                                  "from": (page - 1) * PAGE_SIZE,
                                  "size": PAGE_SIZE,
                                  'sort': [{
                                      'creation_date': {
                                          'order': 'desc'
                                      }
                                  }],
                                  "aggs": {
                                      "tags_stats": {
                                          "terms": {
                                              "field": "tags",
                                              "size": 50,
                                              "min_doc_count": 1,
                                              "order": {
                                                  "_count": "desc"
                                              }
                                          }
                                      }
                                  }
                              })
            hits, took, tags = datas["hits"], datas["took"], datas[
                "aggregations"]["tags_stats"]["buckets"]
            total = hits["total"]
            for h in hits["hits"]:
                results.append({
                    "id":
                    h["_id"],
                    "body":
                    h["_source"]["body"],
                    "title":
                    h["_source"]["title"],
                    "tags":
                    h["_source"]["tags"],
                    "created":
                    time.strftime(
                        '%Y-%m-%d',
                        time.localtime(h["_source"]["creation_date"]))
                })
    total_page = total / PAGE_SIZE + 2
    if total_page < PAGE_COUNTER:
        page_list = range(1, total_page)
    elif page + PAGE_COUNTER < total_page:
        page_list = range(1, page + PAGE_COUNTER)
    else:
        page_list = range(1, total_page)
    if len(page_list) > PAGE_COUNTER:
        page_list = page_list[:PAGE_COUNTER]
    next_page = 0 if int(page) + 1 >= total_page else page + 1
    return render_to_response(
        'list.html', {
            "results": results,
            "total": total,
            "page": page,
            "keywords": keywords,
            "page_list": page_list,
            "next_page": next_page,
            "tags": tags
        }, RequestContext(request))
예제 #26
0
def blog_list(request):
    keywords = request.GET.get("keywords", None)
    page = int(request.GET.get("page", 1))
    if page < 1:
        page = 1
    elif page>PAGE_COUNTER:
        page=PAGE_COUNTER
    tags=set()
    results = []
    total = 0
    if keywords and keywords.strip():
        keywords = strip_tags(keywords.strip()).replace("-"," ")
        with silk_profile(name='Search By Keywords #%s' % keywords):
            datas = es.search(index='it', doc_type='stackoverflow_questions', body={
                "query": {
                    "filtered": {
                        "query": {
                            "match": {
                                "title": {
                                    "query":keywords.lower(),
                                    "minimum_should_match": "75%",
                                    "operator": "and"
                                }
                            }
                        }
                    }
                },
                "from": (page-1) * PAGE_SIZE,
                "size": PAGE_SIZE,
                'sort': [
                    {'creation_date': {'order': 'desc'}}
                ],
                "aggs": {
                    "tags_stats": {
                        "terms": {
                            "field": "tags",
                            "size": 50,
                            "min_doc_count": 1,
                            "order": {"_count": "desc"}
                        }
                    }
                }
            })
            hits, took, tags = datas["hits"], datas["took"], datas["aggregations"]["tags_stats"]["buckets"]
            total = hits["total"]
            for h in hits["hits"]:
                results.append({
                    "id": h["_id"],
                    "body": h["_source"]["body"],
                    "title": h["_source"]["title"],
                    "tags": h["_source"]["tags"],
                    "created": time.strftime('%Y-%m-%d',  time.localtime(h["_source"]["creation_date"]))
                })
    total_page = total / PAGE_SIZE + 2
    if total_page < PAGE_COUNTER:
        page_list = range(1, total_page)
    elif page + PAGE_COUNTER < total_page:
        page_list = range(1, page + PAGE_COUNTER)
    else:
        page_list = range(1, total_page)
    if len(page_list)>PAGE_COUNTER:
        page_list=page_list[:PAGE_COUNTER]
    next_page = 0 if int(page) + 1 >= total_page else page + 1
    return render_to_response('blog/list.html', {"results": results,
                                            "total": total,
                                            "page": page,
                                            "keywords": keywords,
                                            "page_list":page_list,
                                            "next_page":next_page,
                                            "tags":tags
                                            },
                              RequestContext(request))
예제 #27
0
 def setUpClass(cls):
     r = Request.objects.create()
     DataCollector().configure(r)
     with silk_profile(name='test_profile'):
         sleep(0.1)