I have a dictionary used to build a chart from data coming from the database. I have the following working code:
datasets = []
for row in data:
# add serie if not exists already
if not any(d['label'] == row['sql_id'] for d in datasets):
serie = {'label':row['sql_id'],'backgroundColor':GetRandomHexColor(),'data':[]}
datasets.append(serie)
serie = next(item for item in datasets if item['label'] == row['sql_id'])
serie['data'].append({'x': row['sample_time'],'y':row['resources_consumed']})
This build a dictionnary of this form:
{
"datasets": [{
"data": [{
"y": 3,
"x": "2017-12-22 16:01"
}, {
"y": 23,
"x": "2017-12-22 16:02"
}, {
"y": 33,
"x": "2017-12-22 16:03"
}, {
"y": 12,
"x": "2017-12-22 16:04"
}, {
"y": 5,
"x": "2017-12-22 16:05"
}, {
"y": 13,
"x": "2017-12-22 16:06"
}, {
"y": 17,
"x": "2017-12-22 16:11"
}, {
"y": 24,
"x": "2017-12-22 16:12"
}, {
"y": 12,
"x": "2017-12-22 16:13"
}, {
"y": 9,
"x": "2017-12-22 16:14"
}, {
"y": 10,
"x": "2017-12-22 16:15"
}, {
"y": 24,
"x": "2017-12-22 16:16"
}, {
"y": 28,
"x": "2017-12-22 16:17"
}, {
"y": 4,
"x": "2017-12-22 16:18"
}, {
"y": 18,
"x": "2017-12-22 16:19"
}, {
"y": 25,
"x": "2017-12-22 16:20"
}, {
"y": 25,
"x": "2017-12-22 16:21"
}, {
"y": 14,
"x": "2017-12-22 16:22"
}, {
"y": 10,
"x": "2017-12-22 16:23"
}, {
"y": 9,
"x": "2017-12-22 16:24"
}],
"backgroundColor": "#01F79A",
"label": "3qkhfbf2kyvhk"
}, {
"data": [{
"y": 3,
"x": "2017-12-22 16:01"
}, {
"y": 14,
"x": "2017-12-22 16:02"
}, {
"y": 12,
"x": "2017-12-22 16:03"
}, {
"y": 9,
"x": "2017-12-22 16:04"
}, {
"y": 7,
"x": "2017-12-22 16:05"
}, {
"y": 20,
"x": "2017-12-22 16:06"
}, {
"y": 2,
"x": "2017-12-22 16:10"
}, {
"y": 16,
"x": "2017-12-22 16:11"
}, {
"y": 10,
"x": "2017-12-22 16:12"
}, {
"y": 11,
"x": "2017-12-22 16:13"
}, {
"y": 9,
"x": "2017-12-22 16:14"
}, {
"y": 15,
"x": "2017-12-22 16:15"
}, {
"y": 13,
"x": "2017-12-22 16:16"
}, {
"y": 8,
"x": "2017-12-22 16:17"
}, {
"y": 8,
"x": "2017-12-22 16:18"
}, {
"y": 12,
"x": "2017-12-22 16:19"
}, {
"y": 14,
"x": "2017-12-22 16:20"
}, {
"y": 13,
"x": "2017-12-22 16:21"
}, {
"y": 12,
"x": "2017-12-22 16:22"
}, {
"y": 9,
"x": "2017-12-22 16:23"
}, {
"y": 8,
"x": "2017-12-22 16:24"
}],
"backgroundColor": "#743967",
"label": "8u125dk9nfc0q"
}, {
"data": [{
"y": 1,
"x": "2017-12-22 16:02"
}, {
"y": 1,
"x": "2017-12-22 16:03"
}, {
"y": 1,
"x": "2017-12-22 16:04"
}, {
"y": 2,
"x": "2017-12-22 16:11"
}, {
"y": 1,
"x": "2017-12-22 16:12"
}, {
"y": 2,
"x": "2017-12-22 16:15"
}, {
"y": 2,
"x": "2017-12-22 16:16"
}, {
"y": 1,
"x": "2017-12-22 16:17"
}, {
"y": 2,
"x": "2017-12-22 16:19"
}, {
"y": 1,
"x": "2017-12-22 16:20"
}, {
"y": 1,
"x": "2017-12-22 16:22"
}, {
"y": 1,
"x": "2017-12-22 16:24"
}],
"backgroundColor": "#CA3582",
"label": "b9nbhsbx8tqz5"
}, {
"data": [{
"y": 1,
"x": "2017-12-22 16:02"
}, {
"y": 1,
"x": "2017-12-22 16:04"
}, {
"y": 2,
"x": "2017-12-22 16:11"
}, {
"y": 1,
"x": "2017-12-22 16:12"
}, {
"y": 1,
"x": "2017-12-22 16:14"
}, {
"y": 2,
"x": "2017-12-22 16:15"
}, {
"y": 1,
"x": "2017-12-22 16:19"
}, {
"y": 2,
"x": "2017-12-22 16:20"
}, {
"y": 1,
"x": "2017-12-22 16:22"
}, {
"y": 1,
"x": "2017-12-22 16:24"
}],
"backgroundColor": "#8697A2",
"label": "dp0vgyb1hsfjb"
}, {
"data": [{
"y": 5,
"x": "2017-12-22 16:04"
}, {
"y": 4,
"x": "2017-12-22 16:05"
}, {
"y": 8,
"x": "2017-12-22 16:13"
}, {
"y": 1,
"x": "2017-12-22 16:14"
}, {
"y": 9,
"x": "2017-12-22 16:18"
}, {
"y": 8,
"x": "2017-12-22 16:22"
}, {
"y": 1,
"x": "2017-12-22 16:23"
}],
"backgroundColor": "#034D27",
"label": "7726bj0dhtnmt"
}, {
"data": [{
"y": 12,
"x": "2017-12-22 16:04"
}, {
"y": 12,
"x": "2017-12-22 16:13"
}, {
"y": 12,
"x": "2017-12-22 16:18"
}, {
"y": 10,
"x": "2017-12-22 16:22"
}],
"backgroundColor": "#B3FDF5",
"label": "cmx7t67z8wa74"
}, {
"data": [{
"y": 2,
"x": "2017-12-22 16:04"
}, {
"y": 1,
"x": "2017-12-22 16:05"
}, {
"y": 4,
"x": "2017-12-22 16:12"
}, {
"y": 1,
"x": "2017-12-22 16:15"
}, {
"y": 1,
"x": "2017-12-22 16:17"
}, {
"y": 1,
"x": "2017-12-22 16:22"
}, {
"y": 2,
"x": "2017-12-22 16:24"
}],
"backgroundColor": "#3A74FB",
"label": "ft7wcqu3hzvca"
}, {
"data": [{
"y": 7,
"x": "2017-12-22 16:05"
}, {
"y": 6,
"x": "2017-12-22 16:14"
}, {
"y": 6,
"x": "2017-12-22 16:18"
}, {
"y": 6,
"x": "2017-12-22 16:23"
}],
"backgroundColor": "#9733FC",
"label": "7mwz4m103nn1k"
}, {
"data": [{
"y": 8,
"x": "2017-12-22 16:05"
}, {
"y": 9,
"x": "2017-12-22 16:14"
}, {
"y": 8,
"x": "2017-12-22 16:18"
}, {
"y": 12,
"x": "2017-12-22 16:23"
}],
"backgroundColor": "#383B19",
"label": "9nrjf616y6g22"
}]
}
The problem is I need to add missing timeseries in case they don't exists in each data list.
If serie 1 is a date "2017-12-22 16:23", each other series need the date with or without a value. If the value doesn't exists in an array then I need to add it with a 0 values for y
.
I don't know how to efficiently do that without doing a lot of nested for
loops.
EDIT:
Currently I have something like that:
{
"datasets": [{
"data": [{
"y": 3,
"x": "2017-12-22 16:01"
}, {
"y": 23,
"x": "2017-12-22 16:02"
}, {
"y": 33,
"x": "2017-12-22 16:03"
}, {
"y": 12,
"x": "2017-12-22 16:04"
}, {
"y": 5,
"x": "2017-12-22 16:05"
}],
"backgroundColor": "#01F79A",
"label": "3qkhfbf2kyvhk"
}, {
"data": [{
"y": 9,
"x": "2017-12-22 16:04"
}, {
"y": 7,
"x": "2017-12-22 16:05"
}, {
"y": 20,
"x": "2017-12-22 16:06"
}, {
"y": 2,
"x": "2017-12-22 16:10"
}, {
"y": 16,
"x": "2017-12-22 16:11"
}],
"backgroundColor": "#743967",
"label": "8u125dk9nfc0q"
}]
}
What I want is that:
{
"datasets": [{
"data": [{
"y": 3,
"x": "2017-12-22 16:01"
}, {
"y": 23,
"x": "2017-12-22 16:02"
}, {
"y": 33,
"x": "2017-12-22 16:03"
}, {
"y": 12,
"x": "2017-12-22 16:04"
}, {
"y": 5,
"x": "2017-12-22 16:05"
},{
"y": 0,
"x": "2017-12-22 16:06"
}, {
"y": 0,
"x": "2017-12-22 16:10"
}, {
"y": 0,
"x": "2017-12-22 16:11"
}],
"backgroundColor": "#01F79A",
"label": "3qkhfbf2kyvhk"
}, {
"data": [{
"y": 0,
"x": "2017-12-22 16:01"
}, {
"y": 0,
"x": "2017-12-22 16:02"
}, {
"y": 0,
"x": "2017-12-22 16:03"
},{
"y": 9,
"x": "2017-12-22 16:04"
}, {
"y": 7,
"x": "2017-12-22 16:05"
}, {
"y": 20,
"x": "2017-12-22 16:06"
}, {
"y": 2,
"x": "2017-12-22 16:10"
}, {
"y": 16,
"x": "2017-12-22 16:11"
}],
"backgroundColor": "#743967",
"label": "8u125dk9nfc0q"
}]
}
Each different timestamp must exists for each serie. If a timestamp does not exists in a serie I must add it with a 0 y value.
Here is the initial data I get from the database:
Query:
cursor.execute('SELECT strftime(\'%%Y-%%m-%%d %%H:%%M\',s.sample_time) as sample_time,\n'
' id,\n'
' dbid,\n'
' sql_id,\n'
' sql_plan_hash_value,\n'
' sid,\n'
' serial,\n'
' count(sql_id) as resources_consumed\n'
'FROM sash s\n'
'where sql_id in (select sql_id from (\n'
' select\n'
' id,\n'
' dbid,\n'
' SQL_ID ,\n'
' sql_plan_hash_value,\n'
' sid,\n'
' serial,\n'
' count(*) as resources_consumed\n'
' from sash\n'
' where sid = %s \n'
' and serial = %s \n'
' and dbid = %s \n'
' group by sql_id,sql_plan_hash_value,sid,serial,dbid\n'
' order by resources_consumed desc LIMIT 10)\n'
' )\n'
'and sample_time between datetime(\'now\',\'localtime\',\'-60 minutes\') and datetime(\'now\',\'localtime\') \n'
'and sid= %s \n'
'and serial= %s \n'
'group by strftime(\'%%Y-%%m-%%d %%H:%%M\',s.sample_time),sql_id,sql_plan_hash_value,sid,serial,dbid\n'
'order by strftime(\'%%Y-%%m-%%d %%H:%%M\',s.sample_time)', [sid, serial, dbid, sid, serial])
Output:
[{
'sql_id': u'3qkhfbf2kyvhk',
'sql_plan_hash_value': 2234478098L,
'sample_time': u'2017-12-23 10:41',
'dbid': 312109145,
'sid': 64,
'serial': 16655,
'id': 59389,
'resources_consumed': 1
}, {
'sql_id': u'ft7wcqu3hzvca',
'sql_plan_hash_value': 2265968010L,
'sample_time': u'2017-12-23 10:41',
'dbid': 312109145,
'sid': 64,
'serial': 16655,
'id': 59395,
'resources_consumed': 2
}, {
'sql_id': u'0m9b1dywgrdqj',
'sql_plan_hash_value': 3103504081L,
'sample_time': u'2017-12-23 10:42',
'dbid': 312109145,
'sid': 64,
'serial': 16655,
'id': 59469,
'resources_consumed': 1
}, {
'sql_id': u'3qkhfbf2kyvhk',
'sql_plan_hash_value': 2234478098L,
'sample_time': u'2017-12-23 10:42',
'dbid': 312109145,
'sid': 64,
'serial': 16655,
'id': 59511,
'resources_consumed': 17
}, {
'sql_id': u'50kcsz2gh1w84',
'sql_plan_hash_value': 2667639044L,
'sample_time': u'2017-12-23 10:42',
'dbid': 312109145,
'sid': 64,
'serial': 16655,
'id': 59421,
'resources_consumed': 1
}, {
'sql_id': u'8u125dk9nfc0q',
'sql_plan_hash_value': 2470916118L,
'sample_time': u'2017-12-23 10:42',
'dbid': 312109145,
'sid': 64,
'serial': 16655,
'id': 59503,
'resources_consumed': 8
}, {
'sql_id': u'dp0vgyb1hsfjb',
'sql_plan_hash_value': 3272358443L,
'sample_time': u'2017-12-23 10:42',
'dbid': 312109145,
'sid': 64,
'serial': 16655,
'id': 59454,
'resources_consumed': 1
}, {
'sql_id': u'ft7wcqu3hzvca',
'sql_plan_hash_value': 2265968010L,
'sample_time': u'2017-12-23 10:42',
'dbid': 312109145,
'sid': 64,
'serial': 16655,
'id': 59510,
'resources_consumed': 8
}, {
'sql_id': u'1xc91cuvu7j11',
'sql_plan_hash_value': 3080963105L,
'sample_time': u'2017-12-23 10:43',
'dbid': 312109145,
'sid': 64,
'serial': 16655,
'id': 59555,
'resources_consumed': 3
}, {
'sql_id': u'3qkhfbf2kyvhk',
'sql_plan_hash_value': 2234478098L,
'sample_time': u'2017-12-23 10:43',
'dbid': 312109145,
'sid': 64,
'serial': 16655,
'id': 59577,
'resources_consumed': 26
}, {
'sql_id': u'50kcsz2gh1w84',
'sql_plan_hash_value': 2667639044L,
'sample_time': u'2017-12-23 10:43',
'dbid': 312109145,
'sid': 64,
'serial': 16655,
'id': 59532,
'resources_consumed': 1
}, {
'sql_id': u'8u125dk9nfc0q',
'sql_plan_hash_value': 2470916118L,
'sample_time': u'2017-12-23 10:43',
'dbid': 312109145,
'sid': 64,
'serial': 16655,
'id': 59578,
'resources_consumed': 8
}, {
'sql_id': u'ft7wcqu3hzvca',
'sql_plan_hash_value': 2265968010L,
'sample_time': u'2017-12-23 10:43',
'dbid': 312109145,
'sid': 64,
'serial': 16655,
'id': 59565,
'resources_consumed': 3
}, {
'sql_id': u'3qkhfbf2kyvhk',
'sql_plan_hash_value': 2234478098L,
'sample_time': u'2017-12-23 10:44',
'dbid': 312109145,
'sid': 64,
'serial': 16655,
'id': 59644,
'resources_consumed': 17
}, {
'sql_id': u'50kcsz2gh1w84',
'sql_plan_hash_value': 2667639044L,
'sample_time': u'2017-12-23 10:44',
'dbid': 312109145,
'sid': 64,
'serial': 16655,
'id': 59623,
'resources_consumed': 1
}, {
'sql_id': u'8u125dk9nfc0q',
'sql_plan_hash_value': 2470916118L,
'sample_time': u'2017-12-23 10:44',
'dbid': 312109145,
'sid': 64,
'serial': 16655,
'id': 59642,
'resources_consumed': 11
}, {
'sql_id': u'dp0vgyb1hsfjb',
'sql_plan_hash_value': 3272358443L,
'sample_time': u'2017-12-23 10:44',
'dbid': 312109145,
'sid': 64,
'serial': 16655,
'id': 59614,
'resources_consumed': 2
}, {
'sql_id': u'ft7wcqu3hzvca',
'sql_plan_hash_value': 2265968010L,
'sample_time': u'2017-12-23 10:44',
'dbid': 312109145,
'sid': 64,
'serial': 16655,
'id': 59648,
'resources_consumed': 3
}, {
'sql_id': u'3qkhfbf2kyvhk',
'sql_plan_hash_value': 2234478098L,
'sample_time': u'2017-12-23 10:45',
'dbid': 312109145,
'sid': 64,
'serial': 16655,
'id': 59665,
'resources_consumed': 3
}, {
'sql_id': u'50kcsz2gh1w84',
'sql_plan_hash_value': 2667639044L,
'sample_time': u'2017-12-23 10:45',
'dbid': 312109145,
'sid': 64,
'serial': 16655,
'id': 59649,
'resources_consumed': 1
}, {
'sql_id': u'7726bj0dhtnmt',
'sql_plan_hash_value': 453825145,
'sample_time': u'2017-12-23 10:45',
'dbid': 312109145,
'sid': 64,
'serial': 16655,
'id': 59718,
'resources_consumed': 11
}, {
'sql_id': u'8u125dk9nfc0q',
'sql_plan_hash_value': 2470916118L,
'sample_time': u'2017-12-23 10:45',
'dbid': 312109145,
'sid': 64,
'serial': 16655,
'id': 59707,
'resources_consumed': 13
}, {
'sql_id': u'cmx7t67z8wa74',
'sql_plan_hash_value': 4270729444L,
'sample_time': u'2017-12-23 10:45',
'dbid': 312109145,
'sid': 64,
'serial': 16655,
'id': 59705,
'resources_consumed': 19
}, {
'sql_id': u'0m9b1dywgrdqj',
'sql_plan_hash_value': 3103504081L,
'sample_time': u'2017-12-23 10:47',
'dbid': 312109145,
'sid': 64,
'serial': 16655,
'id': 59765,
'resources_consumed': 2
}, {
'sql_id': u'3qkhfbf2kyvhk',
'sql_plan_hash_value': 2234478098L,
'sample_time': u'2017-12-23 10:47',
'dbid': 312109145,
'sid': 64,
'serial': 16655,
'id': 59768,
'resources_consumed': 10
}, {
'sql_id': u'50kcsz2gh1w84',
'sql_plan_hash_value': 2667639044L,
'sample_time': u'2017-12-23 10:47',
'dbid': 312109145,
'sid': 64,
'serial': 16655,
'id': 59770,
'resources_consumed': 1
}, {
'sql_id': u'8u125dk9nfc0q',
'sql_plan_hash_value': 2470916118L,
'sample_time': u'2017-12-23 10:47',
'dbid': 312109145,
'sid': 64,
'serial': 16655,
'id': 59764,
'resources_consumed': 11
}, {
'sql_id': u'dp0vgyb1hsfjb',
'sql_plan_hash_value': 3272358443L,
'sample_time': u'2017-12-23 10:47',
'dbid': 312109145,
'sid': 64,
'serial': 16655,
'id': 59754,
'resources_consumed': 1
}, {
'sql_id': u'3qkhfbf2kyvhk',
'sql_plan_hash_value': 2234478098L,
'sample_time': u'2017-12-23 10:48',
'dbid': 312109145,
'sid': 64,
'serial': 16655,
'id': 59779,
'resources_consumed': 4
}, {
'sql_id': u'8u125dk9nfc0q',
'sql_plan_hash_value': 2470916118L,
'sample_time': u'2017-12-23 10:48',
'dbid': 312109145,
'sid': 64,
'serial': 16655,
'id': 59788,
'resources_consumed': 5
}, {
'sql_id': u'ft7wcqu3hzvca',
'sql_plan_hash_value': 2265968010L,
'sample_time': u'2017-12-23 10:48',
'dbid': 312109145,
'sid': 64,
'serial': 16655,
'id': 59789,
'resources_consumed': 1
}]