예제 #1
0
def test_insert2():
    cass = cassandramodel.CassandraBase()
    cass.connect()
    config = settings.Settings()
    data_path = config.data_path
    p = parser.Parser(data_path)
    cass.insert_batch2(p)
예제 #2
0
 def GET_daterange(self, args):
     qs = urlparse.parse_qs(web.ctx.query[1:])
     if 'term' not in qs:
         return "Error"
     term = qs['term'][0]
     cass = cassandramodel.CassandraBase()
     cass.connect(self.GET_seed())
     results = cass.get_date_range_by_sym(term)
     return json.dumps(results)
예제 #3
0
 def GET_exists(self, args):
     qs = urlparse.parse_qs(web.ctx.query[1:])
     print "sym exists: %s" % (web.ctx.query[1:])
     if 'symbol' not in qs:
         return "Error"
     term = qs['symbol'][0]
     cass = cassandramodel.CassandraBase()
     cass.connect(self.GET_seed())
     results = cass.sym_exists(term)
     return json.dumps(results)
예제 #4
0
    def POST_query(self, args):
        form = request()
        if not form.validates():
            return "Failure. Did you select an option for all the fields?"
        sym = form['symbol'].value
        start_string = form['startdate'].value
        end_string = form['enddate'].value

        date_format = "%Y-%m-%d"
        start = datetime.strptime(start_string,
                                  date_format).strftime("%Y-%m-%d")
        end = datetime.strptime(end_string, date_format).strftime("%Y-%m-%d")

        cass = cassandramodel.CassandraBase()
        cass.connect(self.GET_seed())

        start_time = time.time()
        records = cass.get_by_sym_range2(sym, start, end)
        if len(records) == 0:
            message = "Zero records returned. Perhaps the date range is incorrect?"
            return render.error(message)
        records_unsorted = []
        print "number records: %s" % (len(records))
        for r in records:
            temp = r[1]
            temp['date'] = datetime.strptime(str(temp['date']), "%Y-%m-%d")
            records_unsorted.append(temp)

        records_processed = sorted(records_unsorted, key=lambda k: k['date'])
        elapsed_time = (time.time() - start_time)

        y_max = 0.0
        y_min = 0.0
        data = []
        for q in records_processed:
            temp = float(q['price_adj_close'])
            data.append(temp)
            if temp > y_max:
                y_max = temp

        y_min = y_max

        for d in records_processed:
            temp = float(d['price_adj_close'])
            if temp < y_min:
                y_min = temp

        difference = float(y_max - y_min)
        difference = float(difference / 2)

        y_min_foo = y_min - difference
        y_max_foo = y_max + difference

        if y_min_foo < 0:
            y_min_foo = 0

        chart = SimpleLineChart(1000, 300, y_range=[y_min_foo, y_max_foo])

        chart.add_data(data)
        chart.set_colours(['0000FF'])
        #	chart.fill_linear_stripes(Chart.CHART, 0, 'CCCCCC', 0.1, 'FFFFFF', 0.2)
        #	chart.set_grid(0, 25, 5, 5)

        #	y_max_output = y_max + difference
        #	left_axis = range(y_min_foo, y_max_foo, 1.00)
        #	left_axis[0] = y_min

        left_axis = []
        label = y_min_foo
        delta_y = 1.00
        derp = float(y_max_foo - y_min_foo)
        #	if derp > 15.0:
        delta_y = derp / (10.00)

        while y_min_foo < y_max_foo:
            left_axis.append(y_min_foo)
            y_min_foo = y_min_foo + delta_y

        if len(left_axis) < 10:
            left_axis.append(y_min_foo)

        lines = len(left_axis) - 1
        chart.set_grid(0, lines, 1, 1)

        x_labels = []

        for t in records_processed:
            label = (self.getMonth(t['date'].month), t['date'].year)
            if not label in x_labels:
                x_labels.append(label)

        chart.set_axis_labels(Axis.LEFT, left_axis)
        chart.set_axis_labels(Axis.BOTTOM, x_labels)
        list_len = float(len(x_labels))
        top = float(1)
        stripe_len = float(top / list_len)
        chart.fill_linear_stripes(Chart.CHART, 0, 'CCCCCC', stripe_len,
                                  'FFFFFF', stripe_len)
        imgURL = chart.get_url()

        return render.results(sym, records_processed, elapsed_time, imgURL)