-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[study] Add repository analysis study
Signed-off-by: inishchith <[email protected]>
- Loading branch information
1 parent
30ea904
commit cb01d31
Showing
2 changed files
with
295 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright (C) 2015-2019 Bitergia | ||
# | ||
# This program is free software; you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation; either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU General Public License | ||
# along with this program; if not, write to the Free Software | ||
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | ||
# | ||
# Authors: | ||
# Valerio Cosentino <[email protected]> | ||
# Nishchith Shetty <[email protected]> | ||
# | ||
|
||
from grimoirelab_toolkit.datetime import str_to_datetime | ||
|
||
|
||
def get_unique_repository(): | ||
""" Retrieve all the repository names from the index. """ | ||
|
||
query_unique_repository = """ | ||
{ | ||
"size": 0, | ||
"aggs": { | ||
"unique_repos": { | ||
"terms": { | ||
"field": "origin" | ||
} | ||
} | ||
} | ||
} | ||
""" | ||
|
||
return query_unique_repository | ||
|
||
|
||
def get_last_study_date(repository_url): | ||
""" Retrieve the last study_creation_date of the item corresponding | ||
to given repository from the study index. | ||
""" | ||
|
||
query_last_study_date = """ | ||
{ | ||
"size": 0, | ||
"aggs": { | ||
"1": { | ||
"max": { | ||
"field": "study_creation_date" | ||
} | ||
} | ||
}, | ||
"query": { | ||
"bool": { | ||
"filter": [{ | ||
"term": { | ||
"origin.keyword": "%s" | ||
} | ||
}] | ||
} | ||
} | ||
} | ||
""" % (repository_url) | ||
|
||
return query_last_study_date | ||
|
||
|
||
def get_first_enriched_date(repository_url): | ||
""" Retrieve the first/oldest metadata__updated_on of the item | ||
corresponding to given repository. | ||
""" | ||
|
||
query_first_enriched_date = """ | ||
{ | ||
"size": 0, | ||
"aggs": { | ||
"1": { | ||
"top_hits": { | ||
"docvalue_fields": [ | ||
"metadata__updated_on" | ||
], | ||
"_source": "metadata__updated_on", | ||
"size": 1, | ||
"sort": [{ | ||
"commit_date": { | ||
"order": "asc" | ||
} | ||
}] | ||
} | ||
} | ||
}, | ||
"query": { | ||
"bool": { | ||
"filter": [{ | ||
"term": { | ||
"origin": "%s" | ||
} | ||
}] | ||
} | ||
} | ||
} | ||
""" % (repository_url) | ||
|
||
return query_first_enriched_date | ||
|
||
|
||
def get_files_at_time(repository_url, to_date): | ||
""" Retrieve all the latest changes wrt files until the to_date, | ||
corresponding to the given repository. | ||
""" | ||
|
||
query_files_at_time = """ | ||
{ | ||
"size": 0, | ||
"aggs": { | ||
"file_stats": { | ||
"terms": { | ||
"field": "file_path", | ||
"size": 2147483647, | ||
"order": { | ||
"_key": "desc" | ||
} | ||
}, | ||
"aggs": { | ||
"1": { | ||
"top_hits": { | ||
"size": 1, | ||
"sort": [{ | ||
"metadata__updated_on": { | ||
"order": "desc" | ||
} | ||
}] | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
"query": { | ||
"bool": { | ||
"filter": [{ | ||
"term": { | ||
"origin": "%s" | ||
} | ||
}, | ||
{ | ||
"range": { | ||
"metadata__updated_on": { | ||
"lte": "%s" | ||
} | ||
} | ||
}] | ||
} | ||
} | ||
} | ||
""" % (repository_url, to_date) | ||
|
||
return query_files_at_time | ||
|
||
|
||
def get_to_date(es_in, in_index, out_index, repository_url): | ||
""" Get the appropriate to_date value for incremental insertion. """ | ||
study_data_available = False | ||
|
||
if es_in.indices.exists(index=out_index): | ||
last_study_date = es_in.search( | ||
index=out_index, | ||
body=get_last_study_date(repository_url))["aggregations"]["1"] | ||
|
||
if last_study_date["value"] is not None: | ||
study_data_available = True | ||
to_date = str_to_datetime(last_study_date["value_as_string"]) | ||
|
||
if not study_data_available: | ||
first_item_date = es_in.search( | ||
index=in_index, | ||
body=get_first_enriched_date(repository_url))["aggregations"]["1"]["hits"]["hits"][0]["_source"] | ||
|
||
to_date = str_to_datetime(first_item_date["metadata__updated_on"]) | ||
|
||
return to_date |