Skip to content
Snippets Groups Projects
Commit 4e06d84c authored by Masami  Suzuki's avatar Masami Suzuki Committed by Yas Naoi
Browse files

Issue #3255554 by Masami, yas: Store the cost of each project in short time...

Issue #3255554 by Masami, yas: Store the cost of each project in short time range (less than 10 mins)
parent 16961fc1
No related branches found
No related tags found
No related merge requests found
k8s_js_refresh_interval: 10 k8s_js_refresh_interval: 10
k8s_update_resources_queue_cron_time: 5 k8s_update_resources_queue_cron_time: 5
k8s_update_cost_store_cron_time: 600
k8s_yaml_file_extensions: 'yaml yml' k8s_yaml_file_extensions: 'yaml yml'
k8s_view_items_per_page: 50 k8s_view_items_per_page: 50
k8s_view_expose_items_per_page: true k8s_view_expose_items_per_page: true
......
...@@ -9,6 +9,8 @@ k8s.settings: ...@@ -9,6 +9,8 @@ k8s.settings:
type: integer type: integer
k8s_update_resources_queue_cron_time: k8s_update_resources_queue_cron_time:
type: integer type: integer
k8s_update_cost_store_cron_time:
type: integer
k8s_cloud_config_icon: k8s_cloud_config_icon:
type: integer type: integer
k8s_yaml_file_extensions: k8s_yaml_file_extensions:
......
...@@ -3439,3 +3439,13 @@ function k8s_update_8337(): void { ...@@ -3439,3 +3439,13 @@ function k8s_update_8337(): void {
]; ];
\Drupal::service('cloud')->updateYmlDefinitions($files, 'k8s'); \Drupal::service('cloud')->updateYmlDefinitions($files, 'k8s');
} }
/**
* Add configuration k8s_update_cost_store_cron_time.
*/
function k8s_update_8338(): void {
$config_factory = \Drupal::configFactory();
$config = $config_factory->getEditable('k8s.settings');
$config->set('k8s_update_cost_store_cron_time', 600);
$config->save();
}
...@@ -165,6 +165,16 @@ class K8sAdminSettings extends ConfigFormBase { ...@@ -165,6 +165,16 @@ class K8sAdminSettings extends ConfigFormBase {
'#field_suffix' => 'seconds', '#field_suffix' => 'seconds',
]; ];
$form['cron']['k8s_update_cost_store_cron_time'] = [
'#type' => 'number',
'#title' => 'Update cost store every',
'#description' => $this->t('Run cron to process queue to update cost stores.'),
'#default_value' => $config->get('k8s_update_cost_store_cron_time'),
'#min' => 1,
'#max' => 9999,
'#field_suffix' => 'seconds',
];
$form['cron']['k8s_queue_limit'] = [ $form['cron']['k8s_queue_limit'] = [
'#type' => 'number', '#type' => 'number',
'#title' => 'Queue Limit', '#title' => 'Queue Limit',
......
...@@ -25,11 +25,11 @@ use Symfony\Component\DependencyInjection\ContainerInterface; ...@@ -25,11 +25,11 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
class K8sCloudStorePlugin extends CloudPluginBase implements CloudStorePluginInterface, ContainerFactoryPluginInterface { class K8sCloudStorePlugin extends CloudPluginBase implements CloudStorePluginInterface, ContainerFactoryPluginInterface {
/** /**
* Variable to compare with the time difference. * Interval to update cost stores.
* *
* @var int * @var int
*/ */
private $length = 60 * 60; private $updateInterval = 600;
/** /**
* The K8s Service. * The K8s Service.
...@@ -152,6 +152,10 @@ class K8sCloudStorePlugin extends CloudPluginBase implements CloudStorePluginInt ...@@ -152,6 +152,10 @@ class K8sCloudStorePlugin extends CloudPluginBase implements CloudStorePluginInt
$this->entityLinkRenderer = $entity_link_renderer; $this->entityLinkRenderer = $entity_link_renderer;
$this->fileSystem = $file_system; $this->fileSystem = $file_system;
$this->classResolver = $class_resolver; $this->classResolver = $class_resolver;
$this->updateInterval = $this->configFactory
->get('k8s.settings')
->get('k8s_update_cost_store_cron_time');
} }
/** /**
...@@ -215,7 +219,7 @@ class K8sCloudStorePlugin extends CloudPluginBase implements CloudStorePluginInt ...@@ -215,7 +219,7 @@ class K8sCloudStorePlugin extends CloudPluginBase implements CloudStorePluginInt
* @param string $entity_bundle * @param string $entity_bundle
* Bundle string. * Bundle string.
* *
* @return CloudConfig * @return \Drupal\cloud\Entity\CloudConfigInterface
* Entity or NULL if there is no entity. * Entity or NULL if there is no entity.
*/ */
public function loadConfigEntity(string $entity_bundle): ?CloudConfigInterface { public function loadConfigEntity(string $entity_bundle): ?CloudConfigInterface {
...@@ -385,11 +389,15 @@ class K8sCloudStorePlugin extends CloudPluginBase implements CloudStorePluginInt ...@@ -385,11 +389,15 @@ class K8sCloudStorePlugin extends CloudPluginBase implements CloudStorePluginInt
} }
// Get latest entity. // Get latest entity.
$store_entities = $this->entityTypeManager->getStorage('cloud_store') $store_ids = $this->entityTypeManager->getStorage('cloud_store')
->loadByProperties([ ->getQuery()
'name' => $label, ->condition('name', $label)
'type' => 'k8s_cost_store', ->condition('type', 'k8s_cost_store')
]); ->condition('name', $label)
->sort('created', 'ASC')
->execute();
$store_entities = $this->entityTypeManager->getStorage('cloud_store')->loadMultiple($store_ids);
$store_entity = array_pop($store_entities); $store_entity = array_pop($store_entities);
// To check time difference between current and latest update time // To check time difference between current and latest update time
...@@ -401,7 +409,7 @@ class K8sCloudStorePlugin extends CloudPluginBase implements CloudStorePluginInt ...@@ -401,7 +409,7 @@ class K8sCloudStorePlugin extends CloudPluginBase implements CloudStorePluginInt
// If previous update is within the specific range, // If previous update is within the specific range,
// update process will be skipped. // update process will be skipped.
if ($time_diff < $this->length) { if ($time_diff < $this->updateInterval) {
continue; continue;
} }
} }
...@@ -409,7 +417,13 @@ class K8sCloudStorePlugin extends CloudPluginBase implements CloudStorePluginInt ...@@ -409,7 +417,13 @@ class K8sCloudStorePlugin extends CloudPluginBase implements CloudStorePluginInt
$result[$label] = []; $result[$label] = [];
foreach ($k8s_clusters ?: [] as $k8s_cluster) { foreach ($k8s_clusters ?: [] as $k8s_cluster) {
$cloud_context = $k8s_cluster->value; $cloud_context = $k8s_cluster->value;
$current_time = floor(time() / $this->length) * $this->length; // Round the time to get k8s namespace resource stores created within
// seconds defined as update cost store interval. That is, an entity of
// k8s_cost_store is created by combining the data of
// k8s_namespace_resource_store within the time defined by
// k8s_update_cost_store_cron_time. Without this logic, one entity of
// k8s_cost_store will be created for each k8s_namespace_resource_store.
$current_time = floor(time() / $this->updateInterval) * $this->updateInterval;
$ids = $this->entityTypeManager $ids = $this->entityTypeManager
->getStorage('cloud_store') ->getStorage('cloud_store')
->getQuery() ->getQuery()
...@@ -439,7 +453,10 @@ class K8sCloudStorePlugin extends CloudPluginBase implements CloudStorePluginInt ...@@ -439,7 +453,10 @@ class K8sCloudStorePlugin extends CloudPluginBase implements CloudStorePluginInt
// Count cost in specific time range from all k8s clusters. // Count cost in specific time range from all k8s clusters.
foreach ($resource_entities ?: [] as $resource_entity) { foreach ($resource_entities ?: [] as $resource_entity) {
$created_time = $resource_entity->get('created')->value; $created_time = $resource_entity->get('created')->value;
$time_range = ceil($created_time / $this->length) * $this->length; // Round the time to bundle resource entities created within
// seconds defined as update cost store interval.
// See also the above comment.
$time_range = ceil($created_time / $this->updateInterval) * $this->updateInterval;
if (!isset($result[$label][$time_range])) { if (!isset($result[$label][$time_range])) {
$result[$label][$time_range] = [ $result[$label][$time_range] = [
'total_cost' => [], 'total_cost' => [],
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment