Exemplo n.º 1
0
 def get(self, request):
     u"""
     获取ship数据
     ---
     parameters:
         - name: variety
           description: 品目
           paramType: query
           required: true
         - name: price_code
           description: 数据编号
           paramType: query
           required: true
         - name: contract
           description: 合约
           paramType: query
           required: false
         - name: start
           description: 开始日期
           paramType: query
           required: false
         - name: end
           description: 结束日期
           paramType: query
           required: false
     """
     query = request.query_params.dict()
     variety = query.get('variety')
     price_code = query.get('price_code')
     contract = query.get('contract') or 'main_contract'
     start = query.get('start')
     end = query.get('end')
     data_obj = Ship(variety, price_code, serial=contract, start=start, end=end)
     return BackstageHTTPResponse(BackstageHTTPResponse.API_HTTP_CODE_NORMAL,
                                  data=data_obj.get_all(timestamp=True)).to_response()
Exemplo n.º 2
0
 def get(self, request):
     u"""
     批量获取编号数据
     ---
     parameters:
         - name: line[]
           description: 图表编号
           paramType: query
           required: true
     """
     lines = request.query_params.getlist('line[]')
     chart = DataChart()
     for line in lines:
         line_param = line.split('*')
         if len(line_param) != 2:
             continue
         data_code, table_name = line_param
         data_obj = TableData(data_code, table_name, limit=1, desc=True)
         chart.add_line(data_obj)
     data = chart.get_all()
     for item in data:
         item['time'] = item['line'][0][0] if len(item['line']) else None
         item['data'] = item['line'][0][1] if len(item['line']) else None
     return BackstageHTTPResponse(BackstageHTTPResponse.API_HTTP_CODE_NORMAL,
                                  data=data).to_response()
Exemplo n.º 3
0
 def get(self, request):
     u"""
     获取四大产品线资金流入流出
     ---
     """
     data = []
     categories = ((1, '有色'), (2, '黑色'), (3, '化工'), (4, '农产品'))
     cursor = connection.cursor()
     for category, name in categories:
         sql = (
             'select sum(a.flow_fund) as amount, a.date_time from alpha.day_kline as a '
             'left join alpha.chart_variety as b on a.contract=b.index_contract '
             'where b.category=%s group by a.date_time order by a.date_time desc limit 15'
         )
         cursor.execute(sql, (category, ))
         result = cursor.fetchall()
         result = [{
             'date': date,
             'amount': amount
         } for amount, date in result]
         result.reverse()
         data.append({'name': name, 'data': result})
     return BackstageHTTPResponse(
         BackstageHTTPResponse.API_HTTP_CODE_NORMAL,
         data=data,
         message='获取产品线资金流入流出成功').to_response()
Exemplo n.º 4
0
 def get(self, request):
     u"""
     获取编号数据
     ---
     parameters:
         - name: data_code
           description: 数据编号
           paramType: query
           required: true
         - name: table_name
           description: 数据所在表
           paramType: query
           required: false
         - name: start
           description: 开始日期
           paramType: query
           required: false
         - name: end
           description: 结束日期
           paramType: query
           required: false
     """
     query = request.query_params.dict()
     data_code = query.get('data_code')
     table_name = query.get('table_name')
     start = query.get('start')
     end = query.get('end')
     data_obj = TableData(data_code, table_name, start, end)
     return BackstageHTTPResponse(BackstageHTTPResponse.API_HTTP_CODE_NORMAL,
                                  data=data_obj.get_all(timestamp=True)).to_response()
Exemplo n.º 5
0
    def get(self, request):
        u"""
        获取侧边栏所有图
        ---

        parameters:
            - name: sidebar_id
              description: 侧边栏id
              paramType: query
              required: True
        """
        params = request.query_params.dict()
        sidebar_id = params['sidebar_id']
        sql = ('select a.id, a.chart_id, a.size, b.graph, b.compare, b.p_axis, b.s_axis '
               'from chart_sidebar_chart as a left join chart_chart as b on a.chart_id=b.id '
               'where a.sidebar_id=%s order by a.priority desc, a.id asc;')
        cursor = connection.cursor()
        cursor.execute(sql, sidebar_id)
        result = cursor.fetchall()
        charts = []
        for row in result:
            chart = {'id': row[0],
                     'chart_id': row[1],
                     'size': row[2],
                     'graph': row[3],
                     'compare': row[4],
                     'p_axis': row[5],
                     's_axis': row[6]}
            charts.append(chart)
        return BackstageHTTPResponse(BackstageHTTPResponse.API_HTTP_CODE_NORMAL,
                                     data={'charts': charts}, message='获取侧边栏图列表成功').to_response()
