com.sonyericsson.jenkins.plugins.bfa.db
Class MongoDBKnowledgeBase

java.lang.Object
  extended by com.sonyericsson.jenkins.plugins.bfa.db.KnowledgeBase
      extended by com.sonyericsson.jenkins.plugins.bfa.db.MongoDBKnowledgeBase
All Implemented Interfaces:
hudson.model.Describable<KnowledgeBase>, Serializable

public class MongoDBKnowledgeBase
extends KnowledgeBase

Handling of the MongoDB way of saving the knowledge base.

Author:
Tomas Westling <tomas.westling@sonyericsson.com>
See Also:
Serialized Form

Nested Class Summary
static class MongoDBKnowledgeBase.MongoDBKnowledgeBaseDescriptor
          Descriptor for LocalFileKnowledgeBase.
 
Nested classes/interfaces inherited from class com.sonyericsson.jenkins.plugins.bfa.db.KnowledgeBase
KnowledgeBase.KnowledgeBaseDescriptor
 
Field Summary
static String COLLECTION_NAME
          The name of the cause collection in the database.
static String STATISTICS_COLLECTION_NAME
          The name of the statistics collection in the database.
 
Constructor Summary
MongoDBKnowledgeBase(String host, int port, String dbName, String userName, hudson.util.Secret password, boolean enableStatistics, boolean successfulLogging)
          Standard constructor.
 
Method Summary
 FailureCause addCause(FailureCause cause)
          Saves a new cause to the db and generates a new id for the cause.
 FailureCause addCause(FailureCause cause, boolean doUpdate)
          Does not update the cache, used when we know we will have a lot of save/add calls all at once, e.g.
 void convertFrom(KnowledgeBase oldKnowledgeBase)
          Converts the existing old knowledge base into this one.
protected  void convertRemoved(MongoDBKnowledgeBase oldKnowledgeBase)
          Copies all causes flagged as removed from the old database to this one.
 boolean equals(KnowledgeBase oldKnowledgeBase)
          Called to see if the configuration has changed.
 boolean equals(Object other)
           
static boolean equals(Object firstObject, Object secondObject)
          Checks if two objects equal each other, both being null counts as being equal.
 List<String> getCategories()
          Gets the unique categories of all FailureCauses.
 FailureCause getCause(String id)
          Get the cause with the given id.
 Collection<FailureCause> getCauseNames()
          Get the list of the FailureCause's names and ids.
 Collection<FailureCause> getCauses()
          Get the list of FailureCauses.
 String getDbName()
          Getter for the database name value.
 hudson.model.Descriptor<KnowledgeBase> getDescriptor()
           
 List<ObjectCountPair<String>> getFailureCauseNames(GraphFilterBuilder filter)
          Gets a list of ObjectCountPairs where each pair contains a unique FailureCause-name as key and the number of times that failure cause was triggered as count.
 Map<Integer,List<FailureCause>> getFailureCausesPerBuild(GraphFilterBuilder filter)
          Gets a map where a lists of failure causes are mapped by the build number for which they were triggered.
 List<FailureCauseTimeInterval> getFailureCausesPerTime(int intervalSize, GraphFilterBuilder filter, boolean byCategories)
          Fetches failure causes grouped in time intervals.
 String getHost()
          Getter for the host value.
 List<ObjectCountPair<String>> getNbrOfFailureCategoriesPerName(GraphFilterBuilder filter, int limit)
          Gets a list of ObjectCountPair where each pair contains a unique failure category string as key and the number of times that the failure cause category was triggered as count.
 List<ObjectCountPair<FailureCause>> getNbrOfFailureCauses(GraphFilterBuilder filter)
          Gets a list of ObjectCountPair where each pair contains a unique FailureCause as key and the number of times that failure cause was triggered as count.
 List<ObjectCountPair<String>> getNbrOfFailureCausesPerId(GraphFilterBuilder filter, int maxNbr)
          Gets a list of ObjectCountPair where each pair contains a unique FailureCause id as key and a belonging count value for how many times that FailureCause was triggered.
 long getNbrOfNullFailureCauses(GraphFilterBuilder filter)
          Counts how many statistics posts there are without FailureCause (null) for a given filter.
 hudson.util.Secret getPassword()
          Getter for the MongoDB password.
 int getPort()
          Getter for the port value.
