Data collector using SQL operators package​
​By Damian Calin
The objective is to implement data collectors using SQL functions and run them in background using Wolfram ScheduledTask framework.
The basic idea is to schedule a query to collect data at given frequency and monitor the datapoints in real time .
In the following example I set up a data collector to get cpu/memory usage data and monitor the results in real time.

Load SQL package

Load the last version of SQLOperators package containing SQL function developed so far.
See my previous post : SQL operators as Wolfram Language functions (https://community.wolfram.com/groups/-/m/t/2195893 )
The full package could be find @
​https://www.wolframcloud.com/env/damcalrom/Published/SQLOperators.wl
CloudImport["https://www.wolframcloud.com/env/damcalrom/Published/SQLOperators.wl"];

Demo using a data collector to monitor cpu/memory usage in real time

I use SystemProcessData[] function to get current processes.
SystemProcessData[]
Convert SystemProcessData[] to a SQL table and calculate cpu/memory usage using aggregate SQL functions
tbprocesses:=tableSQL[SystemProcessData[]//Normal//Cases[#,x_(x//Values)]&,SystemProcessData[]//First//Normal//Keys]//​​summarySQL[#,<|"memory":>UnitConvert[Total[Memory],"Megabytes"],​​ "cpu"(Total[(UnitConvert[UserTime,"Seconds"]+UnitConvert[SystemTime,"Seconds"])/UnitConvert[RealTime,"Seconds"]])|>]&//​​addColumnsSQL[#,<|"timestamp"DateObject[]|>]&;​​​​tbprocesses//​​showTableSQL
Total Rows:1 Elapsed:1.75854
Out[]//TableForm=
memory
cpu
timestamp
6685.46
MB
0.40545
Mon 19 Sep 2022 15:27:03GMT+2.
Main function​setDataCollector[query,timespec] Input query - SQL table used to collect data timespec - time specification of ScheduledTask Output Association containing some attributes “data” - SQL table containing collected data “startTime” - collector starttime “task” - ScheduledTask object used by collector.
SetAttributes[setDataCollector,HoldAll];​​setDataCollector[query_,timespec_]:=Module[{res,resfinal,starttime,endtime,task},res=tableSQL[{},query["header"]//Keys];​​ starttime=DateObject[];task=SessionSubmit[ScheduledTask[res=unionSQL[query,res];resfinal=res;endtime=DateObject[],timespec]];Association["data"->resfinal,"startTime"->starttime,"elapsedTime"->(endtime-starttime),"definition"(query//Definition),"task"->task​​] ​​];
Setting up data collector​The query will be launch every 5 seconds for 100 times.The collected data is in table format.
datacol=setDataCollector[tbprocesses,{5,100}];
Querying the data collected
datacol["data"]//​​showTableSQL​​
Total Rows:4 Elapsed:5.8×
-6
10
Out[]//TableForm=
memory
cpu
timestamp
7013.15
MB
0.65662
Mon 19 Sep 2022 16:16:12GMT+2.
7011.55
MB
0.65741
Mon 19 Sep 2022 16:16:07GMT+2.
7013.15
MB
0.65829
Mon 19 Sep 2022 16:16:02GMT+2.
7013.63
MB
0.65966
Mon 19 Sep 2022 16:15:57GMT+2.
Using Dynamic to display data in realtime
In[]:=
Dynamic[datacol["data"]//​​summaryMapSQL[#,{cpu,memory},TimeSeries[#,{timestamp}]//​​ DateListPlot[{#,{#//Normal//Last}},PlotStyle{Automatic,Directive[Red,PointSize[0.05]]},Joined{True,False}]&&]&//​​showTableSQL]​​
Total Rows:1 Elapsed:0.007463
We can use datacol [“task”] to control the ScheduleTask behind data collector
If I want to stop the task :
datacol["task"]//TaskRemove
Out[]=
TaskObject
Task UUID: 6250e266-a1c7-42cc-b519-1be580848342
Task environment: Session
Task type: Scheduled
Evaluation expression: res$747837=unionSQLtbprocesses,res$747837;resfinal$747837=res$747837;endtime$747837=DateObject[]


Donations

If you enjoy the project or find it useful , any donations are welcomed : BTC: bc1qm06aatgwn8ducjtexu6cf0wle0d9r00fy7yp7y ETH: 0x8615b4cdb7aae1ea8fcb63ea7c6b6ccd2a88fbefor via paystring : calin.damian.crypto$paystring.crypto.com