Exemplo n.º 6
0
 def get(self, request):
     u"""
     获取三大交易所历史资金流入流出
     ---
     """
     data = []
     exchanges = (('SHFE', '上期所'), ('DCE', '郑商所'), ('CZCE', '大商所'))
     cursor = connection.cursor()
     for exchange, name in exchanges:
         sql = (
             'SELECT sum(flow_fund) as amount, date_time FROM alpha.day_kline '
             'where exchange=%s and contract like "%%8888" group by date_time '
             'order by date_time desc limit 15;')
         cursor.execute(sql, (exchange, ))
         result = cursor.fetchall()
         result = [{
             'date': date,
             'amount': amount
         } for amount, date in result]
         result.reverse()
         data.append({'name': name, 'data': result})
     return BackstageHTTPResponse(
         BackstageHTTPResponse.API_HTTP_CODE_NORMAL,
         data=data,
         message='获取交易所资金变化成功').to_response()
Exemplo n.º 7
0
 def post(self, request):
     u"""
     公式测试执行
     ---
     parameters:
         - name: formula_id
           description: 交易所
           paramType: form
           required: True
         - name: variety_id
           description: 品种
           paramType: form
           required: True
     """
     params = self.request_data(request)
     formula_id = params['formula_id']
     variety_id = int(params['variety_id'])
     variety = get_object_or_404(VarietiesRecord, pk=variety_id)
     variety = variety.code
     formula = get_object_or_404(Formula, pk=formula_id)
     f = ShareFormula(id=formula.id, title=formula.title, formula=formula.formula,
                      description=formula.description, user_id=formula.user_id)
     env = FormulaEnv(id=formula_id, pre_data=DBPreProcess(varieties=variety))
     if variety_id not in f.varieties:
         log = "编号%s的公式适用品目%s, 不适用当前上下文中品目%s" % (f.id, f.varieties, env.content_variety)
         data = {'success': False, 'log': log}
         return BackstageHTTPResponse(BackstageHTTPResponse.API_HTTP_CODE_NORMAL,
                                      data=data, message='公式执行失败').to_response()
     else:
         executor = ApiFormulaExecutor(f, env)
         log = executor.run()
         data = {'success': executor.success,
                 'log': log,
                 'chart': executor.chart_data}
         return BackstageHTTPResponse(BackstageHTTPResponse.API_HTTP_CODE_NORMAL,
                                      data=data, message='公式执行成功').to_response()
Exemplo n.º 8
0
 def get(self, request):
     u"""
     获取所有品种
     ---
     """
     exchanges = Exchange.objects.all()
     varieties = Variety.objects.all()
     varieties_dict = {}
     for variety in varieties:
         varieties_dict.setdefault(variety.exchange, []).append({'variety': variety.variety,
                                                                 'variety_name': variety.variety_name})
     data = [{'exchange': exchange.exchange,
              'exchange_name': exchange.exchange_name,
              'varieties': varieties_dict.get(exchange.exchange, [])} for exchange in exchanges]
     return BackstageHTTPResponse(BackstageHTTPResponse.API_HTTP_CODE_NORMAL,
                                  data=data, message='获取品目列表成功').to_response()
Exemplo n.º 9
0
    def get(self, request):
        u"""
        获取品种侧边栏
        ---

        parameters:
            - name: variety
              description: 品种
              paramType: query
              required: True
        """
        params = request.query_params.dict()
        variety = params.get('variety')
        sidebars = Sidebar.objects.filter(variety=variety).order_by('-priority', 'id')
        data = [{'sidebar_id': sidebar.id, 'sidebar_name': sidebar.name} for sidebar in sidebars]
        return BackstageHTTPResponse(BackstageHTTPResponse.API_HTTP_CODE_NORMAL,
                                     data=data, message='获取侧边栏列表成功').to_response()
