private void StoreImpressions(FileInfo fileinfo ) { //we aren't messing with non-text files if (fileinfo.Extension.EndsWith("txt") == false) { return; } //write the file to the screen Console.WriteLine(String.Concat("Reading ", fileinfo.FullName)); //set the log separator string[] logseparator = new string[] { ConfigurationManager.AppSettings["LogRecordSeparator"].ToString() }; //use linq to query the file var ImpressionLog = from logdetails in File.ReadAllLines(fileinfo.FullName) let logrow = logdetails.Split(logseparator, 0) select new Impression() { datestamp = Convert.ToDateTime(logrow[0]), bannerid = Convert.ToInt32(logrow[1]), tagname = logrow[2], browser = logrow[4], version = logrow[5], session = logrow[8], referrer = logrow[6], ip = logrow[7] }; //connect to the database setMongoDb sm = new setMongoDb(); mongodb mongo = new mongodb(); var cred = MongoCredential.CreateMongoCRCredential(sm.MongodbName, sm.MongodbUser, sm.MongodbPass); var settings = new MongoClientSettings { Credentials = new[] { cred } }; MongoClient client = new MongoClient(settings); MongoServer server = client.GetServer(); MongoDatabase db = server.GetDatabase(sm.MongodbName); //create the collection (table) name string collect; collect = string.Concat("Impressions", CreateTableName(fileinfo.Name)); //check if the collection can be found, if not create it, then load if (mongo.CreateCollection(db, collect) == true) { foreach (Impression impressionlogentry in ImpressionLog) { MongoCollection<BsonDocument> impressions = db.GetCollection<BsonDocument>(collect); BsonDocument impression = new BsonDocument { {"DateStamp" , impressionlogentry.datestamp }, {"BannerID", impressionlogentry.bannerid}, {"TagName", impressionlogentry.tagname}, {"Browser", impressionlogentry.browser}, {"Version", impressionlogentry.version}, {"SessionID", impressionlogentry.session}, {"IP", impressionlogentry.ip}, {"UrlRefer", impressionlogentry.referrer} }; //add to the table impressions.Insert(impression); } //end for } // end create collection if //clean up the ImpressionLog ImpressionLog = null; }
private void StoreInteractions(FileInfo fileinfo) { //we aren't messing with non-text files if (fileinfo.Extension.EndsWith("txt") == false) { return; } //double check that the user file is the only one being parse here. if (fileinfo.FullName.ToLower().Contains("interaction") == false) { return; } //write the file to the screen Console.WriteLine(String.Concat("Reading ", fileinfo.FullName)); //set the log separator string[] logseparator = new string[] { ConfigurationManager.AppSettings["LogRecordSeparator"].ToString() }; //use linq to query the file var InteractionLog = from logdetails in File.ReadAllLines(fileinfo.FullName) let logrow = logdetails.Split(logseparator, 0) select new Interaction() { DateStamp = Convert.ToDateTime(logrow[0]), BannerID = Convert.ToInt32(logrow[1]), TagName = logrow[2], EventID = Convert.ToInt32(logrow[3]), EventValue = logrow[4], SessionID = logrow[6], ListingID = logrow[5] }; //connect to the database setMongoDb sm = new setMongoDb(); mongodb mongo = new mongodb(); var cred = MongoCredential.CreateMongoCRCredential(sm.MongodbName, sm.MongodbUser, sm.MongodbPass); var settings = new MongoClientSettings { Credentials = new[] { cred } }; MongoClient client = new MongoClient(settings); MongoServer server = client.GetServer(); MongoDatabase db = server.GetDatabase(sm.MongodbName); //create the collection (table) name string collect = string.Concat("Interactions", CreateTableName(fileinfo.Name)); //check if the collection can be found, if not create it, then load if (mongo.CreateCollection(db, collect) == true) { foreach (Interaction interactionlogentry in InteractionLog) { MongoCollection<BsonDocument> interactions = db.GetCollection<BsonDocument>(collect); BsonDocument Interaction = new BsonDocument { {"DateStamp" , interactionlogentry.DateStamp }, {"BannerID", interactionlogentry.BannerID}, {"TagName", interactionlogentry.TagName}, {"EventID", interactionlogentry.EventID}, {"EventValue", interactionlogentry.EventValue}, {"SessionID", interactionlogentry.SessionID}, {"ListingID", interactionlogentry.ListingID} }; interactions.Insert(Interaction); } //end for } // end create collection if //clean up the ImpressionLog InteractionLog = null; }
static void Main(string[] args) { DateTime StartStamp = DateTime.Now; Console.WriteLine(string.Concat("Start: ", StartStamp)); //WriteEvent(null, string.Concat("Start: ", DateTime.Now)); Validators validator = new Validators(); MongoBannerMain me = new MongoBannerMain(); int ProgramAction = 0; string FolderToSearch = string.Empty; bool KeepFiles = bool.TryParse(ConfigurationManager.AppSettings["KeepFiles"].ToString(), out KeepFiles); string ExportFileName = string.Empty; DateTime StartDate = DateTime.Today; DateTime EndDate = DateTime.Today; string MongoCollection = string.Empty; int argCounter = 0; try { foreach (string s in args) { switch (argCounter) { case 0: ProgramAction = validator.ValidateNumber(args[argCounter].ToString()); break; case 1: FolderToSearch = args[argCounter].ToString(); break; case 2: bool.TryParse(args[argCounter], out KeepFiles); break; case 3: ExportFileName = validator.ValidateString(args[argCounter]); break; case 4: if (validator.IsValidDate(args[argCounter])) { StartDate = Convert.ToDateTime(args[argCounter]); } break; case 5: if (validator.IsValidDate(args[argCounter])) { EndDate = Convert.ToDateTime(args[argCounter]); } break; case 6: MongoCollection = args[argCounter].ToString(); if (MongoCollection.Length == 0) MongoCollection = "Interactions201302"; break; } argCounter += 1; } } catch { ProgramAction = 0; FolderToSearch = string.Empty; ExportFileName = string.Empty; } //what are we running? switch (ProgramAction) { case 1: me.ImportFiletoDB(FolderToSearch, KeepFiles); break; case 2: MongoToSQL m2s = new MongoToSQL(); setMongoDb mongoSettings = new setMongoDb(); m2s.mongoCollection = MongoCollection; m2s.mongodbuser = mongoSettings.MongodbUser; m2s.mongodbpassword = mongoSettings.MongodbPass; m2s.mongodbname = mongoSettings.MongodbName; m2s.mongoConnString = ConfigurationManager.ConnectionStrings["MongoConn"].ToString(); m2s.sqlConnString = ConfigurationManager.ConnectionStrings["SQLBannerLinkTracking"].ToString(); m2s.reportStartDate = StartDate.ToShortDateString(); m2s.reportEndDate = EndDate.ToShortDateString(); m2s.MongoExportSqlServerImport(); break; case 3: ExportToFile e2f = new ExportToFile(); //setMongoDb mongoSettings = new setMongoDb(); e2f.mongoCollection = MongoCollection; //e2f.mongodbuser = mongoSettings.MongodbUser; //e2f.mongodbpassword = mongoSettings.MongodbPass; //e2f.mongodbname = mongoSettings.MongodbName; e2f.mongoConnString = ConfigurationManager.ConnectionStrings["MongoConn"].ToString(); e2f.reportStartDate = StartDate.ToShortDateString(); e2f.reportEndDate = EndDate.ToShortDateString(); e2f.exportFileName = ExportFileName; e2f.Export(); break; default: break; } DateTime EndStamp = DateTime.Now; TimeSpan ts = EndStamp - StartStamp; Console.WriteLine(String.Concat("End: ", DateTime.Now, " Duration: ", ts.TotalHours)); //WriteEvent(null, string.Concat("End: ", DateTime.Now)); }