protected  List<com.mongodb.DBObject> getRemovedCauses()
          Gets all causes flagged as removed in a "raw" JSON format.
 Collection<FailureCause> getShallowCauses()
          Get a shallow list of the FailureCauses.
 List<Statistics> getStatistics(GraphFilterBuilder filter, int limit)
          Gets Statistics data.
 Map<org.jfree.data.time.TimePeriod,Double> getUnknownFailureCauseQuotaPerTime(int intervalSize, GraphFilterBuilder filter)
          Gets the quota of unknown failure causes mapped by time periods.
 String getUserName()
          Getter for the MongoDB user name.
 int hashCode()
           
 boolean isStatisticsEnabled()
          If Statistics logging is enabled on this knowledge base or not.
 boolean isSuccessfulLoggingEnabled()
          If all builds should be added to statistics logging, not just unsuccessful builds.
 void removeBuildfailurecause(hudson.model.AbstractBuild build)
          Removes the build failure cause of particular build.
 FailureCause removeCause(String id)
          Removes the cause from the knowledge base.
 FailureCause saveCause(FailureCause cause)
          Saves a cause to the db.
 FailureCause saveCause(FailureCause cause, boolean doUpdate)
          Does not update the cache, used when we know we will have a lot of save/add calls all at once, e.g.
 void saveStatistics(Statistics stat)
          Saves the Statistics.
 void start()
          Called when the KnowledgeBase should be up and running.
 void stop()
          Called when it is time to clean up after the KnowledgeBase.
 
Methods inherited from class com.sonyericsson.jenkins.plugins.bfa.db.KnowledgeBase
convertFromAbstract
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

COLLECTION_NAME

public static final String COLLECTION_NAME
The name of the cause collection in the database.

See Also:
Constant Field Values

STATISTICS_COLLECTION_NAME

public static final String STATISTICS_COLLECTION_NAME
The name of the statistics collection in the database.

See Also:
Constant Field Values
Constructor Detail

MongoDBKnowledgeBase

@DataBoundConstructor
public MongoDBKnowledgeBase(String host,
                                                 int port,
                                                 String dbName,
                                                 String userName,
                                                 hudson.util.Secret password,
                                                 boolean enableStatistics,
                                                 boolean successfulLogging)
Standard constructor.

Parameters:
host - the host to connect to.
port - the port to connect to.
dbName - the database name to connect to.
userName - the user name for the database.
password - the password for the database.
enableStatistics - if statistics logging should be enabled or not.
successfulLogging - if all builds should be logged to the statistics DB
Method Detail

getUserName

public String getUserName()
Getter for the MongoDB user name.

Returns:
the user name.

getPassword

public hudson.util.Secret getPassword()
Getter for the MongoDB password.

Returns:
the password.

getHost

public String getHost()
Getter for the host value.

Returns:
the host string.

getPort

public int getPort()
Getter for the port value.

Returns:
the port number.

getDbName

public String getDbName()
Getter for the database name value.

Returns:
the database name string.

start

public void start()
           throws UnknownHostException,
                  AuthenticationException
Description copied from class: KnowledgeBase
Called when the KnowledgeBase should be up and running.

Specified by:
start in class KnowledgeBase
Throws:
UnknownHostException
AuthenticationException

stop

public void stop()
Description copied from class: KnowledgeBase
Called when it is time to clean up after the KnowledgeBase.

Specified by:
stop in class KnowledgeBase

getCauses

public Collection<FailureCause> getCauses()
                                   throws UnknownHostException,
                                          AuthenticationException
Description copied from class: KnowledgeBase
Get the list of FailureCauses. It is intended to be used in the scanning phase hence it should be returned as quickly as possible, so the list could be cached.

Specified by:
getCauses in class KnowledgeBase
Returns:
the full list of causes.
Throws:
UnknownHostException - if a connection to the host cannot be made.
AuthenticationException - if we cannot authenticate towards the database.
See Also:
Can throw MongoException if unknown fields exist in the database.

getCauseNames

public Collection<FailureCause> getCauseNames()
                                       throws UnknownHostException,
                                              AuthenticationException
Description copied from class: KnowledgeBase
Get the list of the FailureCause's names and ids. The list should be the latest possible from the DB as they will be used for editing. The objects returned should contain at least the id and the name of the cause.