Exemplo n.º 10
0
    def delete(self, request):
        u"""
        删除侧边栏
        ---

        parameters:
            - name: sidebar_id
              description: 侧边栏id
              paramType: form
              required: True
        """
        data = self.request_data(request)
        sidebar_id = data['sidebar_id']
        sidebar = Sidebar.objects.get(pk=sidebar_id)
        sidebar.delete()
        return BackstageHTTPResponse(BackstageHTTPResponse.API_HTTP_CODE_NORMAL,
                                     data=data, message='删除侧边栏成功').to_response()
Exemplo n.º 11
0
    def post(self, request):
        u"""
        侧边栏列表排序
        ---

        parameters:
            - name: id_list
              description: 侧边栏id list
              paramType: form
              required: True
        """
        data = self.request_data(request)
        id_list = json.loads(data['id_list'])
        for index, sidebar_id in enumerate(id_list):
            priority = -(index + 1)
            sidebar = Sidebar.objects.get(pk=sidebar_id)
            sidebar.priority = priority
            sidebar.save()
        return BackstageHTTPResponse(BackstageHTTPResponse.API_HTTP_CODE_NORMAL,
                                     data=data, message='侧边栏排序成功').to_response()
Exemplo n.º 12
0
 def get(self, request):
     u"""
     批量获取编号数据
     ---
     parameters:
         - name: line[]
           description: 图表编号
           paramType: query
           required: true
     """
     lines = request.query_params.getlist('line[]')
     chart = DataChart()
     for line in lines:
         line_param = line.split('*')
         if len(line_param) != 4:
             continue
         data_code, table_name, start, end = line_param
         data_obj = TableData(data_code, table_name, start, end)
         chart.add_line(data_obj)
     return BackstageHTTPResponse(BackstageHTTPResponse.API_HTTP_CODE_NORMAL,
                                  data=chart.get_all()).to_response()
Exemplo n.º 13
0
    def post(self, request):
        u"""
        新增侧边栏
        ---

        parameters:
            - name: variety
              description: 品种
              paramType: form
              required: True
            - name: sidebar_name
              description: 侧边栏名称
              paramType: form
              required: True
        """
        data = self.request_data(request)
        variety = data['variety']
        name = data['sidebar_name']
        sidebar = Sidebar(variety=variety, name=name)
        sidebar.save()
        return BackstageHTTPResponse(BackstageHTTPResponse.API_HTTP_CODE_NORMAL,
                                     data=data, message='新增侧边栏成功').to_response()
Exemplo n.º 14
0
 def get(self, request):
     u"""
     获取编号数据
     ---
     parameters:
         - name: data_code
           description: 数据编号
           paramType: query
           required: true
         - name: table_name
           description: 数据所在表
           paramType: query
           required: false
     """
     query = request.query_params.dict()
     data_code = query.get('data_code')
     table_name = query.get('table_name')
     data_obj = TableData(data_code, table_name, limit=1, desc=True)
     data = data_obj.get_all(timestamp=True)
     data['time'] = data['line'][0][0] if len(data['line']) else None
     data['data'] = data['line'][0][1] if len(data['line']) else None
     return BackstageHTTPResponse(BackstageHTTPResponse.API_HTTP_CODE_NORMAL,
                                  data=data).to_response()
Exemplo n.º 15
0
    def put(self, request):
        u"""
        修改侧边栏
        ---

        parameters:
            - name: id
              description: 侧边栏id
              paramType: form
              required: true
            - name: name
              description: 侧边栏名称
              paramType: form
              required: true
        """
        data = self.request_data(request)
        sidebar_id = data['id']
        sidebar_name = data['name']
        sidebar = Sidebar.objects.get(pk=sidebar_id)
        sidebar.name = sidebar_name
        sidebar.save()
        return BackstageHTTPResponse(BackstageHTTPResponse.API_HTTP_CODE_NORMAL,
                                     data=data, message='修改侧边栏成功').to_response()
