<commit_before>##
# Copyright (c) 2006-2007 Apple Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# DRI: <NAME>, <EMAIL>
##
"""
 Log Stats:
  # Invitations sent per day/week/month
  # bytes out (bytes in not provided in the current log format.)/
  # requests
  user agents

"""

import plistlib
import sys
import time

from twistedcaldav.admin import util

PLIST_VERSION = 4

statsTemplate = plistlib.Dict(
    version=PLIST_VERSION,
    bytesOut="0",
    startDate="",
    endDate="",
    requestStats={},
    timeOfDayStats=[0] * 96,
    requestByTimeOfDayStats={},
    invitations={
        "day":0, 
        "week":0, 
        "month":0, 
        },
    userAgents={},
    activeUsers={},
    )

def _strAdd(value, add):
    return str(long(value) + add)

class Stats(object):
    def __init__(self, fp):
        self.fp = fp
        self._data = None

        if self.fp.exists():
            self._data = plistlib.readPlist(self.fp.path)
            if self._data.version != PLIST_VERSION:
                self._data = None
        
        if self._data is None:
            self._data = statsTemplate
            self.save()

    def addDate(self, date):
        if not self._data.startDate:
            self._data.startDate = date
        self._data.endDate = date

    def getDateRange(self):
        return (self._data.startDate, self._data.endDate)

    def getBytes(self):
        return long(self._data.bytesOut)

    def addBytes(self, bytes):
        self._data.bytesOut = _strAdd(self._data.bytesOut, bytes)

    def addRequestStats(self, request, status, bytes, time):
        if request in self._data.requestStats:
            request_stat = self._data.requestStats[request]
            old_num = long(request_stat['num'])
            request_stat['num'] = _strAdd(request_stat['num'], 1)
            if status >= 200 and status < 300:
                request_stat['numOK'] = _strAdd(request_stat['numOK'], 1)
            elif status == 500:
                request_stat['numISE'] = _strAdd(request_stat['numISE'], 1)
            elif status >= 400 and status < 600:
                request_stat['numBAD'] = _strAdd(request_stat['numBAD'], 1)
            else:
                request_stat['numOther'] = _strAdd(request_stat['numOther'], 1)
            if bytes < request_stat['minbytes']:
                request_stat['minbytes'] = str(bytes)
            if bytes > request_stat['maxbytes']:
                request_stat['maxbytes'] = str(bytes)
            request_stat['avbytes'] = str((long(request_stat['avbytes']) * old_num + bytes) / (old_num + 1))
            if time < request_stat['mintime']:
                request_stat['mintime'] = time
            if time > request_stat['maxtime']:
                request_stat['maxtime'] = time
            request_stat['avtime'] = (request_stat['avtime'] * old_num + time) / (old_num + 1)
        else:
            self._data.requestStats[request] = {}
            request_stat = self._data.requestStats[request]
            request_stat['num'] = "1"
            request_stat['numOK'] = "0"
            request_stat['numBAD'] = "0"
            request_stat['numISE'] = "0"
            request_stat['numOther'] = "0"
            if status >= 200 and status < 300:<commit_msg>Fix for minimum, maximum per-request byte count values.<commit_after>##
# Copyright (c) 2006-2007 Apple Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# DRI: <NAME>, <EMAIL>
##
"""
 Log Stats:
  # Invitations sent per day/week/month
  # bytes out (bytes in not provided in the current log format.)/
  # requests
  user agents

"""

import plistlib
import sys
import time

from twistedcaldav.admin import util

PLIST_VERSION = 4

statsTemplate = plistlib.Dict(
    version=PLIST_VERSION,
    bytesOut="0",
    startDate="",
    endDate="",
    requestStats={},
    timeOfDayStats=[0] * 96,
    requestByTimeOfDayStats={},
    invitations={
        "day":0, 
        "week":0, 
        "month":0, 
        },
    userAgents={},
    activeUsers={},
    )

def _strAdd(value, add):
    return str(long(value) + add)

class Stats(object):
    def __init__(self, fp):
        self.fp = fp
        self._data = None

        if self.fp.exists():
            self._data = plistlib.readPlist(self.fp.path)
            if self._data.version != PLIST_VERSION:
                self._data = None
        
        if self._data is None:
            self._data = statsTemplate
            self.save()

    def addDate(self, date):
        if not self._data.startDate:
            self._data.startDate = date
        self._data.endDate = date

    def getDateRange(self):
        return (self._data.startDate, self._data.endDate)

    def getBytes(self):
        return long(self._data.bytesOut)

    def addBytes(self, bytes):
        self._data.bytesOut = _strAdd(self._data.bytesOut, bytes)

    def addRequestStats(self, request, status, bytes, time):
        if request in self._data.requestStats:
            request_stat = self._data.requestStats[request]
            old_num = long(request_stat['num'])
            request_stat['num'] = _strAdd(request_stat['num'], 1)
            if status >= 200 and status < 300:
                request_stat['numOK'] = _strAdd(request_stat['numOK'], 1)
            elif status == 500:
                request_stat['numISE'] = _strAdd(request_stat['numISE'], 1)
            elif status >= 400 and status < 600:
                request_stat['numBAD'] = _strAdd(request_stat['numBAD'], 1)
            else:
                request_stat['numOther'] = _strAdd(request_stat['numOther'], 1)
            if bytes < long(request_stat['minbytes']):
                request_stat['minbytes'] = str(bytes)
            if bytes > long(request_stat['maxbytes']):
                request_stat['maxbytes'] = str(bytes)
            request_stat['avbytes'] = str((long(request_stat['avbytes']) * old_num + bytes) / (old_num + 1))
            if time < request_stat['mintime']:
                request_stat['mintime'] = time
            if time > request_stat['maxtime']:
                request_stat['maxtime'] = time
            request_stat['avtime'] = (request_stat['avtime'] * old_num + time) / (old_num + 1)
        else:
            self._data.requestStats[request] = {}
            request_stat = self._data.requestStats[request]
            request_stat['num'] = "1"
            request_stat['numOK'] = "0"
            request_stat['numBAD'] = "0"
            request_stat['numISE'] = "0"
            request_stat['numOther'] = "0"
            if status >= 200 and status < 300:
{'fraction_non_alphanumeric': 0.11959388185654009, 'fraction_numerical': 0.013976793248945147, 'mean_word_length': 2.404398563734291, 'pattern_counts': {'":': 6, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4122992', 'n_tokens_mistral': 2329, 'n_tokens_neox': 2202, 'n_words': 588}