def main(): call(['./get_sleep.sh']) with open('data_json.txt') as raw: print '[Info] Loading raw sleep data...' raw_sessions = json.load(raw) timestamp = (datetime.today() - timedelta(days=TIME_DELTA)).isoformat() chk_sessions = [s for s in raw_sessions if s['start'] > timestamp] if len(chk_sessions) > 0: print '[Info] Found {0} sleep sessions to check...'.format(len(chk_sessions)) client = KeenClient( project_id = KEEN_PROJECT_ID, write_key = KEEN_WRITE_KEY, read_key = KEEN_READ_KEY ) old_sessions = client.extraction('sessions', timeframe=TIME_FRAME) new_sessions = [s for s in chk_sessions if 'tyin' + s['start'] not in [s2['id'] for s2 in old_sessions]] if len(new_sessions) > 0: print '[Info] Found {0} sleep sessions to add...'.format(len(new_sessions)) sessions = [] events = [] sessions, events = transform(new_sessions, 'tyin') client.add_events({'sessions': sessions, 'events': events}) print '[Info] Added {0} sleep sessions.'.format(len(new_sessions)) print '[Info] Done.'
def run(self): keen_client = KeenClient( project_id=self.project_id, write_key=self.write_key, ) timezone = pytz.timezone(self.data['user']['timezone']) self.events = [] with click.progressbar(self.data['days'], label='Preparing keen.io events', fill_char=click.style('#', fg='blue')) as days: for day in days: dt = self._parse_date(day['date'], timezone) self.append_event(dt, 'total', { 'seconds': day['grand_total']['total_seconds'], }) categories = [ 'editor', 'language', 'operating_system', 'project', ] for category in categories: for item in day.get(category + 's', []): self.append_event(dt, category, { 'seconds': item['total_seconds'], 'name': item['name'], }) files = {} for project in day.get('projects', []): for f in project.get('dump', {}).get('files', []): if f['name'] not in files: files[f['name']] = 0 files[f['name']] += f['total_seconds'] for name, seconds in files.items(): self.append_event(dt, 'file', { 'seconds': seconds, 'name': name, }) if len(self.events) == 0: click.echo('No events available for exporting to keen.io') return click.echo('Uploading events to keen.io...') start = datetime.utcfromtimestamp(self.data['range']['start']) end = datetime.utcfromtimestamp(self.data['range']['end']) collection = 'WakaTime Data Dump from {start} until {end}'.format( start=start.strftime('%Y-%m-%d'), end=end.strftime('%Y-%m-%d'), ) keen_client.add_events({ collection: self.events, })
def test_post_timeout_batch(self): keen.project_id = "5004ded1163d66114f000000" api_key = "2e79c6ec1d0145be8891bf668599c79a" keen.write_key = scoped_keys.encrypt(api_key, {"allowed_operations": ["write"]}) client = KeenClient(keen.project_id, write_key=keen.write_key, read_key=None, post_timeout=0.0001) client.add_events({"python_test": [{"hello": "goodbye"}]})
class KeenIoEventHandler(IEventHandler): """ The instance should be registered to event trigger for data update. This uploads any data to keenio cloud service. Args: project_id: Project ID provided by keenio. write_key: Write key ID provided by keenio. q_max: internal queue size to be used from another thread. Returns: Instance of this class. """ def __init__(self, project_id, write_key, q_max=5): IEventHandler.__init__(self, q_max=q_max) # for retry self.project_id_ = project_id self.write_key_ = write_key self.client_ = KeenClient(project_id=project_id, write_key=write_key) def _run(self, data): """ Procedure to run when data received from trigger thread. Args: data: Pass to the registered event handlers. """ data_source = data["source"] at = data["at"] upload_items = [] for label, datum in data["data"].items(): upload_item = datum upload_item["label"] = label upload_item["source"] = data_source upload_item["keen"] = {"timestamp": "{}Z".format(at.isoformat())} upload_items.append(upload_item) try: self.client_.add_events({"offgrid": upload_items}) except Exception as e: logger.error("{} failed to send data to keenio at {} by {}".format( type(self).__name__, data["at"], type(e).__name__)) logger.error("Details: {}".format(str(e))) del self.client_ self.client_ = KeenClient(project_id=self.project_id_, write_key=self.write_key_) # TODO: skip retry to avoid exception in this scope. # self.client_.add_events({"offgrid": upload_items}) else: logger.info("{} sent data to keenio at {}".format( type(self).__name__, at))
def main(): counts_by_institutions = get_count_by_institutions() keen_project = keen_settings['private']['project_id'] write_key = keen_settings['private']['write_key'] if keen_project and write_key: client = KeenClient( project_id=keen_project, write_key=write_key, ) client.add_events(counts_by_institutions) else: print(counts_by_institutions)
def send_events(self, events): keen_project = keen_settings['private']['project_id'] write_key = keen_settings['private']['write_key'] if keen_project and write_key: client = KeenClient( project_id=keen_project, write_key=write_key, ) logger.info('Adding {} events to the {} collection'.format(len(events), self.collection_name)) client.add_events({self.collection_name: events}) else: logger.info('Keen not enabled - would otherwise be adding the following {} events to the {} collection'.format(len(events), self.collection_name)) print(events)
def send_events(self, events): keen_project = keen_settings["private"]["project_id"] write_key = keen_settings["private"]["write_key"] if keen_project and write_key: client = KeenClient(project_id=keen_project, write_key=write_key) logger.info("Adding {} events to the {} collection".format(len(events), self.collection_name)) for chunk in self.yield_chunked_events(events): client.add_events({self.collection_name: chunk}) time.sleep(1) else: logger.info( "Keen not enabled - would otherwise be adding the following {} events to the {} collection".format( len(events), self.collection_name ) ) print(events)
def test_direct_persistence_strategy(self): project_id = "5004ded1163d66114f000000" api_key = "2e79c6ec1d0145be8891bf668599c79a" write_key = scoped_keys.encrypt(api_key, {"allowed_operations": ["write"]}) read_key = scoped_keys.encrypt(api_key, {"allowed_operations": ["read"]}) client = KeenClient(project_id, write_key=write_key, read_key=read_key) client.add_event("python_test", {"hello": "goodbye"}) client.add_event("python_test", {"hello": "goodbye"}) client.add_events( { "sign_ups": [{ "username": "******", "referred_by": "steve", "son_of": "my_mom" }], "purchases": [ {"price": 5}, {"price": 6}, {"price": 7} ]} )
def test_direct_persistence_strategy(self): project_id = "5004ded1163d66114f000000" api_key = "2e79c6ec1d0145be8891bf668599c79a" write_key = scoped_keys.encrypt(api_key, {"allowed_operations": ["write"]}) read_key = scoped_keys.encrypt(api_key, {"allowed_operations": ["read"]}) client = KeenClient(project_id, write_key=write_key, read_key=read_key) client.add_event("python_test", {"hello": "goodbye"}) client.add_event("python_test", {"hello": "goodbye"}) client.add_events({ "sign_ups": [{ "username": "******", "referred_by": "steve", "son_of": "my_mom" }], "purchases": [{ "price": 5 }, { "price": 6 }, { "price": 7 }] })
def run(self): keen_client = KeenClient( project_id=self.project_id, write_key=self.write_key, ) timezone = pytz.timezone(self.data['user']['timezone']) self.events = [] with click.progressbar(self.data['days'], label='Preparing keen.io events', fill_char=click.style('#', fg='blue')) as days: for day in days: dt = self._parse_date(day['date'], timezone) self.append_event( dt, 'total', { 'seconds': day['grand_total']['total_seconds'], }) categories = [ 'editor', 'language', 'operating_system', 'project', ] for category in categories: for item in day.get(category + 's', []): sec = item.get( 'total_seconds', item.get('grand_total', {}).get('total_seconds')) self.append_event(dt, category, { 'seconds': sec, 'name': item['name'], }) files = {} for project in day.get('projects', []): for f in project.get('entities', []): if f['name'] not in files: files[f['name']] = 0 files[f['name']] += f['total_seconds'] for name, seconds in files.items(): self.append_event(dt, 'file', { 'seconds': seconds, 'name': name, }) if len(self.events) == 0: click.echo('No events available for exporting to keen.io') return click.echo('Uploading events to keen.io...') start = datetime.utcfromtimestamp(self.data['range']['start']) end = datetime.utcfromtimestamp(self.data['range']['end']) collection = 'WakaTime Data Dump from {start} until {end}'.format( start=start.strftime('%Y-%m-%d'), end=end.strftime('%Y-%m-%d'), ) keen_client.add_events({ collection: self.events, })
self.keen.add_events({ collection : data_array}) return True def is_exist_girl(self, collection, name, timestamp, **kwargs): return self.keen.count(collection, filters=[{"property_name" : "keen.timestamp", "operator" : 'eq', "property_value" : timestamp}, {"property_name" : "캐릭터", "operator" : 'eq', "property_value" : name}]) != 0 def test_function(self, **kwargs): self.keen.add_events({ "test_cases" : [ {"user_name" : "nameuser4", "keen" : { "timestamp" : "2015-09-14T02:09:10.141Z" }, "count" : 7 }, {"user_name" : "nameuser5", "keen" : { "timestamp" : "2015-09-14T02:09:10.141Z" }, "count" : 4}] }) return self.keen.count("test_cases", timeframe="this_14_days") if __name__ == '__main__': keen_client.add_events({ 'test_cases' : [ {"user_name" : u"시부야 린", "count" : 4}]})
from keen.client import KeenClient import sportsRadar import json client = KeenClient( project_id="5ad53b33c9e77c00017c7f18", write_key= "C45BD93F638CE4332874AE2F2DC375431A3BB79553C9FAA30BBD527356185170238BBF1F1E121A29FDD71EF1FE8AEB19CC3C5F792F0330AABB536D56B082EFABE2432C5AB9D66AF4E8AA9BD68DC3F7DA79C35E037A495E8A7B07240915109298", read_key= "1E750240E2DC88FEC6AE0927C57A3152BF578C59D825A9FB9BAD96E230E8B0886C722FDD31852ED880058CF752C30D7D6EBAD67E968FA093E543BB70F9A03C84FFEC6DC76139B0C3AC9F208FC49818A557E1FCC906063DAC87551EFAF9758BA7" ) # player = sportsRadar.getPlayerProfile("a52b2c84-9c3d-4d6e-8a3b-10e75d11c2bc") # playerEventCollection = sportsRadar.getSeasonAverages(player) # # client.add_events({ # "JamesHarden_Season_Averages": playerEventCollection # }) with open('data/AllTeamsBoxScore_short3.json') as json_data: d = json.load(json_data) client.add_events({"All_Players_Box_Scores_17_18": d})