Skip to content

Instantly share code, notes, and snippets.

View jgeurts's full-sized avatar

Jim Geurts jgeurts

View GitHub Profile
@jgeurts
jgeurts / EnumerableExtensions.cs
Created January 17, 2012 14:26
Select list enumerable extensions
public static class EnumerableExtensions
{
public static IEnumerable<SelectListItem> ToSelectListItems<T>(this IEnumerable<T> items, Func<T, string> name, Func<T, string> value, string selectedValue)
{
return items.ToSelectListItems(name, value, selectedValue, false);
}
public static IEnumerable<SelectListItem> ToSelectListItems<T>(this IEnumerable<T> items, Func<T, string> name, Func<T, string> value, string selectedValue, bool includeNotApplicable, string notApplicableValue = "", string notApplicableText = "(Not Applicable)")
{
return items.ToSelectListItems(name, value, x => value(x) == selectedValue, includeNotApplicable, notApplicableValue, notApplicableText);
@jgeurts
jgeurts / gist:1855675
Created February 17, 2012 21:52
AuthSub authentication for Google api
public ActionResult Auth()
{
const string scope = "https://www.google.com/analytics/feeds/";
var next = Url.FullPath("~/home/authresponse");
var url = AuthSubUtil.getRequestUrl(next, scope, false, true);
return Redirect(url);
}
public ActionResult AuthResponse(string token)
{
@jgeurts
jgeurts / gist:1855718
Created February 17, 2012 22:08
Query the Google Analytics account feed to get a list of available sites
var authFactory = new GAuthSubRequestFactory("analytics", ApplicationName)
{
Token = settings.SessionToken
};
var analytics = new AnalyticsService(authFactory.ApplicationName) { RequestFactory = authFactory };
foreach (AccountEntry entry in analytics.Query(new AccountQuery()).Entries)
{
var account = entry.Properties.First(x => x.Name == "ga:accountName").Value;
if (!model.Sites.ContainsKey(account))
@jgeurts
jgeurts / gist:1855745
Created February 17, 2012 22:15
Get general analytics stats stats
var siteUsage = new DataQuery(settings.SiteId, from, to)
{
Metrics = "ga:visits,ga:pageviews,ga:percentNewVisits,ga:avgTimeOnSite,ga:entranceBounceRate,ga:exitRate,ga:pageviewsPerVisit,ga:avgPageLoadTime"
};
var siteUsageResult = (DataEntry)analytics.Query(siteUsage).Entries.FirstOrDefault();
if (siteUsageResult != null)
{
foreach (var metric in siteUsageResult.Metrics)
{
switch (metric.Name)
@jgeurts
jgeurts / gist:1855768
Created February 17, 2012 22:20
Get a list of most visited pages
var topPages = new DataQuery(settings.SiteId, from, to)
{
Metrics = "ga:pageviews",
Dimensions = "ga:pagePath,ga:pageTitle",
Sort = "-ga:pageviews",
NumberToRetrieve = 20
};
foreach (DataEntry entry in analytics.Query(topPages).Entries)
{
var value = entry.Metrics.First().IntegerValue;
@jgeurts
jgeurts / StructureMapDependencyResolver.cs
Last active January 25, 2016 22:38
StructureMap MVC 4 dependency resolver adapter
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Practices.ServiceLocation;
using StructureMap;
// This is part of MvcKickstart (https://nuget.org/packages/mvckickstart)
namespace MvcKickstart.Infrastructure
{
/// <summary>
@jgeurts
jgeurts / gist:2992417
Last active October 6, 2015 12:18
Configure StructureMap dependency resolver
using System.Web.Mvc;
using MvcKickstart.Infrastructure;
using StructureMap;
[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(YourProject.IocConfig), "PreStart", Order = -100)]
// This gets installed automatically with the MvcKickstart nuget (https://nuget.org/packages/mvckickstart)
namespace YourProject
{
public static class IocConfig
@jgeurts
jgeurts / install-graphite-ubuntu-12.04.sh
Created July 14, 2012 16:36 — forked from tkoeppen/install-graphite-ubuntu-10.04.sh
Install Graphite 0.9.10 on Ubuntu 12.04
####################################
# BASIC REQUIREMENTS
# http://graphite.wikidot.com/installation
# http://geek.michaelgrace.org/2011/09/how-to-install-graphite-on-ubuntu/
# Last tested & updated 10/13/2011
####################################
cd
sudo apt-get update
sudo apt-get upgrade
@jgeurts
jgeurts / AndroidAutoIncrementVersion.cs
Created July 17, 2012 20:03
Update android version strings
using System.IO;
using System.Text.RegularExpressions;
namespace AndroidAutoIncrementVersion
{
class Program
{
static void Main(string[] args)
{
try
@jgeurts
jgeurts / gist:3372865
Created August 16, 2012 19:22
Missing statsd dashboard additions to Graphite
# Add to /opt/graphite/webapp/graphite/render/functions.py (Around the "def movingAverage(" line):
def historicalAverage(requestContext, seriesLists, points=7, offset=86400, highPassCount=1, lowPassCount=1):
historicalLists = []
for series in seriesLists:
hlist = []
for i in range(1,points+1):
tShift = str(offset * i) + "s"
shiftedSeries = timeShift(requestContext, [series], tShift)
hlist.append(shiftedSeries[0])