# Path: blog/documents.py
# class ElapsedTimeDocument(Document):
#     url = Keyword()
#     time_taken = Long()
#     log_datetime = Date()
#     ip = Keyword()
#     geoip = Object(GeoIp, required=False)
#     useragent = Object(UserAgent, required=False)
# 
#     class Index:
#         name = 'performance'
#         settings = {
#             "number_of_shards": 1,
#             "number_of_replicas": 0
#         }
# 
#     class Meta:
#         doc_type = 'ElapsedTime'
# 
# class ArticleDocumentManager():
# 
#     def __init__(self):
#         self.create_index()
# 
#     def create_index(self):
#         ArticleDocument.init()
# 
#     def delete_index(self):
#         from elasticsearch import Elasticsearch
#         es = Elasticsearch(settings.ELASTICSEARCH_DSL['default']['hosts'])
#         es.indices.delete(index='blog', ignore=[400, 404])
# 
#     def convert_to_doc(self, articles):
#         return [
#             ArticleDocument(
#                 meta={
#                     'id': article.id},
#                 body=article.body,
#                 title=article.title,
#                 author={
#                     'nikename': article.author.username,
#                     'id': article.author.id},
#                 category={
#                     'name': article.category.name,
#                     'id': article.category.id},
#                 tags=[
#                     {
#                         'name': t.name,
#                         'id': t.id} for t in article.tags.all()],
#                 pub_time=article.pub_time,
#                 status=article.status,
#                 comment_status=article.comment_status,
#                 type=article.type,
#                 views=article.views,
#                 article_order=article.article_order) for article in articles]
# 
#     def rebuild(self, articles=None):
#         ArticleDocument.init()
#         articles = articles if articles else Article.objects.all()
#         docs = self.convert_to_doc(articles)
#         for doc in docs:
#             doc.save()
# 
#     def update_docs(self, docs):
#         for doc in docs:
#             doc.save()
# 
# class ElaspedTimeDocumentManager:
#     @staticmethod
#     def build_index():
#         from elasticsearch import Elasticsearch
#         client = Elasticsearch(settings.ELASTICSEARCH_DSL['default']['hosts'])
#         res = client.indices.exists(index="performance")
#         if not res:
#             ElapsedTimeDocument.init()
# 
#     @staticmethod
#     def delete_index():
#         from elasticsearch import Elasticsearch
#         es = Elasticsearch(settings.ELASTICSEARCH_DSL['default']['hosts'])
#         es.indices.delete(index='performance', ignore=[400, 404])
# 
#     @staticmethod
#     def create(url, time_taken, log_datetime, useragent, ip):
#         ElaspedTimeDocumentManager.build_index()
#         ua = UserAgent()
#         ua.browser = UserAgentBrowser()
#         ua.browser.Family = useragent.browser.family
#         ua.browser.Version = useragent.browser.version_string
# 
#         ua.os = UserAgentOS()
#         ua.os.Family = useragent.os.family
#         ua.os.Version = useragent.os.version_string
# 
#         ua.device = UserAgentDevice()
#         ua.device.Family = useragent.device.family
#         ua.device.Brand = useragent.device.brand
#         ua.device.Model = useragent.device.model
#         ua.string = useragent.ua_string
#         ua.is_bot = useragent.is_bot
# 
#         doc = ElapsedTimeDocument(
#             meta={
#                 'id': int(
#                     round(
#                         time.time() *
#                         1000))
#             },
#             url=url,
#             time_taken=time_taken,
#             log_datetime=log_datetime,
#             useragent=ua, ip=ip)
#         doc.save(pipeline="geoip")
# 
# ELASTICSEARCH_ENABLED = hasattr(settings, 'ELASTICSEARCH_DSL')


from django.core.management.base import BaseCommand
from blog.documents import ElapsedTimeDocument, ArticleDocumentManager, ElaspedTimeDocumentManager, \
    ELASTICSEARCH_ENABLED



# TODO 参数化
class Command(BaseCommand):
    help = 'build search index'

    def handle(self, *args, **options):
        if ELASTICSEARCH_ENABLED:
            ElaspedTimeDocumentManager.build_index()
            manager = ElapsedTimeDocument()

