private void Send() { //Get Date (Year) Today var registYear = DateTime.Now.AddMonths(addMonths).Year; //Loop from 01 to 12 try { using (var context = new VideoEngagementsEntities()) { for (int i = 0; i < 12; i++) { var returnCode = new SqlParameter() { ParameterName = "returnCode", DbType = System.Data.DbType.Int32, Direction = System.Data.ParameterDirection.Output }; string tableName = String.Format("EpisodePlay{0}{1:00}", registYear, i + 1); var result = context.Database.ExecuteSqlCommand("EXEC CreateLogTable @table, @returnCode OUTPUT", new object[] { new SqlParameter("table", tableName), returnCode }); if ((int)returnCode.Value > 0) Console.WriteLine(String.Format("Table {0} has been successfully created.", tableName)); else if ((int)returnCode.Value == -1) Console.WriteLine(String.Format("Table {0} already exists. Skipping...", tableName)); else Console.WriteLine(String.Format("Table {0} creation failed.", tableName)); } } } catch (Exception e) { Console.WriteLine(String.Format("Error: {0}", e.Message)); } }
public ActionResult LogPlaybackOld(int type, int id, int playTypeId, int? fullDuration, bool isPreview, string positionDuration, int? streamType) { Dictionary<string, object> collection = new Dictionary<string, object>(); collection = MyUtility.setError(ErrorCodes.UnknownError, String.Empty); DateTime registDt = DateTime.Now; int positionDuration_Integer = 0; try { positionDuration_Integer = Int32.Parse(positionDuration); } catch (Exception) { } if (fullDuration == null) fullDuration = -1000; try { if (MyUtility.isUserLoggedIn()) { var context = new IPTV2Entities(); var userId = new Guid(User.Identity.Name); int categoryId = 0, assetId = 0; var videoContext = new VideoEngagementsEntities(); int[] fillDuration = { 2, 3, 4 }; switch (type) { case 4: case 1: //EpisodePlay { var episode = context.Episodes.FirstOrDefault(e => e.EpisodeId == id); if (episode != null) { var episodeCategory = episode.EpisodeCategories.FirstOrDefault(e => e.CategoryId != GlobalConfig.FreeTvCategoryId); categoryId = episodeCategory.CategoryId; var asset = episode.PremiumAssets.FirstOrDefault(); if (asset != null) assetId = asset.AssetId; } EpisodePlay episodePlay = null; if (episode.IsLiveChannelActive == true) { episodePlay = new EpisodePlay() { EpisodeId = id, CategoryId = categoryId, UserId = userId, DateTime = registDt, AssetId = assetId, PlayTypeId = playTypeId, Duration = fillDuration.Contains(playTypeId) ? positionDuration_Integer : 0, IsPreview = false, StartPosition = -1000, Length = -1000, StreamType = 0 }; } else { episodePlay = new EpisodePlay() { EpisodeId = id, CategoryId = categoryId, UserId = userId, DateTime = registDt, AssetId = assetId, PlayTypeId = playTypeId, Duration = fillDuration.Contains(playTypeId) ? positionDuration_Integer : 0, IsPreview = isPreview, StartPosition = fillDuration.Contains(playTypeId) ? 0 : positionDuration_Integer, Length = (int)fullDuration, StreamType = streamType == null ? 0 : streamType }; } if (episodePlay != null) videoContext.EpisodePlays.Add(episodePlay); break; } case 2: //ChannelPlay { var channelPlay = new ChannelPlay() { ChannelId = id, //ChannelPlayId = 0, DateTime = registDt, Duration = fillDuration.Contains(playTypeId) ? positionDuration_Integer : 0, PlayTypeId = playTypeId, UserId = userId }; videoContext.ChannelPlays.Add(channelPlay); break; } case 3: //YoutubePlay { break; } } videoContext.SaveChanges(); collection = MyUtility.setError(ErrorCodes.Success, String.Empty); } else collection = MyUtility.setError(ErrorCodes.NotAuthenticated, String.Empty); } catch (Exception e) { MyUtility.LogException(e); } return Content(MyUtility.buildJson(collection), "application/json"); }
public JsonResult LogPlayback(int type, int id, int playTypeId, int? fullDuration, bool isPreview, string positionDuration, string streamType, int? bufferCount, int? minBandwidth, int? maxBandwidth, int? avgBandwidth) { //Response.ContentType = "application/json"; var ReturnCode = new TransactionReturnType() { StatusCode = (int)ErrorCodes.UnknownError, StatusMessage = MyUtility.getErrorMessage(ErrorCodes.UnknownError) }; if (!Request.IsLocal) if (!Request.IsAjaxRequest()) { ReturnCode.StatusCode = (int)ErrorCodes.IsInvalidRequest; ReturnCode.StatusMessage = "Your request is invalid."; return Json(ReturnCode, JsonRequestBehavior.AllowGet); } if (!User.Identity.IsAuthenticated) { ReturnCode.StatusCode = (int)ErrorCodes.IsInvalidRequest; ReturnCode.StatusMessage = "Your request is invalid."; return Json(ReturnCode, JsonRequestBehavior.AllowGet); } try { DateTime registDt = DateTime.Now; int positionDuration_Integer = 0; int sType = 0; try { positionDuration_Integer = Int32.Parse(positionDuration); } catch (Exception) { } try { sType = Int32.Parse(streamType); } catch (Exception) { } if (fullDuration == null) fullDuration = -1000; var context = new IPTV2Entities(); var userId = new Guid(User.Identity.Name); int categoryId = 0, assetId = 0; int[] fillDuration = { 2, 3, 4 }; var returnCode = new SqlParameter() { ParameterName = "returnCode", DbType = System.Data.DbType.Int32, Direction = System.Data.ParameterDirection.Output }; string tableName = String.Format("EpisodePlay{0}", registDt.ToString("yyyyMM")); using (var videoContext = new VideoEngagementsEntities()) { switch (type) { case 4: case 1: //EpisodePlay { var episode = context.Episodes.FirstOrDefault(e => e.EpisodeId == id); if (episode != null) { var excludedCategoryIds = MyUtility.StringToIntList(GlobalConfig.ExcludedCategoryIdsForDisplay); var episodeCategory = episode.EpisodeCategories.FirstOrDefault(e => !excludedCategoryIds.Contains(e.CategoryId)); categoryId = episodeCategory.CategoryId; var asset = episode.PremiumAssets.FirstOrDefault(); if (asset != null) assetId = asset.AssetId; } if (episode.IsLiveChannelActive == true) { var sTypeParam = new SqlParameter() { ParameterName = "StreamType", DbType = System.Data.DbType.Int32, Value = 0 }; var result = videoContext.Database.ExecuteSqlCommand("EXEC LogMediaPlayback @table, @PlayTypeId, @EpisodeId, @AssetId, @UserId, @registDt, @duration, @length, @CategoryId, @StartPosition, @isPreview, @StreamType, @returnCode OUTPUT", new object[] { new SqlParameter("table", tableName), new SqlParameter("PlayTypeId", playTypeId), new SqlParameter("EpisodeId", id), new SqlParameter("AssetId", assetId), new SqlParameter("UserId", userId), new SqlParameter("registDt", registDt), new SqlParameter("duration", fillDuration.Contains(playTypeId) ? positionDuration_Integer : 0), new SqlParameter("length", -1000), new SqlParameter("CategoryId", categoryId), new SqlParameter("StartPosition", -1000), new SqlParameter("isPreview", false), sTypeParam, returnCode }); } else { var result = videoContext.Database.ExecuteSqlCommand("EXEC LogMediaPlayback @table, @PlayTypeId, @EpisodeId, @AssetId, @UserId, @registDt, @duration, @length, @CategoryId, @StartPosition, @isPreview, @StreamType, @returnCode OUTPUT", new object[] { new SqlParameter("table", tableName), new SqlParameter("PlayTypeId", playTypeId), new SqlParameter("EpisodeId", id), new SqlParameter("AssetId", assetId), new SqlParameter("UserId", userId), new SqlParameter("registDt", registDt), new SqlParameter("duration", fillDuration.Contains(playTypeId) ? positionDuration_Integer : 0), new SqlParameter("length",(int)fullDuration), new SqlParameter("CategoryId", categoryId), new SqlParameter("StartPosition", fillDuration.Contains(playTypeId) ? 0 : positionDuration_Integer), new SqlParameter("isPreview", isPreview), new SqlParameter("StreamType", sType), returnCode }); } //Log onto TFCtvWebApi (Json) try { HttpWebRequest req = (HttpWebRequest)WebRequest.Create(GlobalConfig.TFCtvApiVideoPlaybackUri); req.Method = "POST"; req.ContentType = "application/json"; using (var streamWriter = new StreamWriter(req.GetRequestStream())) { string jsonString = String.Empty; var obj = new TfcTvApiPlaybackObj() { PlayTypeId = playTypeId, UserId = userId.ToString(), DateTime = registDt.ToString("o"), EpisodeId = id, CategoryId = categoryId, Duration = fillDuration.Contains(playTypeId) ? positionDuration_Integer : 0, Length = episode.IsLiveChannelActive == true ? -1000 : (int)fullDuration, AssetId = assetId, StartPosition = episode.IsLiveChannelActive == true ? -1000 : fillDuration.Contains(playTypeId) ? 0 : positionDuration_Integer, LastPosition = fillDuration.Contains(playTypeId) ? positionDuration_Integer : 0, ClientIp = MyUtility.GetClientIpAddress(), IsPreview = isPreview, DeviceHeader = Request.UserAgent, BufferCount = bufferCount != null ? (int)bufferCount : -1, MaxBitrate = maxBandwidth != null ? (int)maxBandwidth : -1, MinBitrate = minBandwidth != null ? (int)minBandwidth : -1, AvgBitrate = avgBandwidth != null ? (int)avgBandwidth : -1 }; jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(obj); streamWriter.Write(jsonString); } WebResponse response = req.GetResponse(); var statusDescription = ((HttpWebResponse)response).StatusDescription; Stream dataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(dataStream); string responseFromServer = reader.ReadToEnd(); reader.Close(); dataStream.Close(); response.Close(); } catch (Exception) { } ////Log onto TFCtvWebApi //try //{ // HttpWebRequest req = (HttpWebRequest)WebRequest.Create(GlobalConfig.TFCtvApiVideoPlaybackUri); // req.Method = "POST"; // req.ContentType = "application/x-www-form-urlencoded"; // StringBuilder postData = new StringBuilder(); // postData.Append(String.Format("PlayTypeId={0}&", playTypeId)); // postData.Append(String.Format("UserId={0}&", userId)); // postData.Append(String.Format("DateTime={0}&", registDt.ToString("o"))); // postData.Append(String.Format("EpisodeId={0}&", id)); // postData.Append(String.Format("CategoryId={0}&", categoryId)); // postData.Append(String.Format("Duration={0}&", fillDuration.Contains(playTypeId) ? positionDuration_Integer : 0)); // postData.Append(String.Format("Length={0}&", episode.IsLiveChannelActive == true ? -1000 : (int)fullDuration)); // postData.Append(String.Format("AssetId={0}&", assetId)); // postData.Append(String.Format("StartPosition={0}&", episode.IsLiveChannelActive == true ? -1000 : fillDuration.Contains(playTypeId) ? 0 : positionDuration_Integer)); // postData.Append(String.Format("ClientIp={0}&", MyUtility.GetClientIpAddress())); // postData.Append(String.Format("IsPreview={0}&", isPreview)); // postData.Append(String.Format("DeviceHeader={0}&", Request.UserAgent)); // byte[] param = Encoding.UTF8.GetBytes(postData.ToString()); // req.ContentLength = param.Length; // Stream dataStream = req.GetRequestStream(); // dataStream.Write(param, 0, param.Length); // dataStream.Close(); // WebResponse response = req.GetResponse(); // var statusDescription = ((HttpWebResponse)response).StatusDescription; // dataStream = response.GetResponseStream(); // StreamReader reader = new StreamReader(dataStream); // string responseFromServer = reader.ReadToEnd(); // reader.Close(); // dataStream.Close(); // response.Close(); //} //catch (Exception) { } break; } case 2: //ChannelPlay { break; } case 3: //YoutubePlay { break; } } } if ((int)returnCode.Value > 0) { ReturnCode.StatusCode = (int)ErrorCodes.Success; ReturnCode.StatusMessage = "Playback logged."; } } catch (Exception e) { MyUtility.LogException(e); ReturnCode.StatusMessage = e.Message; } return Json(ReturnCode, JsonRequestBehavior.AllowGet); }