github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/src/com/sap/piper/analytics/InfluxData.groovy (about)

     1  package com.sap.piper.analytics
     2  
     3  import com.cloudbees.groovy.cps.NonCPS
     4  
     5  class InfluxData implements Serializable{
     6  
     7      // each Map in influxCustomDataMap represents a measurement in Influx.
     8      // Additional measurements can be added as a new Map entry of influxCustomDataMap
     9      protected Map fields = [jenkins_custom_data: [:], pipeline_data: [:], step_data: [:]]
    10      // each Map in influxCustomDataMapTags represents tags for certain measurement in Influx.
    11      // Tags are required in Influx for easier querying data
    12      protected Map tags = [jenkins_custom_data: [:], pipeline_data: [:], step_data: [:]]
    13  
    14      public Map getFields(){ return fields }
    15      public Map getTags(){ return tags }
    16  
    17      protected static InfluxData instance
    18  
    19      @NonCPS
    20      public static InfluxData getInstance(){
    21          if(!instance) instance = new InfluxData()
    22          return instance
    23      }
    24  
    25      public static void addField(String measurement, String key, def value) {
    26          add(getInstance().getFields(), measurement, key, value)
    27      }
    28  
    29      public static void addTag(String measurement, String key, def value) {
    30          add(getInstance().getTags(), measurement, key, value)
    31      }
    32  
    33      protected static void add(Map dataMap, String measurement, String field, def value) {
    34          if (!dataMap[measurement]) dataMap[measurement] = [:]
    35          dataMap[measurement][field] = value
    36      }
    37  
    38      public static void reset(){
    39          instance = null
    40      }
    41  
    42      public static void readFromDisk(script) {
    43          script.echo "Transfer Influx data"
    44          def pathPrefix = '.pipeline/influx/'
    45          List influxDataFiles = script.findFiles(glob: "${pathPrefix}**")?.toList()
    46  
    47          influxDataFiles.each({f ->
    48              script.echo "Reading file from disk: ${f}"
    49              List parts = f.toString().replace(pathPrefix, '')?.split('/')?.toList()
    50  
    51              if(parts?.size() == 3){
    52                  def type = parts?.get(1)
    53  
    54                  if(type in ['fields', 'tags']){
    55                      def fileContent = script.readFile(f.getPath())
    56                      def measurement = parts?.get(0)
    57                      def name = parts?.get(2)
    58                      def value
    59                      if (name.endsWith(".json")){
    60                          script.echo "reading JSON content: " + fileContent
    61                          name = name.replace(".json","")
    62                          // net.sf.json.JSONSerializer does only handle lists and maps
    63                          // http://json-lib.sourceforge.net/apidocs/net/sf/json/package-summary.html
    64                          try{
    65                              value = script.readJSON(text: fileContent)
    66                          }catch(net.sf.json.JSONException e){
    67                              // try to wrap the value in an object and read again
    68                              if (e.getMessage() == "Invalid JSON String"){
    69                                  value = script.readJSON(text: "{\"content\": ${fileContent}}").content
    70                              }else{
    71                                  throw e
    72                              }
    73                          }
    74                      }else{
    75                          // handle boolean values
    76                          if(fileContent == 'true'){
    77                              value = true
    78                          }else if(fileContent == 'false'){
    79                              value = false
    80                          }else{
    81                              value = fileContent
    82                          }
    83                      }
    84  
    85                      if(type == 'fields'){
    86                          addField(measurement, name, value)
    87                      }else{
    88                          addTag(measurement, name, value)
    89                      }
    90                  }
    91              } else {
    92                  script.echo "skipped, illegal path"
    93              }
    94          })
    95      }
    96  }