Esempio n. 1
0
class PersistentTimelineAnalysisCoordinator(object):
    
    def __init__(self):
        self.__boardAnalyser = BoardAnalyser()
        self.__timelineAnalyser = BoardTimelineAnalyser()

    def GetAnalysis(self, board):
        boardIsCached = self.__timelineAnalyser.BoardIsCached(board)
        if boardIsCached:
            timelineAnalysis = self.__timelineAnalyser.GetAnalysis(board)
            return timelineAnalysis

        dbBoardAnalysis = BoardAnalysisToDB.GetBoardAnalysisFromDB(board)
        if not dbBoardAnalysis.IsValid:
            boardAnalysis = self.__boardAnalyser.GetAnalysis(board)
            BoardAnalysisToDB.SaveBoardAnalysisToDB(boardAnalysis)

        dbTimelineAnalysis = BoardTimelineAnalysisToDB.GetBoardTimelineAnalysisFromDB(board)            
        if dbTimelineAnalysis.IsValid and dbTimelineAnalysis.YearlyDecisions:
            timelineAnalysis = dbTimelineAnalysis
        else:
            timelineAnalysis = self.__timelineAnalyser.GetAnalysis(board)
            BoardTimelineAnalysisToDB.SaveBoardTimelineAnalysisToDB(timelineAnalysis)

        return timelineAnalysis
    def _analyseAndSave(self, board):
        self.stdout.write('Analysing {}.'.format(board))
        boardAnalyser = BoardAnalyser()
        boardAnalysis = boardAnalyser.GetAnalysis(board)
        BoardAnalysisToDB.SaveBoardAnalysisToDB(boardAnalysis)

        timelineAnalyser = BoardTimelineAnalyser()
        timelineAnalysis = timelineAnalyser.GetAnalysis(board)
        BoardTimelineAnalysisToDB.SaveBoardTimelineAnalysisToDB(
            timelineAnalysis)
Esempio n. 3
0
 def setUp(self):
     from Decisions.Analysers.TimelineAnalysers import BoardTimelineAnalyser, NullBoardTimelineAnalysis
     from Decisions.Analysers.AnalysisBase import OutdatedAnalysis
     self.analyser = BoardTimelineAnalyser()
     self.outdated = OutdatedAnalysis()
     self.nullanalysis = NullBoardTimelineAnalysis()
     self.techBoard = '3.5.01'
     self.legalBoard = '3.1.01'
     self.falseBoard = 'NoneExistantBoard'
     self.realBoardAnalysis = self.analyser.GetAnalysis(self.techBoard)
     self.falseBoardAnalysis = self.analyser.GetAnalysis(self.falseBoard)
     self.legalBoardAnalysis = self.analyser.GetAnalysis(self.legalBoard)
    def test_SaveAndRetreive(self):
        from Decisions.AnalysisStorers import BoardTimelineAnalysisToDB
        from Decisions.Analysers.TimelineAnalysers import BoardTimelineAnalyser

        board = '3.5.01'
        analyser = BoardTimelineAnalyser()
        before = analyser.GetAnalysis(board)
        before.Board = 'test'
        BoardTimelineAnalysisToDB.SaveBoardTimelineAnalysisToDB(before)
        after = BoardTimelineAnalysisToDB.GetBoardTimelineAnalysisFromDB(
            'test')
        self.assertEqual(after.Board, before.Board)
Esempio n. 5
0
    def test_DecisinModelProxy_GetAllForBoardOrderedByDecisionDate(self):
        from Decisions.DBProxy import DecisionModelProxy
        from Decisions.Analysers.BoardAnalyser import BoardAnalyser
        from Decisions.Analysers.TimelineAnalysers import BoardTimelineAnalyser

        board = '3.5.07'

        analyser = BoardAnalyser()
        result = analyser.GetAnalysis(board)
        self.assertEqual(result.Board, board)
        self.assertEqual(len(result.CitationTop5), 5)
        self.assertEqual(len(result.ArticleTop5), 5)
        self.assertEqual(len(result.IpcTop5), 5)
        self.assertEqual(len(result.Early), 5)
        self.assertEqual(len(result.Late), 5)
        self.assertTrue(
            result.Early[0].DecisionDate <= result.Late[0].DecisionDate)

        analyser = BoardTimelineAnalyser()
        result = analyser.GetAnalysis(board)
        self.assertEqual(result.Board, board)
        self.assertEqual(result.YearlyDecisions[2014], 38)
