001 /*
002 * SonarQube, open source software quality management tool.
003 * Copyright (C) 2008-2014 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * SonarQube is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * SonarQube is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public License
017 * along with this program; if not, write to the Free Software Foundation,
018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
019 */
020 package org.sonar.batch.components;
021
022 import org.apache.commons.lang.time.DateUtils;
023 import org.sonar.api.BatchExtension;
024 import org.sonar.api.CoreProperties;
025 import org.sonar.api.database.DatabaseSession;
026 import org.sonar.api.database.model.Snapshot;
027 import org.sonar.api.resources.Qualifiers;
028
029 import javax.annotation.CheckForNull;
030 import java.util.Date;
031 import java.util.List;
032
033 public class PastSnapshotFinderByDays implements BatchExtension {
034
035 private DatabaseSession session;
036
037 public PastSnapshotFinderByDays(DatabaseSession session) {
038 this.session = session;
039 }
040
041 PastSnapshot findFromDays(Snapshot projectSnapshot, int days) {
042 Date targetDate = DateUtils.addDays(projectSnapshot.getCreatedAt(), -days);
043 String hql = "from " + Snapshot.class.getSimpleName() + " where resourceId=:resourceId AND status=:status AND createdAt<:date AND qualifier<>:lib order by createdAt asc";
044 List<Snapshot> snapshots = session.createQuery(hql)
045 .setParameter("date", projectSnapshot.getCreatedAt())
046 .setParameter("resourceId", projectSnapshot.getResourceId())
047 .setParameter("status", Snapshot.STATUS_PROCESSED)
048 .setParameter("lib", Qualifiers.LIBRARY)
049 .getResultList();
050
051 Snapshot snapshot = getNearestToTarget(snapshots, targetDate);
052 return new PastSnapshot(CoreProperties.TIMEMACHINE_MODE_DAYS, targetDate, snapshot).setModeParameter(String.valueOf(days));
053 }
054
055 @CheckForNull
056 static Snapshot getNearestToTarget(List<Snapshot> snapshots, Date currentDate, int distanceInDays) {
057 Date targetDate = DateUtils.addDays(currentDate, -distanceInDays);
058 return getNearestToTarget(snapshots, targetDate);
059 }
060
061 @CheckForNull
062 static Snapshot getNearestToTarget(List<Snapshot> snapshots, Date targetDate) {
063 long bestDistance = Long.MAX_VALUE;
064 Snapshot nearest = null;
065 for (Snapshot snapshot : snapshots) {
066 long distance = distance(snapshot.getCreatedAt(), targetDate);
067 if (distance <= bestDistance) {
068 bestDistance = distance;
069 nearest = snapshot;
070 }
071 }
072 return nearest;
073 }
074
075 static long distance(Date d1, Date d2) {
076 return Math.abs(d1.getTime() - d2.getTime());
077 }
078 }