Friday, May 6, 2011

Banner Impressions Tracking - Database Design

Hi Folks

Looking for some good advice on db design for tracking multiple banner impressions.

IE I have 5 Banners over x Domains

I would like to build data on each banner on how many impressions per day per banner etc. So also be able to do lookups for other date ranges.

Would it be best to have a date per day per row or just track each impression per row.

Hope you can advise.

And thanks in Advance

From stackoverflow
  • I'd recommend to create the most flexible design that would allow you to create new reports as requirements extend in the future. You suggest that the customer wants reports on "impressions per day". What if they come in later and say "what time of the day are impressions shown most at"? How about "when are they clicked on most"?

    So the most flexible way to do this is to have 1 record for each impression, where each record is just banner_id timestamp

    Later on, you can create a stored procedure that aggregates historical data and thus purges HUGE amounts of data that you have accumulated - thus, creating reports on the level of granularity that you care about. I can imagine storing hourly data for a month, and daily data for a year. The stored procs would just write to an archive table:

    • Banner ID
    • Time interval identifier (of the month/year for monthly data, or day/month/year for daily data, etc)
    • Number of impressions
  • Why reinvent the wheel? There are plenty of free ad servers. The most notable one I've heard of is OpenX (used to be phpAdsNew). If nothing else, you can install it and see how they set up their DB.

Like to store all command-line arguments to a bash script into a single variable

Let's say I have a bash script called foo.sh.

I'd like to call it like this

foo.sh Here is a bunch of stuff on the command-line

and I'd like it to store all of that text into a single variable and print it out.

So my output would be:

Here is a bunch of stuff on the command-line

How would I do this?

