<commit_before>#
# Project:
#   glideinWMS
#
# File Version: 
#   $Id: rrdSupport.py,v 1.8.10.1 2010/09/08 03:10:47 parag Exp $
#
# Description:
#   This module implements the basic functions needed
#   to interface to rrdtool
#
# Author:
#   <NAME>
#

import string,time

class BaseRRDSupport:
    #############################################################
    def __init__(self,rrd_obj):
        self.rrd_obj=rrd_obj

    def isDummy(self):
        return (self.rrd_obj==None)

    #############################################################
    # The default will do nothing
    # Children should overwrite it, if needed
    def get_disk_lock(self,fname):
        return dummy_disk_lock()

    #############################################################
    # The default will do nothing
    # Children should overwrite it, if needed
    def get_graph_lock(self,fname):
        return dummy_disk_lock()

    #############################################################
    def create_rrd(self,
                   rrdfname,
                   rrd_step,rrd_archives,
                   rrd_ds):
        """
        Create a new RRD archive

        Arguments:
          rrdfname     - File path name of the RRD archive
          rrd_step     - base interval in seconds
          rrd_archives - list of tuples, each containing the following fileds (in order)
            CF    - consolidation function (usually AVERAGE)
            xff   - xfiles factor (fraction that can be unknown)
            steps - how many of these primary data points are used to build a consolidated data point
            rows  - how many generations of data values are kept
          rrd_ds       - a tuple containing the following fields (in order)
            ds-name   - attribute name
            DST       - Data Source Type (usually GAUGE)
            heartbeat - the maximum number of seconds that may pass between two updates before it becomes unknown
            min       - min value
            max       - max value
          
        For more details see
          http://oss.oetiker.ch/rrdtool/doc/rrdcreate.en.html
        """
        self.create_rrd_multi(rrdfname,
                              rrd_step,rrd_archives,
                              (rrd_ds,))
        return

    #############################################################
    def create_rrd_multi(self,
                         rrdfname,
                         rrd_step,rrd_archives,
                         rrd_ds_arr):
        """
        Create a new RRD archive

        Arguments:
          rrdfname     - File path name of the RRD archive
          rrd_step     - base interval in seconds
          rrd_archives - list of tuples, each containing the following fileds (in order)
            CF    - consolidation function (usually AVERAGE)
            xff   - xfiles factor (fraction that can be unknown)
            steps - how many of these primary data points are used to build a consolidated data point
            rows  - how many generations of data values are kept
          rrd_ds_arr   - list of tuples, each containing the following fields (in order)
            ds-name   - attribute name
            DST       - Data Source Type (usually GAUGE)
            heartbeat - the maximum number of seconds that may pass between two updates before it becomes unknown
            min       - min value
            max       - max value
          
        For more details see
          http://oss.oetiker.ch/rrdtool/doc/rrdcreate.en.html
        """
        if None==self.rrd_obj:
            return # nothing to do in this case

        start_time=(long(time.time()-1)/rrd_step)*rrd_step # make the start time to be aligned on the rrd_step boundary - needed for optimal resoultion selection 
        #print (rrdfname,start_time,rrd_step)+rrd_ds
        args=[str(rrdfname),'-b','%li'%start_time,'-s','%i'%rrd_step]
        for rrd_ds in rrd_ds_arr:
            args.append('DS:%s:%s:%i:%s:%s'%rrd_ds)
        for archive in rrd_archives:
            args.append("RRA:%s:%g:%i:%i"%archive)

        lck=self.get_disk_lock(rrdfname)
        try:
            self.rrd_obj.create(*args)
        finally:
            lck.close()
        return

    #############################################################
    def update_rrd(self,
                   rrdfname,
                   time,val):
        """
        Create an RRD archive with a new value

        Arguments:
          rrdfname - File path name of the RRD archive
          time     - When was the value taken
          val      - What vas the value
        """
        if None==self.rrd_obj:
            return # nothing to do in this case

        lck=self.get_disk_lock(rrdfname)
        try:
            self.rrd_obj.update(str(rrdfname),'%li:%i'%(time,val))
        finally:
            lck.close()

        return

    #############################################################
    def update_rrd_multi(self,
                         rrdfname,
                         time,val_dict):
        """
        Create an RRD archive with a set of values (possibly all of the supported)

        Arguments:
          rrdfname - File path name of the RRD archive
          time     - When was the value taken
          val_dict - What was the value
        """
        if None==self.rrd_obj:
            return # nothing to do in this case

        args=[str(rrdfname)]
        ds_names=val_dict.keys()
        ds_names.sort()

        ds_names_real=[]
        ds_vals=[]
        for ds_name in ds_names:
            if val_dict[ds_name]!=None:
                ds_vals.append("%i"%val_dict[ds_name])
                ds_names_real.append(ds_name)

        if len(ds_names_real)==0:
            return

        args.append('-t')
        args.append(string.join(ds_names_real,':'))
        args.append(('%li:'%time)+string.join(ds_vals,':'))
    
        lck=self.get_disk_lock(rrdfname)
        try:
            #print args
            self.rrd_obj.update(*args)
        finally:<commit_msg>Make it more generic; don't assume the values are integers<commit_after>#
