Exemplo n.º 1
0
 def getTableSize(self, txn, dbtable):
     rows = (yield Select(
         [
             Count(Constant(1)),
         ],
         From=dbtable,
     ).on(txn))
     returnValue(rows[0][0])
Exemplo n.º 2
0
 def nameOrValue(self, tok):
     """
     Inspecting a token present in an expression (for a CHECK constraint on
     this table), return a L{twext.enterprise.dal.syntax} object for that
     value.
     """
     if isinstance(tok, Identifier):
         return ColumnSyntax(self.table.columnNamed(tok.get_name()))
     elif tok.ttype == Number.Integer:
         return Constant(int(tok.value))
Exemplo n.º 3
0
    def histogram(cls, txn):
        """
        Generate a histogram of work items currently in the queue.
        """
        from twext.enterprise.jobs.queue import WorkerConnectionPool

        # Fill out an empty set of results for all the known work types. The SQL
        # query will only return work types that are currently queued, but we want
        # results for all possible work.
        results = {}
        now = datetime.utcnow()
        for workItemType in cls.workTypes():
            workType = workItemType.workType()
            results.setdefault(workType, {
                "queued": 0,
                "assigned": 0,
                "late": 0,
                "failed": 0,
                "completed": WorkerConnectionPool.completed.get(workType, 0),
                "time": WorkerConnectionPool.timing.get(workType, 0.0)
            })

        # Use an aggregate query to get the results for each currently queued
        # work type.
        jobs = yield cls.queryExpr(
            expr=None,
            attributes=(
                cls.workType,
                Count(cls.workType),
                Count(cls.assigned),
                Count(Case((cls.assigned == None).And(cls.notBefore < now), Constant(1), None)),
                Sum(cls.failed),
            ),
            group=cls.workType
        ).on(txn)

        for workType, queued, assigned, late, failed in jobs:
            results[workType].update({
                "queued": queued,
                "assigned": assigned,
                "late": late,
                "failed": failed,
            })

        returnValue(results)