Specified by:
getCauseNames in class KnowledgeBase
Returns:
the full list of the names and ids of the causes..
Throws:
UnknownHostException - if a connection to the host cannot be made.
AuthenticationException - if we cannot authenticate towards the database.
See Also:
Can throw MongoException if unknown fields exist in the database.

getShallowCauses

public Collection<FailureCause> getShallowCauses()
                                          throws Exception
Description copied from class: KnowledgeBase
Get a shallow list of the FailureCauses. The list should be the latest possible from the DB as they will be used in the list of causes to edit. shallow meaning no indications but information enough to show a nice list; at least id and name but description and categories are preferred as well.

Specified by:
getShallowCauses in class KnowledgeBase
Returns:
a shallow list of all causes.
Throws:
Exception - if something in the KnowledgeBase handling goes wrong.
See Also:
KnowledgeBase.getCauseNames()

getCause

public FailureCause getCause(String id)
                      throws UnknownHostException,
                             AuthenticationException
Description copied from class: KnowledgeBase
Get the cause with the given id. The cause returned is intended to be edited right away, so it should be as fresh from the db as possible.

Specified by:
getCause in class KnowledgeBase
Parameters:
id - the id of the cause.
Returns:
the cause or null if a cause with that id could not be found.
Throws:
UnknownHostException
AuthenticationException

addCause

public FailureCause addCause(FailureCause cause)
                      throws UnknownHostException,
                             AuthenticationException
Description copied from class: KnowledgeBase
Saves a new cause to the db and generates a new id for the cause.

Specified by:
addCause in class KnowledgeBase
Parameters:
cause - the cause to add.
Returns:
the same cause but with a new id.
Throws:
UnknownHostException
AuthenticationException

removeCause

public FailureCause removeCause(String id)
                         throws Exception
Description copied from class: KnowledgeBase
Removes the cause from the knowledge base.

Specified by:
removeCause in class KnowledgeBase
Parameters:
id - the id of the cause to remove.
Returns:
the removed FailureCause.
Throws:
Exception - if so.

addCause

public FailureCause addCause(FailureCause cause,
                             boolean doUpdate)
                      throws UnknownHostException,
                             AuthenticationException
Does not update the cache, used when we know we will have a lot of save/add calls all at once, e.g. during a convert.

Parameters:
cause - the FailureCause to add.
doUpdate - true if a cache update should be made, false if not.
Returns:
the added FailureCause.
Throws:
UnknownHostException - If a connection to the Mongo database cannot be made.
AuthenticationException - if we cannot authenticate towards the database.
See Also:
addCause(FailureCause)

saveCause

public FailureCause saveCause(FailureCause cause)
                       throws UnknownHostException,
                              AuthenticationException
Description copied from class: KnowledgeBase
Saves a cause to the db. Assumes that the id is kept from when it was fetched. Can also be an existing cause in another KnowledgeBase implementation with a preexisting id that is being converted via KnowledgeBase.convertFrom(KnowledgeBase).

Specified by:
saveCause in class KnowledgeBase
Parameters:
cause - the cause to add.
Returns:
the same cause but with a new id.
Throws:
UnknownHostException
AuthenticationException

saveCause

public FailureCause saveCause(FailureCause cause,
                              boolean doUpdate)
                       throws UnknownHostException,
                              AuthenticationException
Does not update the cache, used when we know we will have a lot of save/add calls all at once, e.g. during a convert.

Parameters:
cause - the FailureCause to save.
doUpdate - true if a cache update should be made, false if not.
Returns:
the saved FailureCause.
Throws:
UnknownHostException - If a connection to the Mongo database cannot be made.
AuthenticationException - if we cannot authenticate towards the database.
See Also:
saveCause(FailureCause)

convertFrom

public void convertFrom(KnowledgeBase oldKnowledgeBase)
                 throws Exception
Description copied from class: KnowledgeBase
Converts the existing old knowledge base into this one. Will be called after the creation of a new object when then Jenkins config is saved, So it could just be that the old one is exactly the same as this one.

Specified by:
convertFrom in class KnowledgeBase
Parameters:
oldKnowledgeBase - the old one.
Throws:
Exception - if something in the KnowledgeBase handling goes wrong.

getCategories

public List<String> getCategories()
                           throws UnknownHostException,
                                  AuthenticationException
