Exemple #1
0
 def list_incidents(self, request):
     #set_response_header(request, HeaderName.etag, "*") # FIXME
     set_response_header(request, HeaderName.contentType, ContentType.JSON)
     return to_json_text(sorted(
         incidents_from_query(self, request),
         cmp=lambda a, b: cmp(a[0], b[0]), reverse=True,
     ))
    def data(self, labels=False, totals=False):
        rows = []

        incidents_by_type = self.incidents_by_type()
        incidents_by_date = self.incidents_by_date()

        for incident_type in sorted(incidents_by_type):
            if incident_type in set((
                IncidentType.Admin.value, "Echelon", "SITE"
            )):
                continue

            if labels:
                if incident_type is None:
                    row = ["(unclassified)"]
                else:
                    row = [incident_type]
            else:
                row = []

            seen = set()

            for date in sorted(incidents_by_date):
                incidents = (
                    incidents_by_type[incident_type] & incidents_by_date[date]
                )
                seen |= incidents
                row.append("{0}".format(len(incidents)))
                # row.append("{0} ({1})".format(
                #     len(incidents),
                #     ",".join((str(i.number) for i in incidents))
                # ))

            if totals:
                row.append(len(incidents_by_type[incident_type]))

            unseen = incidents_by_type[incident_type] - seen

            if unseen:
                log.msg(
                    "ERROR: No date for some {0} incidents (!?): {1}"
                    .format(incident_type, unseen)
                )

            rows.append(row)

        row = ["Total"]
        seen = set()
        for date in sorted(incidents_by_date):
            incidents = incidents_by_date[date]
            seen |= incidents
            row.append(len(incidents))

        if totals:
            row.append(len(seen))

        rows.append(row)

        return to_json_text(rows)
 def chartColumns(self, request, tag):
     return to_json_text(
         ["Type"] +
         [
             date.strftime("%a %m/%d")
             for date in sorted(self.incidents_by_date())
         ]
     )
Exemple #4
0
 def ping(self, request):
     ack = "ack"
     set_response_header(
         request, HeaderName.etag, ack
     )
     set_response_header(
         request, HeaderName.contentType, ContentType.JSON
     )
     return to_json_text(ack)
Exemple #5
0
 def columns(self, request, tag):
     return to_json_text([
         "Number",
         "Priority",
         "Created", "Dispatched", "On Scene", "Closed",
         "Rangers",
         "Location",
         "Type",
         "Description"
     ])
Exemple #6
0
 def links(self, request):
     #set_response_header(request, HeaderName.etag, ????)
     set_response_header(request, HeaderName.contentType, ContentType.JSON)
     return to_json_text([
         {JSON.name.value: name, JSON.url.value: value}
         for name, value in (
             ("Home page", "/"),
             ("Dispatch Queue", "/queue"),
             ("Daily Incident Summary (Table)", "/reports/daily"),
             ("Daily Incident Summary (Chart)", "/charts/daily"),
         )
     ])
Exemple #7
0
    def list_rangers(self, request):
        set_response_header(request, HeaderName.etag, str(self.dms.rangers_updated))
        set_response_header(request, HeaderName.contentType, ContentType.JSON)

        d = self.dms.rangers()
        d.addCallback(lambda rangers:
            to_json_text(tuple(
                {
                    "handle": ranger.handle,
                    "name"  : ranger.name,
                    "status": ranger.status,
                }
                for ranger in rangers
            ))
        )

        return d              
Exemple #8
0
    def data(self, labels=False, totals=False):
        rows = []

        incidents_by_type = self.incidents_by_type()
        incidents_by_date = self.incidents_by_date()

        for incident_type in sorted(incidents_by_type):
            if labels:
                row = [incident_type]
            else:
                row = []

            seen = set()

            for date in sorted(incidents_by_date):
                incidents = incidents_by_type[incident_type] & incidents_by_date[date]
                seen |= incidents
                row.append("{0}".format(len(incidents)))
                #row.append("{0} ({1})".format(len(incidents), ",".join((str(i.number) for i in incidents))))

            row.append(len(incidents_by_type[incident_type]))

            unseen = incidents_by_type[incident_type] - seen

            if unseen:
                log.msg("ERROR: No date for some {0} incidents (!?): {1}".format(incident_type, unseen))

            rows.append(row)

        if totals:
            row = ["Total"]
            seen = set()
            for date in sorted(incidents_by_date):
                incidents = incidents_by_date[date]
                seen |= incidents
                row.append(len(incidents))
            row.append(len(seen))
            rows.append(row)

        return to_json_text(rows)