From stackoverflow
  • echo "$*"
    

    would do what you want, namely printing out the entire command-line arguments, separated by a space (or, technically, whatever the value of $IFS is). If you wanted to store it into a variable, you could do

    thevar="$*"
    

    If that doesn't answer your question well enough, I'm not sure what else to say...

    Mark Biek : Easier than I thought. Thanks!
  • Have a look at the $* variable. It combines all command line arguments into one.

    echo "$*"
    

    This should do what you want.

    More info here.

  • The command echo will take all arguments and print them out:

    echo Here is a bunch of stuff on the command-line
    

    You should quote the arguments if you have special characters.

  • If you want to avoid having $IFS involved, use $@ (or don't enclose $* in quotes)

    $ cat atsplat
    IFS="_"
    echo "     at: $@"
    echo "  splat: $*"
    echo "noquote: "$*
    
    $ ./atsplat this is a test
         at: this is a test
      splat: this_is_a_test
    noquote: this is a test
    

    The IFS behavior follows variable assignments, too.

    $ cat atsplat2
    IFS="_"
    atvar=$@
    splatvar=$*
    echo "     at: $atvar"
    echo "  splat: $splatvar"
    echo "noquote: "$splatvar
    
    $ ./atsplat2 this is a test
         at: this is a test
      splat: this_is_a_test
    noquote: this is a test
    

    Note that if the assignment to $IFS were made after the assignment of $splatvar, then all the outputs would be the same ($IFS would have no effect in the "atsplat2" example).

Gravatar for Iphone? How do I generate a hexadecimal MD5 hash?

I'd like to use gravatar in my iPhone application. Is there anyway to generate a hexadecimal MD5 hash in Objective-C for iPhone? Using openssl on iPhone is a no-go.

From stackoverflow
  • This is how I did it before I removed it from my app:

    #import <CommonCrypto/CommonDigest.h>
    
    NSString* md5( NSString *str ) {
      const char *cStr = [str UTF8String];
      unsigned char result[CC_MD5_DIGEST_LENGTH];
    
      CC_MD5( cStr, strlen(cStr), result );
    
      return [[NSString
          stringWithFormat:@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
          result[0], result[1],
          result[2], result[3],
          result[4], result[5],
          result[6], result[7],
          result[8], result[9],
          result[10], result[11],
          result[12], result[13],
          result[14], result[15]
          ] lowercaseString];
    }
    

    It's only fair to add that I didn't write this myself. I found it somewhere on the internet but I didn't record where.

  • The code I used for generating the necessary MD5 hash is up on my github repository, in the CommonCrypto subfolder. There are a bunch of similar routines in there which will either show you how to use CommonCrypto or how to format strings of hex byte values, base-64, etc.

    A potentially better way of generating the string would be:

    NSMutableString * str = [[NSMutableString alloc] initWithCapacity: 33];
    int i;
    for ( i = 0; i < 16; i++ )
    {
      [str appendFormat: @"%02x", result[i]];
    }
    NSString * output = [str copy];
    [str release];
    return ( [output autorelease] );
    

    If you're going to use the code in the answer above, however, I'd personally suggest changing the %02X's to %02x and forgoing the -lowercaseString call completely-- might as well generate the hex values lowercase to start with.

How can I duplicate a website's settings in IIS7?

I'm going mad trying to get an ASP.NET application running on IIS7 (Server2008).

I have one web site set up in IIS7 which works fine. I'm trying to create another one now but I only get 404s for all .aspx files. The Handling Mappers are all different from the working site's. Is there any way I can duplicate the working site's settings so I don't have to go through 50 mappings manually.

Or is there a better way to get the correct settings?

From stackoverflow
  • For IIS 5 (and 6?), Microsoft had a free download called MetaEdit. It was used for just this - backing up and restoring an IIS configuration from machine to machine. Check MS's web site to see if there is an updated version for IIS 7.

  • If you are merely trying to re-install the default ASP.NET mappings, the best way to do that is use

    aspnet_regiis.exe
    

    which is in the %windir%\Microsoft.NET\Framework\v___ folders (depends on which version you're trying to set -- looks like 3.5?)

    Use the documentation at http://msdn.microsoft.com/en-us/library/k6h9cz8h.aspx to see whether you want to use the -i, -r, or -s option.

    Mr. Flibble : This didn't seem to make any changes. Everything is already setup as per aspnet_regiis.exe.
  • IIS7's default configurations are held in c:\windows\system32\inetsrv\config\applicationHost.config

    Any changes you make on a per site/application basis are stored in the web.config for that site/application.

    You should be able to simply copy the files onto the second server.

    Mr. Flibble : Copying the section from a working site to the new one made it work. Cheers.

Working with a hashtable of unknown but similar objects (C#)

I have a hash table which can contain any number of objects. All of these objects implement some similar methods / properties and some of their own.

For example all objects in the hashtable may have a method called PrintText taking a single parameter of type string. All the objects are however instantiated from different classes.

Is it possible for me to pull out a particular object from the hashtable by its key without knowing its type before runtime, and access all its own methods and properties (not just the common ones)?

Normally I would do something like,

MyClass TheObject = MyHashTable[Key];

But the object being pulled out could be derived from any class so I cannot do that in this instance.

From stackoverflow
  • You could define an interface containing the common methods and properties, and implement this interface in all your classes. Then you can easily access these methods and properties.

    But to access the specific methods of an object (not contained in the interface), you will need to know the type of the object.

    Update:

    It's not clear from your question, but when you write about a hashtable, I assume you mean the Hashtable class. In that case, you should have a look at the generic Dictionary class (available since .NET 2.0). This class will make your code typesafe and saves you from a lot of type-casting, e.g:

    IMyInterface a = new MyObject();
    
    // with Hashtable
    Hashtable ht = new Hashtable();
    ht.Add("key", a);
    IMyInterface b = (IMyInterface)ht["key"];
    
    // with Dictionary
    var dic = new Dictionary<string, IMyInterface>();
    dic.Add("key", a);
     // no cast required, value objects are of type IMyInterface :
    IMyInterface c = dic["key"];
    
    Craig Bovis : Exactly what I needed, thanks Martin. I didn't realise Interfaces could be used in this way!
  • To solve problems with common methods and properties you can solve by making your classes to implement the same interface. However, I don't see how you can access non-common members. You can try to use Reflection.

  • dynamic in C# 4. Reflection in earlier versions.

    EDIT: In some cases, defining a common interface can be both an efficient and clear way of achieving something of the nature you describe. ('inspired' by the accepted answer and/or others mentioning it - can't remember the timeline)

    Marc Gravell : While duck-typing will work, an interface (as per the accepted answer) would be vastly preferable. Dynamic should be seen as a last resort in most cases (with the exception of COM/DLR where it is much more useful). But +1 for mentioning it.
    Ruben Bartelink : Yep, agree. (Also I nabbed the reference to interfaces from other responses, but didnt declare it by putting an EDIT: in front of the text (though I was open in the description of the edit!))
  • You can say:

    object theObject = MyHashTable[Key];
    

    Then you can say:

    theObject.GetType()
      .GetMethod("PrintText")
      .Invoke(theObject, new object[] {"paramvalue"});
    

UIView animations interacting badly

Hi--

I'm seeing what appears to be interaction between separate animations, and I'd really appreciate any suggestions to eliminate this effect.

Basically: I've got an iPhone app which includes a button 'a' on the root view. Tapping 'a' pushes a view on a navigation view controller stack, with a flip animation. The pushed view has a button to pop the view back to the root.

The underlying code:

- (IBAction)pushOneView{
TheAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationTransition: UIViewAnimationTransitionFlipFromRight 
                    forView:delegate.navigationController.view cache:NO];
[delegate.navigationController 
            pushViewController:oneViewController animated:NO];
[UIView commitAnimations];

}

This seems to work fine, and the animation is quite smooth.

The root view also includes a subview ('panelView'), and another button, 'b'. panelView can display either of two other subviews--tapping 'b' swaps between those subviews, with a spin animation. The code:

-(IBAction)swapPanels{
UIViewController *coming;
UIViewController *going;
float rotation;

if (self.aPanel.view.superview == nil) {
    coming = aPanel;
    going = bPanel;
    rotation = 3.14;
}
else {
    coming = bPanel;
    going = aPanel;
    rotation = -3.14;
}

// First half of spin
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.25];
CGAffineTransform swirlTransform = CGAffineTransformMakeRotation(rotation);
panelView.transform = swirlTransform;
[panelView setAlpha:0.1];
[UIView commitAnimations];

// Finish spin
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.25];
CGAffineTransform secondTransform = 
                        CGAffineTransformRotate(swirlTransform, rotation);
panelView.transform = secondTransform;
[panelView setAlpha:1];
[UIView commitAnimations];

// Swap the panels
[going.view removeFromSuperview];
[panelView insertSubview:coming.view atIndex:0];

}

This also seems to work fine. Each of the swappable panels contains a picker and some labels.

However, I notice that the 'a' transition becomes slow and jerky if the 'b' transition has been executed before it. In other words, if I start the app, run 'a' back and forth several times, it runs smoothly. Then exercise 'b' back and forth a few times. Then try 'a' again...'a' is now jerky, and will remain so until an app restart.

This is 100% repeatable. It is subtle using the simulator, but quite obvious on a device. I have tested for leaks--none are shown by leaks tool. If the animation is removed from the 'b' operation (just comment-out the animation steps), the effect on 'a' is not observed after the 'b' subview swap is exercised. If the pickers are removed from the swappable panel nibs, the effect is similarly eliminated. If the 'a' animation transition is set to cache, then after 'b' it does not stutter in the middle, but seems to ignore animating, simply swapping the view (this may be a matter of perception).

In case I'm not clear: I am NOT triggering these separate operations at the same time. Animation 'a', after 'b' has been executed--and completed--is not the same as if 'b' had never been executed. Is there clean-up I should be doing after an animation? Is my subview-swapping code flawed? Or...?

Thanks in advance for any suggestions.

From stackoverflow
  • You do have two overlapping animations in your 'b' section. When you create an animation using a begin / commit block, it gets handed off to a background thread to perform. In your 'b' section, the two animations that should be sequential are actually being fired off at nearly the same time. This can cause bizarre behavior, possibly like what you are seeing.

    I'd recommend adding a callback in your first animation to a delegate method using code like the following:

    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(rotationAnimationFinished:finished:context:)];
    

    within your first animation block in 'b' (the "First half of spin" part). Then you need to define the callback method within your class, in this case

    - (void)rotationAnimationFinished:(NSString *)animationID finished:(BOOL)finished context:(void *)context;
    

    Within that delegate method, which will be called when the first part of the spin animation has finished, place the remaining code from your 'b' method (everything after your "Finish spin" comment).

    By separating apart these animations, that might prevent the weirdness you're seeing.

    jpb : Thanks much for the suggestion, Brad, but I don't think we're there yet. While I understand your point, one test of it is to eliminate the second half of the 'b' animation entirely...and doing so makes no difference in the behavior of the 'a' animation.
  • Brad's answer does seem to have lead me to a solution: the symptoms continued to suggest that something about running those 'b' animations left the system in a different state than when it started. Finally, it occurred to me that the transform property of the panelView was left set after the 'b' animations...and perhaps that impacted further animations of a superview containing panelView.

    So I set up method as Brad suggested, to execute once the 'b' animations were complete--but all that method does is set the panelView transform back to the default: CGAffineTransformIdentity:

    • (void)spinFinished:(NSString *)animationID finished:(BOOL)finished context:(void *)context { panelView.transform = CGAffineTransformIdentity; }

    With this in place, post-'b' executions of the 'a' animation appear to be back to normal. So thanks again, Brad, for your help here. It is much appreciated.

nonatomic property in an NSOperation object (threaded environment), when to use it?

I'm still getting the hang of working in a threaded environment using NSOperation, and I'm interested in understand if using the 'nonatomic' property is even worthwhile if you're subclassing NSOperation?

If your defaultQueue is set to 1, is the additional overhead of nonatomic worth it? Might it be needed if you're executing more than 1 thread per operation queue?

Thanks for providing a solution on understanding this property in a threaded environment better.

From stackoverflow
  • If you have an object (or a property of that object) that you can guarantee it not accessed from multiple threads at the same time, then you can avoid potential overhead of atomic access to that property by using nonatomic. In the simplest case of creating an operation and enqueuing it on an NSOperationQueue, each NSOperation instance will either be getting set up in the invoking thread or running in a single background thread; so at any one time, only a single thread should be accessing it.

    The more general question of when nonatomic is useful depends in part on whether you are running with garbage collection enabled. According to the Objective-C release notes:

    Access to properties is atomic by default. This is trivial under GC for almost everything and also trivial under non-GC for everything but objects and structures. In particular atomic access to retained objects under non-GC conditions can be expensive. As such, a nonatomic property attribute is available.