Description copied from class: KnowledgeBase
Gets the unique categories of all FailureCauses.

Specified by:
getCategories in class KnowledgeBase
Returns:
the list of categories.
Throws:
UnknownHostException
AuthenticationException

convertRemoved

protected void convertRemoved(MongoDBKnowledgeBase oldKnowledgeBase)
                       throws Exception
Copies all causes flagged as removed from the old database to this one.

Parameters:
oldKnowledgeBase - the old database.
Throws:
Exception - if something goes wrong.

getRemovedCauses

protected List<com.mongodb.DBObject> getRemovedCauses()
                                               throws Exception
Gets all causes flagged as removed in a "raw" JSON format.

Returns:
the list of removed causes.
Throws:
Exception - if so.

equals

public boolean equals(KnowledgeBase oldKnowledgeBase)
Description copied from class: KnowledgeBase
Called to see if the configuration has changed.

Specified by:
equals in class KnowledgeBase
Parameters:
oldKnowledgeBase - the previous config.
Returns:
true if it is the same.

equals

public boolean equals(Object other)
Overrides:
equals in class Object

equals

public static boolean equals(Object firstObject,
                             Object secondObject)
Checks if two objects equal each other, both being null counts as being equal.

Parameters:
firstObject - the firstObject.
secondObject - the secondObject.
Returns:
true if equal or both null, false otherwise.

hashCode

public int hashCode()
Overrides:
hashCode in class KnowledgeBase

isStatisticsEnabled

public boolean isStatisticsEnabled()
Description copied from class: KnowledgeBase
If Statistics logging is enabled on this knowledge base or not.

Specified by:
isStatisticsEnabled in class KnowledgeBase
Returns:
true if so. False if not or not implemented.

isSuccessfulLoggingEnabled

public boolean isSuccessfulLoggingEnabled()
Description copied from class: KnowledgeBase
If all builds should be added to statistics logging, not just unsuccessful builds. Only relevant if KnowledgeBase.isStatisticsEnabled() is true.

Specified by:
isSuccessfulLoggingEnabled in class KnowledgeBase
Returns:
true if set, false otherwise or if not implemented

saveStatistics

public void saveStatistics(Statistics stat)
                    throws UnknownHostException,
                           AuthenticationException
Description copied from class: KnowledgeBase
Saves the Statistics.

Specified by:
saveStatistics in class KnowledgeBase
Parameters:
stat - the Statistics.
Throws:
UnknownHostException
AuthenticationException

getStatistics

public List<Statistics> getStatistics(GraphFilterBuilder filter,
                                      int limit)
                               throws UnknownHostException,
                                      AuthenticationException
Description copied from class: KnowledgeBase
Gets Statistics data. This method needs to be implemented in subclass for graph support.

Overrides:
getStatistics in class KnowledgeBase
Parameters:
filter - the filter to use when fetching data
limit - number of statistics items to fetch, set to nonpositive value to fetch all
Returns:
the list of statistics.
Throws:
UnknownHostException
AuthenticationException

getNbrOfNullFailureCauses

public long getNbrOfNullFailureCauses(GraphFilterBuilder filter)
Description copied from class: KnowledgeBase
Counts how many statistics posts there are without FailureCause (null) for a given filter. This method needs to be implemented in subclass for graph support.

Overrides:
getNbrOfNullFailureCauses in class KnowledgeBase
Parameters:
filter - the filter to use when fetching data
Returns:
number of statistics posts without FailureCause

getUnknownFailureCauseQuotaPerTime

public Map<org.jfree.data.time.TimePeriod,Double> getUnknownFailureCauseQuotaPerTime(int intervalSize,
                                                                                     GraphFilterBuilder filter)
Description copied from class: KnowledgeBase
Gets the quota of unknown failure causes mapped by time periods. This method needs to be implemented in subclass for graph support.

Overrides:
getUnknownFailureCauseQuotaPerTime in class KnowledgeBase
Parameters:
intervalSize - the interval sizes in which the data is grouped. Should be set to Calendar.MONTH, Calendar.DATE or Calendar.HOUR_OF_DAY.
filter - The filter to use when fetching the data
Returns:
failure cause quotas

getNbrOfFailureCausesPerId