Exemple #9
0
    def data(self, request, tag):
        def format_date(d):
            if d is None:
                return ""
            else:
                return d.strftime("%a.%H:%M")

        data = []

        for number, etag in incidents_from_query(self.ims, request):
            incident = self.ims.storage.read_incident_with_number(number)

            if incident.summary:
                summary = incident.summary
            elif incident.report_entries:
                for entry in incident.report_entries:
                    if not entry.system_entry:
                        summary = entry.text
                        break
            else:
                summary = ""

            data.append([
                incident.number,
                incident.priority,
                format_date(incident.created),
                format_date(incident.dispatched),
                format_date(incident.on_scene),
                format_date(incident.closed),
                ", ".join(ranger.handle for ranger in incident.rangers),
                str(incident.location),
                ", ".join(incident.incident_types),
                summary,
            ])

        return to_json_text(data)
Exemple #10
0
 def list_incidents(self, request):
     #set_response_header(request, HeaderName.etag, "*") # FIXME
     set_response_header(request, HeaderName.contentType, ContentType.JSON)
     return to_json_text(tuple(incidents_from_query(self, request)))
Exemple #11
0
    def load(self):
        configParser = SafeConfigParser()

        def readConfig(configFile):
            for okFile in configParser.read((configFile.path,)):
                log.msg("Read configuration file: {0}".format(configFile.path))

        def valueFromConfig(section, option, default):
            try:
                value = configParser.get(section, option)
                if value:
                    return value
                else:
                    return default
            except (NoSectionError, NoOptionError):
                return default

        def filePathFromConfig(section, option, root, segments):
            if section is None:
                path = None
            else:
                path = valueFromConfig(section, option, None)

            if path is None:
                fp = root
                for segment in segments: 
                    fp = fp.child(segment)

            elif path.startswith("/"):
                fp = FilePath(path)

            else:
                fp = root
                for segment in path.split(os.path.sep):
                    fp = fp.child(segment)

            return fp

        readConfig(self.configFile)

        self.ServerRoot = filePathFromConfig(
            "Core", "ServerRoot",
            self.configFile.parent().parent(), ()
        )
        log.msg("Server root: {0}".format(self.ServerRoot.path))

        self.ConfigRoot = filePathFromConfig("Core", "ConfigRoot", self.ServerRoot, ("conf",))
        log.msg("Config root: {0}".format(self.ConfigRoot.path))

        self.UserDB = filePathFromConfig("Core", "UserDB", self.ConfigRoot, ("users.pwdb",))
        log.msg("User DB: {0}".format(self.UserDB.path))

        self.DataRoot = filePathFromConfig("Core", "DataRoot", self.ServerRoot, ("data",))
        log.msg("Data root: {0}".format(self.DataRoot.path))

        self.Resources = filePathFromConfig("Core", "Resources", self.ServerRoot, ("resources",))
        log.msg("Resources: {0}".format(self.Resources.path))

        rejectClients = valueFromConfig("Core", "RejectClients", "")
        rejectClients = tuple([e for e in rejectClients.split("\n") if e])
        
        self.RejectClients = rejectClients
        self.RejectClientsRegex = tuple([regex_compile(e) for e in rejectClients])
        log.msg("RejectClients: {0}".format(self.RejectClients))

        self.DMSHost     = valueFromConfig("DMS", "Hostname", None)
        self.DMSDatabase = valueFromConfig("DMS", "Database", None)
        self.DMSUsername = valueFromConfig("DMS", "Username", None)
        self.DMSPassword = valueFromConfig("DMS", "Password", None)

        self.IncidentTypes = (
            "Admin",
            "Art",
            "Assault",
            "Commerce",
            "Echelon",
            "Eviction",
            "Fire",
            "Gate",
            "Green Dot",
            "HQ",
            "Law Enforcement",
            "Lost Child",
            "Medical",
            "Mental Health",
            "MOOP",
            "SITE",
            "Staff",
            "Theme Camp",
            "Vehicle",

            "Junk",
        )

        #
        # Persist some objects
        #

        self.dms = DutyManagementSystem(
            host     = self.DMSHost,
            database = self.DMSDatabase,
            username = self.DMSUsername,
            password = self.DMSPassword,
        )

        storage = Storage(self.DataRoot)
        storage.provision()
        self.storage = storage

        self.IncidentTypesJSON = to_json_text(self.IncidentTypes)