PageRenderTime 171ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/storage/src/main/java/jahspotify/storage/statistics/MongoDBHistoricalStorage.java

http://github.com/johanlindquist/jahspotify
Java | 182 lines | 129 code | 32 blank | 21 comment | 6 complexity | 172598c2c93ebdd1efe0eada7ee52ccb MD5 | raw file
  1. package jahspotify.storage.statistics;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. import java.util.*;
  21. import javax.annotation.PostConstruct;
  22. import com.google.gson.Gson;
  23. import com.mongodb.*;
  24. import com.mongodb.util.JSON;
  25. import jahspotify.media.Link;
  26. import org.apache.commons.logging.*;
  27. import org.springframework.beans.factory.annotation.*;
  28. import org.springframework.stereotype.Service;
  29. /**
  30. * @author Johan Lindquist
  31. */
  32. @Service
  33. @Qualifier(value = "mongodb")
  34. public class MongoDBHistoricalStorage implements HistoricalStorage
  35. {
  36. private Log _log = LogFactory.getLog(MongoDBHistoricalStorage.class);
  37. @Value(value="${jahspotify.storage.mongodb.host}")
  38. private String _dbHost = "localhost";
  39. @Value(value="${jahspotify.storage.mongodb.port}")
  40. private int _dbPort = 27017;
  41. @Value(value="${jahspotify.storage.mongodb.db-name}")
  42. private String _dbName = "JahSpotify";
  43. private Mongo _mongoDBInstance;
  44. private DB _db;
  45. private static Gson _gson = new Gson();
  46. @PostConstruct
  47. public void initialize()
  48. {
  49. try
  50. {
  51. _mongoDBInstance = new Mongo();
  52. _db = _mongoDBInstance.getDB(_dbName);
  53. }
  54. catch (Exception e)
  55. {
  56. }
  57. }
  58. @Override
  59. public TrackHistory getHistory(final Link trackLink)
  60. {
  61. final DBCollection tracks = _db.getCollection("history");
  62. final DBObject query = new BasicDBObject( "id", trackLink.getUri() );
  63. final DBCursor dbObjects = tracks.find(query);
  64. if (dbObjects.size() == 1)
  65. {
  66. return _gson.fromJson(JSON.serialize(dbObjects.next()), TrackHistory.class);
  67. }
  68. return null;
  69. }
  70. @Override
  71. public Collection<TrackHistory> getHistory(final int index, final int count, final HistoryCriteria... historyCriterias)
  72. {
  73. final DBCollection tracks = _db.getCollection("history");
  74. final DBCursor dbObjects = tracks.find();
  75. dbObjects.skip(index);
  76. dbObjects.limit(count);
  77. final BasicDBObject orderBy = new BasicDBObject();
  78. orderBy.put("startTime",-1);
  79. dbObjects.sort(orderBy);
  80. return new AbstractCollection<TrackHistory>()
  81. {
  82. @Override
  83. public Iterator<TrackHistory> iterator()
  84. {
  85. return new MongoDBHistoryCursor(dbObjects);
  86. }
  87. @Override
  88. public int size()
  89. {
  90. return dbObjects.size();
  91. }
  92. };
  93. }
  94. @Override
  95. public void addHistory(final TrackHistory trackHistory)
  96. {
  97. final DBCollection history = _db.getCollection("history");
  98. final BasicDBObject basicDBObject = (BasicDBObject) JSON.parse(new Gson().toJson(trackHistory));
  99. final WriteResult insert = history.insert(basicDBObject);
  100. }
  101. @Override
  102. public TrackStatistics getTrackStatistics(final Link trackLink)
  103. {
  104. final DBCollection tracks = _db.getCollection("history");
  105. final BasicDBObject query = new BasicDBObject();
  106. query.put("trackLink.id",trackLink.getId());
  107. final DBCursor dbObjects = tracks.find(query);
  108. final BasicDBObject orderBy = new BasicDBObject();
  109. orderBy.put("startTime",-1);
  110. dbObjects.sort(orderBy);
  111. TrackStatistics trackStatistics = new TrackStatistics();
  112. int numCompleted = 0;
  113. int numSkipped = 0;
  114. int totalSecondsPlayed = 0;
  115. if (dbObjects.hasNext())
  116. {
  117. final TrackHistory trackHistory = _gson.fromJson(JSON.serialize(dbObjects.next()), TrackHistory.class);
  118. trackStatistics.setTrackLink( trackHistory.getTrackLink() );
  119. trackStatistics.setLastPlayed(trackHistory.getStartTime());
  120. totalSecondsPlayed += trackHistory.getSecondsPlayed();
  121. numSkipped += trackHistory.isCompleteTrackPlayed() ? 0 : 1;
  122. numCompleted += trackHistory.isCompleteTrackPlayed() ? 1 : 0;
  123. if (!dbObjects.hasNext())
  124. {
  125. trackStatistics.setFirstPlayed(trackHistory.getStartTime());
  126. }
  127. }
  128. while (dbObjects.hasNext())
  129. {
  130. final TrackHistory trackHistory = _gson.fromJson(JSON.serialize(dbObjects.next()), TrackHistory.class);
  131. totalSecondsPlayed += trackHistory.getSecondsPlayed();
  132. numSkipped += trackHistory.isCompleteTrackPlayed() ? 0 : 1;
  133. numCompleted += trackHistory.isCompleteTrackPlayed() ? 1 : 0;
  134. if (!dbObjects.hasNext())
  135. {
  136. trackStatistics.setFirstPlayed(trackHistory.getStartTime());
  137. }
  138. }
  139. trackStatistics.setNumTimesCompleted(numCompleted);
  140. trackStatistics.setNumTimesSkipped(numSkipped);
  141. trackStatistics.setNumTimesPlayed(numCompleted+numSkipped);
  142. trackStatistics.setTotalPlaytime(totalSecondsPlayed);
  143. return trackStatistics;
  144. }
  145. @Override
  146. public int getHistoryCount(final HistoryCriteria... historyCriterias)
  147. {
  148. return 0;
  149. }
  150. }