예제 #1
0
def save_many(list):
  con = db.getCon()
  con.cursor().executemany("""
    insert into idle_event
    values (:ts, :id)
  """, list)
  con.commit()
예제 #2
0
def _select_recent_ids(sec):
    c = db.getCon().cursor()
    c.execute(
        """
    select distinct id
    from status_event
    where ts > ?
  """,
        (int((time.time() - sec) * 1000),),
    )
    return map(lambda row: row["id"], c)
예제 #3
0
def get_recent(sec):

  c = db.getCon().cursor()
  c.execute("""
    select * from tmp_event
    where ts > ? order by ts desc
  """, (
    int((time.time() - sec) * 1000),
  ))
  events = []
  for row in c: events.append(row)
  return events
예제 #4
0
def get_latest(id):
  c = db.getCon().cursor()
  c.execute("""
    select ts
    from idle_event
    where id = ?
    order by ts desc
    limit 1
  """, (id,))

  row = c.fetchone()
  return row['ts'] if row else None
예제 #5
0
 def go(self):
     now = int(time.time() * 1000)
     values = map(lambda (id, status): {"ts": now, "id": id, "status": status}, _get().iteritems())
     con = db.getCon()
     con.cursor().executemany(
         """
   insert into status_event
   values (:ts, :id, :status)
 """,
         values,
     )
     con.commit()
예제 #6
0
def _select_latest_available_time(id):
    c = db.getCon().cursor()
    c.execute(
        """
    select ts from status_event
    where id = ? and status = 'available'
    order by ts desc limit 1
  """,
        (id,),
    )
    row = c.fetchone()
    return row["ts"] if row else None
예제 #7
0
def _select_latest_status(id):
    c = db.getCon().cursor()
    c.execute(
        """
    select status from status_event
    where id = ?
    order by ts desc limit 1
  """,
        (id,),
    )
    row = c.fetchone()
    return row["status"] if row else None
예제 #8
0
def get_recent_avg(sec, id=None):
  c = db.getCon().cursor()
  c.execute("""
    select id, avg(tmp) as tmp from tmp_event
    where ts > :ts %s
    group by id
  """ % ("and id = :id" if id else ""), {
    'ts': int((time.time() - 5) * 1000),
    'id': id
  })

  if id:
    row = c.fetchone()
    return row['tmp'] if row else None

  return dict(map(lambda row: [row['id'], row['tmp']], c))