# Project:
#   glideinWMS
#
# File Version: 
#   $Id: rrdSupport.py,v 1.8.10.2 2010/09/10 22:01:14 sfiligoi Exp $
#
# Description:
#   This module implements the basic functions needed
#   to interface to rrdtool
#
# Author:
#   <NAME>
#

import string,time

class BaseRRDSupport:
    #############################################################
    def __init__(self,rrd_obj):
        self.rrd_obj=rrd_obj

    def isDummy(self):
        return (self.rrd_obj==None)

    #############################################################
    # The default will do nothing
    # Children should overwrite it, if needed
    def get_disk_lock(self,fname):
        return dummy_disk_lock()

    #############################################################
    # The default will do nothing
    # Children should overwrite it, if needed
    def get_graph_lock(self,fname):
        return dummy_disk_lock()

    #############################################################
    def create_rrd(self,
                   rrdfname,
                   rrd_step,rrd_archives,
                   rrd_ds):
        """
        Create a new RRD archive

        Arguments:
          rrdfname     - File path name of the RRD archive
          rrd_step     - base interval in seconds
          rrd_archives - list of tuples, each containing the following fileds (in order)
            CF    - consolidation function (usually AVERAGE)
            xff   - xfiles factor (fraction that can be unknown)
            steps - how many of these primary data points are used to build a consolidated data point
            rows  - how many generations of data values are kept
          rrd_ds       - a tuple containing the following fields (in order)
            ds-name   - attribute name
            DST       - Data Source Type (usually GAUGE)
            heartbeat - the maximum number of seconds that may pass between two updates before it becomes unknown
            min       - min value
            max       - max value
          
        For more details see
          http://oss.oetiker.ch/rrdtool/doc/rrdcreate.en.html
        """
        self.create_rrd_multi(rrdfname,
                              rrd_step,rrd_archives,
                              (rrd_ds,))
        return

    #############################################################
    def create_rrd_multi(self,
                         rrdfname,
                         rrd_step,rrd_archives,
                         rrd_ds_arr):
        """
        Create a new RRD archive

        Arguments:
          rrdfname     - File path name of the RRD archive
          rrd_step     - base interval in seconds
          rrd_archives - list of tuples, each containing the following fileds (in order)
            CF    - consolidation function (usually AVERAGE)
            xff   - xfiles factor (fraction that can be unknown)
            steps - how many of these primary data points are used to build a consolidated data point
            rows  - how many generations of data values are kept
          rrd_ds_arr   - list of tuples, each containing the following fields (in order)
            ds-name   - attribute name
            DST       - Data Source Type (usually GAUGE)
            heartbeat - the maximum number of seconds that may pass between two updates before it becomes unknown
            min       - min value
            max       - max value
          
        For more details see
          http://oss.oetiker.ch/rrdtool/doc/rrdcreate.en.html
        """
        if None==self.rrd_obj:
            return # nothing to do in this case

        start_time=(long(time.time()-1)/rrd_step)*rrd_step # make the start time to be aligned on the rrd_step boundary - needed for optimal resoultion selection 
        #print (rrdfname,start_time,rrd_step)+rrd_ds
        args=[str(rrdfname),'-b','%li'%start_time,'-s','%i'%rrd_step]
        for rrd_ds in rrd_ds_arr:
            args.append('DS:%s:%s:%i:%s:%s'%rrd_ds)
        for archive in rrd_archives:
            args.append("RRA:%s:%g:%i:%i"%archive)

        lck=self.get_disk_lock(rrdfname)
        try:
            self.rrd_obj.create(*args)
        finally:
            lck.close()
        return

    #############################################################
    def update_rrd(self,
                   rrdfname,
                   time,val):
        """
        Create an RRD archive with a new value

        Arguments:
          rrdfname - File path name of the RRD archive
          time     - When was the value taken
          val      - What vas the value
        """
        if None==self.rrd_obj:
            return # nothing to do in this case

        lck=self.get_disk_lock(rrdfname)
        try:
            self.rrd_obj.update(str(rrdfname),'%li:%s'%(time,val))
        finally:
            lck.close()

        return

    #############################################################
    def update_rrd_multi(self,
                         rrdfname,
                         time,val_dict):
        """
        Create an RRD archive with a set of values (possibly all of the supported)

        Arguments:
          rrdfname - File path name of the RRD archive
          time     - When was the value taken
          val_dict - What was the value
        """
        if None==self.rrd_obj:
            return # nothing to do in this case

        args=[str(rrdfname)]
        ds_names=val_dict.keys()
        ds_names.sort()

        ds_names_real=[]
        ds_vals=[]
        for ds_name in ds_names:
            if val_dict[ds_name]!=None:
                ds_vals.append("%s"%val_dict[ds_name])
                ds_names_real.append(ds_name)

        if len(ds_names_real)==0:
            return

        args.append('-t')
        args.append(string.join(ds_names_real,':'))
        args.append(('%li:'%time)+string.join(ds_vals,':'))
    
        lck=self.get_disk_lock(rrdfname)
        try:
            #print args
            self.rrd_obj.update(*args)
        finally:
{'fraction_non_alphanumeric': 0.14097061736546715, 'fraction_numerical': 0.0034664905909541103, 'mean_word_length': 1.840365682137834, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '4113571', 'n_tokens_mistral': 3420, 'n_tokens_neox': 3255, 'n_words': 954}