def new_document_pages_this_month(): DocumentPage = apps.get_model(app_label='documents', model_name='DocumentPage') qss = qsstats.QuerySetStats(DocumentPage.objects.all(), 'document_version__document__date_added') return qss.this_month() or '0'
def total_document_page_per_month(): DocumentPage = apps.get_model( app_label='documents', model_name='DocumentPage' ) qss = qsstats.QuerySetStats( DocumentPage.objects.all(), 'document_version__document__date_added' ) this_year = datetime.date.today().year result = [] for month in range(1, datetime.date.today().month + 1): next_month = month + 1 if month == 12: next_month = 1 year = this_year + 1 else: next_month = month + 1 year = this_year result.append( { force_text( MONTH_NAMES[month] ): qss.until(datetime.date(year, next_month, 1)) } ) return { 'series': { 'Pages': result } }
def total_document_version_per_month(): from .models import DocumentVersion qss = qsstats.QuerySetStats(DocumentVersion.objects.all(), 'document__date_added') this_year = datetime.date.today().year result = [] for month in range(1, datetime.date.today().month + 1): next_month = month + 1 if next_month == 12: next_month = 1 year = this_year + 1 else: next_month = month + 1 year = this_year result.append({month: qss.until(datetime.date(year, next_month, 1))}) return { 'series': { 'Versions': result } }
def edits_over_time(oldest_page): graph = pyflot.Flot() qss = qsstats.QuerySetStats(Page.versions.all(), 'history_date') graph.add_time_series(qss.time_series(oldest_page), label=_("pages")) qss = qsstats.QuerySetStats(MapData.versions.all(), 'history_date') graph.add_time_series(qss.time_series(oldest_page), label=_("maps")) qss = qsstats.QuerySetStats(PageFile.versions.all(), 'history_date') graph.add_time_series(qss.time_series(oldest_page), label=_("files")) qss = qsstats.QuerySetStats(Redirect.versions.all(), 'history_date') graph.add_time_series(qss.time_series(oldest_page), label=_("redirects")) return [graph.prepare_series(s) for s in graph._series]
def total_document_version_per_month(): DocumentVersion = apps.get_model(app_label='documents', model_name='DocumentVersion') qss = qsstats.QuerySetStats(DocumentVersion.objects.all(), 'document__date_added') now = timezone.now() result = [] for month in range(1, now.month + 1): next_month = month + 1 if month == 12: next_month = 1 year = now.year + 1 else: next_month = month + 1 year = now.year result.append({ force_text(MONTH_NAMES[month]): qss.until(timezone.datetime(year, next_month, 1, tzinfo=now.tzinfo)) }) return {'series': {'Versions': result}}
def total_document_page_per_month(): DocumentPage = apps.get_model( app_label='documents', model_name='DocumentPage' ) qss = qsstats.QuerySetStats( DocumentPage.objects.all(), 'document_version__document__date_added' ) now = timezone.now() result = [] for month in range(1, now.month + 1): next_month = month + 1 if month == 12: next_month = 1 year = now.year + 1 else: next_month = month + 1 year = now.year result.append( { get_month_name(month_number=month): qss.until( timezone.datetime(year, next_month, 1, tzinfo=now.tzinfo) ) } ) return { 'series': { 'Pages': result } }
def users_registered_over_time(oldest_page, filters): oldest_user = User.objects.order_by('date_joined')[0].date_joined graph = pyflot.Flot() qss = qsstats.QuerySetStats(User.objects.all(), 'date_joined') graph.add_time_series(_summed_series(qss.time_series(oldest_user))) return [graph.prepare_series(s) for s in graph._series]
def handle(self, **options): qs = Temperatura.objects.all() qss = qsstats.QuerySetStats(qs, 'pub_date') today = datetime.date.today() seven_days_ago = today - datetime.timedelta(days=7) time_series = qss.time_series(seven_days_ago, today) print 'New users in the last 7 days: %s' % [t[1] for t in time_series]
def get_date_stats(self, column='date_joined'): """ Renvoyer des statistiques de créations de profils """ qs = qsstats.QuerySetStats(self, column) return { 'minute': qs.this_minute(), 'hour': qs.this_hour(), 'day': qs.this_day(), 'month': qs.this_month(), 'year': qs.this_year() }
def run_stats(request_obj, **kwargs): """ Returns stats on a given request set. """ stats = {} try: # Average response time. stats['average_response'] = request_obj.filter(status="Closed") \ .extra({"average": "avg(updated_datetime - requested_datetime)"}) \ .values("average") stats['average_response'] = stats['average_response'][0][ "average"].days # Total request count. stats['request_count'] = request_obj.count() # Request types. if kwargs.has_key('request_types') is False: stats['request_types'] = request_obj.values('service_name') \ .annotate(count=Count('service_name')).order_by('-count')[:10] # Opened requests by day (limit: 30) time_delta = datetime.timedelta(days=30) latest = request_obj.latest('requested_datetime') qss = qsstats.QuerySetStats(request_obj, 'requested_datetime') time_series = qss.time_series(latest.requested_datetime - time_delta, latest.requested_datetime) stats['opened_by_day'] = [t[1] for t in time_series] # Open request count. stats['open_request_count'] = request_obj.filter(status="Open").count() # Closed request count. stats['closed_request_count'] = request_obj.filter( status="Closed").count() # Recently opened requests. if kwargs.has_key('open_requests') is False: stats['open_requests'] = request_obj.filter(status="Open") \ .order_by('-requested_datetime')[:10] except: stats['average_response'] = 0 stats['request_count'] = 0 stats['request_types'] = [] stats['open_request_count'] = 0 stats['closed_request_count'] = 0 stats['opened_by_day'] = [0] # Return return stats
def new_documents_per_month(): qss = qsstats.QuerySetStats(Document.passthrough.all(), 'date_added') today = datetime.date.today() this_year = datetime.date(year=today.year, month=1, day=1) return { 'series': { 'Documents': map(lambda x: {x[0].month: x[1]}, qss.time_series(start=this_year, end=today, interval='months')) } }
def new_document_pages_per_month(): qss = qsstats.QuerySetStats(DocumentPage.objects.all(), 'document_version__document__date_added') today = datetime.date.today() this_year = datetime.date(year=today.year, month=1, day=1) return { 'series': { 'Pages': map(lambda x: {x[0].month: x[1]}, qss.time_series(start=this_year, end=today, interval='months')) } }
def new_documents_this_month(user=None): AccessControlList = apps.get_model(app_label='acls', model_name='AccessControlList') Document = apps.get_model(app_label='documents', model_name='Document') queryset = Document.objects.all() if user: queryset = AccessControlList.objects.restrict_queryset( permission=permission_document_view, user=user, queryset=queryset) qss = qsstats.QuerySetStats(queryset, 'date_added') return qss.this_month() or '0'
def get_month_long_averages(self): monthly = {} for m in self.metrics: month_query = MetricRecord.objects.filter(metric=m, measurement__gt=0) monthly[m.name] = qsstats.QuerySetStats( month_query, 'datetime').time_series( datetime.date.today() - datetime.timedelta(days=self.days), datetime.date.today(), aggregate=Avg('measurement'), interval='months') return monthly
def get_last_30_day_averages(self): last_30_days = {} for m in self.metrics: if not m.monthly: measurements = MetricRecord.objects.filter(metric=m) last_30_days[m.name] = qsstats.QuerySetStats( measurements, 'datetime').time_series( datetime.date.today() - datetime.timedelta(days=30), datetime.date.today(), aggregate=Avg('measurement'), interval='days') return last_30_days
def estadisticas(request): GOOGLE_API_KEY = 'AIzaSyAQz45z6xJrAwMPHncF4vUgmbfGrCoHwGE' qs = User.objects.all() qss = qsstats.QuerySetStats(qs, 'date_joined') #hoy = datetime.date.today() #hace_2_semanas = hoy - datetime.timedelta(weeks=2) #users_stats = qss.time_series(hace_2_semanas, hoy) return render_to_response('bmpedido/estadisticas.html', locals(), context_instance=RequestContext(request))
def items_over_time(oldest_page, filters): graph = pyflot.Flot() pages_added = qsstats.QuerySetStats( Page.versions.filter(version_info__type__in=ADDED_TYPES, **filters), 'history_date').time_series(oldest_page) pages_deleted = qsstats.QuerySetStats( Page.versions.filter(version_info__type__in=DELETED_TYPES, **filters), 'history_date').time_series(oldest_page) num_pages_over_time = _sum_from_add_del(pages_added, pages_deleted) maps_added = qsstats.QuerySetStats( MapData.versions.filter(version_info__type__in=ADDED_TYPES, **filters), 'history_date').time_series(oldest_page) maps_deleted = qsstats.QuerySetStats( MapData.versions.filter(version_info__type__in=DELETED_TYPES, **filters), 'history_date').time_series(oldest_page) num_maps_over_time = _sum_from_add_del(maps_added, maps_deleted) files_added = qsstats.QuerySetStats( PageFile.versions.filter(version_info__type__in=ADDED_TYPES, **filters), 'history_date').time_series(oldest_page) files_deleted = qsstats.QuerySetStats( PageFile.versions.filter(version_info__type__in=DELETED_TYPES, **filters), 'history_date').time_series(oldest_page) num_files_over_time = _sum_from_add_del(files_added, files_deleted) redir_added = qsstats.QuerySetStats( Redirect.versions.filter(version_info__type__in=ADDED_TYPES, **filters), 'history_date').time_series(oldest_page) redir_deleted = qsstats.QuerySetStats( Redirect.versions.filter(version_info__type__in=DELETED_TYPES, **filters), 'history_date').time_series(oldest_page) num_redirects_over_time = _sum_from_add_del(redir_added, redir_deleted) graph.add_time_series(num_pages_over_time, label=_("pages")) graph.add_time_series(num_maps_over_time, label=_("maps")) graph.add_time_series(num_files_over_time, label=_("files")) graph.add_time_series(num_redirects_over_time, label=_("redirects")) return [graph.prepare_series(s) for s in graph._series]
def get_week_long_averages(self): weekly = {} for m in self.metrics: if not m.monthly: measurements = MetricRecord.objects.filter(metric=m) weekly[m.name] = qsstats.QuerySetStats( measurements, 'datetime').time_series(datetime.date.today() - datetime.timedelta(days=self.days), datetime.date.today(), aggregate=Avg('measurement'), interval='weeks') return weekly
def new_documents_per_month(): Document = apps.get_model(app_label='documents', model_name='Document') qss = qsstats.QuerySetStats(Document.passthrough.all(), 'date_added') now = timezone.now().date() start = timezone.datetime(year=now.year, month=1, day=1).date() return { 'series': { 'Documents': map(lambda x: {get_month_name(month_number=x[0].month): x[1]}, qss.time_series(start=start, end=now, interval='months')) } }
def new_documents_per_month(): Document = apps.get_model(app_label='documents', model_name='Document') qss = qsstats.QuerySetStats(Document.passthrough.all(), 'date_added') today = datetime.date.today() this_year = datetime.date(year=today.year, month=1, day=1) return { 'series': { 'Documents': map(lambda x: {force_text(MONTH_NAMES[x[0].month]): x[1]}, qss.time_series(start=this_year, end=today, interval='months')) } }
def get_events(obj, event_type_id, start, end): ####################################################################### # attention! this takes ~1 second per item! ####################################################################### # log.debug('get_events: {}'.format(obj)) qs = obj.events.filter(event_type_id=event_type_id) # qs = obj.events qss = qsstats.QuerySetStats(qs, 'created') time_series = qss.time_series(start, end, 'months') return time_series
def new_document_versions_per_month(): DocumentVersion = apps.get_model(app_label='documents', model_name='DocumentVersion') qss = qsstats.QuerySetStats(DocumentVersion.objects.all(), 'document__date_added') now = timezone.now().date() start = timezone.datetime(year=now.year, month=1, day=1).date() return { 'series': { 'Versions': map(lambda x: {force_text(MONTH_NAMES[x[0].month]): x[1]}, qss.time_series(start=start, end=now, interval='months')) } }
def new_document_versions_per_month(): DocumentVersion = apps.get_model(app_label='documents', model_name='DocumentVersion') qss = qsstats.QuerySetStats(DocumentVersion.objects.all(), 'document__date_added') today = datetime.date.today() this_year = datetime.date(year=today.year, month=1, day=1) return { 'series': { 'Versions': map(lambda x: {x[0].month: x[1]}, qss.time_series(start=this_year, end=today, interval='months')) } }
def get(self, request, format=None, **kwargs): app = request.GET.get("app") model = request.GET.get("model") column = request.GET.get("column") model_klass = apps.get_model(app_label=app, model_name=model) queryset = model_klass.objects.all() qs_stats = qsstats.QuerySetStats(queryset, column, aggregate=Count("pk")) xdata = ["Today", "This Week", "This Month"] ydata = [ qs_stats.this_day(), qs_stats.this_week(), qs_stats.this_month() ] extra_serie1 = { "tooltip": { "y_start": "", "y_end": " %s" % model_klass._meta.verbose_name_plural.title(), } } chartdata = { "x": xdata, "name1": "Hosts", "y1": ydata, "extra1": extra_serie1 } charttype = "discreteBarChart" chartcontainer = "%s_stats" % model.lower() context = { "charttype": charttype, "chartdata": chartdata, "chartcontainer": chartcontainer, "extra": { "x_is_date": False, "x_axis_format": "", "tag_script_js": True, "jquery_on_ready": False, }, } return Response(context, template_name="api/web/ipam_stats.html")
def handle(self, *args, **options): # for g in GeoPolygon.objects.all(): # q = g.wood_volume_per_ha/10 # print(q) # g.firerisk = q # g.save() # meteos = Meteocondition.objects.all() meteos = GeoPolygon.objects.all() # meteo = qsstats.QuerySetStats(meteos, 'pr') meteo = qsstats.QuerySetStats(meteos, 'firerisk') day1 = datetime.date(2012, 4, 1) day2 = datetime.date(2012, 4, 30) # time_series = meteo.time_series(day2, day1) time_series = meteo.time_series(day2, day1)
def get_day_of_week_averages(self): daily = {} for m in self.metrics: if not m.monthly: measurements = MetricRecord.objects.filter(metric=m) daily[m.name] = qsstats.QuerySetStats( measurements, 'datetime').time_series(datetime.date.today() - datetime.timedelta(days=self.days), datetime.date.today(), aggregate=Avg('measurement'), interval='days') for key, value in daily.items(): days_of_week = {'7': []} for i in range(1, 7): days_of_week[str(i)] = [] for measurement in value: days_of_week[str( measurement[0].isoweekday())].append(measurement) for day, measurements in days_of_week.items(): total = sum([r[1] for r in days_of_week[day]]) zeroes = 0 for i in days_of_week[day]: if i[1] < 1: zeroes += 1 if len(days_of_week[day]) - zeroes: calc = float(total / (len(days_of_week[day]) - zeroes)) else: calc = 0.0 days_of_week[day] = (None, calc) daily[key] = [v for d, v in days_of_week.items()] return daily
def new_document_pages_this_month(user=None): AccessControlList = apps.get_model( app_label='acls', model_name='AccessControlList' ) DocumentPage = apps.get_model( app_label='documents', model_name='DocumentPage' ) queryset = DocumentPage.objects.all() if user: queryset = AccessControlList.objects.filter_by_access( permission=permission_document_view, user=user, queryset=queryset ) qss = qsstats.QuerySetStats( queryset, 'document_version__document__date_added' ) return qss.this_month() or '0'
def total_document_per_month(): Document = apps.get_model(app_label='documents', model_name='Document') qss = qsstats.QuerySetStats(Document.objects.all(), 'date_added') this_year = datetime.date.today().year result = [] for month in range(1, datetime.date.today().month + 1): next_month = month + 1 if next_month == 12: next_month = 1 year = this_year + 1 else: next_month = month + 1 year = this_year result.append({month: qss.until(datetime.date(year, next_month, 1))}) return {'series': {'Documents': result}}
def init_with_context(self, context): self.site_name = get_admin_site_name(context) request = context["request"] # append intro module self.children.append( HTMLContentModule( "<strong>Welcome to the openIPAM.</strong>", html=""" <div style="margin: 10px 20px;"> <p> We are now using <a href="%(feature_request_link)s" target="_blank">Issues on GitHub</a> to help aid us with features and bugs. Please make an issue on GitHub to give us feedback. </p> <p>Item to consider when using the new interface:</p> <ul id="new-interface-list"> <li>Permissions - Do you have all your permissions?</li> <li>Hosts - Do you see all your hosts?</li> <li>DNS Entries - Do you see all DNS Entries?</li> </ul> <p>If you have any questions, please email: <a href="mailto:%(email)s">%(email)s</a></p> </div> """ % { "email": CONFIG.get("EMAIL_ADDRESS"), "feature_request_link": "https://github.com/openipam/django-openipam/issues/", }, )) self.children.append( HTMLContentModule( "Navigation", html=""" <ul> <li><a href="%(url_hosts)s">List Hosts</a></li> <li><a href="%(url_add_hosts)s">Add Host</a></li> <li><a href="%(url_dns)s">DNS Records</a></li> </ul> <ul> <li style="border-top: 1px solid #e5e5e5;"> <a href="%(url_feature_request)s">Feature or Bug?</a> </li> <li><a href="%(url_profile)s">Profile</a></li> </ul> """ % { "url_hosts": reverse_lazy("list_hosts"), "url_add_hosts": reverse_lazy("add_hosts"), "url_dns": reverse_lazy("list_dns"), "url_feature_request": reverse_lazy("feature_request"), "url_profile": reverse_lazy("profile"), }, )) if request.user.is_staff or request.user.is_superuser: # append an app list module for "Administration" self.children.append(IPAMAppList(_("Administration"), models=())) # append recent stats module hosts = Host.objects.all() hosts_stats = qsstats.QuerySetStats(hosts, "changed", aggregate=Count("mac"), today=datetime.now()) users = User.objects.all() users_stats = qsstats.QuerySetStats(users, "date_joined", today=datetime.now()) hosts_today = cache.get("hosts_today") hosts_week = cache.get("hosts_week") hosts_month = cache.get("hosts_month") if hosts_today is None: hosts_today = hosts_stats.this_day() cache.set("hosts_today", hosts_today) if hosts_week is None: hosts_week = hosts_stats.this_week() cache.set("hosts_week", hosts_week) if hosts_month is None: hosts_month = hosts_stats.this_month() cache.set("hosts_month", hosts_month) users_today = cache.get("users_today") users_week = cache.get("users_week") users_month = cache.get("users_month") if users_today is None: users_today = users_stats.this_day() cache.set("users_today", users_today) if users_week is None: users_week = users_stats.this_week() cache.set("users_week", users_week) if users_month is None: users_month = users_stats.this_month() cache.set("users_month", users_month) self.children.append( HTMLContentModule( "Recent Stats", html=""" <div style="margin: 10px 20px;" class="well well-sm"> <h5>Hosts</h5> <p><strong>%(hosts_today)s</strong> hosts changed today.</p> <p><strong>%(hosts_week)s</strong> hosts changed this week.</p> <p><strong>%(hosts_month)s</strong> hosts changed this month.</p> </div> <div style="margin: 10px 20px;" class="well well-sm"> <h5>Users</h5> <p><strong>%(users_today)s</strong> users joined today.</p> <p><strong>%(users_week)s</strong> users joined this week.</p> <p><strong>%(users_month)s</strong> users joined this month.</p> </div> """ % { "hosts_today": hosts_today, "hosts_week": hosts_week, "hosts_month": hosts_month, "users_today": users_today, "users_week": users_week, "users_month": users_month, }, )) # append a recent actions module self.children.append( modules.RecentActions(_("Recent Actions"), limit=5))
def get_graph_data(self, slug, *args, **kwargs): module = get_module_by_slug(slug)() q = ModuleStat.objects.filter(modulename=slug) qs = qsstats.QuerySetStats(q, 'added', module.get_aggregate_function() or Avg('value')) data = qs.time_series(self.startdate, self.stopdate, interval=self.interval) return data