/*
 * 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.requests.RecentNuGetRequests;
import jetbrains.buildServer.nuget.server.feed.server.index.NuGetIndexEntry;
import jetbrains.buildServer.nuget.server.feed.server.index.PackagesIndex;
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;

import java.util.Iterator;

/**
 * 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.feedDailyRequests";
  public static final String TOTAL_PACKAGES = "jetbrains.nuget.totalPackages";
  private final NuGetServerSettings mySettings;
  private final RecentNuGetRequests myRequests;
  private final PackagesIndex myIndex;

  public NuGetServerUsageStatisticsProvider(@NotNull final NuGetServerSettings settings,
                                            @NotNull final RecentNuGetRequests requests,
                                            @NotNull final PackagesIndex index) {
    mySettings = settings;
    myRequests = requests;
    myIndex = index;
  }

  public void accept(@NotNull UsageStatisticsPublisher publisher) {
    if (mySettings.isNuGetServerEnabled()) {
      publisher.publishStatistic(SERVER_ENABLED_KEY, "enabled");
      publisher.publishStatistic(TOTAL_REQUESTS, myRequests.getTotalRequests());
      publisher.publishStatistic(TOTAL_PACKAGES, countEntries());
    }
  }
  
  private int countEntries() {
    int count = 0;
    final Iterator<NuGetIndexEntry> it = myIndex.getNuGetEntries();
    while(it.hasNext()) {
      it.next();
      count++;
    }
    return count;
  }

  public void accept(@NotNull UsageStatisticsPresentationManager presentationManager) {
    presentationManager.applyPresentation(TOTAL_REQUESTS, "Feed Requests Count per Day", "NuGet", null, null);
    presentationManager.applyPresentation(TOTAL_PACKAGES, "Packages Count", "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);
  }
}

@@ -666,24 +666,64 @@
 .server;%0D%0A%0D%0A
+import com.intellij.openapi.util.Pair;%0D%0A
 import jetbr
@@ -1465,24 +1465,51 @@
 ullable;%0D%0A%0D%0A
+import java.util.HashSet;%0D%0A
 import java.
@@ -1523,16 +1523,39 @@
 erator;%0D
+%0Aimport java.util.Set;%0D
 %0A%0D%0A/**%0D%0A
@@ -2007,16 +2007,99 @@
 ages%22;%0D%0A
+  public static final String DIFF_PACKAGES = %22jetbrains.nuget.differentPackages%22;%0D%0A
   privat
@@ -2851,65 +2851,192 @@
     
-publisher.publishStatistic(TOTAL_PACKAGES, countEntries()
+final Pair%3CInteger, Integer%3E entries = countEntries();%0D%0A      publisher.publishStatistic(TOTAL_PACKAGES, entries.first);%0D%0A      publisher.publishStatistic(DIFF_PACKAGES, entries.second
 );%0D%0A
@@ -3061,19 +3061,38 @@
 private 
-int
+Pair%3CInteger, Integer%3E
  countEn
@@ -3122,16 +3122,80 @@
 t = 0;%0D%0A
+    final Set%3CString%3E packagesCounter = new HashSet%3CString%3E();%0D%0A
     fina
@@ -3292,59 +3292,179 @@
     
-it.next();%0D%0A      count++;%0D%0A    %7D%0D%0A    return count
+NuGetIndexEntry next = it.next();%0D%0A      packagesCounter.add(next.getAttributes().get(%22Id%22));%0D%0A      count++;%0D%0A    %7D%0D%0A    return Pair.create(count, packagesCounter.size())
 ;%0D%0A 
@@ -3761,32 +3761,143 @@
 , null, null);%0D%0A
+    presentationManager.applyPresentation(DIFF_PACKAGES, %22Different Package Ids Count%22, %22NuGet%22, null, null);%0D%0A
     presentation

include number of packages in stats