public List<ObjectCountPair<String>> getNbrOfFailureCausesPerId(GraphFilterBuilder filter,
                                                                int maxNbr)
Description copied from class: KnowledgeBase
Gets a list of ObjectCountPair where each pair contains a unique FailureCause id as key and a belonging count value for how many times that FailureCause was triggered. This method needs to be implemented in subclass for graph support.

Overrides:
getNbrOfFailureCausesPerId in class KnowledgeBase
Parameters:
filter - the filter to use when fetching data
maxNbr - the number of items to fetch, set to nonpositive value to fetch all
Returns:
list of ObjectCountPairs

getNbrOfFailureCauses

public List<ObjectCountPair<FailureCause>> getNbrOfFailureCauses(GraphFilterBuilder filter)
Description copied from class: KnowledgeBase
Gets a list of ObjectCountPair where each pair contains a unique FailureCause as key and the number of times that failure cause was triggered as count. The list is sorted by counts, meaning that the FailureCause that has been triggered the most comes first. This method needs to be implemented in subclass for graph support.

Overrides:
getNbrOfFailureCauses in class KnowledgeBase
Parameters:
filter - the filter to use when fetching data
Returns:
list of ObjectCountPairs.

getFailureCauseNames

public List<ObjectCountPair<String>> getFailureCauseNames(GraphFilterBuilder filter)
Description copied from class: KnowledgeBase
Gets a list of ObjectCountPairs where each pair contains a unique FailureCause-name as key and the number of times that failure cause was triggered as count. This list is sorted by counts, meaning that the FailureCause that has been triggered the most comes first. This method needs to be implemented in subclass for graph support.

Overrides:
getFailureCauseNames in class KnowledgeBase
Parameters:
filter - The filter to use when fetching the data
Returns:
List of ObjectCountPairs that consist of a name and count

getFailureCausesPerBuild

public Map<Integer,List<FailureCause>> getFailureCausesPerBuild(GraphFilterBuilder filter)
Description copied from class: KnowledgeBase
Gets a map where a lists of failure causes are mapped by the build number for which they were triggered. This method needs to be implemented in subclass for graph support.

Overrides:
getFailureCausesPerBuild in class KnowledgeBase
Parameters:
filter - the filter to use when fetching data
Returns:
map of failure causes

getFailureCausesPerTime

public List<FailureCauseTimeInterval> getFailureCausesPerTime(int intervalSize,
                                                              GraphFilterBuilder filter,
                                                              boolean byCategories)
Description copied from class: KnowledgeBase
Fetches failure causes grouped in time intervals. The returned list does not have to be sorted, and one list element is created for each FailureCause for each time interval there exist data. This method needs to be implemented in subclass for graph support.

Overrides:
getFailureCausesPerTime in class KnowledgeBase
Parameters:
intervalSize - the interval sizes in which the data is grouped. Should be set to Calendar.MONTH, Calendar.DATE or Calendar.HOUR_OF_DAY.
filter - the filter to use when fetching data
byCategories - set to true in order to group failure causes by their categories
Returns:
list of FailureCauseTimeIntervals

getNbrOfFailureCategoriesPerName

public List<ObjectCountPair<String>> getNbrOfFailureCategoriesPerName(GraphFilterBuilder filter,
                                                                      int limit)
Description copied from class: KnowledgeBase
Gets a list of ObjectCountPair where each pair contains a unique failure category string as key and the number of times that the failure cause category was triggered as count. The list is sorted by counts, meaning that the Category that has been triggered the most comes first. This method needs to be implemented in subclass for graph support.

Overrides:
getNbrOfFailureCategoriesPerName in class KnowledgeBase
Parameters:
filter - the filter to use when fetching data
limit - the number of categories to fetch, set to nonpositive value to fetch all
Returns:
list of ObjectCountPairs

removeBuildfailurecause

public void removeBuildfailurecause(hudson.model.AbstractBuild build)
                             throws Exception
Description copied from class: KnowledgeBase
Removes the build failure cause of particular build.

Specified by:
removeBuildfailurecause in class KnowledgeBase
Parameters:
build - the AbstractBuild.
Throws:
Exception - if something in the KnowledgeBase handling goes wrong.

getDescriptor

public hudson.model.Descriptor<KnowledgeBase> getDescriptor()


Copyright © 2004-2014. All Rights Reserved.