/*
 * Copyright 2000-2011 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package jetbrains.buildServer.nuget.server.feed.server;

import jetbrains.buildServer.nuget.server.feed.server.controllers.RecentNuGetRequests;
import jetbrains.buildServer.usageStatistics.UsageStatisticsProvider;
import jetbrains.buildServer.usageStatistics.UsageStatisticsPublisher;
import jetbrains.buildServer.usageStatistics.presentation.UsageStatisticsFormatter;
import jetbrains.buildServer.usageStatistics.presentation.UsageStatisticsPresentationManager;
import jetbrains.buildServer.usageStatistics.presentation.UsageStatisticsPresentationProvider;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 * Created by Eugene Petrenko (eugene.petrenko@gmail.com)
 * Date: 23.11.11 20:42
 */
public class NuGetServerUsageStatisticsProvider implements UsageStatisticsProvider, UsageStatisticsPresentationProvider {
  public static final String SERVER_ENABLED_KEY = "jetbrains.nuget.server";
  public static final String TOTAL_REQUESTS = "jetbrains.nuget.differentRequests";
  private final NuGetServerRunnerSettings mySettings;
  @NotNull
  private final RecentNuGetRequests myRequests;

  public NuGetServerUsageStatisticsProvider(@NotNull final NuGetServerRunnerSettings settings,
                                            @NotNull final RecentNuGetRequests requests) {
    mySettings = settings;
    myRequests = requests;
  }

  public void accept(@NotNull UsageStatisticsPublisher publisher) {
    if (mySettings.isNuGetFeedEnabled()) {
      publisher.publishStatistic(SERVER_ENABLED_KEY, "enabled");
      publisher.publishStatistic(TOTAL_REQUESTS, myRequests.getTotalRequests());
    }
  }

  public void accept(@NotNull UsageStatisticsPresentationManager presentationManager) {
    presentationManager.applyPresentation(TOTAL_REQUESTS, "NuGet Feed Request Kinds", "NuGet", null, null);
    presentationManager.applyPresentation(SERVER_ENABLED_KEY, "NuGet Feed Server", "NuGet", new UsageStatisticsFormatter() {
      @NotNull
      public String format(@Nullable Object statisticValue) {
        return statisticValue == null ? "disabled" : statisticValue.toString();
      }
    }, null);
  }
}

@@ -758,16 +758,86 @@
 uests;%0D%0A
+import jetbrains.buildServer.serverSide.metadata.BuildMetadataEntry;%0D%0A
 import j
@@ -1336,16 +1336,46 @@
 ble;%0D%0A%0D%0A
+import java.util.Iterator;%0D%0A%0D%0A
 /**%0D%0A * 
@@ -1744,16 +1744,96 @@
 ests%22;%0D%0A
+  public static final String TOTAL_PACKAGES = %22jetbrains.nuget.totalPackages%22;%0D%0A
   privat
@@ -1879,28 +1879,16 @@
 tings;%0D%0A
-  @NotNull%0D%0A
   privat
@@ -1927,16 +1927,56 @@
 quests;%0D
+%0A  private final PackagesIndex myIndex;%0D
 %0A%0D%0A  pub
@@ -2153,16 +2153,97 @@
 requests
+,%0D%0A                                            @NotNull final PackagesIndex index
 ) %7B%0D%0A   
@@ -2295,16 +2295,38 @@
 uests;%0D%0A
+    myIndex = index;%0D%0A
   %7D%0D%0A%0D%0A 
@@ -2585,25 +2585,307 @@
 ts());%0D%0A    
-%7D
+  publisher.publishStatistic(TOTAL_PACKAGES, countEntries());%0D%0A    %7D%0D%0A  %7D%0D%0A  %0D%0A  private int countEntries() %7B%0D%0A    int count = 0;%0D%0A    final Iterator%3CBuildMetadataEntry%3E it = myIndex.getEntries();%0D%0A    while(it.hasNext()) %7B%0D%0A      it.next();%0D%0A      count++;%0D%0A    %7D%0D%0A    return count;
 %0D%0A  %7D%0D%0A%0D%0A  p
@@ -3029,22 +3029,16 @@
 UESTS, %22
-NuGet 
 Feed Req
@@ -3069,24 +3069,123 @@
 ll, null);%0D%0A
+    presentationManager.applyPresentation(TOTAL_PACKAGES, %22Packages Count%22, %22NuGet%22, null, null);%0D%0A
     presenta

add number of packages to Usage Stats