Esempio n. 6
0
class test_BoardTimelineAnalyser(TestCase):
        
    @classmethod
    def setUpClass(cls):    
        super(test_BoardTimelineAnalyser, cls).setUpClass()
        django.setup()
        
    def setUp(self):
        from Decisions.Analysers.TimelineAnalysers import BoardTimelineAnalyser, NullBoardTimelineAnalysis
        from Decisions.Analysers.AnalysisBase import OutdatedAnalysis
        self.analyser = BoardTimelineAnalyser()
        self.outdated = OutdatedAnalysis()
        self.nullanalysis = NullBoardTimelineAnalysis()
        self.techBoard = '3.5.01'
        self.legalBoard = '3.1.01'
        self.falseBoard = 'NoneExistantBoard'
        self.realBoardAnalysis = self.analyser.GetAnalysis(self.techBoard)
        self.falseBoardAnalysis = self.analyser.GetAnalysis(self.falseBoard)
        self.legalBoardAnalysis = self.analyser.GetAnalysis(self.legalBoard)

    def test_GetAnalysisReturnsSomething(self):
        self.assertFalse(self.realBoardAnalysis is None)

    def test_AnalysisIsNotOutofdate(self):
        self.assertFalse(self.realBoardAnalysis == self.outdated)
        self.assertFalse(self.falseBoardAnalysis == self.outdated)
        
    def test_AnalysisOnlyNullForNonBoard(self):
        self.assertEqual(self.falseBoardAnalysis, self.nullanalysis, 'unreal board did not return NullBoardAnalysis')
        self.assertNotEqual(self.realBoardAnalysis, self.nullanalysis, 'real board returned NullBoardAnalysis')

    def test_YearlyDecisionsNotEmpty(self):
        self.assertNotEqual (self.realBoardAnalysis.YearlyDecisions, None)

    def test_YearlyDecisionsCountFor3501_to1999(self):
        countYears = [year for year in self.realBoardAnalysis.YearlyDecisions if year <= 1999]
        self.assertEqual(len(countYears), 18)
        
    def test_TotalDecisionsCountFor3501_to1999(self):
        to1999 = [v for k, v in self.realBoardAnalysis.YearlyDecisions.items() if v <= 1999]
        self.assertTrue(sum(to1999) > 1600)

    def test_AnalysesAreCached(self):
        board1 = self.techBoard
        board2 = self.legalBoard
        a1 = self.realBoardAnalysis
        self.assertTrue(board1 in self.analyser.CachedBoardList, 'First analysis: board1')
        a2 = self.legalBoardAnalysis
        self.assertTrue(board2 in self.analyser.CachedBoardList, 'Second analysis: board2')
        self.assertTrue(board1 in self.analyser.CachedBoardList, 'Second analysis: board1')
        a3 = self.analyser.GetAnalysis(board1) # do not replace by self.realBoardAnalysis
        self.assertTrue(board2 in self.analyser.CachedBoardList, 'Third analysis: board2')
        self.assertTrue(board1 in self.analyser.CachedBoardList, 'Third analysis: board1')

        
    def test_removeOldFromCache(self):
        board1 = self.techBoard
        board2 = self.legalBoard
        a1 = self.realBoardAnalysis
        a2 = self.legalBoardAnalysis
        cachedBoards1 = self.analyser.CachedBoardList
        a1.Timestamp = datetime.now() - timedelta(days = 2)
        a2.Timestamp = datetime.now()
        a3 = self.analyser.GetAnalysis(board2)
        cachedBoards2 = self.analyser.CachedBoardList
        self.assertTrue(board1 in cachedBoards1)
        self.assertTrue(board2 in cachedBoards1)
        self.assertFalse(board1 in cachedBoards2)
        self.assertTrue(board2 in cachedBoards2)
Esempio n. 7
0
 def __init__(self):
     self.__boardAnalyser = BoardAnalyser()
     self.__timelineAnalyser = BoardTimelineAnalyser()