def __init__(self, rabbitmq_ip, queue_name, db_ip, db_port, db_name, collection_name): self.db = Mongo(db_ip, db_port, db_name) self.collection_name = collection_name self.connection = pika.BlockingConnection( pika.ConnectionParameters(host=rabbitmq_ip)) self.channel = self.connection.channel() self.channel.queue_declare(queue=queue_name, durable=True) self.channel.basic_qos(prefetch_count=5) self.channel.basic_consume(self.callback, queue=queue_name) print ' [*] Waiting for messages. To exit press CTRL+C' self.channel.start_consuming()
def execute_no_pbs(t): global db if db is None: from db.mongo import Mongo db = Mongo() t.execute() result = collect_data.decorate_result(t.json_result) print(db.bench6.insert_one(result)) return t
def job_bandwidth(): created_at = datetime.datetime.now() values = [] logging.basicConfig() cg = cmdgen.CommandGenerator() Mongo.create(host='localhost', port=27017) created_at = datetime.datetime.now() comm_data = cmdgen.CommunityData('jztec', 'jztec',) transport = cmdgen.UdpTransportTarget(('192.168.11.253', 161)) variables = (1, 3, 6, 1, 2, 1, 2, 2, 1, 10) errIndication, errStatus, errIndex, results = cg.nextCmd(comm_data, transport, variables) for result in results: for name,val in result: Mongo.db.ui['bandwidths'].insert({'ip':'192.168.11.253','port': str(name[10:]),'type':0,'created_at':created_at,"value":int(val.prettyPrint())}) variables = (1, 3, 6, 1, 2, 1, 2, 2, 1, 16) errIndication, errStatus, errIndex, results = cg.nextCmd(comm_data, transport, variables) for result in results: for name,val in result: Mongo.db.ui['bandwidths'].insert({'ip':'192.168.11.253','port': str(name[10:]),'type':1,'created_at':created_at,"value":int(val.prettyPrint())})
def main(self, path): #import tornado stuff import tornado.web, tornado.httpserver, tornado.ioloop, tornado.options from tornado.options import options from config import options_setup from db.mongo import Mongo #parse the app config tornado.options.parse_config_file(os.path.join(path, 'config/settings.py')) #parse the command line args tornado.options.parse_command_line() #connect to our db using our options set in settings.py Mongo.create(host=options.db_host, port=options.db_port) #init our url routes url_routes = self.init_routes() #init a logger self.init_logging(options.log) #add in any app settings settings = { "static_path": options.static_path, "cookie_secret": options.cookie_secret, "login_url": options.login_url, } #setup the controller action routes #tornado.process.fork_processes(0) self.application = tornado.web.Application(url_routes, **settings) self.application.pika = PikaClient() #instantiate a server instance http_server = tornado.httpserver.HTTPServer(self.application) #bind server to port http_server.listen(options.port) dss_jobs() #log our start message Log.info("Ready and listening") #start the server tornado.ioloop.IOLoop.instance().start()
def main(self, path): #import tornado stuff import tornado.web, tornado.httpserver, tornado.ioloop, tornado.options from tornado.options import options from config import options_setup from db.mongo import Mongo #parse the app config tornado.options.parse_config_file(os.path.join(path, 'config/settings.py')) #parse the command line args tornado.options.parse_command_line() #connect to our db using our options set in settings.py Mongo.create(host=options.db_host, port=options.db_port) #init our url routes url_routes = self.init_routes() #init a logger self.init_logging(options.log) #add in any app settings settings = { "static_path": options.static_path, "cookie_secret": options.cookie_secret, "login_url": options.login_url, } #setup the controller action routes self.application = tornado.web.Application(url_routes, **settings) #instantiate a server instance http_server = tornado.httpserver.HTTPServer(self.application) #bind server to port http_server.listen(options.port) #log our start message Log.info("Ready and listening") #start the server tornado.ioloop.IOLoop.instance().start()
class StatusWorker(object): def __init__(self, rabbitmq_ip, queue_name, db_ip, db_port, db_name, collection_name): self.db = Mongo(db_ip, db_port, db_name) self.collection_name = collection_name self.connection = pika.BlockingConnection( pika.ConnectionParameters(host=rabbitmq_ip)) self.channel = self.connection.channel() self.channel.queue_declare(queue=queue_name, durable=True) self.channel.basic_qos(prefetch_count=5) self.channel.basic_consume(self.callback, queue=queue_name) print ' [*] Waiting for messages. To exit press CTRL+C' self.channel.start_consuming() def callback(self, ch, method, properties, body): print " [x] Received %r" % (body, ) item = json.loads(body) self.db.insert(self.collection_name, item) ch.basic_ack(delivery_tag=method.delivery_tag)
class RegisterWorker(object): def __init__(self, rabbitmq_ip, queue_name, db_ip, db_port, db_name, collection_name): self.collection_name = collection_name self.db = Mongo(db_ip, db_port, db_name) def start(self): self.connection = pika.BlockingConnection(pika.ConnectionParameters(host=rabbitmq_ip)) self.channel = self.connection.channel() self.channel.queue_declare(queue=queue_name) self.channel.basic_qos(prefetch_count=1) self.channel.basic_consume(self.on_request, queue=queue_name) self.channel.start_consuming() def on_request(self, ch, method, props, body): print "Receive %s"%body item = json.loads(body) result = self.register(item) ch.basic_publish(exchange='', routing_key=props.reply_to, properties=pika.BasicProperties(correlation_id = \ props.correlation_id), body=result) ch.basic_ack(delivery_tag = method.delivery_tag) def register(self, item): items = self.db.find(self.collection_name, {'id': item['id']}) if items.count() == 0: print "Discover New Device %s" %item['id'] self.db.insert(self.collection_name, item) items = self.db.find(self.collection_name, {'id': item['id']}) if items.count() == 1: return items[0]['id'] return "DB Error" def __del__(self): self.connection.close()
def get_records(profileid, include): mongo = Mongo(profileid) report = '' report += mongo.get_accounts() report += mongo.get_entitlements() if include.atv or include.all: report += mongo.get_atv_subscriptions() if include.vodafone or include.all: report += mongo.get_vodafone_accounts() return report
def __init__(self, db, kw, start, end): # self.timer = datetime.datetime.now() self.mongo = Mongo(db=db) self.kwds = Keywords(keywords=kw) self.lang = LanguageKit(keywords=self.kwds) self.start = str(start) self.end = str(end) self.docs = [self.lang.FilterRetweets(t["text"]) for t in self.mongo.db["posts"].find( {"lang":"en", 'timestamp':{"$gte": self.start, "$lt": self.end}}, {"text":1,"_id":0})] # Spamfilter, remove redundant self.docs = list(set(filter(None, self.docs))) self.docs = self.lang.FilterSpam(self.docs) self.docs = self.lang.SimpleSpamFilter(self.docs) self.multicore = Multicore() self.frequency = [] self.scores = [] self.trace = Config().out self.output = Output(self.trace)
# -*- coding: utf-8 -*- ''' Created on 2012-3-19 @author: zongzong ''' from tornado.options import options from config import options_setup from db.mongo import Mongo from tornado.options import define import string import uuid import json import datetime def main(): from models.product import Product from models.order import Order from models.package import Package # from views.paginator import Paginator from models.exchange import Exchange # # from datetime import datetime # start = datetime(2010, 4, 1) # end = datetime(2015, 5, 1) # for exchange in Exchange.get_status_exchanges(1): need_do = [] orders = Order.get_working_orders(1, exchange['_id']) for order in orders: print order['custom_time']
''' Created on 2012-3-19 @author: zongzong ''' from tornado.options import options from config import options_setup from db.mongo import Mongo from tornado.options import define import string import uuid import json def main(): # from models.user import User # a = string.maketrans('None', '','','') # print a # u = Mongo.db.ui['users'].User.find({'roletype':2}) # us = [u for u in User.getUsers(2)] # print us # user = User.instance('2222', '2222') # Mongo.db.ui['user'].insert(user) # users2 = Mongo.db.ui['users'].find() # print '----' # for user2 in Mongo.db.ui['users'].find(): # print user2 # Mongo.db.ui['users'].remove() users = [user2 for user2 in Mongo.db.ui['users'].find()] print users # print users
packages = Package.getPackageByName(order['package_id']) for package in packages: need_back = need_back+json.dumps({'port':product['port'],'ex_bandwidth':order['bandwidth'],\ 'status':order['status'],'port_name':product['port'], 'ipAddress':exchange['ipAddress'], \ 'package_id':package['package_name']}) # Order.back_order(order['_id']) print 'set_package_job end at ',datetime.datetime.now() # orders = {} # flag = False # if(need_do!=''): # orders['need_do']=need_do # flag = True # if(need_back!=''): # orders['need_back']=need_back # flag = True # if(flag!=True): # rpc = TelnetRpcClient(options.service_ip) # encoded = encrypt_util.encode(str(orders)) # response = rpc.call("rpc_queue",encoded) def dss_jobsPackage(): logging.error("") sche = JzScheduler() sche.add_interval_job(job_package,minutes=30) sche.start() if __name__ == '__main__': Mongo.create(host='192.168.11.111', port=27017) dss_jobsPackage()
# us = [u for u in User.getUsers(2)] # print us # user = User.instance('2222', '2222') # Mongo.db.ui['user'].insert(user) # users2 = Mongo.db.ui['users'].find() # print '----' # for user2 in Mongo.db.ui['users'].find(): # print user2 # Mongo.db.ui['users'].remove() # users = [user2 for user2 in Mongo.db.ui['users'].find()] # print users # print users # print user.name() Mongo.db.ui.products.find_one({'_id' : uuid.UUID(c_id)}) # ws = [({'cname':u['cname']},{'percent':u['percent']}) for u in Mongo.db.ui['bandwidths'].find({'p_id' : '2fdc94f0-fcde-11e1-b73a-005056c00008'})] # custormers = [w for w in Mongo.db.ui['custormers'].find()] # print custormers # c = Mongo.db.ui.custormers.find_one({'_id' : uuid.UUID('a1287530-fc97-11e1-aef2-005056c00008')}) # print ws # print json.dumps(ws) # custormers = [w for w in Mongo.db.ui['custormers'].find({'_id':'liz', 'status':0})] # print custormers if __name__ == "__main__": Mongo.create(host="211.147.81.50", port=options.db_port) # Mongo.create(host='192.168.11.118', port=options.db_port) main() # print 'no service-policy input %sM' % 2
class App(): handlers = [] updater = Updater() db = Mongo()
# module list export RESTRICTIONS={restriction_command} WORKDIR=/tmp/bench-stat-{random_id} mkdir -p $WORKDIR cd $WORKDIR {sys.executable} {collect_script} --test-id={test_id} --rep-id={rep_id} --commit={latest} rm -rf $WORKDIR """.strip() # repo = Repo(__root__) # latest = repo.get_latest() flowdb = FlowDB(Mongo()) latest = 'c4a410af274b9e6a6a5075214b2b5d38498e27ab' # tests = 19 # repetitions = 15 tests = 1 repetitions = 50 indices = [(test_id, rep_id) for test_id in range(0, tests) for rep_id in range(0, repetitions)] random.shuffle(indices) def get_restrictions(commit, test): restrictions = list()
@author: huangchong ''' import tornado.web, tornado.httpserver, tornado.ioloop, tornado.options from tornado.options import options from config import options_setup import sys, os #parse the app config tornado.options.parse_config_file('E:/workspacePY/bandwidth/src/config/settings.py') #parse the command line args tornado.options.parse_command_line() from db.mongo import Mongo Mongo.create(host=options.db_host, port=options.db_port) from models.order import Order from models.product import Product from models.exchange import Exchange from client.rpc_client import TelnetRpcClient import json,datetime from util import encrypt_util rpc = TelnetRpcClient(options.service_ip) need_do = '' orders = Order.get_working_orders() for order in orders:
# -*- coding: utf-8 -*- ''' Created on 2012-3-19 @author: zongzong ''' from tornado.options import options from config import options_setup from db.mongo import Mongo from tornado.options import define import string import uuid import json import datetime def main(): from models.user import User from models.product import Product from models.order import Order from views.paginator import Paginator from models.exchange import Exchange from datetime import datetime start = datetime(2010, 4, 1) end = datetime(2015, 5, 1) a = Mongo.db.ui['orders'].find({ 'status': 0, "begin_at": { "$lte": datetime.now() } }) #
need_back = need_back+json.dumps({'port':product['port'],'ex_bandwidth':order['bandwidth'],\ 'status':order['status'],'port_name':product['port'], 'ipAddress':exchange['ipAddress'], \ 'package_id':package['package_name']}) # Order.back_order(order['_id']) print 'set_package_job end at ', datetime.datetime.now() # orders = {} # flag = False # if(need_do!=''): # orders['need_do']=need_do # flag = True # if(need_back!=''): # orders['need_back']=need_back # flag = True # if(flag!=True): # rpc = TelnetRpcClient(options.service_ip) # encoded = encrypt_util.encode(str(orders)) # response = rpc.call("rpc_queue",encoded) def dss_jobsPackage(): logging.error("") sche = JzScheduler() sche.add_interval_job(job_package, minutes=30) sche.start() if __name__ == '__main__': Mongo.create(host='192.168.11.111', port=27017) dss_jobsPackage()
from flask import Flask from flask import request from db.mongo import Mongo from worm.worm import Spider from google.protobuf import json_format import json from proto.login_pb2 import LoginRsp from proto.train_pb2 import * mongo = Mongo() app = Flask('ticket') spider = Spider() @app.route('/login', methods=['POST']) def login(): data = json.loads(request.data) print(data) rsp = LoginRsp() if verify_user(data): rsp.ok = True else: rsp.ok = False rsp.errno = 'Register failed' return rsp.SerializeToString() def verify_user(data): res = mongo.find_user({'id': data['id']})
#!/usr/bin/python # -*- coding: utf-8 -*- # author: Jan Hybs from db.mongo import Mongo import pandas as pd import numpy as np import seaborn as sns sns.set(color_codes=True) from matplotlib import pyplot as plt from scipy.stats.stats import pearsonr db = Mongo() match = {'$match': {'node': 'janhybs'}} cursor = db.bench.aggregate([ match, { '$group': { '_id': { 'node': '$node', 'name': '$name' }, 'dur': { '$push': '$duration' }, 'eff': { '$push': '$time.eff' }, 'real': { '$push': '$time.real' }, 'user': { '$push': '$time.user'
def setdb(self): from db.mongo import Mongo self.db = Mongo(db=self.dbname)
def __init__(self, rabbitmq_ip, queue_name, db_ip, db_port, db_name, collection_name): self.collection_name = collection_name self.db = Mongo(db_ip, db_port, db_name)
''' Created on 2012-3-19 @author: zongzong ''' from tornado.options import options from config import options_setup from db.mongo import Mongo from tornado.options import define import string def main(): from models.user import User # a = string.maketrans('None', '','','') # print a # u = Mongo.db.ui['users'].User.find({'roletype':2}) # us = [u for u in User.getUsers(2)] # print us # user = User.instance('2222', '2222') # Mongo.db.ui['user'].insert(user) # users2 = Mongo.db.ui['users'].find() # print '----' # for user2 in Mongo.db.ui['users'].find(): # print user2 # users = [user2 for user2 in Mongo.db.ui['users'].find({'roletype':2})] # print users # print user.name() custormers = [ w for w in Mongo.db.ui['custormers'].find({ '_id': 'liz', 'status': 0
class StreamListener(tweepy.StreamListener): def __init__(self, path=None, db=None): tweepy.StreamListener.__init__(self) self.csv = path self.dbname = db self.data = [] if path: self.setpath() if db or (not db and not path): self.setdb() def setpath(self): if not os.path.isfile(self.csv): output = "Id,Username,Content,Timestamp,RTs,Favorites,ReplyTo,Hastags,Urls\n" self.file = open(self.csv, "ab+") self.file.write(output.encode('utf-8')) else: self.file = open(self.csv, "ab+") def setdb(self): from db.mongo import Mongo self.db = Mongo(db=self.dbname) def writestream(self, status): if self.csv: date = self.Clean(str(status.created_at)) text = self.Clean(status.text) urls = [] hashtags = [] for hashtag in status.entities["hashtags"]: hashtags.append(hashtag['text']) for url in status.entities["urls"]: urls.append(url['expanded_url']) self.output = str(status.id)+","+\ status.author.screen_name+","+\ text+","+\ date+","+\ str(status.retweet_count)+","+\ str(status.favorite_count)+","+\ str(status.in_reply_to_status_id)+","+\ ' '.join(hashtags)+","+\ ' '.join(urls)+","+\ "\n" self.file.write(self.output.encode('utf-8')) if self.dbname or (not self.dbname and not self.csv): data = {} data = self.process_dict(status.__getstate__(), data, self.process) if data.has_key('author'): del data['author'] if data.has_key('created_at'): data['timestamp'] = self.db.DateTime(data['created_at']) print data['timestamp'] if len(self.data) < 10: if data.has_key('lang'): """Filter for english tweets.""" if data['lang'] == 'en': self.data.append(data) else: self.db.Update(self.data) self.data = [] self.data.append(data) def on_status(self, status): try: self.writestream(status) except Exception, e: exc_type, exc_obj, exc_tb = sys.exc_info() fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] print >> sys.stderr, 'Encountered Exception:', e, fname, exc_tb.tb_lineno import traceback with open("../outputs/errors.log", "a+") as f: f.write(exc_type + " " + fname + " " + exc_tb.tb_lineno + "\n") with open("../outputs/errors.log", "a+") as f: f.write(traceback.format_exc() + "\n") pass try: print "%s:\n%s\n%s%s\n" % ( status.author.screen_name, status.text, status.created_at, "\n", ) except Exception, e: pass
# -*- coding: utf-8 -*- ''' Created on 2012-3-19 @author: zongzong ''' from tornado.options import options from config import options_setup from db.mongo import Mongo from tornado.options import define import string import uuid import json import datetime def main(): from models.user import User from models.product import Product from models.order import Order from views.paginator import Paginator from models.exchange import Exchange from datetime import datetime start = datetime(2010, 4, 1) end = datetime(2015, 5, 1) a =Mongo.db.ui['orders'].find({'status' : 0,"begin_at":{"$lte":datetime.now()}}) # # begin_at= '02/28/2013 00:00' ## b = # print b
def checkDBConnection(): db = Mongo()
@author: huangchong ''' import tornado.web, tornado.httpserver, tornado.ioloop, tornado.options from tornado.options import options from config import options_setup import sys, os #parse the app config tornado.options.parse_config_file( 'E:/workspacePY/bandwidth/src/config/settings.py') #parse the command line args tornado.options.parse_command_line() from db.mongo import Mongo Mongo.create(host=options.db_host, port=options.db_port) from models.order import Order from models.product import Product from models.exchange import Exchange from client.rpc_client import TelnetRpcClient import json, datetime from util import encrypt_util rpc = TelnetRpcClient(options.service_ip) need_do = '' orders = Order.get_working_orders() for order in orders: product = Product.lookup(order['p_id'])
''' Created on 2012-3-19 @author: zongzong ''' from tornado.options import options from config import options_setup from db.mongo import Mongo from tornado.options import define import string def main(): from models.user import User # a = string.maketrans('None', '','','') # print a # u = Mongo.db.ui['users'].User.find({'roletype':2}) # us = [u for u in User.getUsers(2)] # print us # user = User.instance('2222', '2222') # Mongo.db.ui['user'].insert(user) # users2 = Mongo.db.ui['users'].find() # print '----' # for user2 in Mongo.db.ui['users'].find(): # print user2 # users = [user2 for user2 in Mongo.db.ui['users'].find({'roletype':2})] # print users # print user.name() custormers = [w for w in Mongo.db.ui['custormers'].find({'_id':'liz', 'status':0})] print custormers
from flask import Flask from flask import render_template from db.mongo import Mongo app = Flask(__name__) app.debug = True #reload changes @app.route('/') def hello_world(): return 'Hello World!' @app.route('/devshop') def devshop(): collection = driver.getCollection('local', 'Clients') client = collection.find_one() name = client['_id'] return render_template('hello.html', name=name) if __name__ == '__main__': print("Initialize devshop") driver = Mongo() #print(driver) #driver.test() app.run(host='0.0.0.0') #app.run() #run only locally