Add a missing key in a nested dictionary?

Cyrille MODIANO

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
}]
martineau

Here's something for Python 2 that does what I think you want. I left some print() calls it in which display the contents of a couple of important data structures that are built and used internally. Seeing what's in them should make it easier to understand how it works.

from __future__ import print_function
from collections import defaultdict
from pprint import pformat
from random import randint
from series import data

def indent(text, amount, char=' '):
    """ Indent each line of text by indicated number of characters. """
    padding = amount * char
    return ''.join(padding+line for line in text.splitlines(True))

def GetRandomHexColor():
    return '#{:06X}'.format(randint(0, 0xffffff))


# Extract the needed information from data grouped so that the sample times
# are grouped together by sql_id.
timestamps = defaultdict(dict)
for row in data:
    timestamps[row['sql_id']][row['sample_time']] = row['resources_consumed']
print('timestamps:')
print(indent(pformat(dict(timestamps)), 4))

# Create a sorted list of all the unique timestamps that exist.
master_series = sorted(set(stamp for stamps in timestamps.values()
                            for stamp in stamps))
print()
print('master_series:')
print(indent(pformat(master_series), 4))

# Create list of entries where each has a sublist that consists of values for
# every timestamp in the master_series.
datasets = [{'label': sql_id,
             'backgroundColor': GetRandomHexColor(),
             'data': [{'y': timestamps[sql_id][stamp]
                                if stamp in timestamps[sql_id] else 0,
                       'x': stamp} for stamp in master_series]
            } for sql_id in sorted(timestamps)]
print()
print('datasets:')
print(indent(pformat(datasets), 4))

Output:

timestamps:
    {u'0m9b1dywgrdqj': {u'2017-12-23 10:42': 1, u'2017-12-23 10:47': 2},
     u'1xc91cuvu7j11': {u'2017-12-23 10:43': 3},
     u'3qkhfbf2kyvhk': {u'2017-12-23 10:41': 1,
                        u'2017-12-23 10:42': 17,
                        u'2017-12-23 10:43': 26,
                        u'2017-12-23 10:44': 17,
                        u'2017-12-23 10:45': 3,
                        u'2017-12-23 10:47': 10,
                        u'2017-12-23 10:48': 4},
     u'50kcsz2gh1w84': {u'2017-12-23 10:42': 1,
                        u'2017-12-23 10:43': 1,
                        u'2017-12-23 10:44': 1,
                        u'2017-12-23 10:45': 1,
                        u'2017-12-23 10:47': 1},
     u'7726bj0dhtnmt': {u'2017-12-23 10:45': 11},
     u'8u125dk9nfc0q': {u'2017-12-23 10:42': 8,
                        u'2017-12-23 10:43': 8,
                        u'2017-12-23 10:44': 11,
                        u'2017-12-23 10:45': 13,
                        u'2017-12-23 10:47': 11,
                        u'2017-12-23 10:48': 5},
     u'cmx7t67z8wa74': {u'2017-12-23 10:45': 19},
     u'dp0vgyb1hsfjb': {u'2017-12-23 10:42': 1,
                        u'2017-12-23 10:44': 2,
                        u'2017-12-23 10:47': 1},
     u'ft7wcqu3hzvca': {u'2017-12-23 10:41': 2,
                        u'2017-12-23 10:42': 8,
                        u'2017-12-23 10:43': 3,
                        u'2017-12-23 10:44': 3,
                        u'2017-12-23 10:48': 1}}

master_series:
    [u'2017-12-23 10:41',
     u'2017-12-23 10:42',
     u'2017-12-23 10:43',
     u'2017-12-23 10:44',
     u'2017-12-23 10:45',
     u'2017-12-23 10:47',
     u'2017-12-23 10:48']

