0

I am currently exporting data from Grafana. I do that using a script which looks like this one:

JsonSlurper slurper = new JsonSlurper()

println "Collect list of servers:"

def result = slurper.parse(new URL('http://xyz'))

def servers = [:].withDefault { [] }
result.data.each{ it -> 
    servers[it.server] = "${it.server};${it.app};${it.team}" 
}
servers.sort().each { k, v -> println v}


println "Collect data for servers:"



Date currentDate = new Date();
Date fromDate = currentDate - 30
long unixTimeNow = currentDate.getTime() / 1000
long unixFromDate = fromDate.getTime() / 1000
long stepSec = 7200

boolean header = true
servers.sort().each{ server, label ->

 File resultFile = new File("C:\\Users\\GrafanaExport${server}.csv")
 if(resultFile.exists()) {
        resultFile.delete()
    }
    
    def queries = [
        ['node_load1',"http://abc{server}.start${unixFromDate}&end=${unixTimeNow}&step=${stepSec}"],]
    
    def resultTable = [:].withDefault { [";",";",";"] }
    queries.eachWithIndex { q, i ->
        println(q)
        result = slurper.parse(new URL(q[1]))
        result?.data?.result[0]?.values?.each{ it ->
            resultTable[it[0]][i] = it[1] + ";"
        }
    }

    if(header) {
        resultFile << "timestamp;" + queries.collect{it[0]}.join(';') + '\n'
        header = false
    }
    resultFile << label + '\n'
    resultTable.sort().each { k, v -> 
        resultFile << "${k};${v.join('')}\n"
    }
}

This works fine but what I want to get instead is not the data from now until some time ago but from a certain timestamp until some time ago. Because previously I already exported data and I would like to get the same dates/timestamps for the data. The previously exported data is stamped with this timestamps (snip of the example timestamps):

enter image description here

That means the data was exported every two hours (because of this a step of 7200 seconds) and at every full and in unix time every even hour. I now want to know how I need to amend my code in order to get the data for the same timestamps as before?

Thanks a lot!

Tobitor
  • 1,388
  • 1
  • 23
  • 58
  • 1
    `java.time.LocalDateTime.of(2020, 2, 23, 16, 42).toDate()` – cfrick Jan 19 '21 at 10:08
  • Ok, thank you! If I understand correctly you would use this to modify `currentDate`? For example if I would like to export data from 31st of December 2020 starting at 10 a.m. (unix time) until 30 days before this date (so 2nd of December e.g.) I should amend the line `Date currentDate = new Date();` to `Date currentDate = new Date(java.time.LocalDateTime.of(2020, 12, 31, 10, 00).toDate());`? Is that correct? Sorry for this question but I am very new to a language like Groovy. – Tobitor Jan 19 '21 at 11:13

0 Answers0