// GET: api/ThreadProfiles public async Task <HttpResponseMessage> Get( [FromUri] string repository, [FromUri] int page, [FromUri] int length, [FromUri] DateTime?start, [FromUri] DateTime?end, [FromUri] string tags) { EasyAnalysis.Framework.ConnectionStringProviders.IConnectionStringProvider mongoDBCSProvider = EasyAnalysis.Framework.ConnectionStringProviders.ConnectionStringProvider.CreateConnectionStringProvider(EasyAnalysis.Framework.ConnectionStringProviders.ConnectionStringProvider.ConnectionStringProviderType.MongoDBConnectionStringProvider); var client = new MongoClient(mongoDBCSProvider.GetConnectionString(repository));//"mongodb://app-svr.cloudapp.net:27017/" + repository); var database = client.GetDatabase(repository); var threadProfiles = database.GetCollection <BsonDocument>("thread_profiles"); var builder = Builders <BsonDocument> .Filter; FilterDefinition <BsonDocument> filter = "{}"; if (start.HasValue && end.HasValue) { filter = filter & builder.Gte("create_on", start) & builder.Lt("create_on", end); } if (!string.IsNullOrWhiteSpace(tags)) { var array = tags.Split('|'); filter = filter & builder.All("tags", array); } var result = await threadProfiles .Find(filter) .Sort("{create_on: -1}") .Skip((page - 1) * length) .Limit(length) .ToListAsync(); var response = Request.CreateResponse(); response.StatusCode = HttpStatusCode.OK; var jsonWriterSettings = new JsonWriterSettings { OutputMode = JsonOutputMode.Strict }; response.Content = new StringContent(result.ToJson(jsonWriterSettings), Encoding.UTF8, "application/json"); return(response); }
// GET: api/UserProfiles public async Task <HttpResponseMessage> Get( [FromUri] string repository, [FromUri] string display_name, [FromUri] string month ) { EasyAnalysis.Framework.ConnectionStringProviders.IConnectionStringProvider mongoDBCSProvider = EasyAnalysis.Framework.ConnectionStringProviders.ConnectionStringProvider.CreateConnectionStringProvider(EasyAnalysis.Framework.ConnectionStringProviders.ConnectionStringProvider.ConnectionStringProviderType.MongoDBConnectionStringProvider); var client = new MongoClient(mongoDBCSProvider.GetConnectionString(repository)); var database = client.GetDatabase(repository); var userProfiles = database.GetCollection <BsonDocument>("asker_activities"); var builder = Builders <BsonDocument> .Filter; FilterDefinition <BsonDocument> filter = "{}"; if (!string.IsNullOrEmpty(display_name)) { filter = filter & builder.Regex("display_name", BsonRegularExpression.Create(new Regex("^" + display_name, RegexOptions.IgnoreCase))); } if (!string.IsNullOrEmpty(month)) { filter = filter & builder.Eq("month", month); } var result = await userProfiles .Find(filter) .Sort("{ total: -1 }") .ToListAsync(); var response = Request.CreateResponse(); response.StatusCode = HttpStatusCode.OK; var jsonWriterSettings = new JsonWriterSettings { OutputMode = JsonOutputMode.Strict }; response.Content = new StringContent(result.ToJson(jsonWriterSettings), Encoding.UTF8, "application/json"); return(response); }
public async Task <HttpResponseMessage> GetRelatedTags( [FromUri] string repository, [FromUri] DateTime?start, [FromUri] DateTime?end, [FromUri] string tags) { EasyAnalysis.Framework.ConnectionStringProviders.IConnectionStringProvider mongoDBCSProvider = EasyAnalysis.Framework.ConnectionStringProviders.ConnectionStringProvider.CreateConnectionStringProvider(EasyAnalysis.Framework.ConnectionStringProviders.ConnectionStringProvider.ConnectionStringProviderType.MongoDBConnectionStringProvider); var client = new MongoClient(mongoDBCSProvider.GetConnectionString(repository));//"mongodb://app-svr.cloudapp.net:27017/" + repository); var database = client.GetDatabase(repository); var threadProfiles = database.GetCollection <BsonDocument>("thread_profiles"); var builder = Builders <BsonDocument> .Filter; FilterDefinition <BsonDocument> filter = "{}"; List <string> wellKnownTags = new List <string> { "uwp", "wp8.1", "w8.1", "u8.1", "wpsl", "c#", "c++", "vb", "javascript" }; if (start.HasValue && end.HasValue) { filter = filter & builder.Gte("create_on", start) & builder.Lt("create_on", end); } if (!string.IsNullOrWhiteSpace(tags)) { var array = tags.Split('|'); wellKnownTags.AddRange(array); wellKnownTags = wellKnownTags.Distinct().ToList(); filter = filter & builder.All("tags", array); } var result = await threadProfiles.Aggregate() .Match(filter) .Project("{ _id: 0, tags: 1 }") .Unwind("tags") .Group("{ _id: '$tags', freq: { $sum: 1 } }") .Project("{ _id: 0, name: '$_id', freq: 1 }") .Sort("{ freq: -1 }") .ToListAsync(); IList <BsonDocument> resultToRemove = new List <BsonDocument>(); foreach (var item in result) { if (wellKnownTags.Contains(item.GetValue("name").AsString)) { resultToRemove.Add(item); } } foreach (var itemToRemove in resultToRemove) { result.Remove(itemToRemove); } result = result.Take(20).ToList(); var response = Request.CreateResponse(); response.StatusCode = HttpStatusCode.OK; var jsonWriterSettings = new JsonWriterSettings { OutputMode = JsonOutputMode.Strict }; response.Content = new StringContent(result.ToJson(jsonWriterSettings), Encoding.UTF8, "application/json"); return(response); }
// GET: api/UserProfiles public async Task <HttpResponseMessage> Get( [FromUri] string repository, [FromUri] int length, [FromUri] List <string> months ) { EasyAnalysis.Framework.ConnectionStringProviders.IConnectionStringProvider mongoDBCSProvider = EasyAnalysis.Framework.ConnectionStringProviders.ConnectionStringProvider.CreateConnectionStringProvider(EasyAnalysis.Framework.ConnectionStringProviders.ConnectionStringProvider.ConnectionStringProviderType.MongoDBConnectionStringProvider); var client = new MongoClient(mongoDBCSProvider.GetConnectionString(repository)); var database = client.GetDatabase(repository); var userProfiles = database.GetCollection <BsonDocument>("asker_activities"); var builder = Builders <BsonDocument> .Filter; FilterDefinition <BsonDocument> filter = "{}"; if (months.Count > 0) { filter = filter & builder.In("month", months); } IList <BsonDocument> result = new List <BsonDocument>(); var cursor = await userProfiles.Aggregate() .Match(filter) .Group("{_id: '$id', asked: {$sum:'$total'}, answered: {$sum:'$answered'}, marked: {$sum: '$marked'}, months:{$push:'$month' }}") .Project("{_id: 0, id: '$_id', asked: 1, answered: 1, marked: 1, months:1}") //.Match("{asked: {$gte:7}") .Sort("{ asked: -1 }") .Limit(length) .ToListAsync(); foreach (var data in cursor) { //BsonArray _tags = new BsonArray(); BsonArray _platform_tags = new BsonArray(); BsonArray _language_tags = new BsonArray(); BsonArray _other_tags = new BsonArray(); BsonArray _threads = new BsonArray(); BsonArray _marked_threads = new BsonArray(); FilterDefinition <BsonDocument> filterid = filter & builder.Eq("id", data.GetValue("id").AsString); var op = await userProfiles .Find(filterid) .ToListAsync(); foreach (var item in op) { data["display_name"] = item.GetValue("display_name").AsString; // Unpacking Tags foreach (var tag in item.GetValue("tags").AsBsonArray) { if (wellKnownPlatformTags.Contains(tag["name"].AsString.ToLower())) { _platform_tags.Add(tag); } else if (wellKnownLanguageTags.Contains(tag["name"].AsString.ToLower())) { _language_tags.Add(tag); } else { _other_tags.Add(tag); } } data["platform_tags"] = MergeAndCleanArray(_platform_tags); data["language_tags"] = MergeAndCleanArray(_language_tags); data["other_tags"] = MergeAndCleanArray(_other_tags); } result.Add(data); } var response = Request.CreateResponse(); response.StatusCode = HttpStatusCode.OK; var jsonWriterSettings = new JsonWriterSettings { OutputMode = JsonOutputMode.Strict }; response.Content = new StringContent(result.ToJson(jsonWriterSettings), Encoding.UTF8, "application/json"); return(response); }