예제 #1
0
파일: wago.py 프로젝트: smurfix/MoaT
	def run(self,ctx,**k):
		event = self.params(ctx)
		if not len(event):
			raise SyntaxError(u'Usage: mode ‹report|count TIME›')
		elif event[0] == "report":
			if len(event) > 1:
				raise SyntaxError(u'Usage: mode ‹report|count TIME›')
		elif event[0] == "count":
			if len(event) > 1:
				self.parent.values["timespec"] = simple_time_delta(event[1:])
		else:
			raise SyntaxError(u'Usage: mode ‹report|count TIME›')
		self.parent.values["mode"] = event[0]
예제 #2
0
파일: wago.py 프로젝트: ZigmundRat/moat
 def run(self, ctx, **k):
     event = self.params(ctx)
     if not len(event):
         raise SyntaxError(u'Usage: mode ‹report|count TIME›')
     elif event[0] == "report":
         if len(event) > 1:
             raise SyntaxError(u'Usage: mode ‹report|count TIME›')
     elif event[0] == "count":
         if len(event) > 1:
             self.parent.values["timespec"] = simple_time_delta(event[1:])
     else:
         raise SyntaxError(u'Usage: mode ‹report|count TIME›')
     self.parent.values["mode"] = event[0]
예제 #3
0
파일: pwm.py 프로젝트: ZigmundRat/moat
	def run(self,ctx,**k):
		event = self.params(ctx)
		if not len(event):
			raise SyntaxError(u'Usage: interval ‹timespec›')

		self.parent.attrs["interval"] = simple_time_delta(event[:])
예제 #4
0
파일: cmd.py 프로젝트: ZigmundRat/moat
	async def do(self,args):
		if args and self.options.layer < 0 and not self.options.copy: # list all
			pass
		elif not args or (self.options.layer < 0 and not self.options.copy):
			raise SyntaxError("Usage: set -l LAYER [options] data_tag")
		tag = ' '.join(args)
		await self.setup()

		if self.options.copy:
			if self.options.interval or self.options.max_age:
				raise SyntaxError("Copying and setting parameters is not compatible")
		if self.options.interval:
			self.options.interval = simple_time_delta(self.options.interval)
		async with self.db() as db:
			await db.Do("SET TIME_ZONE='+00:00'", _empty=True)
			try:
				dtid, = await db.DoFn("select id from data_type where tag=${tag}", tag=tag)
			except NoData:
				raise SyntaxError("Tag '%s' unknown" % (tag,))
			upd = {}

			if args and self.options.copy:
				try:
					cdtid, = await db.DoFn("select id from data_type where tag=${tag}", tag=self.options.copy)
				except NoData:
					raise SyntaxError("Source tag unknown")
				if self.options.layer < 0:
					c, = await db.DoFn("select count(*) from data_agg_type where data_type=${id}", id=dtid)
					if c > 0:
						raise SyntaxError("Some layers already exist, copy separately.")
					from .process import agg_type
					t = agg_type(self,db)
					n = 0
					async for d in db.DoSelect("select * from data_agg_type where data_type=${dtid}", dtid=cdtid, _dict=True):
						await t.set(d)
						t.data_type = dtid
						t.id = None
						t.last_id = 0
						t.timestamp = datetime(1999,1,1,0,0,0)
						await t.save()
						n += 1
					print("%d records copied." % n)
					return
				else:
					i,m = await db.DoFn("select `interval`,max_age from data_agg_type where data_type=${dtid} and layer=${layer}", dtid=dtid,layer=self.options.layer)
					if not self.options.interval:
						self.options.interval = i
					if not self.options.max_age:
						self.options.max_age = m
					
			if args and self.options.layer < 0 and not self.options.copy: # list all
				seen = False
				async for d in db.DoSelect("select * from data_agg_type where data_type=${dtid}", dtid=dtid, _dict=True):
					add_human(d)
					if self.root.verbose > 1:
						if seen:
							print("===")
						pprint(remap(d, lambda p, k, v: v is not None))
						seen = True
					else:
						print(d['layer'],d['timestamp'],d['human']['interval'],d['human']['max_age'], sep='\t')
				return

			try:
				iid,intv = await db.DoFn("select id,`interval` from data_agg_type where data_type=${id} and layer=${layer}", id=dtid, layer=self.options.layer)
			except NoData:
				if not self.options.interval:
					raise SyntaxError("The interval must be specified")
				upd['interval'] = intv = self.options.interval
				iid=None

				if self.options.layer > 0:
					try:
						lid,lint = await db.DoFn("select id,`interval` from data_agg_type where data_type=${id} and layer=${layer}", id=dtid,layer=self.options.layer-1)
					except NoData:
						raise SyntaxError("Layers need to be contiguous")
					else:
						if self.options.interval % lint:
							raise SyntaxError("The interval must be a multiple of the next-lower layer's interval")
				upd['layer'] = self.options.layer
			else:
				if self.options.interval:
					n, = await db.DoFn("select count(*) from data_agg where data_agg_type=${iid}", iid=iid)
					if n > 0:
						raise SyntaxError("Existing data! The interval cannot be changed")
				
			if self.options.max_age:
				if self.options.max_age == '-':
					max_age = None
				else:
					max_age = simple_time_delta(self.options.max_age)
					if max_age < 3*intv:
						raise SyntaxError("maxage is too low, this makes no sense")
				upd['max_age'] = max_age
			if not upd:
				raise SyntaxError("No change specified")

			if iid is None:
				upd['data_type'] = dtid
			ks = list(upd.keys())
			if iid is None:
				await db.Do("insert into data_agg_type ("+','.join("`%s`"%(k,) for k in ks)+") values ("+','.join("${%s}"%(k,) for k in ks)+")", **upd)
			else:
				await db.Do("update data_agg_type set "+','.join("`%s`=${%s}"%(k,k) for k in ks)+" where id=${id}", id=iid, _empty=True, **upd)