Ejemplo n.º 1
0
        protected void ExportByViewsButton_Click(Object sender, EventArgs e)
        {
            GenericExportManager <ProductViewSummary> exportManager = GenericExportManager <ProductViewSummary> .Instance;
            GenericExportOptions <ProductViewSummary> options       = new GenericExportOptions <ProductViewSummary>();

            options.CsvFields = new string[] { "ProductName", "Views" };

            CacheWrapper cacheWrapper = Cache[_ViewsCacheKey] as CacheWrapper;
            SortableCollection <KeyValuePair <ICatalogable, int> > productViews;
            Dictionary <string, object> viewsDataWrapper = null;
            IList <ProductViewSummary>  viewsSummay      = new List <ProductViewSummary>();

            if (cacheWrapper == null)
            {
                //GET VIEWS
                productViews = PageViewDataSource.GetViewsByProduct(50, 0, "ViewCount DESC");
                //CACHE THE DATA
                viewsDataWrapper = new Dictionary <string, object>();
                viewsDataWrapper["DataSource"] = productViews;
                cacheWrapper = new CacheWrapper(viewsDataWrapper);
                Cache.Remove(_ViewsCacheKey);
                Cache.Add(_ViewsCacheKey, cacheWrapper, null, DateTime.UtcNow.AddMinutes(5).AddSeconds(-1), TimeSpan.Zero, System.Web.Caching.CacheItemPriority.NotRemovable, null);
            }
            else
            {
                //USE CACHED VALUES
                viewsDataWrapper = (Dictionary <string, object>)cacheWrapper.CacheValue;
                productViews     = (SortableCollection <KeyValuePair <ICatalogable, int> >)viewsDataWrapper["DataSource"];
            }

            // CONVERT TO SUMMARY LIST TO GENERATE CSV
            foreach (KeyValuePair <ICatalogable, int> dataRow in productViews)
            {
                viewsSummay.Add(new ProductViewSummary(dataRow.Key.Name, dataRow.Value));
            }

            options.ExportData = viewsSummay;
            options.FileTag    = "POPULAR_PRODUCTS_BY_VIEWS";
            exportManager.BeginExport(options);
        }
        private void initViewsChart()
        {
            string       cacheKey     = "242003F5-5A58-44e9-BFB0-C077C6BEDBF2";
            CacheWrapper cacheWrapper = Cache[cacheKey] as CacheWrapper;
            Dictionary <string, object> viewsData;

            if (cacheWrapper == null)
            {
                //GET VIEWS
                SortableCollection <KeyValuePair <ICatalogable, int> > productViews = PageViewDataSource.GetViewsByProduct(8, 0, "ViewCount DESC");
                if (productViews.Count > 0)
                {
                    //BUILD BAR CHART
                    ViewsChart1.Series["Views"].Points.Clear();
                    for (int i = 0; i < productViews.Count; i++)
                    {
                        DataPoint point = new DataPoint(ViewsChart1.Series["Views"]);
                        point.SetValueXY(((ICatalogable)productViews[i].Key).Name, new object[] { productViews[i].Value });
                        ViewsChart1.Series["Views"].Points.Add(point);
                    }
                    ViewsChart1.DataBind();

                    //BIND THE DATA GRID
                    ViewsGrid.DataSource = productViews;
                    ViewsGrid.DataBind();
                    //CACHE THE DATA
                    viewsData = new Dictionary <string, object>();
                    viewsData["DataSource"] = productViews;
                    cacheWrapper            = new CacheWrapper(viewsData);
                    Cache.Remove(cacheKey);
                    Cache.Add(cacheKey, cacheWrapper, null, LocaleHelper.LocalNow.AddMinutes(5).AddSeconds(-1), TimeSpan.Zero, System.Web.Caching.CacheItemPriority.NotRemovable, null);
                }
                else
                {
                    //NO PRODUCTS HAVE BEEN VIEWED YET OR PAGE TRACKING IS DISABLED
                    Control container = ViewsChart1.Parent;
                    container.Controls.Clear();
                    Panel noViewsPanel = new Panel();
                    noViewsPanel.CssClass = "emptyData";
                    Label noViewsMessage = new Label();
                    noViewsMessage.Text = "No products have been viewed yet or page tracking is disabled.";
                    noViewsPanel.Controls.Add(noViewsMessage);
                    container.Controls.Add(noViewsPanel);

                    // REMOVE VIEWS DATA TAB
                    Tabs.Tabs[3].Visible = false;
                }
            }
            else
            {
                //USE CACHED VALUES
                viewsData = (Dictionary <string, object>)cacheWrapper.CacheValue;
                SortableCollection <KeyValuePair <ICatalogable, int> > productViews = (SortableCollection <KeyValuePair <ICatalogable, int> >)viewsData["DataSource"];
                //BUILD BAR CHART
                ViewsChart1.Series["Views"].Points.Clear();
                for (int i = 0; i < productViews.Count; i++)
                {
                    DataPoint point = new DataPoint(ViewsChart1.Series["Views"]);
                    point.SetValueXY(((ICatalogable)productViews[i].Key).Name, new object[] { productViews[i].Value });
                    ViewsChart1.Series["Views"].Points.Add(point);
                }
                ViewsChart1.DataBind();

                ViewsGrid.DataSource = productViews;
                ViewsGrid.DataBind();
            }
        }