diff options
author | Tim Laszlo <tim.laszlo@gmail.com> | 2012-06-06 09:18:40 -0500 |
---|---|---|
committer | Tim Laszlo <tim.laszlo@gmail.com> | 2012-06-06 14:35:28 -0500 |
commit | 54f46a42ceb443528d54dd5734449dfeb5e79df4 (patch) | |
tree | 308a6d99333cde9d8fa199111de19f43488430c4 | |
parent | 6a968b1468fff08e0aeebae04dd7cf0888a4014e (diff) | |
download | bcfg2-54f46a42ceb443528d54dd5734449dfeb5e79df4.tar.gz bcfg2-54f46a42ceb443528d54dd5734449dfeb5e79df4.tar.bz2 bcfg2-54f46a42ceb443528d54dd5734449dfeb5e79df4.zip |
DBStats: Save interaction metadata
Start saving group and bundle membership for interactions
-rw-r--r-- | src/lib/Bcfg2/Server/Plugins/DBStats.py | 2 | ||||
-rwxr-xr-x | src/lib/Bcfg2/Server/Reports/importscript.py | 33 | ||||
-rw-r--r-- | src/lib/Bcfg2/Server/Reports/reports/models.py | 48 |
3 files changed, 81 insertions, 2 deletions
diff --git a/src/lib/Bcfg2/Server/Plugins/DBStats.py b/src/lib/Bcfg2/Server/Plugins/DBStats.py index 469fafa5c..ca948aabd 100644 --- a/src/lib/Bcfg2/Server/Plugins/DBStats.py +++ b/src/lib/Bcfg2/Server/Plugins/DBStats.py @@ -49,7 +49,7 @@ class DBStats(Bcfg2.Server.Plugin.Plugin, start = time.time() for i in [1, 2, 3]: try: - load_stat(metadata.hostname, + load_stat(metadata, newstats, self.core.encoding, 0, diff --git a/src/lib/Bcfg2/Server/Reports/importscript.py b/src/lib/Bcfg2/Server/Reports/importscript.py index 18500f0c1..2aade17b3 100755 --- a/src/lib/Bcfg2/Server/Reports/importscript.py +++ b/src/lib/Bcfg2/Server/Reports/importscript.py @@ -7,6 +7,7 @@ new statistics engine import binascii import os import sys +import traceback try: import Bcfg2.Server.Reports.settings except Exception: @@ -28,6 +29,7 @@ from getopt import getopt, GetoptError from datetime import datetime from time import strptime from django.db import connection +from Bcfg2.Server.Plugins.Metadata import ClientMetadata from Bcfg2.Server.Reports.Updater import update_database, UpdaterError import logging import Bcfg2.Logger @@ -93,7 +95,11 @@ def load_stats(sdata, encoding, vlevel, logger, quick=False, location=''): for statistics in node.findall('Statistics'): load_stat(name, statistics, encoding, vlevel, logger, quick, location) -def load_stat(client_name, statistics, encoding, vlevel, logger, quick, location): +def load_stat(cobj, statistics, encoding, vlevel, logger, quick, location): + if isinstance(cobj, ClientMetadata): + client_name = cobj.hostname + else: + client_name = cobj client, created = Client.objects.get_or_create(name=client_name) if created and vlevel > 0: logger.info("Client %s added to db" % client_name) @@ -125,6 +131,31 @@ def load_stat(client_name, statistics, encoding, vlevel, logger, quick, location logger.info("Interaction for %s at %s with id %s INSERTED in to db" % (client.id, timestamp, current_interaction.id)) + if isinstance(cobj, ClientMetadata): + try: + imeta = InteractionMetadata(interaction=current_interaction) + profile, created = Group.objects.get_or_create(name=cobj.profile) + imeta.profile = profile + imeta.save() # save here for m2m + + #FIXME - this should be more efficient + group_set = [] + for group_name in cobj.groups: + group, created = Group.objects.get_or_create(name=group_name) + if created: + logger.debug("Added group %s" % group) + imeta.groups.add(group) + for bundle_name in cobj.bundles: + bundle, created = Bundle.objects.get_or_create(name=bundle_name) + if created: + logger.debug("Added bundle %s" % bundle) + imeta.bundles.add(bundle) + imeta.save() + except: + logger.error("Failed to save interaction metadata for %s: %s" % + (client_name, traceback.format_exc().splitlines()[-1])) + + counter_fields = {TYPE_CHOICES[0]: 0, TYPE_CHOICES[1]: 0, TYPE_CHOICES[2]: 0} diff --git a/src/lib/Bcfg2/Server/Reports/reports/models.py b/src/lib/Bcfg2/Server/Reports/reports/models.py index 4983bad20..a9374bb7f 100644 --- a/src/lib/Bcfg2/Server/Reports/reports/models.py +++ b/src/lib/Bcfg2/Server/Reports/reports/models.py @@ -345,3 +345,51 @@ class InternalDatabaseVersion(models.Model): class Meta: get_latest_by = "version" + + +class Group(models.Model): + """ + Groups extracted from interactions + + name - The group name + + TODO - Most of this is for future use + TODO - set a default group + """ + + name = models.CharField(max_length=255, unique=True) + profile = models.BooleanField(default=False) + public = models.BooleanField(default=False) + category = models.CharField(max_length=1024, blank=True) + comment = models.TextField(blank=True) + + def __unicode__(self): + return self.name + + +class Bundle(models.Model): + """ + Bundles extracted from interactions + + name - The bundle name + """ + + name = models.CharField(max_length=255, unique=True) + + def __unicode__(self): + return self.name + + +class InteractionMetadata(models.Model): + """ + InteractionMetadata + + Hold extra data associated with the client and interaction + """ + + interaction = models.OneToOneField(Interaction, primary_key=True, related_name='metadata') + profile = models.ForeignKey(Group, related_name="+") + groups = models.ManyToManyField(Group) + bundles = models.ManyToManyField(Bundle) + + |