SHARE
TWEET

Untitled

a guest Dec 18th, 2019 91 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import httpclient, htmlparser, streams, xmlTree, strutils, csvtools, times
  2. import simple_parseopt
  3.  
  4. const
  5.   URL = "http://127.0.0.1:8000/silk/profiling/?show=250&order_by=Recent"
  6.   INNER_URL = "http://127.0.0.1:8000/silk/profile/{profile_id}"
  7.  
  8.  
  9. type
  10.   Row = object
  11.     overall: int
  12.     queries: int
  13.     numQueries: int
  14.  
  15.  
  16. proc get(filename: string): Response =
  17.   var browser = newHttpClient()
  18.   var response = browser.request(URL)
  19.  
  20.   return response
  21.  
  22. let options = get_options:
  23.   fromDT: string
  24.  
  25. when isMainModule:
  26.   let
  27.     response = get("test.csv")
  28.     body: StringStream = newStringStream(response.body)
  29.     parsedBody = parseHtml(body)
  30.  
  31.   var
  32.     rows = newSeq[Row]()
  33.     row = Row()
  34.     rowTs: Time
  35.   for cell in parsedBody.findAll("div"):
  36.     let outerClass = cell.attr "class"
  37.  
  38.     if outerClass == "timestamp-div":
  39.       let ts = cell.innerText.strip(trailing=true, chars={'m', 's'})
  40.       rowTs = ts.parseTime("HH:mm:ss\'.\'fff", utc())
  41.  
  42.     if outerClass == "time-taken-div":
  43.       let timeTaken = cell.child("span")
  44.       row.overall = parseInt(timeTaken.innerText.strip(trailing=true, chars={'m', 's'}))
  45.  
  46.     if outerClass == "time-taken-queries-div":
  47.       let queries = cell.child("span")
  48.       row.queries = parseInt(queries.innerText.strip(trailing=true, chars={'m', 's'}))
  49.  
  50.     if outerClass == "num-queries-div":
  51.       let numQueries = cell.child("span")
  52.       row.numQueries = parseInt(numQueries.innerText.strip(trailing=true, chars={'m', 's'}))
  53.       if options.fromDT.len > 0:
  54.         let filterDT = options.fromDT.parseTime("HH:mm:ss\'.\'fff", local())
  55.         if rowTs > filterDT:
  56.           rows.add(row)
  57.           row = Row()
  58.  
  59.   writeToCsv[Row](rows, "/home/ryan/profile_stats/results.csv")
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top