def start(): c = MeshATC('rpc.pravah.io:5555') feed = c.subscribe(geospace) datalake = Datalake(os.getenv('PRAVAH_DB_USERNAME'), os.getenv('PRAVAH_DB_PASSWORD'), c.get_channel()) for m, c in feed: jsonObj = json.loads(MessageToJson(m)) obj = datalake.insert(c, jsonObj) print('[ATC] ' + str(obj) + ': ' + c)
def __init__(self, channel, auth_token, endpoint='rpc.pravah.io:6666', mode='secure'): super().__init__(endpoint, auth_token, mode) self.channel = channel if channel[0] != '/': raise Exception( 'Not a valid channel name. Channel name starts with a \'/\'') self.proto_module = importlib.import_module( parent_module + '.' + self.get_module_name(channel)) self.parent_module = importlib.import_module(parent_module) self.FeedMessage = getattr(self.proto_module, feed_message_string) self.datalake = Datalake(channel, auth_token)
def main(): datalake = Datalake('pravah', 'Pr@v@h@dm!n', '/SolarPowerProduction') cur = datalake.aggregate( group_by='item.stations.id', push={ 'timestamp': '$item.stations.timestamp', 'currentPowerOutput': '$item.stations.powerGenerationParameters.currentPowerOutput', 'powerGeneratedToday': '$item.stations.powerGenerationParameters.powerGeneratedToday', 'irradiation': '$item.stations.powerGenerationParameters.irradiation' }, start="2020/01/03 06:30:00", end="2020/01/03 23:30:00") stations = {} for i in cur: if not is_int(i['_id'][0]): timestamp = [] current_power_output = [] power_generated_today = [] irradiation = [] station_id = i['_id'][0] for t in i['all']: timestamp.append( datetime.fromtimestamp(int( t['timestamp'][0])).strftime("%d/%m/%Y %H:%M:%S")) current_power_output.append(t['currentPowerOutput'][0]) power_generated_today.append(t['powerGeneratedToday'][0]) irradiation.append(t['irradiation'][0]) stations[station_id] = { 'timestamp': timestamp, 'currentPowerOutput': current_power_output, 'powerGeneratedToday': power_generated_today, 'irradiation': irradiation } with open('stations_data.json', 'w') as st: json.dump(stations, st)
def main(): datalake = Datalake('pravah', '', '/SolarPowerProduction') station = 'kitpitampura' cur = datalake.get({ 'geospace': '/in/delhi', 'item.stations.id': station }, past_hours=24) # start="2020/01/03 06:30:00", end="2020/01/03 23:30:00" for i in cur: for s in i['item']['stations']: if s['id'] != station: continue try: d = datetime.fromtimestamp(int(s['timestamp'])) except: d = '' try: p = str(s['powerGenerationParameters']['currentPowerOutput']) except: p = '' print(str(i) + ' ' + str(s['id']) + ' \t: ' + str(d) + ' : ' + p)
import os import json import pytz from dlake import Datalake from datetime import datetime datalake = Datalake(os.getenv('PRAVAH_DB_USER'), os.getenv('PRAVAH_DB_PASSWORD'), '/SolarPowerProduction') def todays(): today_string = datetime.now().strftime('%Y/%m/%d') cur = datalake.aggregate( match={'geospace': '/in/delhi'}, start=today_string + ' 00:00:00', end=today_string + ' 23:59:59', pipeline=[{ '$unwind': '$item.stations' }, { '$group': { '_id': '$item.stations.id', 'powerGeneratedToday': { '$push': { 'timestamp': '$item.stations.timestamp', 'powerGeneratedToday': '$item.stations.powerGenerationParameters.powerGeneratedToday' } }, 'currentPowerOutput': {
class Pravah(MeshRPC): def __init__(self, channel, auth_token, endpoint='rpc.pravah.io:6666', mode='secure'): super().__init__(endpoint, auth_token, mode) self.channel = channel if channel[0] != '/': raise Exception( 'Not a valid channel name. Channel name starts with a \'/\'') self.proto_module = importlib.import_module( parent_module + '.' + self.get_module_name(channel)) self.parent_module = importlib.import_module(parent_module) self.FeedMessage = getattr(self.proto_module, feed_message_string) self.datalake = Datalake(channel, auth_token) def subscribe(self, geospace): s = super().subscribe(self.channel, geospace) feed = self.FeedMessage() channelLen = len(self.channel) try: for msg in s: feed.ParseFromString(msg.raw) yield feed, msg.topic.pop()[channelLen:] except grpc.RpcError as e: raise MeshRPCException(e.details()) def unsubscribe(self, geospaces): return super().unsubscribe(self.channel, geospaces) def registerToPublish(self, geospace): try: super().registerToPublish(self.channel, geospace) except MeshRPCException as e: raise def publish(self, geospace, d): if not isinstance(d, dict): return self.rawPublish(geospace, d) d['header'][ 'version'] = self.parent_module.pravah_protocol_version.get( self.channel, "0.0.1") feed = '' try: feed = self.FeedMessage() # TODO: ParseDict does not raise exception if parsing fails ParseDict(d, feed, True) raw = feed.SerializeToString() except: raise self.rawPublish(geospace, raw) def rawPublish(self, geospace, raw): try: res = super().publish(self.channel, geospace, raw) except MeshRPCException as e: raise def get_channel(self): return self.channel def get_module_name(self, module): return module[1:] + '_pb2' def get_historical_data(self, query={}, **kwargs): return self.datalake.get(query, **kwargs) def latest(self): return self.datalake.latest() def get_static_data(self): pass