# Path: blog/documents.py
# class ElapsedTimeDocument(Document):
#     url = Keyword()
#     time_taken = Long()
#     log_datetime = Date()
#     ip = Keyword()
#     geoip = Object(GeoIp, required=False)
#     useragent = Object(UserAgent, required=False)
# 
#     class Index:
#         name = 'performance'
#         settings = {
#             "number_of_shards": 1,
#             "number_of_replicas": 0
#         }
# 
#     class Meta:
#         doc_type = 'ElapsedTime'
# 
# class ArticleDocumentManager():
# 
#     def __init__(self):
#         self.create_index()
# 
#     def create_index(self):
#         ArticleDocument.init()
# 
#     def delete_index(self):
#         from elasticsearch import Elasticsearch
#         es = Elasticsearch(settings.ELASTICSEARCH_DSL['default']['hosts'])
#         es.indices.delete(index='blog', ignore=[400, 404])
# 
#     def convert_to_doc(self, articles):
#         return [
#             ArticleDocument(
#                 meta={
#                     'id': article.id},
#                 body=article.body,
#                 title=article.title,
#                 author={
#                     'nikename': article.author.username,
#                     'id': article.author.id},
#                 category={
#                     'name': article.category.name,
#                     'id': article.category.id},
#                 tags=[
#                     {
#                         'name': t.name,
#                         'id': t.id} for t in article.tags.all()],
#                 pub_time=article.pub_time,
#                 status=article.status,
#                 comment_status=article.comment_status,
#                 type=article.type,
#                 views=article.views,
#                 article_order=article.article_order) for article in articles]
# 
#     def rebuild(self, articles=None):
#         ArticleDocument.init()
#         articles = articles if articles else Article.objects.all()
#         docs = self.convert_to_doc(articles)
#         for doc in docs:
#             doc.save()
# 
#     def update_docs(self, docs):
#         for doc in docs:
#             doc.save()
# 
# class ElaspedTimeDocumentManager:
#     @staticmethod
#     def build_index():
#         from elasticsearch import Elasticsearch
#         client = Elasticsearch(settings.ELASTICSEARCH_DSL['default']['hosts'])
#         res = client.indices.exists(index="performance")
#         if not res:
#             ElapsedTimeDocument.init()
# 
#     @staticmethod
#     def delete_index():
#         from elasticsearch import Elasticsearch
#         es = Elasticsearch(settings.ELASTICSEARCH_DSL['default']['hosts'])
#         es.indices.delete(index='performance', ignore=[400, 404])
# 
#     @staticmethod
#     def create(url, time_taken, log_datetime, useragent, ip):
#         ElaspedTimeDocumentManager.build_index()
#         ua = UserAgent()
#         ua.browser = UserAgentBrowser()
#         ua.browser.Family = useragent.browser.family
#         ua.browser.Version = useragent.browser.version_string
# 
#         ua.os = UserAgentOS()
#         ua.os.Family = useragent.os.family
#         ua.os.Version = useragent.os.version_string
# 
#         ua.device = UserAgentDevice()
#         ua.device.Family = useragent.device.family
#         ua.device.Brand = useragent.device.brand
#         ua.device.Model = useragent.device.model
#         ua.string = useragent.ua_string
#         ua.is_bot = useragent.is_bot
# 
#         doc = ElapsedTimeDocument(
#             meta={
#                 'id': int(
#                     round(
#                         time.time() *
#                         1000))
#             },
#             url=url,
#             time_taken=time_taken,
#             log_datetime=log_datetime,
#             useragent=ua, ip=ip)
#         doc.save(pipeline="geoip")
# 
# ELASTICSEARCH_ENABLED = hasattr(settings, 'ELASTICSEARCH_DSL')


# Path: blog/management/commands/build_index.py
from django.core.management.base import BaseCommand
from blog.documents import ElapsedTimeDocument, ArticleDocumentManager, ElaspedTimeDocumentManager, \
    ELASTICSEARCH_ENABLED




# TODO 参数化
class Command(BaseCommand):
    help = 'build search index'

    def handle(self, *args, **options):
        if ELASTICSEARCH_ENABLED:
            ElaspedTimeDocumentManager.build_index()
            manager = ElapsedTimeDocument()

            manager.init()