datasets:
    [{'backgroundColor': '#BBF2C0',
      'data': [{'x': u'2017-12-23 10:41', 'y': 0},
               {'x': u'2017-12-23 10:42', 'y': 1},
               {'x': u'2017-12-23 10:43', 'y': 0},
               {'x': u'2017-12-23 10:44', 'y': 0},
               {'x': u'2017-12-23 10:45', 'y': 0},
               {'x': u'2017-12-23 10:47', 'y': 2},
               {'x': u'2017-12-23 10:48', 'y': 0}],
      'label': u'0m9b1dywgrdqj'},
     {'backgroundColor': '#09BC4F',
      'data': [{'x': u'2017-12-23 10:41', 'y': 0},
               {'x': u'2017-12-23 10:42', 'y': 0},
               {'x': u'2017-12-23 10:43', 'y': 3},
               {'x': u'2017-12-23 10:44', 'y': 0},
               {'x': u'2017-12-23 10:45', 'y': 0},
               {'x': u'2017-12-23 10:47', 'y': 0},
               {'x': u'2017-12-23 10:48', 'y': 0}],
      'label': u'1xc91cuvu7j11'},
     {'backgroundColor': '#19F805',
      'data': [{'x': u'2017-12-23 10:41', 'y': 1},
               {'x': u'2017-12-23 10:42', 'y': 17},
               {'x': u'2017-12-23 10:43', 'y': 26},
               {'x': u'2017-12-23 10:44', 'y': 17},
               {'x': u'2017-12-23 10:45', 'y': 3},
               {'x': u'2017-12-23 10:47', 'y': 10},
               {'x': u'2017-12-23 10:48', 'y': 4}],
      'label': u'3qkhfbf2kyvhk'},
     {'backgroundColor': '#A85778',
      'data': [{'x': u'2017-12-23 10:41', 'y': 0},
               {'x': u'2017-12-23 10:42', 'y': 1},
               {'x': u'2017-12-23 10:43', 'y': 1},
               {'x': u'2017-12-23 10:44', 'y': 1},
               {'x': u'2017-12-23 10:45', 'y': 1},
               {'x': u'2017-12-23 10:47', 'y': 1},
               {'x': u'2017-12-23 10:48', 'y': 0}],
      'label': u'50kcsz2gh1w84'},
     {'backgroundColor': '#9FEC4A',
      'data': [{'x': u'2017-12-23 10:41', 'y': 0},
               {'x': u'2017-12-23 10:42', 'y': 0},
               {'x': u'2017-12-23 10:43', 'y': 0},
               {'x': u'2017-12-23 10:44', 'y': 0},
               {'x': u'2017-12-23 10:45', 'y': 11},
               {'x': u'2017-12-23 10:47', 'y': 0},
               {'x': u'2017-12-23 10:48', 'y': 0}],
      'label': u'7726bj0dhtnmt'},
     {'backgroundColor': '#4FBF10',
      'data': [{'x': u'2017-12-23 10:41', 'y': 0},
               {'x': u'2017-12-23 10:42', 'y': 8},
               {'x': u'2017-12-23 10:43', 'y': 8},
               {'x': u'2017-12-23 10:44', 'y': 11},
               {'x': u'2017-12-23 10:45', 'y': 13},
               {'x': u'2017-12-23 10:47', 'y': 11},
               {'x': u'2017-12-23 10:48', 'y': 5}],
      'label': u'8u125dk9nfc0q'},
     {'backgroundColor': '#ED6D56',
      'data': [{'x': u'2017-12-23 10:41', 'y': 0},
               {'x': u'2017-12-23 10:42', 'y': 0},
               {'x': u'2017-12-23 10:43', 'y': 0},
               {'x': u'2017-12-23 10:44', 'y': 0},
               {'x': u'2017-12-23 10:45', 'y': 19},
               {'x': u'2017-12-23 10:47', 'y': 0},
               {'x': u'2017-12-23 10:48', 'y': 0}],
      'label': u'cmx7t67z8wa74'},
     {'backgroundColor': '#8E3D97',
      'data': [{'x': u'2017-12-23 10:41', 'y': 0},
               {'x': u'2017-12-23 10:42', 'y': 1},
               {'x': u'2017-12-23 10:43', 'y': 0},
               {'x': u'2017-12-23 10:44', 'y': 2},
               {'x': u'2017-12-23 10:45', 'y': 0},
               {'x': u'2017-12-23 10:47', 'y': 1},
               {'x': u'2017-12-23 10:48', 'y': 0}],
      'label': u'dp0vgyb1hsfjb'},
     {'backgroundColor': '#1FC7DC',
      'data': [{'x': u'2017-12-23 10:41', 'y': 2},
               {'x': u'2017-12-23 10:42', 'y': 8},
               {'x': u'2017-12-23 10:43', 'y': 3},
               {'x': u'2017-12-23 10:44', 'y': 3},
               {'x': u'2017-12-23 10:45', 'y': 0},
               {'x': u'2017-12-23 10:47', 'y': 0},
               {'x': u'2017-12-23 10:48', 'y': 1}],
      'label': u'ft7wcqu3hzvca'}]

Here's the series.py module that's imported. All it does is define the list of sample data you now have in your question:

""" Define data from database. """

data = [
    {
        '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
    }
]

Happy holidays!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Accessing key, value in a nested dictionary

Add Key in Dictionary dynamically

Get Value of a Nested Key in Dictionary

Add all of the key values in a nested Dictionary

nested list/dictionary comprehensions how to add a missing element

Add opposite number of values in each key of nested dictionary

Sum values of a key in a nested dictionary

Finding key in a Nested dictionary

Add new element to inner dictionary in nested dictionary

Get the key with the maximum nested key in a python dictionary

Splitting a nested dictionary based on value of nested key

How to sort a nested dictionary by a nested key

Python - get key in nested dictionary

Python add to list in nested dictionary

Nested dictionary key and values

Add up values of same key in a nested dictionary

How to add new key in the existing dictionary and derive a nested dictionary from it in python?

How to add a key/value pair into nested dictionary

Add padding to values of nested dictionary?

Python [Nested Dictionary] - How to add new key/values to create this structure of dictionary: d = {key:{key:value, key:value, key:value}}

Insert NaN if key is missing in dictionary

Getting Key in Nested Dictionary

How to add values in column by key in nested dictionary of list in python

add value of nested dictionary if key exists

convert nested tuples in lists into a dictionary and add new key values

Nested Dictionary using enum as key

Accessing the first key in a nested Dictionary

How to add a key:pair value to a nested dictionary, mapped to an item

How to add key value pairs to dictionary of list when key is missing

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive