예제 #1
0
    def test_add_subtract(self):
        '''Test add and subtract on some subclasses'''

        bar = vincent.Bar()
        area = vincent.Area()

        area + ({
            'value': 'basis'
        }, 'marks', 0, 'properties', 'enter', 'interpolate')
        bar + ('red', 'marks', 0, 'properties', 'hover', 'fill', 'value')

        assert area.marks[0]['properties']['enter'].has_key('interpolate')
        assert bar.marks[0]['properties']['hover']['fill']['value'] == 'red'

        bar - ('domain', 'scales', 1)
        bar -= ('name', 'scales', 1)
        area - ('scale', 'axes', 0)
        area -= ('type', 'axes', 1)

        assert bar.scales[1] == {'nice': True, 'range': 'height'}
        assert area.axes == [{'type': 'x'}, {'scale': 'y'}]
예제 #2
0
def api_score_timeline(player_id):
    score_series = common.scores.get_score_timeline(
        player_id,
        int(time.time()) - SECONDS_IN_3_WEEKS)
    vis = vincent.Area()
    vis.tabular_data(score_series, axis_time='day')
    vis += ({'labels': {'angle': {'value': 25}}}, 'axes', 0, 'properties')
    vis += ({'value': 22}, 'axes', 0, 'properties', 'labels', 'dx')
    vis.update_vis(padding={'bottom': 50, 'left': 60, 'right': 40, 'top': 10})
    vis.update_vis(width=700)

    # here we go..
    # determine the bottom limit - pad below by a bit, and truncate to mult of 10
    domain_min = round((score_series.min() - 10) / 10.0) * 10
    vis += (False, 'scales', 1, 'zero')
    vis += (domain_min, 'scales', 1, 'domainMin')
    vis += (domain_min, 'marks', 0, 'properties', 'enter', 'y2', 'value')

    # colors..
    # remove the hover color change, it is stupid
    vis -= ('hover', 'marks', 0, 'properties')
    # and change the main color to red because I wanna
    vis += ({'value': '#c1251f'}, 'marks', 0, 'properties', 'update', 'fill')
    return json.dumps(vis.vega)
예제 #3
0
    def __email_summary(self):
        email_interval = self.__config['email']['interval']
        email_subject = '服务器监控数据'

        while True:
            time.sleep(email_interval)

            now = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
            stat_data = {
                'cpu_stat': {
                    'used_percent': [],
                    'created_at': []
                },
                'mem_stat': {
                    'used_percent': [],
                    'created_at': []
                }
            }
            for table_name in stat_data.keys():
                for_select = 'SELECT used_percent, created_at FROM %s WHERE created_at < "%s" ORDER BY created_at' \
                         % (table_name, now)
                for row in self.__db.query(for_select):
                    stat_data[table_name]['used_percent'].append(row[0])
                    stat_data[table_name]['created_at'].append(row[1])
                for_delete = 'DELETE FROM %s WHERE created_at < "%s"' % (
                    table_name, now)
                self.__db.execute(for_delete)

            # cpu
            cpu_stat_data = self.__data_preprocess(stat_data['cpu_stat'])
            # memory
            mem_stat_data = self.__data_preprocess(stat_data['mem_stat'])

            cpu_data_count = len(cpu_stat_data['created_at'])
            mem_data_count = len(mem_stat_data['created_at'])

            if self.__config['email_type'] == 'advanced':
                # 生成绘图JSON数据的细节还得仔细研究研究
                for index in xrange(cpu_data_count):
                    cpu_stat_data['created_at'][index] = datetime.strptime(
                        cpu_stat_data['created_at'][index],
                        '%Y-%m-%d %H:%M:%S')
                series = pandas.Series(cpu_stat_data['used_percent'],
                                       index=cpu_stat_data['created_at'])
                cpu_graph = vincent.Area(series)
                cpu_graph.axis_titles(x=u'Time', y=u'Usage (%)')
                ax = AxisProperties(labels=PropertySet(angle=ValueRef(
                    value=-30)))
                cpu_graph.axes[0].properties = ax
                cpu_graph_json = cpu_graph.to_json()

                for index in xrange(mem_data_count):
                    mem_stat_data['created_at'][index] = datetime.strptime(
                        mem_stat_data['created_at'][index],
                        '%Y-%m-%d %H:%M:%S')
                series = pandas.Series(mem_stat_data['used_percent'],
                                       index=mem_stat_data['created_at'])
                mem_graph = vincent.Area(series)
                mem_graph.axis_titles(x=u'Time', y=u'Usage (%)')
                ax = AxisProperties(labels=PropertySet(angle=ValueRef(
                    value=-30)))
                mem_graph.axes[0].properties = ax
                mem_graph_json = mem_graph.to_json()

                email_content = self.__render_content(
                    template_name='templates/monitor_stat.html',
                    data={
                        'cpu_stat': {
                            'data': cpu_graph_json,
                            'target_file_name': 'cpu_graph.png'
                        },
                        'mem_stat': {
                            'data': mem_graph_json,
                            'target_file_name': 'mem_graph.png'
                        }
                    })
                if email_content is None:
                    print u'模板渲染失败!'
                    break
                print self.__email_it(subject=email_subject,
                                      content=email_content,
                                      attach_files=[{
                                          'file_name': 'cpu_graph.png',
                                          'file_id': 'cpu_stat'
                                      }, {
                                          'file_name': 'mem_graph.png',
                                          'file_id': 'mem_stat'
                                      }])
            else:
                # 娶最大的5个
                max_n = 5

                cpu_data_tuples = [(cpu_stat_data['created_at'][index],
                                    cpu_stat_data['used_percent'][index])
                                   for index in xrange(cpu_data_count)]
                cpu_data_sorted = sorted(cpu_data_tuples,
                                         key=lambda item: item[1])
                cpu_sorted_max = []
                if cpu_data_count >= max_n:
                    cpu_sorted_max.extend(cpu_data_sorted[0 - max_n:])

                mem_data_tuples = [(mem_stat_data['created_at'][index],
                                    mem_stat_data['used_percent'][index])
                                   for index in xrange(mem_data_count)]
                mem_data_sorted = sorted(mem_data_tuples,
                                         key=lambda item: item[1])
                mem_sorted_max = []
                if mem_data_count >= max_n:
                    mem_sorted_max.extend(mem_data_sorted[0 - max_n:])
                email_content = self.__render_content(
                    template_name='templates/monitor_stat.html',
                    data={
                        'max_n': max_n,
                        'cpu_stat': {
                            'max_n_data': cpu_sorted_max
                        },
                        'mem_stat': {
                            'max_n_data': mem_sorted_max
                        }
                    })
                if email_content is None:
                    print u'模板渲染失败!'
                    break
                print self.__email_it(subject=email_subject,
                                      content=email_content)
예제 #4
0
# -*- coding: utf-8 -*-
'''
Builds a Vega grammar specification from vincent.Area()
'''

import vincent
import random

vis = vincent.Area()
vis.tabular_data([random.randint(10, 100) for x in range(0, 16, 1)])

#Generate both the Vega JSON and a data JSON.
path = r'/vega.json'
vis.to_json(path, split_data=True, html=True)

#Lets add a data interpolation parameter and resave the JSON
vis + ({'value': 'basis'}, 'marks', 0, 'properties', 'enter', 'interpolate')
vis.to_json(path, split_data=True, html=True)
예제 #5
0
import pandas as p
import vincent
from pandas.tseries.resample import TimeGrouper
from pandas.tseries.offsets import DateOffset

tweets = p.read_csv('./tweets.csv')

tweets['created_at'] = p.to_datetime(p.Series(tweets['created_at']))

# set index to 'created_at'
tweets.set_index('created_at', drop=False, inplace=True)
tweets.index = tweets.index.tz_localize('GMT').tz_convert('EST')

# convert to 12 hour format
tweets.index = tweets.index - DateOffset(hours=12)

# created_at index is formatted to per minute
tweets_pm = tweets['created_at'].resample('1t', how='count')

# create time series graph via Vincent
vincent.core.initialize_notebook()
area = vincent.Area(tweets_pm)
area.colors(brew='Spectral')
area.display()
예제 #6
0
# <codecell>

price['YHOO']

# <codecell>

line = vincent.Line(price[['FB', 'YHOO']])
line.axis_titles(x='Date', y='Price')
line.legend(title='FB vs YHOO')
line.width = 600
line.height = 400
line.display()

# <codecell>

area = vincent.Area(list_data)
area.width = 600
area.height = 400
area.display()

# <codecell>

stacked = vincent.StackedArea(multi_iter1, iter_idx='index')
stacked.axis_titles(x='Index', y='Value')
stacked.legend(title='Categories')
stacked.height = 300
stacked.width = 600
stacked.display()

# <codecell>
예제 #7
0
#Daily data
dates = pd.date_range('4/1/2013 00:00:00', periods=1441, freq='T')
data = [random.randint(20, 100) for x in range(len(dates))]
series = pd.Series(data, index=dates)
vis = vincent.Line()
vis.tabular_data(series, axis_time='day')
vis += ({'value': 'basis'}, 'marks', 0, 'properties', 'enter', 'interpolate')
vis.update_vis(width=800)
vis.axis_label(x_label='Time', y_label='Data')
vis.to_json(path, html=True)