Exemplo n.º 16
0
    def get(self, request):
        u"""
        批量获取ship数据
        ---

        parameters:
            - name: line[]
              description: ship编号
              paramType: query
              required: true
        """
        lines = request.query_params.getlist('line[]')
        chart = DataChart()
        for line in lines:
            line_param = line.split('*')
            if len(line_param) != 5:
                continue
            variety, price_code, contract, start, end = line_param
            contract = contract or 'main_contract'
            data_obj = Ship(variety, price_code, serial=contract, start=start, end=end)
            chart.add_line(data_obj)
        return BackstageHTTPResponse(BackstageHTTPResponse.API_HTTP_CODE_NORMAL,
                                     data=chart.get_all()).to_response()
Exemplo n.º 17
0
 def get(self, request):
     u"""
     获取图信息(包括曲线历史数据)
     ---
     parameters:
         - name: id
           description: 图id
           paramType: query
           required: True
     """
     data = request.query_params.dict()
     chart_id = data['id']
     chart = Chart.objects.get(pk=chart_id)
     chart_lines = ChartLine.objects.filter(chart_id=chart_id)
     data = []
     if chart.compare == Chart.COMPARE_YEAR:
         date, start, end = [], None, None
         for i in range(3):
             year = when.future(years=-i).strftime('%Y-01-01')
             if start:
                 end = start - datetime.timedelta(days=1)
             else:
                 end = None
             start = datetime.datetime.strptime(year, '%Y-%m-%d').date()
             date.insert(0, (start, end))
         for chart_line in chart_lines:
             line_data = ProxyData(chart_line.data_code, chart_line.table_name)
             year_data = []
             for start, end in date:
                 line_data.start = start
                 line_data.end = end
                 year_data.append({'year': start.strftime('%Y'),
                                   'data': line_data.get_list(timestamp=True)})
             data.append({'title': line_data.title,
                          'unit': line_data.unit,
                          'line': year_data,
                          'line_type': line_data.table,
                          'count': line_data.count(),
                          'data_code': line_data.data_code})
     elif chart.compare == Chart.COMPARE_MONTH:
         date, start, end = [], None, None
         for i in range(3):
             month = when.future(months=-i).strftime('%Y-%m-01')
             if start:
                 end = start - datetime.timedelta(days=1)
             else:
                 end = None
             start = datetime.datetime.strptime(month, '%Y-%m-%d').date()
             date.insert(0, (start, end))
         for chart_line in chart_lines:
             line_data = ProxyData(chart_line.data_code, chart_line.table_name)
             month_data = []
             for start, end in date:
                 line_data.start = start
                 line_data.end = end
                 month_data.append({'month': start.strftime('%Y-%m'),
                                    'data': line_data.get_list(timestamp=True)})
             data.append({'title': line_data.title,
                          'unit': line_data.unit,
                          'line': month_data,
                          'line_type': line_data.table,
                          'count': line_data.count(),
                          'data_code': line_data.data_code})
     else:
         six_month_ago = when.future(months=-6).date()
         for chart_line in chart_lines:
             line_data = ProxyData(chart_line.data_code, chart_line.table_name, start=six_month_ago)
             line_data = line_data.get_all(timestamp=True)
             line_data['title'] = chart_line.line_name
             data.append(line_data)
     return BackstageHTTPResponse(BackstageHTTPResponse.API_HTTP_CODE_NORMAL,
                                  data=data,
                                  message='获取图数据成功').to_response()