#Resample to hourly, which can take a lambda function in addition to the 
#standard mean, max, min, etc. 
half_day = series['4/1/2013 00:00:00':'4/1/2013 12:00:00']
hourly = half_day.resample('H', how=lambda x: x.mean() + random.randint(-30, 40))
area = vincent.Area()
area.tabular_data(hourly, axis_time='hour')
area += ({'value': 'basis'}, 'marks', 0, 'properties', 'enter', 'interpolate')
area.update_vis(width=800)
area.axis_label(x_label='Time (Hourly)', y_label='Data')
area.to_json(path, html=True)

#Subset of minute data
half_hour = series['4/1/2013 00:00:00':'4/1/2013 00:30:00']
vis.tabular_data(half_hour, axis_time='minute')
vis.axis_label(x_label='Time (Minutes)', title='Data vs. Time')
vis.to_json(path, html=True)



tweets.set_index('created_at', drop=False, inplace=True)
tweets.index = tweets.index.tz_localize('GMT').tz_convert('Europe/Athens')
tweets.index = tweets.index - DateOffset(hours = 24)
tweets.index


#created_at timeseries in a per minute minute format
tweets1m = tweets['created_at'].resample('1t').count()

#average tweets per minute
avg = tweets1m.mean()

#plot tweets timeseries
import vincent
vincent.core.initialize_notebook()
area = vincent.Area(tweets1m)
area.colors(brew='Spectral')
area.axis_titles(x='time', y='tweets')
area.display()

#find most frequent tokens with NLTK
#remove english stopwords
import nltk
from nltk.corpus import stopwords
from nltk import FreqDist

#added "greek" file at ../nltk_data/corpora/stopwords

#Combined the following files:
#file source #1 https://code.grnet.gr/projects/gss/repository/revisions/d59fbcd2f0cd/entry/solr/conf/stopwords.txt
#file source #2 http://grepcode.com/file/repo1.maven.org/maven2/gr.skroutz/elasticsearch-skroutz-greekstemmer/0.0.4/org/elasticsearch/index/analysis/stopwords.txt
예제 #9
0
#NOAA 46401 Wave Period
vis1 = vincent.Line(width=600)
vis1.tabular_data(NOAA_46041, columns=['dominant_wave_period (s)'],
                  axis_time='day')
vis1.axis_label(x_label='Time', y_label='Dominant Wave Period (s)')
vis1.to_json('vis1.json')

#NOAA 46050 Binned Wind Speed
vis2 = vincent.Bar(width=600)
vis2.tabular_data(ws_binned)
vis2.axis_label(x_label='Wind Speed (m/s)', y_label='# of Obs')
vis2.to_json('vis2.json')

#NOAA 46243 Wave Height
vis3 = vincent.Area(width=600)
vis3.tabular_data(NOAA_46243, columns=['significant_wave_height (m)'],
                  axis_time='day')
vis3.axis_label(x_label='Time', y_label='Significant Wave Height (m)')
vis3.to_json('vis3.json')

#Map all buoys
buoy_map = folium.Map(location=[46.3014, -123.7390], zoom_start=7,
                      tiles='Stamen Terrain')
buoy_map.polygon_marker(location=[47.3489, -124.708], fill_color='#43d9de',
                        radius=12, popup=(vis1, 'vis1.json'))
buoy_map.polygon_marker(location=[44.639, -124.5339], fill_color='#43d9de',
                        radius=12, popup=(vis2, 'vis2.json'))
buoy_map.polygon_marker(location=[46.216, -124.1280], fill_color='#43d9de',
                        radius=12, popup=(vis3, 'vis3.json'))
buoy_map.create_map(path='NOAA_buoys.html')
예제 #10
0
# NOAA 46401 Wave Period.
vis1 = vincent.Line(NOAA_46041['dominant_wave_period (s)'],
                    width=400,
                    height=200)
vis1.axis_titles(x='Time', y='Dominant Wave Period (s)')
vis1.to_json('vis1.json')

# NOAA 46050 Binned Wind Speed.
vis2 = vincent.Bar(ws_binned, width=400, height=200)
vis2.axis_titles(x='Wind Speed (m/s)', y='# of Obs')
vis2.to_json('vis2.json')

# NOAA 46243 Wave Height.
vis3 = vincent.Area(NOAA_46243['significant_wave_height (m)'],
                    width=400,
                    height=200)
vis3.axis_titles(x='Time', y='Significant Wave Height (m)')
vis3.to_json('vis3.json')

# Map all buoys.
buoy_map = folium.Map(location=[46.3014, -123.7390],
                      zoom_start=7,
                      tiles='Stamen Terrain')
buoy_map.polygon_marker(location=[47.3489, -124.708],
                        fill_color='#43d9de',
                        radius=12,
                        popup=(vis1, 'vis1.json'))
buoy_map.polygon_marker(location=[44.639, -124.5339],
                        fill_color='#43d9de',
                        radius=12,