Exemplo n.º 18
0
    def get(self, request):
        u"""
        获取今日资金流入流出量最大的几个品种的历史资金沉淀
        ---

        parameters:
            - name: in_limit
              description: 品种数量
              paramType: query
              required: false
            - name: out_limit
              description: 品种数量
              paramType: query
              required: false
        """
        today = datetime.date.today()
        params = request.query_params.dict()
        # 流出
        out_limit = int(
            params.get('out_limit')) if params.get('out_limit') else 5
        sql = (
            'select a.variety, a.index_contract, a.variety_name from chart_variety as a '
            'left join day_kline as b on a.index_contract=b.contract where b.flow_fund<=0 '
            'and b.date_time=%s order by b.flow_fund asc limit %s')
        cursor = connection.cursor()
        cursor.execute(sql, (today, out_limit))
        flow_out_data = cursor.fetchall()
        flow_out = []
        for variety, contract, name in flow_out_data:
            sql = ('select sendimentary_money, date_time from day_kline '
                   'where contract=%s order by date_time desc limit 10')
            cursor.execute(sql, (contract, ))
            variety_data = cursor.fetchall()
            variety_data = [{
                'date': date,
                'amount': amount
            } for amount, date in variety_data]
            variety_data.reverse()
            flow_out.append({'name': name, 'data': variety_data})
        # 流入
        in_limit = int(params.get('in_limit')) if params.get('in_limit') else 5
        sql = (
            'select a.variety, a.index_contract, a.variety_name from chart_variety as a '
            'left join day_kline as b on a.index_contract=b.contract where b.flow_fund>=0 '
            'and b.date_time=%s order by b.flow_fund desc limit %s')
        cursor = connection.cursor()
        cursor.execute(sql, (today, in_limit))
        flow_in_data = cursor.fetchall()
        flow_in = []
        for variety, contract, name in flow_in_data:
            sql = ('select sendimentary_money, date_time from day_kline '
                   'where contract=%s order by date_time desc limit 10')
            cursor.execute(sql, (contract, ))
            variety_data = cursor.fetchall()
            variety_data = [{
                'date': date,
                'amount': amount
            } for amount, date in variety_data]
            variety_data.reverse()
            flow_in.append({'name': name, 'data': variety_data})

        return BackstageHTTPResponse(
            BackstageHTTPResponse.API_HTTP_CODE_NORMAL,
            data={
                'flow_in': flow_in,
                'flow_out': flow_out
            },
            message='获取历史资金流入流出成功').to_response()
Exemplo n.º 19
0
 def get(self, request):
     u"""
     获取品种资金沉淀
     ---
     parameters:
         - name: in_limit
           description: 品种数量
           paramType: query
           required: false
         - name: out_limit
           description: 品种数量
           paramType: query
           required: false
         - name: pre_limit
           description: 品种数量
           paramType: query
           required: false
     """
     today = datetime.date.today()
     params = request.query_params.dict()
     # 资金沉淀
     pre_limit = int(
         params.get('pre_limit')) if params.get('pre_limit') else 7
     sql = (
         'select a.variety, b.sendimentary_money, a.variety_name from chart_variety as a '
         'left join day_kline as b on a.index_contract=b.contract where b.date_time=%s '
         'order by b.sendimentary_money desc limit %s')
     cursor = connection.cursor()
     cursor.execute(sql, (today, pre_limit))
     pre_data = cursor.fetchall()
     pre_data = [{
         'variety': variety,
         'name': name,
         'amount': amount
     } for variety, amount, name in pre_data]
     # 流出
     out_limit = int(
         params.get('out_limit')) if params.get('out_limit') else 5
     sql = (
         'select a.variety, b.flow_fund, a.variety_name from chart_variety as a '
         'left join day_kline as b on a.index_contract=b.contract where b.flow_fund<=0 '
         'and b.date_time=%s order by b.flow_fund asc limit %s')
     cursor = connection.cursor()
     cursor.execute(sql, (today, out_limit))
     flow_out_data = cursor.fetchall()
     flow_out_data = [{
         'variety': variety,
         'name': name,
         'amount': amount
     } for variety, amount, name in flow_out_data]
     # 流入
     in_limit = int(params.get('in_limit')) if params.get('in_limit') else 5
     sql = (
         'select a.variety, b.flow_fund, a.variety_name from chart_variety as a '
         'left join day_kline as b on a.index_contract=b.contract where b.flow_fund>=0 '
         'and b.date_time=%s order by b.flow_fund desc limit %s')
     cursor = connection.cursor()
     cursor.execute(sql, (today, in_limit))
     flow_in_data = cursor.fetchall()
     flow_in_data = [{
         'variety': variety,
         'name': name,
         'amount': amount
     } for variety, amount, name in flow_in_data]
     return BackstageHTTPResponse(
         BackstageHTTPResponse.API_HTTP_CODE_NORMAL,
         data={
             'pre': pre_data,
             'flow_in': flow_in_data,
             'flow_out': flow_out_data
         },
         message='获取前几个品种的资金沉淀和流入流出').to_response()